Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- To integrate Coinbase Commerce as a payment method in a Laravel app, you can follow these steps:
- Create a Coinbase Commerce account and generate an API key.
- Install the Coinbase Commerce PHP SDK in your Laravel application using Composer.
- bash
- composer require coinbase/coinbase-commerce
- Add the Coinbase Commerce API key to your Laravel app's .env file as COINBASE_COMMERCE_API_KEY.
- Create a new controller to handle Coinbase Commerce payments. For example, you could create a CoinbaseController with methods for creating and handling payments.
- In the CoinbaseController, use the Coinbase Commerce SDK to create a new payment:
- use CoinbaseCommerce\ApiClient;
- use CoinbaseCommerce\Resources\Charge;
- class CoinbaseController extends Controller
- {
- public function createPayment(Request $request)
- {
- $apiClient = new ApiClient();
- $apiClient->setApiKey(env('COINBASE_COMMERCE_API_KEY'));
- $chargeData = [
- 'name' => 'My Product',
- 'description' => 'My Product Description',
- 'local_price' => [
- 'amount' => '10.00',
- 'currency' => 'USD',
- ],
- 'pricing_type' => 'fixed_price',
- 'metadata' => [
- 'customer_id' => '123',
- ],
- ];
- $charge = Charge::create($chargeData, $apiClient);
- return view('payments.coinbase', ['charge' => $charge]);
- }
- }
- This 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.
- Create a new view for the payment form. In this example, we'll create a coinbase.blade.php view:
- <!DOCTYPE html>
- <html>
- <head>
- <title>Pay with Coinbase Commerce</title>
- </head>
- <body>
- <h1>Pay with Coinbase Commerce</h1>
- <form action="{{ route('coinbase.pay') }}" method="POST">
- @csrf
- <script src="https://commerce.coinbase.com/v1/checkout.js?c={{ $charge->code }}"></script>
- <button type="submit">Pay with Coinbase Commerce</button>
- </form>
- </body>
- </html>
- This code includes the Coinbase Commerce checkout script and a form to submit the payment.
- Create a new route in your Laravel app to handle the payment form submission:
- css
- Route::post('/coinbase/pay', [CoinbaseController::class, 'handlePayment'])->name('coinbase.pay');
- In the CoinbaseController, create a new method to handle the payment submission:
- php
- class CoinbaseController extends Controller
- {
- public function handlePayment(Request $request)
- {
- $chargeCode = $request->input('code');
- $apiClient = new ApiClient();
- $apiClient->setApiKey(env('COINBASE_COMMERCE_API_KEY'));
- $charge = Charge::retrieve($chargeCode, $apiClient);
- if ($charge->timeline[0]->status === 'COMPLETED') {
- return view('payments.success', ['charge' => $charge]);
- } else {
- return view('payments.failure', ['charge' => $charge]);
- }
- }
- }
- This 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.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement