Advertisement
SMSabuj

Coinbase Commerce

Mar 30th, 2023
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.30 KB | Cryptocurrency | 0 0
  1. To integrate Coinbase Commerce as a payment method in a Laravel app, you can follow these steps:
  2.  
  3. Create a Coinbase Commerce account and generate an API key.
  4. Install the Coinbase Commerce PHP SDK in your Laravel application using Composer.
  5.  
  6. bash
  7. composer require coinbase/coinbase-commerce
  8.  
  9. Add the Coinbase Commerce API key to your Laravel app's .env file as COINBASE_COMMERCE_API_KEY.
  10. Create a new controller to handle Coinbase Commerce payments. For example, you could create a CoinbaseController with methods for creating and handling payments.
  11. In the CoinbaseController, use the Coinbase Commerce SDK to create a new payment:
  12.  
  13. use CoinbaseCommerce\ApiClient;
  14. use CoinbaseCommerce\Resources\Charge;
  15.  
  16. class 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.  
  42. 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.
  43.  
  44. Create 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.  
  62. This code includes the Coinbase Commerce checkout script and a form to submit the payment.
  63.  
  64. Create a new route in your Laravel app to handle the payment form submission:
  65.  
  66. css
  67. Route::post('/coinbase/pay', [CoinbaseController::class, 'handlePayment'])->name('coinbase.pay');
  68.  
  69.  
  70. In the CoinbaseController, create a new method to handle the payment submission:
  71.  
  72. php
  73. class 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.  
  92. 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