|
| 1 | +<?php |
| 2 | +/* |
| 3 | +* 2007-2015 PrestaShop |
| 4 | +* |
| 5 | +* NOTICE OF LICENSE |
| 6 | +* |
| 7 | +* This source file is subject to the Academic Free License (AFL 3.0) |
| 8 | +* that is bundled with this package in the file LICENSE.txt. |
| 9 | +* It is also available through the world-wide-web at this URL: |
| 10 | +* http://opensource.org/licenses/afl-3.0.php |
| 11 | +* If you did not receive a copy of the license and are unable to |
| 12 | +* obtain it through the world-wide-web, please send an email |
| 13 | +* to license@prestashop.com so we can send you a copy immediately. |
| 14 | +* |
| 15 | +* DISCLAIMER |
| 16 | +* |
| 17 | +* Do not edit or add to this file if you wish to upgrade PrestaShop to newer |
| 18 | +* versions in the future. If you wish to customize PrestaShop for your |
| 19 | +* needs please refer to http://www.prestashop.com for more information. |
| 20 | +* |
| 21 | +* @author PrestaShop SA <contact@prestashop.com> |
| 22 | +* @copyright 2007-2015 PrestaShop SA |
| 23 | +* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) |
| 24 | +* International Registered Trademark & Property of PrestaShop SA |
| 25 | +*/ |
| 26 | + |
| 27 | +/** |
| 28 | + * @since 1.5.0 |
| 29 | + */ |
| 30 | +class PaystackPaystacksuccessModuleFrontController extends ModuleFrontController |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @see FrontController::postProcess() |
| 34 | + */ |
| 35 | + public function verify_txn($code){ |
| 36 | + $test_secretkey = Configuration::get('PAYSTACK_TEST_SECRETKEY'); |
| 37 | + $live_secretkey = Configuration::get('PAYSTACK_LIVE_SECRETKEY'); |
| 38 | + $mode = Configuration::get('PAYSTACK_MODE'); |
| 39 | + |
| 40 | + if ($mode == '1') { |
| 41 | + $key = $test_secretkey; |
| 42 | + }else{ |
| 43 | + $key = $live_secretkey; |
| 44 | + } |
| 45 | + $key = str_replace(' ', '', $key); |
| 46 | + |
| 47 | + $contextOptions = array( |
| 48 | + 'http'=>array( |
| 49 | + 'method'=>"GET", |
| 50 | + 'header'=> array("Authorization: Bearer ".$key."\r\n") |
| 51 | + ) |
| 52 | + ); |
| 53 | + |
| 54 | + $context = stream_context_create($contextOptions); |
| 55 | + $url = 'https://api.paystack.co/transaction/verify/'.$code; |
| 56 | + $request = Tools::file_get_contents($url, false, $context); |
| 57 | + $result = Tools::jsonDecode($request); |
| 58 | + return $result; |
| 59 | + } |
| 60 | + public function initContent() |
| 61 | + { |
| 62 | + $cart = $this->context->cart; |
| 63 | + $txn_code = Tools::getValue('reference'); |
| 64 | + if(Tools::getValue('reference') == ""){ |
| 65 | + $txn_code = $_POST['reference']; |
| 66 | + } |
| 67 | + $amount = Tools::getValue('amount'); |
| 68 | + $email = Tools::getValue('email'); |
| 69 | + $verification = $this->verify_txn($txn_code); |
| 70 | + |
| 71 | + if(($verification->status===false) || (!property_exists($verification, 'data')) || ($verification->data->status !== 'success')){ |
| 72 | + $date = date("Y-m-d h:i:sa"); |
| 73 | + $email = $email; |
| 74 | + $total = $amount; |
| 75 | + $status = 'failed'; |
| 76 | + Tools::redirect('404'); |
| 77 | + } else { |
| 78 | + $email = $verification->data->customer->email; |
| 79 | + $date = $verification->data->transaction_date; |
| 80 | + $total = $verification->data->amount/100; |
| 81 | + $status = 'approved'; |
| 82 | + $currency_order = new Currency($cart->id_currency); |
| 83 | + |
| 84 | + $extra_vars = array( |
| 85 | + 'transaction_id' => $txn_code, |
| 86 | + 'id' => 1, |
| 87 | + 'payment_method' => 'Paystack', |
| 88 | + 'status' => 'Paid', |
| 89 | + 'currency' => $currency_order->iso_code, |
| 90 | + 'intent' => '$intent' |
| 91 | + ); |
| 92 | + |
| 93 | + $customer = new Customer($cart->id_customer); |
| 94 | + |
| 95 | + $this->module->validateOrder( |
| 96 | + $cart->id, |
| 97 | + Configuration::get('PS_OS_PAYSTACK'), |
| 98 | + $total, |
| 99 | + $this->module->displayName, |
| 100 | + 'Paystack Reference: '.$txn_code, |
| 101 | + $extra_vars, |
| 102 | + (int)$cart->id_currency, |
| 103 | + false, |
| 104 | + $customer->secure_key |
| 105 | + ); |
| 106 | + |
| 107 | + 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); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments