1

I am integrating Coinbase Commerce API in my PHP web application for accepting payments. I am taking reference from official PHP library for the Coinbase commerce

So I have a addfunds.php Where can choose the amount and payments method, here is the code of add funds file `

   $data = [
          
            "business_name" => "", 
            "customer_email" => $user['email'], 
            "customer_name" => $user['client_id'].','.$paymentCode.','.$method_id.','.$order_id, 
            "local_price" => [
                  "amount" => $amount, 
                  "currency" => "USD" 
               ], 
            "memo" => "Balance recharge - ".  $user['email'] 
         ];  
         $fields_string = json_encode($data);
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.commerce.coinbase.com/invoices');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'X-Cc-Api-Key: '.$extra['api_key'];
$headers[] = 'X-Cc-Version: 2018-03-22';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

header('location:'.$result->data->hosted_url);
$success = 1;
$successText = "Your payment was initiated successfully, you are being redirected..";

` This is how my code looks likes as of now, I am getting the details in $data json from my database, This code works. It redirects the user to the coinbase payment page.

$result->data

This return Json from Coinbase which contains all the details about transaction like amount, payment code, currency, invoice url... etc.

But I'm confused how I can authenticate the payment. Weather the payment is completed or not. I have checked The API documentation from coinbase. But I'm not getting it, I think I can do it with the conditional statement. So firstly I have to fetch the invoice of the user. For that I can get the. Payment food from $result->data and fetch the invoice. I can say this is how I can fetch invoice from coinbase

`

<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('GET', 'https://api.commerce.coinbase.com/invoices/invoice_code_or_invoice_id', [
  'headers' => [
    'accept' => 'application/json',
  ],
]);

echo $response->getBody();

`

I can use $result->data->code in the place of invoice_code_or_invoice_id and get invoice status from it and check wheather the transaction is completed or not, by referring to coinbase documentation about invoice status but the problem is the payment period given is 60 minutes so i have to check this condition after 60 minutes? or how can i do it. Thank you for reading my question i hope someone helps, thankyou

I tried myself to check the condition and do by i dont know about how can i check condition after specific time or how i know the statuss is marked complete

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

0 Answers0