From f0e9dc4da7ce910d710e18057fee6f6ed7ae9c98 Mon Sep 17 00:00:00 2001 From: Stephen Amaza Date: Mon, 2 Apr 2018 17:31:16 +0100 Subject: [PATCH 1/3] Enable GHS and USD payments on integrations --- .../catalog/controller/payment/paystack.php | 6 +- .../upload/catalog/model/payment/paystack.php | 8 ++- .../default/template/payment/paystack.tpl | 1 + .../catalog/controller/payment/paystack.php | 12 ++-- .../upload/catalog/model/payment/paystack.php | 8 ++- .../default/template/payment/paystack.tpl | 1 + .../controller/extension/payment/paystack.php | 61 ++++--------------- .../upload/catalog/model/payment/paystack.php | 8 ++- .../default/template/payment/paystack.tpl | 1 + .../catalog/controller/payment/paystack.php | 55 +++-------------- 2.x/upload/catalog/model/payment/paystack.php | 8 ++- .../default/template/payment/paystack.tpl | 1 + README.md | 6 +- 13 files changed, 61 insertions(+), 115 deletions(-) diff --git a/1.5.x/upload/catalog/controller/payment/paystack.php b/1.5.x/upload/catalog/controller/payment/paystack.php index c913933..2885cfa 100755 --- a/1.5.x/upload/catalog/controller/payment/paystack.php +++ b/1.5.x/upload/catalog/controller/payment/paystack.php @@ -25,6 +25,7 @@ protected function index() $this->data['ref'] = uniqid('' . $this->session->data['order_id'] . '-'); $this->data['amount'] = intval($order_info['total'] * 100); $this->data['email'] = $order_info['email']; + $this->data['currency'] = $order_info['currency_code']; $this->data['callback'] = $this->url->link('payment/paystack/callback', 'trxref=' . rawurlencode($this->data['ref']), 'SSL'); if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/paystack.tpl')) { @@ -45,12 +46,13 @@ protected function query_api_transaction_verify($reference) $skey = $this->config->get('paystack_test_secret'); } - $context = stream_context_create(array( + $context = stream_context_create( + array( 'http'=>array( 'method'=>"GET", 'header'=>"Authorization: Bearer " . $skey, ) - ) + ) ); $url = 'https://api.paystack.co/transaction/verify/'. rawurlencode($reference); $request = file_get_contents($url, false, $context); diff --git a/1.5.x/upload/catalog/model/payment/paystack.php b/1.5.x/upload/catalog/model/payment/paystack.php index c896b17..d2993ee 100755 --- a/1.5.x/upload/catalog/model/payment/paystack.php +++ b/1.5.x/upload/catalog/model/payment/paystack.php @@ -17,8 +17,12 @@ public function getMethod($address, $total) $status = false; } - // Paystack Only switches NGN for now - if ($status && (strtoupper($this->currency->getCode())!=='NGN')) { + // Paystack Only switches NGN, GHS and USD for now + if ($status + && ((strtoupper($this->currency->getCode())!=='NGN') + || (strtoupper($this->config->getCode())!=='GHS') + || (strtoupper($this->config->getCode())!=='USD')) + ) { $status = false; } diff --git a/1.5.x/upload/catalog/view/theme/default/template/payment/paystack.tpl b/1.5.x/upload/catalog/view/theme/default/template/payment/paystack.tpl index 7f6ccc7..f53444b 100755 --- a/1.5.x/upload/catalog/view/theme/default/template/payment/paystack.tpl +++ b/1.5.x/upload/catalog/view/theme/default/template/payment/paystack.tpl @@ -17,6 +17,7 @@ key: '', email: '', amount: , + currency: '', ref: '', callback: function(response){ window.location.href=''; diff --git a/2.2.x/upload/catalog/controller/payment/paystack.php b/2.2.x/upload/catalog/controller/payment/paystack.php index f1a3ac4..29d58b2 100755 --- a/2.2.x/upload/catalog/controller/payment/paystack.php +++ b/2.2.x/upload/catalog/controller/payment/paystack.php @@ -25,6 +25,7 @@ public function index() $data['ref'] = uniqid('' . $this->session->data['order_id'] . '-'); $data['amount'] = intval($order_info['total'] * 100); $data['email'] = $order_info['email']; + $data['currency'] = $order_info['currency_code']; $data['callback'] = $this->url->link('payment/paystack/callback', 'trxref=' . rawurlencode($data['ref']), 'SSL'); if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/paystack.tpl')) { @@ -44,12 +45,13 @@ private function query_api_transaction_verify($reference) $skey = $this->config->get('paystack_test_secret'); } - $context = stream_context_create(array( - 'http'=>array( - 'method'=>"GET", - 'header'=>"Authorization: Bearer " . $skey, + $context = stream_context_create( + array( + 'http'=>array( + 'method'=>"GET", + 'header'=>"Authorization: Bearer " . $skey, + ) ) - ) ); $url = 'https://api.paystack.co/transaction/verify/'. rawurlencode($reference); $request = file_get_contents($url, false, $context); diff --git a/2.2.x/upload/catalog/model/payment/paystack.php b/2.2.x/upload/catalog/model/payment/paystack.php index f7d1999..df6d300 100755 --- a/2.2.x/upload/catalog/model/payment/paystack.php +++ b/2.2.x/upload/catalog/model/payment/paystack.php @@ -24,8 +24,12 @@ public function getMethod($address, $total) $status = false; } - // Paystack Only switches NGN for now - if ($status && (strtoupper($this->config->get('config_currency'))!=='NGN')) { + // Paystack only switches NGN, GHS and USD for now + if ($status + && ((strtoupper($this->config->get('config_currency'))!=='NGN') + || (strtoupper($this->config->get('config_currency'))!=='GHS') + || (strtoupper($this->config->get('config_currency'))!=='USD')) + ) { $status = false; } diff --git a/2.2.x/upload/catalog/view/theme/default/template/payment/paystack.tpl b/2.2.x/upload/catalog/view/theme/default/template/payment/paystack.tpl index c81f38e..fecddf0 100755 --- a/2.2.x/upload/catalog/view/theme/default/template/payment/paystack.tpl +++ b/2.2.x/upload/catalog/view/theme/default/template/payment/paystack.tpl @@ -16,6 +16,7 @@ key: '', email: '', amount: , + currency: '', ref: '', callback: function(response){ window.location.href=''; diff --git a/2.3.x/upload/catalog/controller/extension/payment/paystack.php b/2.3.x/upload/catalog/controller/extension/payment/paystack.php index d8aa8d4..65b6519 100644 --- a/2.3.x/upload/catalog/controller/extension/payment/paystack.php +++ b/2.3.x/upload/catalog/controller/extension/payment/paystack.php @@ -1,6 +1,7 @@ language->load('extension/payment/paystack'); @@ -24,6 +25,7 @@ public function index() $data['ref'] = uniqid('' . $this->session->data['order_id'] . '-'); $data['amount'] = intval($order_info['total'] * 100); $data['email'] = $order_info['email']; + $data['currency'] = $order_info['currency_code']; $data['callback'] = $this->url->link('extension/payment/paystack/callback', 'trxref=' . rawurlencode($data['ref']), 'SSL'); if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/paystack.tpl')) { @@ -43,59 +45,18 @@ private function query_api_transaction_verify($reference) $skey = $this->config->get('paystack_test_secret'); } - $context = stream_context_create(array( - 'http'=>array( - 'method'=>"GET", - 'header'=>"Authorization: Bearer " . $skey, + $context = stream_context_create( + array( + 'http'=>array( + 'method'=>"GET", + 'header'=>"Authorization: Bearer " . $skey, + ) ) - ) ); $url = 'https://api.paystack.co/transaction/verify/'. rawurlencode($reference); $request = file_get_contents($url, false, $context); return json_decode($request, true); } - // protected function query_api_transaction_verify($reference) - // { - // $url = 'https://api.paystack.co/transaction/verify/' . urlencode($reference); - // $data = array(); - - // if ($this->config->get('paystack_live')) { - // $skey = $this->config->get('paystack_live_secret'); - // } else { - // $skey = $this->config->get('paystack_test_secret'); - // } - - // //open connection - // $ch = curl_init(); - - // //set the url, and the header - // curl_setopt($ch, CURLOPT_URL, $url); - // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - - // // Paystack's servers require TLSv1.2 - // // Force CURL to use this - // if (!defined('CURL_SSLVERSION_TLSV1_2')) { - // define('CURL_SSLVERSION_TLSV1_2', 6); - // } - // curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSV1_2); - - // curl_setopt( - // $ch, CURLOPT_HTTPHEADER, [ - // 'Authorization: Bearer ' . $skey] - // ); - - // //execute post - // $result = curl_exec($ch); - - // //close connection - // curl_close($ch); - - // if ($result) { - // $data = json_decode($result, true); - // } - - // return $data; - // } private function redir_and_die($url, $onlymeta = false) { @@ -114,7 +75,7 @@ public function callback() // order id is what comes before the first dash in trxref $order_id = substr($trxref, 0, strpos($trxref, '-')); // if no dash were in transation reference, we will have an empty order_id - if(!$order_id) { + if (!$order_id) { $order_id = 0; } diff --git a/2.3.x/upload/catalog/model/payment/paystack.php b/2.3.x/upload/catalog/model/payment/paystack.php index f7d1999..b0f1c03 100755 --- a/2.3.x/upload/catalog/model/payment/paystack.php +++ b/2.3.x/upload/catalog/model/payment/paystack.php @@ -24,8 +24,12 @@ public function getMethod($address, $total) $status = false; } - // Paystack Only switches NGN for now - if ($status && (strtoupper($this->config->get('config_currency'))!=='NGN')) { + // Paystack only switches NGN, GHS and USD for now + if ($status + && ((strtoupper($this->config->get('config_currency'))!=='NGN') + || (strtoupper($this->config->get('config_currency'))!=='GHS') + || (strtoupper($this->config->get('config_currency'))!=='USD')) + ) { $status = false; } diff --git a/2.3.x/upload/catalog/view/theme/default/template/payment/paystack.tpl b/2.3.x/upload/catalog/view/theme/default/template/payment/paystack.tpl index c81f38e..fecddf0 100755 --- a/2.3.x/upload/catalog/view/theme/default/template/payment/paystack.tpl +++ b/2.3.x/upload/catalog/view/theme/default/template/payment/paystack.tpl @@ -16,6 +16,7 @@ key: '', email: '', amount: , + currency: '', ref: '', callback: function(response){ window.location.href=''; diff --git a/2.x/upload/catalog/controller/payment/paystack.php b/2.x/upload/catalog/controller/payment/paystack.php index b293a1c..780e5bc 100755 --- a/2.x/upload/catalog/controller/payment/paystack.php +++ b/2.x/upload/catalog/controller/payment/paystack.php @@ -25,6 +25,7 @@ public function index() $data['ref'] = uniqid('' . $this->session->data['order_id'] . '-'); $data['amount'] = intval($order_info['total'] * 100); $data['email'] = $order_info['email']; + $data['currency'] = $order_info['currency_code']; $data['callback'] = $this->url->link('payment/paystack/callback', 'trxref=' . rawurlencode($data['ref']), 'SSL'); if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/paystack.tpl')) { @@ -43,59 +44,19 @@ private function query_api_transaction_verify($reference) $skey = $this->config->get('paystack_test_secret'); } - $context = stream_context_create(array( - 'http'=>array( - 'method'=>"GET", - 'header'=>"Authorization: Bearer " . $skey, + $context = stream_context_create( + array( + 'http'=>array( + 'method'=>"GET", + 'header'=>"Authorization: Bearer " . $skey, + ) ) - ) ); $url = 'https://api.paystack.co/transaction/verify/'. rawurlencode($reference); $request = file_get_contents($url, false, $context); return json_decode($request, true); } - // protected function query_api_transaction_verify($reference) - // { - // $url = 'https://api.paystack.co/transaction/verify/' . urlencode($reference); - // $data = array(); - - // if ($this->config->get('paystack_live')) { - // $skey = $this->config->get('paystack_live_secret'); - // } else { - // $skey = $this->config->get('paystack_test_secret'); - // } - - // //open connection - // $ch = curl_init(); - - // //set the url, and the header - // curl_setopt($ch, CURLOPT_URL, $url); - // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - - // // Paystack's servers require TLSv1.2 - // // Force CURL to use this - // if (!defined('CURL_SSLVERSION_TLSV1_2')) { - // define('CURL_SSLVERSION_TLSV1_2', 6); - // } - // curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSV1_2); - - // curl_setopt( - // $ch, CURLOPT_HTTPHEADER, [ - // 'Authorization: Bearer ' . $skey] - // ); - - // //execute post - // $result = curl_exec($ch); - - // //close connection - // curl_close($ch); - - // if ($result) { - // $data = json_decode($result, true); - // } - - // return $data; - // } + private function redir_and_die($url, $onlymeta = false) { if (!headers_sent() && !$onlymeta) { diff --git a/2.x/upload/catalog/model/payment/paystack.php b/2.x/upload/catalog/model/payment/paystack.php index 5e2804c..1e95f68 100755 --- a/2.x/upload/catalog/model/payment/paystack.php +++ b/2.x/upload/catalog/model/payment/paystack.php @@ -24,8 +24,12 @@ public function getMethod($address, $total) $status = false; } - // Paystack Only switches NGN for now - if ($status && (strtoupper($this->currency->getCode())!=='NGN')) { + // Paystack only switches NGN, GHS and USD for now + if ($status + && ((strtoupper($this->currency->getCode())!=='NGN') + || (strtoupper($this->config->getCode())!=='GHS') + || (strtoupper($this->config->getCode())!=='USD')) + ) { $status = false; } diff --git a/2.x/upload/catalog/view/theme/default/template/payment/paystack.tpl b/2.x/upload/catalog/view/theme/default/template/payment/paystack.tpl index c81f38e..fecddf0 100755 --- a/2.x/upload/catalog/view/theme/default/template/payment/paystack.tpl +++ b/2.x/upload/catalog/view/theme/default/template/payment/paystack.tpl @@ -16,6 +16,7 @@ key: '', email: '', amount: , + currency: '', ref: '', callback: function(response){ window.location.href=''; diff --git a/README.md b/README.md index 45c9b5d..5a78f32 100644 --- a/README.md +++ b/README.md @@ -10,12 +10,12 @@ Install to receive payments for your goods in naira from any Mastercard, Visa or - OpenSSL v1.0.1 or more recent ## Notes -- Paystack currently only accepts the following currencies: `NGN`. +- Paystack currently only accepts the following currencies: `NGN`, `GHS` and `USD`. - You need to have created an account on [paystack.com](https://dashboard.paystack.co/#/signup). ## Features: - Paystack payment gateway integration -- Activates payment module only when cart currency is `NGN` +- Activates payment module only when cart currency is `NGN`, `GHS` or `USD`. - Activate payment module only when order total reaches the amount you specified - Captures call back notification to automatically update order status - Simply turn on Live Mode to accept live payments. @@ -30,7 +30,7 @@ Install to receive payments for your goods in naira from any Mastercard, Visa or 5. Configure the module accordingly. - To get your live and test secret keys, visit [the Paystack Dashboard](https://dashboard.paystack.co/#/settings/developer). 6. Enable Paystack payment gateway on your OpenCart admin. -7. Add and set NGN as your default store currency. +7. Add and set NGN, GHS or USD. as your default store currency. 8. Proceed to [Paystack OpenCart Extension](http://www.opencart.com/index.php?route=extension/extension/info&extension_id=25767&filter_search=paystack) to rate our work. ## Other Configuration From fe4f8609d8c15fbd267ab73a31a41802ab787279 Mon Sep 17 00:00:00 2001 From: Stephen Amaza Date: Wed, 11 Apr 2018 18:48:14 +0100 Subject: [PATCH 2/3] Fix: Update conditional so that return is true for NGN, GHS or USD --- 1.5.x/upload/catalog/model/payment/paystack.php | 8 ++++---- 2.2.x/upload/catalog/model/payment/paystack.php | 8 ++++---- 2.3.x/upload/catalog/model/payment/paystack.php | 10 +++++----- 2.x/upload/catalog/model/payment/paystack.php | 8 ++++---- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/1.5.x/upload/catalog/model/payment/paystack.php b/1.5.x/upload/catalog/model/payment/paystack.php index d2993ee..5b2ad9a 100755 --- a/1.5.x/upload/catalog/model/payment/paystack.php +++ b/1.5.x/upload/catalog/model/payment/paystack.php @@ -19,11 +19,11 @@ public function getMethod($address, $total) // Paystack Only switches NGN, GHS and USD for now if ($status - && ((strtoupper($this->currency->getCode())!=='NGN') - || (strtoupper($this->config->getCode())!=='GHS') - || (strtoupper($this->config->getCode())!=='USD')) + && ((strtoupper($this->currency->getCode())=='NGN') + || (strtoupper($this->currency->getCode())=='GHS') + || (strtoupper($this->currency->getCode())=='USD')) ) { - $status = false; + $status = true; } $method_data = array(); diff --git a/2.2.x/upload/catalog/model/payment/paystack.php b/2.2.x/upload/catalog/model/payment/paystack.php index df6d300..b63acc8 100755 --- a/2.2.x/upload/catalog/model/payment/paystack.php +++ b/2.2.x/upload/catalog/model/payment/paystack.php @@ -26,11 +26,11 @@ public function getMethod($address, $total) // Paystack only switches NGN, GHS and USD for now if ($status - && ((strtoupper($this->config->get('config_currency'))!=='NGN') - || (strtoupper($this->config->get('config_currency'))!=='GHS') - || (strtoupper($this->config->get('config_currency'))!=='USD')) + && ((strtoupper($this->config->get('config_currency')) =='NGN') + || (strtoupper($this->config->get('config_currency')) =='GHS') + || (strtoupper($this->config->get('config_currency')) =='USD')) ) { - $status = false; + $status = true; } $method_data = array(); diff --git a/2.3.x/upload/catalog/model/payment/paystack.php b/2.3.x/upload/catalog/model/payment/paystack.php index b0f1c03..b63acc8 100755 --- a/2.3.x/upload/catalog/model/payment/paystack.php +++ b/2.3.x/upload/catalog/model/payment/paystack.php @@ -26,11 +26,11 @@ public function getMethod($address, $total) // Paystack only switches NGN, GHS and USD for now if ($status - && ((strtoupper($this->config->get('config_currency'))!=='NGN') - || (strtoupper($this->config->get('config_currency'))!=='GHS') - || (strtoupper($this->config->get('config_currency'))!=='USD')) - ) { - $status = false; + && ((strtoupper($this->config->get('config_currency')) =='NGN') + || (strtoupper($this->config->get('config_currency')) =='GHS') + || (strtoupper($this->config->get('config_currency')) =='USD')) + ) { + $status = true; } $method_data = array(); diff --git a/2.x/upload/catalog/model/payment/paystack.php b/2.x/upload/catalog/model/payment/paystack.php index 1e95f68..c7f00e0 100755 --- a/2.x/upload/catalog/model/payment/paystack.php +++ b/2.x/upload/catalog/model/payment/paystack.php @@ -26,11 +26,11 @@ public function getMethod($address, $total) // Paystack only switches NGN, GHS and USD for now if ($status - && ((strtoupper($this->currency->getCode())!=='NGN') - || (strtoupper($this->config->getCode())!=='GHS') - || (strtoupper($this->config->getCode())!=='USD')) + && ((strtoupper($this->currency->getCode())=='NGN') + || (strtoupper($this->currency->getCode())=='GHS') + || (strtoupper($this->currency->getCode())=='USD')) ) { - $status = false; + $status = true; } $method_data = array(); From 1c94fa1c8234ec69c76c57dcbc02ef3695b95b07 Mon Sep 17 00:00:00 2001 From: Stephen Amaza Date: Mon, 21 May 2018 15:48:02 +0100 Subject: [PATCH 3/3] Refactor currency conditional This avoids errors in checking admissable currencies --- .../upload/catalog/model/payment/paystack.php | 11 +- .../upload/catalog/model/payment/paystack.php | 11 +- .../controller/extension/payment/paystack.php | 234 +++++++++--------- .../upload/catalog/model/payment/paystack.php | 11 +- 2.x/upload/catalog/model/payment/paystack.php | 11 +- 5 files changed, 145 insertions(+), 133 deletions(-) diff --git a/1.5.x/upload/catalog/model/payment/paystack.php b/1.5.x/upload/catalog/model/payment/paystack.php index 5b2ad9a..63a0cc3 100755 --- a/1.5.x/upload/catalog/model/payment/paystack.php +++ b/1.5.x/upload/catalog/model/payment/paystack.php @@ -18,10 +18,13 @@ public function getMethod($address, $total) } // Paystack Only switches NGN, GHS and USD for now - if ($status - && ((strtoupper($this->currency->getCode())=='NGN') - || (strtoupper($this->currency->getCode())=='GHS') - || (strtoupper($this->currency->getCode())=='USD')) + if ($status && (!in_array( + strtoupper($this->currency->getCode()), + [ + 'NGN', + 'GHS' + ] + )) ) { $status = true; } diff --git a/2.2.x/upload/catalog/model/payment/paystack.php b/2.2.x/upload/catalog/model/payment/paystack.php index b63acc8..080848d 100755 --- a/2.2.x/upload/catalog/model/payment/paystack.php +++ b/2.2.x/upload/catalog/model/payment/paystack.php @@ -25,10 +25,13 @@ public function getMethod($address, $total) } // Paystack only switches NGN, GHS and USD for now - if ($status - && ((strtoupper($this->config->get('config_currency')) =='NGN') - || (strtoupper($this->config->get('config_currency')) =='GHS') - || (strtoupper($this->config->get('config_currency')) =='USD')) + if ($status && (!in_array( + strtoupper($this->config->get('config_currency')), + [ + 'NGN', + 'GHS' + ] + )) ) { $status = true; } diff --git a/2.3.x/upload/catalog/controller/extension/payment/paystack.php b/2.3.x/upload/catalog/controller/extension/payment/paystack.php index 65b6519..e2a91dc 100644 --- a/2.3.x/upload/catalog/controller/extension/payment/paystack.php +++ b/2.3.x/upload/catalog/controller/extension/payment/paystack.php @@ -1,117 +1,117 @@ -language->load('extension/payment/paystack'); - - $data['text_testmode'] = $this->language->get('text_testmode'); - $data['button_confirm'] = $this->language->get('button_confirm'); - - $data['livemode'] = $this->config->get('paystack_live'); - - if ($this->config->get('paystack_live')) { - $data['key'] = $this->config->get('paystack_live_public'); - } else { - $data['key'] = $this->config->get('paystack_test_public'); - } - - $this->load->model('checkout/order'); - - $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']); - - if ($order_info) { - - $data['ref'] = uniqid('' . $this->session->data['order_id'] . '-'); - $data['amount'] = intval($order_info['total'] * 100); - $data['email'] = $order_info['email']; - $data['currency'] = $order_info['currency_code']; - $data['callback'] = $this->url->link('extension/payment/paystack/callback', 'trxref=' . rawurlencode($data['ref']), 'SSL'); - - if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/paystack.tpl')) { - return $this->load->view($this->config->get('config_template') . '/template/payment/paystack.tpl', $data); - } else { - return $this->load->view('payment/paystack.tpl', $data); - - } - } - } - - private function query_api_transaction_verify($reference) - { - if ($this->config->get('paystack_live')) { - $skey = $this->config->get('paystack_live_secret'); - } else { - $skey = $this->config->get('paystack_test_secret'); - } - - $context = stream_context_create( - array( - 'http'=>array( - 'method'=>"GET", - 'header'=>"Authorization: Bearer " . $skey, - ) - ) - ); - $url = 'https://api.paystack.co/transaction/verify/'. rawurlencode($reference); - $request = file_get_contents($url, false, $context); - return json_decode($request, true); - } - - private function redir_and_die($url, $onlymeta = false) - { - if (!headers_sent() && !$onlymeta) { - header('Location: ' . $url); - } - echo ""; - die(); - } - - public function callback() - { - if (isset($this->request->get['trxref'])) { - $trxref = $this->request->get['trxref']; - - // order id is what comes before the first dash in trxref - $order_id = substr($trxref, 0, strpos($trxref, '-')); - // if no dash were in transation reference, we will have an empty order_id - if (!$order_id) { - $order_id = 0; - } - - $this->load->model('checkout/order'); - - $order_info = $this->model_checkout_order->getOrder($order_id); - - - - if ($order_info) { - if ($this->config->get('paystack_debug')) { - $this->log->write('PAYSTACK :: CALLBACK DATA: ' . print_r($this->request->get, true)); - } - - // Callback paystack to get real transaction status - $ps_api_response = $this->query_api_transaction_verify($trxref); - - $order_status_id = $this->config->get('config_order_status_id'); - - if (array_key_exists('data', $ps_api_response) && array_key_exists('status', $ps_api_response['data']) && ($ps_api_response['data']['status'] === 'success')) { - $order_status_id = $this->config->get('paystack_approved_status_id'); - $redir_url = $this->url->link('checkout/success'); - } else if (array_key_exists('data', $ps_api_response) && array_key_exists('status', $ps_api_response['data']) && ($ps_api_response['data']['status'] === 'failure')) { - $order_status_id = $this->config->get('paystack_declined_status_id'); - $redir_url = $this->url->link('checkout/checkout', '', 'SSL'); - } else { - $order_status_id = $this->config->get('paystack_error_status_id'); - $redir_url = $this->url->link('checkout/checkout', '', 'SSL'); - } - - $this->model_checkout_order->addOrderHistory($order_id, $order_status_id); - $this->redir_and_die($redir_url); - } - - } - - - } -} +language->load('extension/payment/paystack'); + + $data['text_testmode'] = $this->language->get('text_testmode'); + $data['button_confirm'] = $this->language->get('button_confirm'); + + $data['livemode'] = $this->config->get('paystack_live'); + + if ($this->config->get('paystack_live')) { + $data['key'] = $this->config->get('paystack_live_public'); + } else { + $data['key'] = $this->config->get('paystack_test_public'); + } + + $this->load->model('checkout/order'); + + $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']); + + if ($order_info) { + + $data['ref'] = uniqid('' . $this->session->data['order_id'] . '-'); + $data['amount'] = intval($order_info['total'] * 100); + $data['email'] = $order_info['email']; + $data['currency'] = $order_info['currency_code']; + $data['callback'] = $this->url->link('extension/payment/paystack/callback', 'trxref=' . rawurlencode($data['ref']), 'SSL'); + + if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/paystack.tpl')) { + return $this->load->view($this->config->get('config_template') . '/template/payment/paystack.tpl', $data); + } else { + return $this->load->view('payment/paystack.tpl', $data); + + } + } + } + + private function query_api_transaction_verify($reference) + { + if ($this->config->get('paystack_live')) { + $skey = $this->config->get('paystack_live_secret'); + } else { + $skey = $this->config->get('paystack_test_secret'); + } + + $context = stream_context_create( + array( + 'http'=>array( + 'method'=>"GET", + 'header'=>"Authorization: Bearer " . $skey, + ) + ) + ); + $url = 'https://api.paystack.co/transaction/verify/'. rawurlencode($reference); + $request = file_get_contents($url, false, $context); + return json_decode($request, true); + } + + private function redir_and_die($url, $onlymeta = false) + { + if (!headers_sent() && !$onlymeta) { + header('Location: ' . $url); + } + echo ""; + die(); + } + + public function callback() + { + if (isset($this->request->get['trxref'])) { + $trxref = $this->request->get['trxref']; + + // order id is what comes before the first dash in trxref + $order_id = substr($trxref, 0, strpos($trxref, '-')); + // if no dash were in transation reference, we will have an empty order_id + if (!$order_id) { + $order_id = 0; + } + + $this->load->model('checkout/order'); + + $order_info = $this->model_checkout_order->getOrder($order_id); + + + + if ($order_info) { + if ($this->config->get('paystack_debug')) { + $this->log->write('PAYSTACK :: CALLBACK DATA: ' . print_r($this->request->get, true)); + } + + // Callback paystack to get real transaction status + $ps_api_response = $this->query_api_transaction_verify($trxref); + + $order_status_id = $this->config->get('config_order_status_id'); + + if (array_key_exists('data', $ps_api_response) && array_key_exists('status', $ps_api_response['data']) && ($ps_api_response['data']['status'] === 'success')) { + $order_status_id = $this->config->get('paystack_approved_status_id'); + $redir_url = $this->url->link('checkout/success'); + } else if (array_key_exists('data', $ps_api_response) && array_key_exists('status', $ps_api_response['data']) && ($ps_api_response['data']['status'] === 'failure')) { + $order_status_id = $this->config->get('paystack_declined_status_id'); + $redir_url = $this->url->link('checkout/checkout', '', 'SSL'); + } else { + $order_status_id = $this->config->get('paystack_error_status_id'); + $redir_url = $this->url->link('checkout/checkout', '', 'SSL'); + } + + $this->model_checkout_order->addOrderHistory($order_id, $order_status_id); + $this->redir_and_die($redir_url); + } + + } + + + } +} diff --git a/2.3.x/upload/catalog/model/payment/paystack.php b/2.3.x/upload/catalog/model/payment/paystack.php index b63acc8..080848d 100755 --- a/2.3.x/upload/catalog/model/payment/paystack.php +++ b/2.3.x/upload/catalog/model/payment/paystack.php @@ -25,10 +25,13 @@ public function getMethod($address, $total) } // Paystack only switches NGN, GHS and USD for now - if ($status - && ((strtoupper($this->config->get('config_currency')) =='NGN') - || (strtoupper($this->config->get('config_currency')) =='GHS') - || (strtoupper($this->config->get('config_currency')) =='USD')) + if ($status && (!in_array( + strtoupper($this->config->get('config_currency')), + [ + 'NGN', + 'GHS' + ] + )) ) { $status = true; } diff --git a/2.x/upload/catalog/model/payment/paystack.php b/2.x/upload/catalog/model/payment/paystack.php index c7f00e0..69df53e 100755 --- a/2.x/upload/catalog/model/payment/paystack.php +++ b/2.x/upload/catalog/model/payment/paystack.php @@ -25,10 +25,13 @@ public function getMethod($address, $total) } // Paystack only switches NGN, GHS and USD for now - if ($status - && ((strtoupper($this->currency->getCode())=='NGN') - || (strtoupper($this->currency->getCode())=='GHS') - || (strtoupper($this->currency->getCode())=='USD')) + if ($status && (!in_array( + strtoupper($this->currency->getCode()), + [ + 'NGN', + 'GHS' + ] + )) ) { $status = true; }