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 c871b38 commit 1f21534
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 40 deletions.
30 changes: 16 additions & 14 deletions bitpay/bp_lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@

require_once 'bp_options.php';

function bpIPN($url){
$ch = curl_init();
$request_headers = array();
function bpIPN($url)
{
$ch = curl_init();
$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);
$response = json_decode($responseString, true);
curl_close($ch);
$responseString = curl_exec($ch);
$response = json_decode($responseString, true);
curl_close($ch);
return $response;

}
Expand All @@ -50,7 +51,7 @@ function bpCurl($url, $apiKey, $post = false)
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);
Expand Down Expand Up @@ -145,11 +146,12 @@ function bpVerifyNotification($apiKey = false, $env = null)
// $options can include ('apiKey')
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;

if ($env == 'Test') {
$response = bpIPN('https://test.bitpay.com/invoices/' . $invoiceId);
} else {
$response = bpIPN('https://bitpay.com/invoices/' . $invoiceId);
}

return $response;
}
46 changes: 23 additions & 23 deletions bitpay_callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
require 'bitpay/bp_lib.php';
require 'includes/application_top.php';

function bplog($contents) {
function bplog($contents)
{
if (true === isset($contents)) {
if (true === is_resource($contents)) {
error_log(serialize($contents));
Expand All @@ -37,7 +38,8 @@ function bplog($contents) {
}
}

function validateResponse($response, $keys) {
function validateResponse($response, $keys)
{
if (is_array($response) &&
array_key_exists($keys[0], $response) &&
array_key_exists($keys[0], $response[$keys[0]]) &&
Expand All @@ -48,26 +50,24 @@ function validateResponse($response, $keys) {
return false;
}

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

global $db;
$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':
if (true === function_exists('zen_remove_order')) {
zen_remove_order($order_id, $restock = true);
}
break;
}
global $db;
$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':
if (true === function_exists('zen_remove_order')) {
zen_remove_order($order_id, $restock = true);
}
break;
}
5 changes: 2 additions & 3 deletions includes/modules/payment/bitpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,11 @@ public function after_process()
$options['apiKey'] = MODULE_PAYMENT_BITPAY_APIKEY_DEV;
$options['env'] = 'Test';
}


$invoice = bpCreateInvoice($insert_id, $order->info['total'], $insert_id, $options);

if (!is_array($invoice) or array_key_exists('error', $invoice)) {

$this->log('createInvoice error ' . var_export($invoice['error'], true));
zen_remove_order($insert_id, $restock = true);
// unfortunately, there's not a good way of telling the customer that it's hosed. Their cart is still full so they can try again w/ a different payment option.
Expand Down

0 comments on commit 1f21534

Please sign in to comment.