-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbanglapay.php
More file actions
executable file
·228 lines (193 loc) · 8.29 KB
/
banglapay.php
File metadata and controls
executable file
·228 lines (193 loc) · 8.29 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
class banglapay extends PaymentModule
{
private $_html = '';
private $_postErrors = array();
function __construct()
{
$this->name = 'banglapay';
$this->tab = 'payments_gateways';
$this->author = 'Nascenia';
$this->version = 1;
$this->controllers = array('payment', 'dbblredirect');
$this->bootstrap = true;
parent::__construct(); // The parent construct is required for translations
$this->page = basename(__FILE__, '.php');
$this->displayName = $this->l('Dutch Bangla Payment');
$this->description = $this->l('Accepts payment by DBBL cards (Nexus, Master Visa)');
}
public function install()
{
if (!parent::install() || !$this->createDbblPaymentTable() || !$this->registerHook('payment') || !$this->registerHook('paymentReturn') || !$this->registerHook('header') || !$this->installTab())
return false;
$this->setupStatus();
$this->setupDefaultConfigurationValues();
$this->updateDbblPaymentTransactionAmount();
$command = "rm -f " . _PS_THEME_DIR_ . "modules/" . $this->name . " && ln -s " . _PS_MODULE_DIR_ . $this->name . "/themes/autumn/modules/" . $this->name . " " . _PS_THEME_DIR_ . "modules/" . $this->name . " 2>&1";
echo "Command: " . $command;
file_put_contents(_PS_ROOT_DIR_ . "/log/dbbl-commands.log", $command . "\n", FILE_APPEND);
$output = shell_exec($command);
file_put_contents(_PS_ROOT_DIR_ . "/log/dbbl-commands.log", $output . "\n", FILE_APPEND);
return true;
}
public function uninstall()
{
if (!parent::uninstall() || !$this->uninstallTab())
return false;
// if(!$this->dropDbblPaymentTable())
// return false;
return true;
}
public function installTab()
{
$parentTab = new Tab();
$parentTab->active = 1;
$parentTab->name = array();
$parentTab->class_name = "AdminBanglapay";
foreach (Language::getLanguages() as $lang) {
$parentTab->name[$lang['id_lang']] = "Banglapay";
}
$parentTab->id_parent = 0;
$parentTab->module = $this->name;
$response = $parentTab->add();
return $response;
}
public function uninstallTab()
{
$id_tabs = array(
(int)Tab::getIdFromClassName('AdminBanglapay')
);
foreach ($id_tabs as $id_tab) {
$tab = new Tab($id_tab);
$tab->delete();
}
return true;
}
public function setupStatus()
{
// check if the order status is defined
if (!defined('_PS_OS_AWAITING_DBBL_PAYMENT_')) {
// order status is not defined - check if, it exists in the table
$rq = Db::getInstance()->getRow('
SELECT `id_order_state` FROM `' . _DB_PREFIX_ . 'order_state_lang`
WHERE id_lang = \'' . pSQL('1') . '\' AND name = \'' . pSQL('Awaiting DBBL payment') . '\'');
if ($rq && isset($rq['id_order_state']) && intval($rq['id_order_state']) > 0) {
// order status exists in the table - define it.
define('_PS_OS_AWAITING_DBBL_PAYMENT_', $rq['id_order_state']);
} else {
// order status doesn't exist in the table
// insert it into the table and then define it.
Db::getInstance()->Execute('
INSERT INTO `' . _DB_PREFIX_ . 'order_state` (`unremovable`, `color`, `module_name`) VALUES(1, \'lightblue\', \'' . $this->name . '\')');
$stateid = Db::getInstance()->Insert_ID();
Db::getInstance()->Execute('INSERT INTO `' . _DB_PREFIX_ . 'order_state_lang` (`id_order_state`, `id_lang`, `name`)
VALUES(' . intval($stateid) . ', 1, \'Awaiting DBBL payment\')');
define('_PS_OS_AWAITING_DBBL_PAYMENT_', $stateid);
Configuration::updateValue('PS_OS_AWAITING_DBBL_PAYMENT', $stateid);
}
}
}
public function setupDefaultConfigurationValues()
{
if (Configuration::get('BANGLAPAY_DBBL_LIB_DIRECTORY') == null || Configuration::get('BANGLAPAY_DBBL_LIB_DIRECTORY') == "")
Configuration::updateValue('BANGLAPAY_DBBL_LIB_DIRECTORY', "/home/deployer/dbbl/production");
if (Configuration::get('BANGLAPAY_TRANSACTION_ID_PREFIX') == null || Configuration::get('BANGLAPAY_TRANSACTION_ID_PREFIX') == "")
Configuration::updateValue('BANGLAPAY_TRANSACTION_ID_PREFIX', "gj-");
return true;
}
public function hookPayment($params)
{
if (!$this->active)
return;
if (!$this->checkCurrency($params['cart']))
return;
$this->smarty->assign(array(
'this_path' => $this->_path,
'this_path_dbbl' => $this->_path,
'this_path_ssl' => Tools::getShopDomainSsl(true, true) . __PS_BASE_URI__ . 'modules/' . $this->name . '/'
));
return $this->display(__FILE__, 'payment.tpl');
}
public function hookDisplayHeader()
{
$this->context->controller->addCSS($this->_path . 'banglapay.css', 'all');
}
public function hookPaymentReturn($params)
{
if (!$this->active)
return;
$state = $params['objOrder']->getCurrentState();
return $this->display(__FILE__, 'payment_return.tpl');
}
public function checkCurrency($cart)
{
$currency_order = new Currency($cart->id_currency);
$currencies_module = $this->getCurrency($cart->id_currency);
if (is_array($currencies_module))
foreach ($currencies_module as $currency_module)
if ($currency_order->id == $currency_module['id_currency'])
return true;
return false;
}
function createDbblPaymentTable()
{
$db = Db::getInstance();
$query = "CREATE TABLE `" . _DB_PREFIX_ . "dbbl_payments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cart_id` int(11) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
`status` varchar(255) DEFAULT NULL,
`result` varchar(255) DEFAULT NULL,
`result_code` varchar(255) DEFAULT NULL,
`card_number` varchar(255) DEFAULT NULL,
`dbbl_transaction_id` varchar(255) DEFAULT NULL,
`dbbl_request` varchar(255) DEFAULT NULL,
`dbbl_response` varchar(500) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB";
$db->Execute($query);
$db->Execute("ALTER TABLE `" . _DB_PREFIX_ . "dbbl_payments` ADD COLUMN amount FLOAT(11) DEFAULT NULL AFTER `order_id`");
return true;
}
function dropDbblPaymentTable()
{
$db = Db::getInstance();
$query = "DROP TABLE `" . _DB_PREFIX_ . "dbbl_payments`";
$db->Execute($query);
return true;
}
public function getContent()
{
if (Tools::isSubmit('banglapay_config')) {
$this->updateConfiguration();
}
$this->loadConfiguration();
return $this->display(__FILE__, 'config.tpl');
}
public function loadConfiguration()
{
$dbbl_lib_directory = Configuration::get('BANGLAPAY_DBBL_LIB_DIRECTORY');
$transaction_id_prefix = Configuration::get('BANGLAPAY_TRANSACTION_ID_PREFIX');
$this->context->smarty->assign('banglapay_lib_dir', $dbbl_lib_directory);
$this->context->smarty->assign('banglapay_transaction_id_prefix', $transaction_id_prefix);
}
public function updateConfiguration()
{
Configuration::updateValue('BANGLAPAY_DBBL_LIB_DIRECTORY', Tools::getValue('banglapay_lib_dir'));
Configuration::updateValue('BANGLAPAY_TRANSACTION_ID_PREFIX', Tools::getValue('banglapay_transaction_id_prefix'));
}
public function updateDbblPaymentTransactionAmount()
{
$sql = 'SELECT * FROM ' . _DB_PREFIX_ . 'dbbl_payments where amount is null';
$results = Db::getInstance()->ExecuteS($sql);
foreach ($results as $row){
$cart = new Cart($row["cart_id"]);
$amount = $cart->getOrderTotal(true, Cart::BOTH);
Db::getInstance()->Execute('
UPDATE `' . _DB_PREFIX_ . 'dbbl_payments` set `amount` = ' . $amount . '
where `id` = \'' . $row["id"] . '\'');
}
}
}