How To Integrate Bitcoin Payments with PHP

As the use of Bitcoin as a price technique is growing, many groups have commenced accepting payments on his virtual currencymore than 100,000 groups are presently accepting Bitcoin (some of them are large and together with Microsoft and Dell), making it the most famous virtual currency these dayslet’s examine how it works and the way to receive payments in Bitcoins in your internet application.

How Does Bitcoin Work?

Bitcoin is a digital currency and payment gadget that allows peer-to-peer transactions among customerswhich means that there’s no middleman in the transactions. but, all transactions are demonstrated and recorded in a public dispensed ledger, that is known as the blockchain.

Considering that Bitcoin is a decentralized system, the computer sources from many humans are used for fee processing. those people are known as miners and are rewarded with Bitcoins for their service. Miners also can prioritize transactions, this means that they can charge transaction fees and provide quicker processing of transactions to the users who have paid the higher rate.

Facts necessary to make transactions in Bitcoins are stored in pockets. A pocket is a software program application that holds statistics about the person‘s account, in addition to credentials that show ownership.

 

PHP Integration

The perfect Bitcoin integration solution is offered through Bitcoin charge processors. In return for a small transaction pricethose websites provide you with a pocket, a rest API, prioritize your transactions with miners and send fee information to your websitemany of them additionally provide libraries that may be used to without difficulty combine with their APIs. In this examplewe will use one from Coinbase. it may be installed via Composer:

require coinbase/coinbase

 

Instantiate the class by entering the application key and secret:

 

<?php

require 'vendor/autoload.php';

use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;
use Coinbase\Wallet\Value\Money;
use Coinbase\Wallet\Resource\Checkout;

$configuration = Configuration::apiKey('app_key', 'app_secret');
$client = Client::create($configuration); 

 

Now, we can create a checkout — an API resource that is used when integrating Bitcoin as a payment option on your Web site.

 

$params = array(
	'name'               => 'My cool product',
	'description'		 => 'Describe my product here',
	'amount'             => new Money('15.99', 'USD'),
	'metadata'           => array(
								'email' => 'john@doe.com',
							),
	'style'				=> 'buy_now_small'
);

$checkout = new Checkout($params);
$client->createCheckout($checkout); 

 

The Checkout class is able to generate an iframe or button code that will be displayed on your Web site and that the users will use to make the transaction:

 

<iframe id="coinbase_inline_iframe_<?php echo $checkout->getEmbedCode(); ?>"
        src="https://www.coinbase.com/checkouts/<?php echo $checkout->getEmbedCode(); ?>/inline"
        style="width:460px;height:370px; border: none; box-shadow: 0 1px 3px rgba(0,0,0,0.25)"
        allowtransparency="true" frameborder="0">
</iframe>

 

The iframe will display the details about your product, such as the call, description, and priceit will also display the address that can be used to make the payment. The user will then make the payment with his or her Bitcoin wallet. After that, Coinbase will process the transaction and ship a callback on your server. The callback notifies your utility that the transaction changed into a success and is sent to the URL that you have laid out in your Coinbase account.

<?php

require 'vendor/autoload.php';

use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;
use Coinbase\Wallet\Value\Money;
use Coinbase\Wallet\Resource\Checkout;

$configuration = Configuration::apiKey('app_key', 'app_secret');
$client = Client::create($configuration);

$raw_body = file_get_contents('php://input');
$signature = @$_SERVER['HTTP_CB_SIGNATURE'];

// Check if the callback is valid
$authenticity = $client->verifyCallback($raw_body, $signature); // boolean

if(!$authenticity) {
	die();
}	

// Fetch data sent by Coinbase
$obj = json_decode($raw_body);
$data = ($obj->data->metadata);

// e.g. we have sent "email" as metadata - access that value with $data->email

// Payment successful - do something here

Conclusion

This tutorial has shown how to allow users to pay for your product or service in Bitcoins. Many other options are also available in the API, such as buying/selling Bitcoins or showing the current exchange rates.

Please follow and like us:



Leave a Comment

Your email address will not be published. Required fields are marked *