· 2 years ago · Mar 30, 2023, 10:30 AM
1To integrate Coinbase Commerce as a payment method in a Laravel app, you can follow these steps:
2
3Create a Coinbase Commerce account and generate an API key.
4Install the Coinbase Commerce PHP SDK in your Laravel application using Composer.
5
6bash
7composer require coinbase/coinbase-commerce
8
9Add the Coinbase Commerce API key to your Laravel app's .env file as COINBASE_COMMERCE_API_KEY.
10Create a new controller to handle Coinbase Commerce payments. For example, you could create a CoinbaseController with methods for creating and handling payments.
11In the CoinbaseController, use the Coinbase Commerce SDK to create a new payment:
12
13use CoinbaseCommerce\ApiClient;
14use CoinbaseCommerce\Resources\Charge;
15
16class CoinbaseController extends Controller
17{
18 public function createPayment(Request $request)
19 {
20 $apiClient = new ApiClient();
21 $apiClient->setApiKey(env('COINBASE_COMMERCE_API_KEY'));
22
23 $chargeData = [
24 'name' => 'My Product',
25 'description' => 'My Product Description',
26 'local_price' => [
27 'amount' => '10.00',
28 'currency' => 'USD',
29 ],
30 'pricing_type' => 'fixed_price',
31 'metadata' => [
32 'customer_id' => '123',
33 ],
34 ];
35
36 $charge = Charge::create($chargeData, $apiClient);
37 return view('payments.coinbase', ['charge' => $charge]);
38 }
39}
40
41
42This code creates a new Charge object using the Coinbase Commerce API, sets the name, description, local_price, pricing_type, and metadata fields, and returns the Charge object to a view.
43
44Create a new view for the payment form. In this example, we'll create a coinbase.blade.php view:
45
46<!DOCTYPE html>
47<html>
48 <head>
49 <title>Pay with Coinbase Commerce</title>
50 </head>
51 <body>
52 <h1>Pay with Coinbase Commerce</h1>
53 <form action="{{ route('coinbase.pay') }}" method="POST">
54 @csrf
55 <script src="https://commerce.coinbase.com/v1/checkout.js?c={{ $charge->code }}"></script>
56 <button type="submit">Pay with Coinbase Commerce</button>
57 </form>
58 </body>
59</html>
60
61
62This code includes the Coinbase Commerce checkout script and a form to submit the payment.
63
64Create a new route in your Laravel app to handle the payment form submission:
65
66css
67Route::post('/coinbase/pay', [CoinbaseController::class, 'handlePayment'])->name('coinbase.pay');
68
69
70In the CoinbaseController, create a new method to handle the payment submission:
71
72php
73class CoinbaseController extends Controller
74{
75 public function handlePayment(Request $request)
76 {
77 $chargeCode = $request->input('code');
78
79 $apiClient = new ApiClient();
80 $apiClient->setApiKey(env('COINBASE_COMMERCE_API_KEY'));
81
82 $charge = Charge::retrieve($chargeCode, $apiClient);
83
84 if ($charge->timeline[0]->status === 'COMPLETED') {
85 return view('payments.success', ['charge' => $charge]);
86 } else {
87 return view('payments.failure', ['charge' => $charge]);
88 }
89 }
90}
91
92This code retrieves the Charge object using the Coinbase Commerce API and checks if the payment was completed successfully. If the payment was successful, it returns a success view; otherwise, it returns a failure view.