PHP Voorbeelden
Wrapper functie om BitKassa API aan te roepen:
function CallBitKassaApi( $action, $params )
{
$merchantId = 'banketbakkerhenk';
$secretApiKey = 'abcde12345';
// voeg action en merchant_id (vereist voor elke API call) toe aan de params array
$params['action'] = $action;
$params['merchant_id'] = $merchantId;
// creeer json en authenticatie
$jsonData = json_encode($params);
$t = time();
$a = hash('sha256',$secretApiKey.$jsonData.$t).$t; // authenticatie hash
$p = base64_encode($jsonData);
// roep API url (zou ook kunnen met curl e.d.)
$jsonResult = file_get_contents("https://www.bitkassa.nl/api/v1?p=$p&a=$a");
// retourneer resultaat als array (of breek af met foutmelding)
if (!$jsonResult) die('Kon geen verbinding krijgen');
$result = json_decode($jsonResult,true);
if (!$result['success']) die('Error: '.$result['error']);
return $result;
}
Betaling starten:
$params = array(
// verplicht:
'currency' => 'EUR',
'amount' => 1295,
// optioneel:
'description' => 'Chocoladetaart XL',
'return_url' => 'https://www.bakkerhenk.nl/webshop/landingpage.php',
'update_url' => 'https://www.bakkerhenk.nl/update_order.php',
'meta_info' => '123456789', // kan bijvoorbeeld een order of klant ID zijn in je eigen database, voor je eigen referentie
);
$result = CallBitKassaApi('start_payment',$params);
// stuur klant door naar betaalpagina
header('Location: '.$result['payment_url']);
Status van een betaling opvragen:
$params = array( 'payment_id' => 'abcdefghijk' );
$result = CallBitKassaApi('get_payment_status',$params);
print('De status van uw betaling is: '.$result['payment_status']);
Wrapper functie om de POST parameters te lezen die wij meesturen met een callback of redirect:
function GetBitKassaParams()
{
$mySecretApiKey = 'abcde12345';
$p = $_POST['p'];
$a = $_POST['a'];
$jsonData = base64_decode($p);
// controleer authentication hash (zodat je zeker weet dat deze request echt van ons kwam,
// en niet van iemand die zijn order ten onrechte op 'paid' probeert te zetten)
$t = substr($a,64);
$verify = hash('sha256',$mySecretApiKey.$jsonData.$t).$t;
if ($a!=$verify) die('Authentication error');
return json_decode($jsonData,true);
}
Callback script dat je zou kunnen meegeven als update_url:
$params = GetBitKassaParams();
$paymentId = $params['payment_id'];
$status = $params['payment_status'];
$myRef = $params['meta_info'];
if ($status=='paid')
{
// (...verstuur product...)
}
Terugkeer pagina die je zou kunnen meegeven als return_url, waarheen wij de klant redirecten na de betaling:
$params = GetBitKassaParams();
$paymentId = $params['payment_id'];
$status = $params['payment_status'];
$myRef = $params['meta_info'];
if ($status=='pending' || $status=='paid')
{
print('Dank voor uw betaling, zodra deze een bevestiging vanuit het Bitcoin-netwerk krijgt gaan we uw order zo snel mogelijk verwerken!');
}
else if ($status=='expired') print('Er is geen betaling ontvangen :(');
else if ($status=='cancelled') print('U heeft de betaling geannuleerd :(');
PHP Examples
Wrapper function to call the BitKassa API:
function CallBitKassaApi( $action, $params )
{
$merchantId = 'joesbakery';
$secretApiKey = 'abcde12345';
// insert action and merchant_id (required for every API call) into the params array
$params['action'] = $action;
$params['merchant_id'] = $merchantId;
// create json and authentication
$jsonData = json_encode($params);
$t = time();
$a = hash('sha256',$secretApiKey.$jsonData.$t).$t; // authentication hash
$p = base64_encode($jsonData);
// call API url (also possible with cURL etc)
$jsonResult = file_get_contents("https://www.bitkassa.nl/api/v1?p=$p&a=$a");
// abort on error, or return result as associative array
if (!$jsonResult) die('Could not connect');
$result = json_decode($jsonResult,true);
if (!$result['success']) die('Error: '.$result['error']);
return $result;
}
Start a payment:
$params = array(
// required:
'currency' => 'EUR',
'amount' => 1295,
// optional:
'description' => 'Chocolate Pie XL',
'return_url' => 'https://www.joesbakery.uk/webshop/landingpage.php',
'update_url' => 'https://www.joesbakery.uk/update_order.php',
'meta_info' => '123456789', // could be an order or customer ID within your own database, for your own reference
);
$result = CallBitKassaApi('start_payment',$params);
// redirect customer to payment page
header('Location: '.$result['payment_url']);
Retrieve payment status:
$params = array( 'payment_id' => 'abcdefghijk' );
$result = CallBitKassaApi('get_payment_status',$params);
print('The status of your payment is: '.$result['payment_status']);
Wrapper function to process the POST parameters we include with a callback or redirect:
function GetBitKassaParams()
{
$mySecretApiKey = 'abcde12345';
$p = $_POST['p'];
$a = $_POST['a'];
$jsonData = base64_decode($p);
// verify authentication hash to be sure this request actually came from us,
// and not someone trying to forge their payment status into 'paid'
$t = substr($a,64);
$verify = hash('sha256',$mySecretApiKey.$jsonData.$t).$t;
if ($a!=$verify) die('Authentication error');
return json_decode($jsonData,true);
}
Callback script you could use as update_url:
$params = GetBitKassaParams();
$paymentId = $params['payment_id'];
$status = $params['payment_status'];
$myRef = $params['meta_info'];
if ($status=='paid')
{
// (...send product...)
}
Return page you could use as return_url, where we redirect the customer after the payment:
$params = GetBitKassaParams();
$paymentId = $params['payment_id'];
$status = $params['payment_status'];
$myRef = $params['meta_info'];
if ($status=='pending' || $status=='paid')
{
print('Thanks for your payment, we will process your order as soon as your transaction gets confirmed by the Bitcoin network!');
}
else if ($status=='expired') print('No payment received :(');
else if ($status=='cancelled') print('You cancelled the payment :(');