From 1f21534086118af8e7b8f629d6a6501fe8731bee Mon Sep 17 00:00:00 2001 From: Joshua Lewis Date: Tue, 30 Jul 2019 11:56:59 -0400 Subject: [PATCH] Updated IPN for API v2 usage --- bitpay/bp_lib.php | 30 ++++++++++--------- bitpay_callback.php | 46 ++++++++++++++--------------- includes/modules/payment/bitpay.php | 5 ++-- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/bitpay/bp_lib.php b/bitpay/bp_lib.php index 997982d..9948c05 100755 --- a/bitpay/bp_lib.php +++ b/bitpay/bp_lib.php @@ -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; } @@ -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); @@ -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; } diff --git a/bitpay_callback.php b/bitpay_callback.php index 14eafb9..a663229 100755 --- a/bitpay_callback.php +++ b/bitpay_callback.php @@ -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)); @@ -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]]) && @@ -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; +} diff --git a/includes/modules/payment/bitpay.php b/includes/modules/payment/bitpay.php index 7a3707c..ec7261b 100755 --- a/includes/modules/payment/bitpay.php +++ b/includes/modules/payment/bitpay.php @@ -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.