Vincent Webb

Not Your Average Blogger

Accept Bitcoin Payment With Coinkite using PHP

This took me a little while to figure out, so I thought I'd post it here for anyone else that may come along searching for a way to accept bitcoin payments in php using coinkite.

Coinkite provides a very nice API for accepting payments. You actually only need a few lines of code. Here's how it works:

First open a coinkite account, then goto Merchant -> Api Keys
and create your key. Make note of the 'API Secret' in the edit window, you will need this later.

Next goto Metchant -> Pay Buttons and create 1 button. The price does not matter, we will override this later. If you have webhooks this would be the place to install them.

Now for the fun part. Coinkite does not have a good PHP example on their page showing how to do this, but here is the code I wrote that will generate the required JWT signature so you can override the button settings:

<?php  
$apisecret="Sc28eXXXX-XXXXa14f-30XXXXbe2528bXXX"; //YOUR API SECRET
$url = "https://coinkite.com/pay/965BD41XXX-XXXXXX"; //Coinkite button url
$params = array('p' => 1000, 'c' => 'USD'); //BUTTON OVERRIDE OPTIONS


function urlsafeB64Encode($input){return str_replace('=', '',  
strtr(base64_encode($input), '+/', '-_'));}  
$header = array('typ' => 'JWT', 'alg' => 'HS256');

$part1 =  urlsafeB64Encode(json_encode($header));
$part2 =  urlsafeB64Encode(json_encode($params));

$signed =  urlsafeB64Encode(hash_hmac('SHA256', $part1 . ".". $part2,
$apisecret, true));

$finalurl =  $url."?".$part1.".".$part2.".".$signed;

echo "<a href='".$finalurl."'>".$finalurl."</a>";  
?>

So all you need to do is change the first 3 lines in the snippit of code above and you can create a new link on your checkout page that will allow people to pay in bitcoin.

The 'p' parameter is the price, and you can set it in USD and it will automatically convert into bitcoins when the visitors clicks to pay.

Hopefully this post saves you some time with creating your Coinkite Pay Buttons dynamically in PHP.

Let me know if you use this code below in the comments.

comments powered by Disqus