From 855b5d76e2fa5a588b58fa1a2af350d6e7736911 Mon Sep 17 00:00:00 2001 From: Roosevelt Date: Fri, 23 May 2025 14:36:45 +0100 Subject: [PATCH] pretashop after paymment fix --- .../controllers/front/paystacksuccess.php | 48 ++--- paystack/paystack.php | 188 +++++++++--------- paystack/views/templates/front/embedded.tpl | 16 +- 3 files changed, 123 insertions(+), 129 deletions(-) diff --git a/paystack/controllers/front/paystacksuccess.php b/paystack/controllers/front/paystacksuccess.php index 8419904..ddf7226 100644 --- a/paystack/controllers/front/paystacksuccess.php +++ b/paystack/controllers/front/paystacksuccess.php @@ -27,8 +27,6 @@ /** * @since 1.5.0 */ - - include_once dirname(__FILE__) . "/class-paystack-plugin-tracker.php"; class PaystackPaystacksuccessModuleFrontController extends ModuleFrontController { /** @@ -55,12 +53,31 @@ public function verify_txn($code){ $context = stream_context_create($contextOptions); $url = 'https://api.paystack.co/transaction/verify/'.$code; - $request = Tools::file_get_contents($url, false, $context); - $result = Tools::jsonDecode($request); - return $result; + + /** + * Tools::file_get_contents() and Tools::jsonDecode() are deprecated in 1.7.6 + * $request = Tools::file_get_contents($url, false, $context); + * $result = Tools::jsonDecode($request); + * switch to cURL for better performance + * and error handling + */ + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Authorization: Bearer ' . $key, + 'Cache-Control: no-cache', + )); + $response = curl_exec($ch); + $err = curl_error($ch); + curl_close($ch); + PrestaShopLogger::addLog('[Paystack cURL Error] ' . $err, 3); + return json_decode($response); } public function initContent() { + $cart = $this->context->cart; $txn_code = Tools::getValue('reference'); if(Tools::getValue('reference') == ""){ @@ -69,7 +86,7 @@ public function initContent() $amount = Tools::getValue('amount'); $email = Tools::getValue('email'); $verification = $this->verify_txn($txn_code); - + if(($verification->status===false) || (!property_exists($verification, 'data')) || ($verification->data->status !== 'success')){ $date = date("Y-m-d h:i:sa"); $email = $email; @@ -77,23 +94,6 @@ public function initContent() $status = 'failed'; Tools::redirect('404'); } else { - - //PSTK - Logger - $mode = Configuration::get('PAYSTACK_MODE'); - $test_pk = Configuration::get('PAYSTACK_TEST_PUBLICKEY'); - $live_pk = Configuration::get('PAYSTACK_LIVE_PUBLICKEY'); - if ($mode == '1') { - $key = $test_pk; - }else{ - $key = $live_pk; - } - $key = str_replace(' ', '', $key); - $pstk_logger = new presta_1_7_paystack_plugin_tracker('presta-1.7', $key ); - $pstk_logger->log_transaction_success($txn_code); - // PSTK Logger done ----------------- - - - $email = $verification->data->customer->email; $date = $verification->data->transaction_date; $total = $verification->data->amount/100; @@ -126,4 +126,4 @@ public function initContent() Tools::redirect('index.php?controller=order-confirmation&id_cart='.$cart->id.'&id_module='.$this->module->id.'&id_order='.$this->module->currentOrder.'&key='.$customer->secure_key.'&reference='.$txn_code); } } -} +} \ No newline at end of file diff --git a/paystack/paystack.php b/paystack/paystack.php index b19fec9..5f8ac39 100644 --- a/paystack/paystack.php +++ b/paystack/paystack.php @@ -39,23 +39,22 @@ class Paystack extends PaymentModule public $owner; public $address; public $extra_mail_vars; - + public function __construct() { $this->name = 'paystack'; $this->tab = 'payments_gateways'; - $this->version = '1.0.3'; + $this->version = '1.0.2'; $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_); - $this->author = 'Paystack'; + $this->author = 'Douglas Kendyson'; $this->controllers = array('payment', 'validation'); $this->is_eu_compatible = 0; - $this->module_key = '7bd648045911885fe8a9a3c6f550d76e'; $this->currencies = true; $this->currencies_mode = 'checkbox'; - $config = Configuration::getMultiple(array('PAYSTACK_TEST_SECRETKEY', 'PAYSTACK_TEST_PUBLICKEY', 'PAYSTACK_LIVE_SECRETKEY', 'PAYSTACK_LIVE_PUBLICKEY', 'PAYSTACK_MODE')); - + $config = Configuration::getMultiple(array('PAYSTACK_TEST_SECRETKEY','PAYSTACK_TEST_PUBLICKEY','PAYSTACK_LIVE_SECRETKEY','PAYSTACK_LIVE_PUBLICKEY','PAYSTACK_MODE')); + $this->bootstrap = true; parent::__construct(); @@ -79,13 +78,13 @@ public function __construct() public function install() { - if (!parent::install() || !$this->registerHook('paymentReturn') || !$this->registerHook('paymentOptions') || !$this->registerHook('header')) { + if (!parent::install() || !$this->registerHook('paymentReturn') || !$this->registerHook('paymentOptions') || !$this->registerHook('header')) { return false; } // TODO : Cek insert new state, Custom CSS $newState = new OrderState(); - + $newState->send_email = true; $newState->module_name = $this->name; $newState->invoice = true; @@ -110,7 +109,7 @@ public function install() if ($newState->add()) { Configuration::updateValue('PS_OS_PAYSTACK', $newState->id); - copy(dirname(__FILE__) . '/logo.png', _PS_IMG_DIR_ . 'tmp/order_state_mini_' . (int)$newState->id . '_1.png'); + copy(dirname(__FILE__).'/logo.png', _PS_IMG_DIR_.'tmp/order_state_mini_'.(int)$newState->id.'_1.png'); } else { return false; } @@ -121,13 +120,11 @@ public function install() public function uninstall() { - if ( - !Configuration::deleteByName('PAYSTACK_TEST_SECRETKEY') - || !Configuration::deleteByName('PAYSTACK_TEST_PUBLICKEY') - || !Configuration::deleteByName('PAYSTACK_LIVE_PUBLICKEY') - || !Configuration::deleteByName('PAYSTACK_LIVE_SECRETKEY') - || !parent::uninstall() - ) { + if (!Configuration::deleteByName('PAYSTACK_TEST_SECRETKEY') + || !Configuration::deleteByName('PAYSTACK_TEST_PUBLICKEY') + || !Configuration::deleteByName('PAYSTACK_LIVE_PUBLICKEY') + || !Configuration::deleteByName('PAYSTACK_LIVE_SECRETKEY') + || !parent::uninstall()) { return false; } return true; @@ -149,8 +146,11 @@ private function _displayPaystack() { return $this->display(__FILE__, 'infos.tpl'); } - - public function addJsRC($js_uri) + + + + + public function addJsRC($js_uri) { $this->context->controller->addJS($js_uri); } @@ -177,73 +177,77 @@ public function getContent() public function hookPaymentOptions($params) { - if (!$this->active) { + + + if (!$this->active) { return; } if (!$this->checkCurrency($params['cart'])) { return; } - if (!$this->checkCurrencyNGN($params['cart'])) { + + if (!$this->checkCurrencyNGN($params['cart'])) { return; } - $config = $this->getConfigFieldsValues(); + $config = $this->getConfigFieldsValues(); if ($config['PAYSTACK_MODE'] == 1) { - $key = $config['PAYSTACK_TEST_PUBLICKEY']; - } else { - $key = $config['PAYSTACK_LIVE_PUBLICKEY']; - } + $key = $config['PAYSTACK_TEST_PUBLICKEY']; + }else{ + $key = $config['PAYSTACK_LIVE_PUBLICKEY']; - if ($key == '') { - return; } + + if($key == ''){ + return; + } $gateway_chosen = 'none'; if (Tools::getValue('gateway') == 'paystack') { $cart = $this->context->cart; $gateway_chosen = 'paystack'; - $customer = new Customer((int)($cart->id_customer)); - $amount = $cart->getOrderTotal(true, Cart::BOTH); - $currency_order = new Currency($cart->id_currency); - $params = array( - "reference" => 'order_' . $params['cart']->id . '_' . time(), - "amount" => number_format($amount, 2), - "pcolor" => '', - "scolor" => '', - "total_amount" => $amount * 100, - "key" => $key, + $customer = new Customer((int)($cart->id_customer)); + + $amount = $cart->getOrderTotal(true, Cart::BOTH); + $currency_order = new Currency($cart->id_currency); + $params = array( + "reference" => 'order_'.$params['cart']->id.'_'.time(), + "amount" => number_format($amount, 2), + "pcolor" => '', + "scolor" => '', + "total_amount" => $amount*100, + "key" => $key, "currency" => $currency_order->iso_code, - "email" => $customer->email, - ); - $this->context->smarty->assign( - array( - 'gateway_chosen' => 'paystack', - 'form_url' => $this->context->link->getModuleLink($this->name, 'paystacksuccess', array(), true), - ) - ); + "email" => $customer->email, + ); + $this->context->smarty->assign(array( + 'gateway_chosen' => 'paystack', + 'form_url' => $this->context->link->getModuleLink($this->name, 'paystacksuccess', array(), true), + )); $this->context->smarty->assign( - $params + $params ); - } + } + + $newOption = new PaymentOption(); $newOption->setCallToActionText($this->trans('Paystack (Debit/credit cards)', array(), 'Modules.Paystack.Shop')) - ->setAction($this->context->link->getModuleLink($this->name, 'validation', array(), true)) - ->setAdditionalInformation($this->context->smarty->fetch('module:paystack/views/templates/hook/intro.tpl')) - ->setLogo(Media::getMediaPath(_PS_MODULE_DIR_ . $this->name . '/card-logos.png')) - ->setInputs( - array( - 'wcst_iframe' => array( - 'name' => 'wcst_iframe', - 'type' => 'hidden', - 'value' => '1', - ) - ) - ); - if ($gateway_chosen == 'paystack') { + ->setAction($this->context->link->getModuleLink($this->name, 'validation', array(), true)) + ->setAdditionalInformation($this->context->smarty->fetch('module:paystack/views/templates/hook/intro.tpl')) + ->setLogo(Media::getMediaPath(_PS_MODULE_DIR_.$this->name.'/card-logos.png')) + ->setInputs(array( + 'wcst_iframe' => array( + 'name' =>'wcst_iframe', + 'type' =>'hidden', + 'value' =>'1', + ) + )); + if ($gateway_chosen == 'paystack') { $newOption->setAdditionalInformation( $this->context->smarty->fetch('module:paystack/views/templates/front/embedded.tpl') ); } + $payment_options = [ $newOption, ]; @@ -271,23 +275,23 @@ public function hookPaymentReturn($params) ) )) { $paystackOwner = $this->owner; - - $this->smarty->assign( - array( - 'shop_name' => $this->context->shop->name, - 'total' => Tools::displayPrice( - $params['order']->getOrdersTotalPaid(), - new Currency($params['order']->id_currency), - false - ), - 'paystackDetails' => $paystackDetails, - 'paystackAddress' => $paystackAddress, - 'paystackOwner' => $paystackOwner, - 'status' => 'ok', - 'reference' => $reference, - 'contact_url' => $this->context->link->getPageLink('contact', true) - ) - ); + $paystackDetails = $this->details; + $paystackAddress = $this->address; + + $this->smarty->assign(array( + 'shop_name' => $this->context->shop->name, + 'total' => Tools::displayPrice( + $params['order']->getOrdersTotalPaid(), + new Currency($params['order']->id_currency), + false + ), + 'paystackDetails' => $paystackDetails, + 'paystackAddress' => $paystackAddress, + 'paystackOwner' => $paystackOwner, + 'status' => 'ok', + 'reference' => $reference, + 'contact_url' => $this->context->link->getPageLink('contact', true) + )); } else { $this->smarty->assign( array( @@ -314,12 +318,12 @@ public function checkCurrency($cart) } return false; } - - public function checkCurrencyNGN($cart) + + public function checkCurrencyNGN($cart) { $currency_order = new Currency($cart->id_currency); - if ($currency_order->iso_code == 'NGN' || $currency_order->iso_code == 'GHS' || $currency_order->iso_code == 'ZAR' || $currency_order->iso_code == 'USD' || $currency_order->iso_code == 'XOF' || $currency_order->iso_code == 'KES' || $currency_order->iso_code == 'EGP') { - return true; + if ($currency_order->iso_code == 'NGN') { + return true; } return false; } @@ -332,7 +336,7 @@ public function renderForm() 'title' => $this->trans('User details', array(), 'Modules.Paystack.Admin'), 'icon' => 'icon-user' ), - + 'input' => array( array( 'type' => 'switch', @@ -340,25 +344,25 @@ public function renderForm() 'name' => 'PAYSTACK_MODE', 'is_bool' => true, 'required' => true, - 'values' => array( + 'values' =>array( array( 'id' => 'active_on', 'value' => true, 'label' => $this->trans('Test', array(), 'Modules.Paystack.Admin') - ), array( + ),array( 'id' => 'active_off', 'value' => false, 'label' => $this->trans('False', array(), 'Modules.Paystack.Admin') ) ), ), - array( + array( 'type' => 'text', 'label' => $this->trans('Test Secret key', array(), 'Modules.Paystack.Admin'), 'name' => 'PAYSTACK_TEST_SECRETKEY', - + ), - array( + array( 'type' => 'text', 'label' => $this->trans('Test Public key', array(), 'Modules.Paystack.Admin'), 'name' => 'PAYSTACK_TEST_PUBLICKEY', @@ -367,13 +371,13 @@ public function renderForm() 'type' => 'text', 'label' => $this->trans('Live Secret key', array(), 'Modules.Paystack.Admin'), 'name' => 'PAYSTACK_LIVE_SECRETKEY', - + ), array( 'type' => 'text', 'label' => $this->trans('Live Public key', array(), 'Modules.Paystack.Admin'), 'name' => 'PAYSTACK_LIVE_PUBLICKEY', - ), + ), ), 'submit' => array( 'title' => $this->trans('Save', array(), 'Admin.Actions'), @@ -387,13 +391,13 @@ public function renderForm() $helper->table = $this->table; $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT')); $helper->default_form_language = $lang->id; - $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ?: 0; + $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? : 0; $this->fields_form = array(); $helper->id = (int)Tools::getValue('id_carrier'); $helper->identifier = $this->identifier; $helper->submit_action = 'btnSubmit'; - $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' - . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name; + $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure=' + .$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->tpl_vars = array( 'fields_value' => $this->getConfigFieldsValues(), diff --git a/paystack/views/templates/front/embedded.tpl b/paystack/views/templates/front/embedded.tpl index 65e2ddc..8ee695b 100644 --- a/paystack/views/templates/front/embedded.tpl +++ b/paystack/views/templates/front/embedded.tpl @@ -4,28 +4,18 @@ {if isset($gateway_chosen) && $gateway_chosen == 'paystack'}
- +
-{/if} +{/if} \ No newline at end of file