Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Updated IPN for API v2 usage
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoshualewis committed Jul 30, 2019
1 parent 6eab796 commit c871b38
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 113 deletions.
199 changes: 96 additions & 103 deletions bitpay/bp_lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,49 @@

require_once 'bp_options.php';

function bpCurl($url, $apiKey, $post = false) {
global $bpOptions;

#print_r($post);die();
function bpIPN($url){
$ch = curl_init();
$length = 0;
if ($post)
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$length = strlen($post);
}
$request_headers = array();
$request_headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$request_headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$responseString = curl_exec($ch);


if($responseString == false) {

$response = curl_error($ch);
} else {

$response = json_decode($responseString, true);
}
$response = json_decode($responseString, true);
curl_close($ch);
return $response;
return $response;

}

function bpCurl($url, $apiKey, $post = false)
{
global $bpOptions;

$ch = curl_init();
$length = 0;
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$length = strlen($post);
}
$request_headers = array();
$request_headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$responseString = curl_exec($ch);

if ($responseString == false) {

$response = curl_error($ch);
} else {

$response = json_decode($responseString, true);
}
curl_close($ch);
return $response;
}
// $orderId: Used to display an orderID to the buyer. In the account summary view, this value is used to
// identify a ledger entry if present.
Expand All @@ -69,94 +81,75 @@ function bpCurl($url, $apiKey, $post = false) {
//
// $options keys can include any of:
// ('itemDesc', 'itemCode', 'notificationEmail', 'notificationURL', 'redirectURL', 'apiKey'
// 'currency', 'physical', 'fullNotifications', 'transactionSpeed', 'buyerName',
// 'buyerAddress1', 'buyerAddress2', 'buyerCity', 'buyerState', 'buyerZip', 'buyerEmail', 'buyerPhone')
// 'currency', 'physical', 'extendedNotifications', 'transactionSpeed', 'buyerName',
// 'buyerAddress1', 'buyerAddress2', 'buyerCity', 'buyerState', 'buyerZip', 'buyerEmail', 'buyerPhone')
// If a given option is not provided here, the value of that option will default to what is found in bp_options.php
// (see api documentation for information on these options).
function bpCreateInvoice($orderId, $price, $posData, $options = array()) {
global $bpOptions;

$options = array_merge($bpOptions, $options); // $options override any options found in bp_options.php

$options['posData'] = '{"posData": "' . $posData . '"';
if ($bpOptions['verifyPos']) // if desired, a hash of the POS data is included to verify source in the callback
$options['posData'].= ', "hash": "' . crypt($posData, $options['apiKey']).'"';
$options['posData'].= '}';

$options['orderID'] = $orderId;
$options['price'] = $price;
if($options['env'] == 'Test'){
#sandbox token
$options['token'] = MODULE_PAYMENT_BITPAY_APIKEY_DEV;
}else{
#production token
$options['token'] = MODULE_PAYMENT_BITPAY_APIKEY;
}



$postOptions = array('orderID', 'itemDesc', 'itemCode', 'notificationEmail', 'notificationURL', 'redirectURL',
'posData', 'price', 'currency', 'physical', 'fullNotifications', 'token','transactionSpeed', 'buyerName',
'buyerAddress1', 'buyerAddress2', 'buyerCity', 'buyerState', 'buyerZip', 'buyerEmail', 'buyerPhone');
foreach($postOptions as $o)
if (array_key_exists($o, $options))
$post[$o] = $options[$o];
$post = json_encode($post);


if($options['env'] == 'Test'){
$response = bpCurl('https://test.bitpay.com/invoices/', $options['apiKey'], $post);
}else{
$response = bpCurl('https://bitpay.com/invoices/', $options['apiKey'], $post);
}
function bpCreateInvoice($orderId, $price, $posData, $options = array())
{
global $bpOptions;

$options = array_merge($bpOptions, $options); // $options override any options found in bp_options.php

$options['posData'] = '{"posData": "' . $posData . '"';
if ($bpOptions['verifyPos']) // if desired, a hash of the POS data is included to verify source in the callback
{
$options['posData'] .= ', "hash": "' . crypt($posData, $options['apiKey']) . '"';
}

$options['posData'] .= '}';

$options['orderID'] = $orderId;
$options['price'] = $price;
if ($options['env'] == 'Test') {
#sandbox token
$options['token'] = MODULE_PAYMENT_BITPAY_APIKEY_DEV;
} else {
#production token
$options['token'] = MODULE_PAYMENT_BITPAY_APIKEY;
}

$postOptions = array('orderID', 'itemDesc', 'itemCode', 'notificationEmail', 'notificationURL', 'redirectURL',
'posData', 'price', 'currency', 'physical', 'extendedNotifications', 'token', 'transactionSpeed', 'buyerName',
'buyerAddress1', 'buyerAddress2', 'buyerCity', 'buyerState', 'buyerZip', 'buyerEmail', 'buyerPhone');
foreach ($postOptions as $o) {
if (array_key_exists($o, $options)) {
$post[$o] = $options[$o];
}
}

$post = json_encode($post);

return $response;
if ($options['env'] == 'Test') {
$response = bpCurl('https://test.bitpay.com/invoices/', $options['apiKey'], $post);
} else {
$response = bpCurl('https://bitpay.com/invoices/', $options['apiKey'], $post);
}

return $response;
}

// Call from your notification handler to convert $_POST data to an object containing invoice data
function bpVerifyNotification($apiKey = false) {
global $bpOptions;
if (!$apiKey)
$apiKey = $bpOptions['apiKey'];

$post = file_get_contents("php://input");
if (!$post)
return 'No post data';
function bpVerifyNotification($apiKey = false, $env = null)
{
global $bpOptions;

$json = json_decode($post, true);
$all_data = json_decode(file_get_contents("php://input"), true);

if (is_string($json))
return $json; // error
$data = $all_data['data'];
$event = $all_data['event'];

if (!array_key_exists('posData', $json))
return 'no posData';

$posData = json_decode($json['posData'], true);
if($bpOptions['verifyPos'] and $posData['hash'] != crypt($posData['posData'], $apiKey))
return 'authentication failed (bad hash)';
$json['posData'] = $posData['posData'];

if (!array_key_exists('id', $json))
{
return 'Cannot find invoice ID';
}

return bpGetInvoice($json['id'], $apiKey);
return bpGetInvoice($data['id'], $env);
}

// $options can include ('apiKey')
function bpGetInvoice($invoiceId, $apiKey=false) {
global $bpOptions;
if (!$apiKey)
$apiKey = $bpOptions['apiKey'];

$response = bpCurl('https://bitpay.com/api/invoice/'.$invoiceId, $apiKey);
if (is_string($response))
return $response; // error
$response['posData'] = json_decode($response['posData'], true);
return $response;
function bpGetInvoice($invoiceId, $env)
{
if($env == 'Test'){
$response = bpIPN('https://test.bitpay.com/invoices/' . $invoiceId);
}else{
$response = bpIPN('https://bitpay.com/invoices/' . $invoiceId);
}

return $response;
}

?>
2 changes: 1 addition & 1 deletion bitpay/bp_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

$bpOptions['physical'] = 'true';

$bpOptions['fullNotifications'] = 'true';
$bpOptions['extendedNotifications'] = 'true';

$bpOptions['transactionSpeed'] = 'low';

Expand Down
18 changes: 10 additions & 8 deletions bitpay_callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,21 @@ function validateResponse($response, $keys) {
return false;
}

$response = bpVerifyNotification(MODULE_PAYMENT_BITPAY_APIKEY);
$keys = array('posData', 'status');
if(MODULE_PAYMENT_BITPAY_STATUS_ENV == 'True'){
$response = bpVerifyNotification(MODULE_PAYMENT_BITPAY_APIKEY,'Prod');
}
else{
$response = bpVerifyNotification(MODULE_PAYMENT_BITPAY_APIKEY_DEV,'Test');
}

if (!validateResponse($response, $keys)) {
bplog(date('H:i') . " bitpay callback error: " . $response . "\n");
} else {
global $db;
$order_id = $response[$keys[0]][$keys[0]];
$status = $response[$keys[1]];
$status = $response['data']['status'];
$order_id = $response['data']['orderId'];

switch ($status) {
case 'confirmed':
case 'complete':

$db->Execute("update ". TABLE_ORDERS. " set orders_status = " . MODULE_PAYMENT_BITPAY_PAID_STATUS_ID . " where orders_id = ". intval($order_id));
break;
case 'expired':
Expand All @@ -68,4 +71,3 @@ function validateResponse($response, $keys) {
}
break;
}
}
2 changes: 1 addition & 1 deletion includes/modules/payment/bitpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function after_process()
'physical' => $order->content_type == 'physical' ? 'true' : 'false',
'currency' => $order->info['currency'],
'buyerName' => $order->customer['firstname'] . ' ' . $order->customer['lastname'],
'fullNotifications' => 'true',
'extendedNotifications' => 'true',
'notificationURL' => zen_href_link('bitpay_callback.php', $parameters = '', $connection = 'NONSSL', $add_session_id = true, $search_engine_safe = true, $static = true),
'redirectURL' => zen_href_link('account'),
'transactionSpeed' => MODULE_PAYMENT_BITPAY_TRANSACTION_SPEED,
Expand Down

0 comments on commit c871b38

Please sign in to comment.