Create charge using PHP

Hi, I want to integrate payments with coinbase into my PHP website, but it gives me the following error: Fatal error : Uncaught GuzzleHttp\Exception\ClientException: Client error: POST https://api.commerce.coinbase.com/charges resulted in a 401 Unauthorized response: {“error”:{“type”:“authorization_error”,“message”:“You are not authorized to do that.”}}

Code:

            require_once('assets/payments/coinbase/vendor/autoload.php');
            
            $client = new \GuzzleHttp\Client();
            
            $response = $client->request('POST', 'https://api.commerce.coinbase.com/charges', [
              'body' => '{"local_price":{"amount":1,"currency":"BTC"},"metadata":{"customer_id":"customer1","customer_name":"Person1"},"name":"Example1","description":"Create Charge using PHP","pricing_type":"fixed_price","redirect_url":"https://charge/completed/page","cancel_url":"https://charge/canceled/page"}',
              'headers' => [
                'X-CC-Version' => '2018-03-22',
                'accept' => 'application/json',
                'content-type' => 'application/json',
              ],
            ]);
            
            echo $response->getBody();

Hi @medel! Thank you for posting your concern here at the Coinbase Developer forum and we also appreciate you providing your code snippet you used for your integration. We understand that you want to integrate Payments to your PHP website. However, we noticed your code snippet on the headers field and somehow found out that one possible reason why you are encountering that error is because the API key on the headers field is missing. We’ve tried to use and run your code by adding our own API key into the headers field, and we received 200 OK successfully.

We suggest you try to run and retry to integrate your payments to your website, but this time, include your API key into your headers field.

Please note that this was only our suggestions based on the code snippet you provided. We regret to inform you that as much as we wanted to help you with your code, we are actually only able to provide assistance and troubleshooting for issues with the raw Coinbase APIs and cannot provide detailed guidance with writing your code in specific programming languages and frameworks.

We hope this helps!

1 Like

How could I integrate the API? what code snippet should i put?

Hello @medel! For you to be able to successfully integrate your payments to your website, as mentioned in the previous reply, you should include your API key into your headers field. Based on this documentation, authenticated API requests should be made with a X-CC-Api-Key header. Your secret API key should be passed as the value. You may also refer to the below code snippet.

<?php
require_once('assets/payments/coinbase/vendor/autoload.php');
            $client = new \GuzzleHttp\Client();
            $response = $client->request('POST', 'https://api.commerce.coinbase.com/charges', [
              'body' => '{"local_price":{"amount":1,"currency":"BTC"},"metadata":{"customer_id":"customer1","customer_name":"Person1"},"name":"Example1","description":"Create Charge using PHP","pricing_type":"fixed_price","redirect_url":"https://charge/completed/page","cancel_url":"https://charge/canceled/page"}',
              'headers' => [
                'X-CC-Version' => '2018-03-22',
                'accept' => 'application/json',
                'content-type' => 'application/json',
                'X-CC-Api-Key' => 'YOUR_API_KEY',
              ],
            ]); 
            echo $response->getBody();
?>

We hope we were able to help you with regard to your concern. Thank you and have a great day!

1 Like

thx!
And how can I make it redirect to the payment page?

Hello @medel! You may use the following body parameters in your code to redirect a charge to a page or website.

  • “redirect_url” [Optional]

    • Once payments become successful, it will be redirected to the stated website in this parameter.

    • Example Request: "redirect_url":" https://beta.commerce.coinbase.com/payment-succesful "

  • “cancel_url” [Optional]

    • Once payments are cancelled, it will be redirected to the stated website in this parameter.

    • Example Request: "cancel_url":" https://beta.commerce.coinbase.com/cancelled-payments "

For more information about Create a Charge endpoint, you may go through this link: Create a charge | Coinbase Cloud

We hope this helps!

1 Like

Yes, but I mean so that clicking the pay button takes you to the payment page
How can I know the URL of the payment page?

Hello @medel! We would like to inform you that after creating a charge, it will return a JSON response wherein a charge object is returned with payment addresses for each currency plus a URL to a hosted page where a customer can complete their payment as stated on this documentation. You may search for the hosted_url parameter in the JSON response and embed this on your pay button to direct customers to a hosted page to complete their payment.

If you have any more questions/concerns, please feel free to reach out. Have a nice day!

1 Like