Im trying to create a button and assign a callback URL with coinbase.
I'm getting some CAPTCHA data returned when trying to post to coinbase API.
I think my webhost is getting blocked by CloudFlare, disabling my code.
Here's what I have:
<a class="coinbase-button" data-code="<?php
$data = array(
"button" => array(
"name" => "Ticket",
"price_string" => "0.01",
"price_currency_iso" => "BTC",
"custom" => $OrderNext . "- " . $ticket,
"callback_url" => "https://x.com/callback.php",
"description" => "Ticket - " . $ticket ,
"type" => "buy_now",
"style" => "buy_now_large"
)
);
$json_data = json_encode($data);
$ch = curl_init("https://coinbase.com/api/v1/buttons?api_key=xxxxxxxxxxxxxxxxxxxxxx");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json_data))
);
if( ! $output = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
$result = json_decode($output);
$output
returns a CAPTCHA page.
$result
is null.
Thanks for any help.
I never used coinbase but have you checked if your host does have curl support enabled in phpinfo()? Or maybe curl_init / curl_exec is in the list of disabled functions.
EDIT:
you're using a https url, you must either provide the CA certificate to be checked against setting the flag CURLOPT_CAINFO, or disable the verification of the certificate setting the CURLOPT_SSL_VERIFYPEER to false.
Brian from Coinbase here. It looks like you are hitting the API to generate a new payment button for each page load which, depending on the amount of traffic you get, could trigger our rate limiting either internally or through CloudFlare.
We currently have the merchant create button api limited to 10,000 calls per day internally, for example, for most merchants.
A better approach would be to hit the API once for each product (or if the price changes) and store the resulting 'code' parameter. If you save this in your database you can reuse it on every page load. This will help your pages load faster also.
More details: https://coinbase.com/api/doc/1.0/buttons/create.html
Hope it helps!
You can use code like this for generating button for payment
$apikey ="***************";
$apisecret = "************************";
$nonce = sprintf('%0.0f',round(microtime(true) * 1000000));
$url = "https://api.sandbox.coinbase.com/v1/buttons?nonce=" . $nonce;
$parameters = [];
$parameters["button"]["name"] = "Checkout Invoice";
$parameters["button"]["custom"] = $orderId;
$parameters["button"]["price_string"] = $priceString;
$parameters["button"]["type"] = "buy_now";
$parameters["button"]["subscription"] = false;
$parameters["button"]["price_currency_iso"] = "USD";
$parameters["button"]["description"] = "Checkout Invoice";
$parameters["button"]["style"] = "custom_large";
$parameters["button"]["include_email"] = true;
$parameters["button"]["callback_url"] = url("bitcoin/callback");
$parameters = http_build_query($parameters, true);
$signature = hash_hmac("sha256", $nonce . $url . $parameters, $apisecret);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
"ACCESS_KEY: " . $apikey,
"ACCESS_NONCE: " . $nonce,
"ACCESS_SIGNATURE: " . $signature
)));
curl_setopt_array($ch, array(
CURLOPT_POSTFIELDS => $parameters,
CURLOPT_POST => true,
));
$response = curl_exec($ch);
curl_close($ch);
$decodeResponse = json_decode($response);
It will return to button code for payment on coinbase.