forked from PaystackHQ/plugin-prestashop-1.6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprestapaystack.php
187 lines (168 loc) · 7.19 KB
/
prestapaystack.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/*
* 2016 Paystack
*
* @author kendysonD
*/
if (!defined('_CAN_LOAD_FILES_'))
exit;
include_once(_PS_MODULE_DIR_.'prestapaystack/classes/paystackcode.php');
class PrestaPaystack extends PaymentModule{
private $_postErrors = array();
public function __construct(){
$this->name = 'prestapaystack';
$this->tab = 'payments_gateways';
$this->version = '1.0.3';
$this->bootstrap = true;
$this->author = 'Douglas Kendyson';
$this->description = 'Paystack for prestashop, Accept online card payments in Naira.';
$this->module_key = 'b3d1676652fa4e8c9be762f5ec29aa2b';
parent::__construct();
$this->displayName = 'Paystack';
require(_PS_MODULE_DIR_.'prestapaystack/backward_compatibility/backward.php');
$this->context->smarty->assign('base_dir', __PS_BASE_URI__);
}
public function install(){
$this->registerHook('orderConfirmation');
$this->registerHook('return');
if (!parent::install() || !$this->registerHook('payment') || !$this->registerHook('return') || !$this->registerHook('orderConfirmation') ||
!Db::getInstance()->Execute('CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'paystack_txncodes` (
`id` int(10) NOT NULL,
`cart_id` int(11) NOT NULL,
`code` varchar(32) DEFAULT NULL
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;ALTER TABLE `'._DB_PREFIX_.'paystack_txncodes`
ADD PRIMARY KEY (`id`);ALTER TABLE `'._DB_PREFIX_.'paystack_txncodes`
MODIFY `id` int(10) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=24;')) // prod | test
return false;
if (!$this->installOrderState())
return false;
return true;
}
public function uninstall(){
// Uninstall parent and unregister Configuration
// Db::getInstance()->Execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'paystack_txncodes`');
// $orderState = new OrderState((int)Configuration::get('VOGU_WAITING_PAYMENT'));
// $orderState->delete();
// Configuration::deleteByName('VOGU_WAITING_PAYMENT');
if (!parent::uninstall())
return false;
return true;
}
public function getHookController($hook_name){
// Include the controller file
require_once(dirname(__FILE__).'/controllers/hook/'.$hook_name.'.php');
// Build the controller name dynamically
$controller_name = $this->name.$hook_name.'Controller';
// Instantiate controller
$controller = new $controller_name($this, __FILE__,$this->_path);
// Return the controller
return $controller;
}
public function installOrderState(){
if (Configuration::get('PS_OS_PRESTAPAYSTACK_PAYMENT') < 1){
$order_state = new OrderState();
$order_state->send_email = false;
$order_state->module_name = $this->name;
$order_state->invoice = false;
$order_state->color = '#98c3ff';
$order_state->logable = true;
$order_state->shipped = false;
$order_state->unremovable = false;
$order_state->delivery = false;
$order_state->hidden = false;
$order_state->paid = false;
$order_state->deleted = false;
$order_state->name = array((int)Configuration::get('PS_LANG_DEFAULT') => pSQL($this->l('Paystack - Awaiting payment')));
$order_state->template = array();
foreach (LanguageCore::getLanguages() as $l)
$order_state->template[$l['id_lang']] = 'prestapaystack';
// We copy the mails templates in mail directory
foreach (LanguageCore::getLanguages() as $l)
{
$module_path = dirname(__FILE__).'/views/templates/mails/'.$l['iso_code'].'/';
$application_path = dirname(__FILE__).'/../../mails/'.$l['iso_code'].'/';
if (!copy($module_path.'prestapaystack.txt', $application_path.'prestapaystack.txt') ||
!copy($module_path.'prestapaystack.html', $application_path.'prestapaystack.html'))
return false;
}
if ($order_state->add())
{
// We save the order State ID in Configuration database
Configuration::updateValue('PS_OS_PRESTAPAYSTACK_PAYMENT', $order_state->id);
// We copy the module logo in order state logo directory
copy(dirname(__FILE__).'/logo.png', dirname(__FILE__).'/../../img/os/'.$order_state->id.'.gif');
copy(dirname(__FILE__).'/logo.png', dirname(__FILE__).'/../../img/tmp/order_state_mini_'.$order_state->id.'.gif');
}
else
return false;
}
return true;
}
public function getContent()
{
$controller = $this->getHookController('getContent');
return $controller->run();
}
public function hookReturn($params){
$this->smarty->assign(array('vogURedirection' => Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this->name.'/'));
return $this->display(__FILE__, 'return.tpl');
}
public function validation($verification){
$transaction = array();
$t = array();
if(Tools::getValue('txn_code') !== ''){
$txn_code = Tools::getValue('txn_code');
$amount = Tools::getValue('amounttotal');
$email = Tools::getValue('email');
$o_exist = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'paystack_txncodes` WHERE `code` = "'.$txn_code.'"');//Rproduct::where('code', '=', $code)->first();
if (count($o_exist) > 0) {
$idCart = @$o_exist[0][cart_id];
$this->context->cart = @new Cart((int)$idCart);
}
if ($verification->status == 'success') {
$email = $verification->data->customer->email;
$date = $verification->data->transaction_date;
$total = $verification->data->amount/100;
$status = 'approved';
}else{
$date = date("Y-m-d h:i:sa");
$email = $email;
$total = $amount;
$status = 'failed';
}
$transaction_id = $txn_code;
}
if (Validate::isLoadedObject($this->context->cart)){
if ($this->context->cart->getOrderTotal() != $total){
Logger::AddLog('[Paystack] The shopping card '.(int)$idCart.' doesn\'t have the correct amount expected during payment validation', 2, null, null, null, true);
}else{
$currency = new Currency((int)$this->context->cart->id_currency);
if (trim(Tools::strtolower($status)) == 'approved'){
$this->validateOrder((int)$this->context->cart->id, (int)Configuration::get('PS_OS_PAYMENT'), (float)$this->context->cart->getOrderTotal(), $this->displayName, $transaction_id, array(), NULL, false, $this->context->cart->secure_key);
$new_order = new Order((int)$this->currentOrder);
if (Validate::isLoadedObject($new_order)){
$payment = $new_order->getOrderPaymentCollection();
$payment[0]->transaction_id = $transaction_id;
$payment[0]->update();
}else{
Logger::AddLog('[Paystack] The shopping card '.(int)$idCart.' has an incorrect token given from VogU during payment validation', 2, null, null, null, true);
}
}else{
Logger::AddLog('[Paystack] The shopping card '.(int)$idCart.' has an incorrect token given from VogU during payment validation', 2, null, null, null, true);
}
}
}else{
Logger::AddLog('[Paystack] The shopping card '.(int)$idCart.' was not found during the payment validation step', 2, null, null, null, true);
}
}
public function hookDisplayPayment($params){
if (!$this->active || Configuration::get('PAYSTACK_MODE') == '')
return false;
$controller = $this->getHookController('displayPayment');
return $controller->run($params);
}
public function hookDisplayPaymentReturn($params){
$controller = $this->getHookController('displayPaymentReturn');
return $controller->run($params);
}
}