-
Notifications
You must be signed in to change notification settings - Fork 1
/
cosmospay.php
267 lines (212 loc) · 9.51 KB
/
cosmospay.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/*
* 2007-2015 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2015 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
// https://github.com/PrestaShop/paymentexample
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
if (!defined('_PS_VERSION_')) {
exit;
}
class cosmospay extends PaymentModule
{
protected $_html = '';
protected $_postErrors = array();
public $details;
public $owner;
public $address;
public $extra_mail_vars;
public function __construct()
{
$this->name = 'cosmospay';
$this->tab = 'payments_gateways';
$this->version = '1.0.16';
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
$this->author = 'Bitcanna';
$this->controllers = array('validation');
$this->is_eu_compatible = 1;
$this->currencies = true;
$this->currencies_mode = 'checkbox';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Cosmos Pay');
$this->description = $this->l('The Cosmos Pay Module allows you to accept cryptocurrency payments on your Prestashop site.');
if (!count(Currency::checkPaymentCurrencies($this->id))) {
$this->warning = $this->l('No currency has been set for this module.');
}
}
public function install()
{
$sqlCreate = "CREATE TABLE `" . _DB_PREFIX_ . "cosmos_transaction` (
`id_order` int UNSIGNED NOT NULL,
`chain_pay` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`lcd_pay` varchar(200) DEFAULT NULL,
`viewDenom` varchar(10) NOT NULL DEFAULT 'BCNA',
`target_address` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`memo` varchar(20) DEFAULT NULL,
`status` enum('new','unconfirmed','complete','canceled','timeout') DEFAULT NULL,
`tx_hash` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`coin_amount` decimal(16,3) DEFAULT NULL,
`fiat_amount` decimal(16,2) DEFAULT NULL,
`date_add` datetime NOT NULL,
`method` enum('keplr','another') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT 'keplr'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; ";
Db::getInstance()->execute($sqlCreate);
if (!parent::install() || !$this->registerHook('paymentOptions') || !$this->registerHook('paymentReturn')) {
return false;
}
return true;
}
public function uninstall(){
$sql = "DROP TABLE `" . _DB_PREFIX_ . "cosmos_transaction`";
Db::getInstance()->execute($sql);
// TODO delete config
// $sqlConfig1 = "DELETE FROM `" . _DB_PREFIX_ . "configuration` WHERE `name` = 'COSMOS_PAY_DISCLAIMER'";
return parent::uninstall();
}
public function processConfiguration()
{
if (Tools::isSubmit('accept_disclaimer')) {
$disclamer1 = Tools::getValue('disclamer1');
var_dump($disclamer1);
$this->context->smarty->assign('disclaimer_error', false);
if($disclamer1 === 'on') {
Configuration::updateValue('COSMOS_PAY_DISCLAIMER', true);
} else
$this->context->smarty->assign('disclaimer_error', true);
}
if (Tools::isSubmit('mymod_pc_form')) {
$moduleTitle = Tools::getValue('moduleTitle');
$moduleDesc = Tools::getValue('moduleDesc');
$selectChain = Tools::getValue('selectChain');
$chainsAddr = Tools::getValue('selectChainAddr');
$checkChains = Tools::getValue('checkChains');
$input = Tools::getValue('input');
//var_dump($input);
Configuration::updateValue('CONF_COSMOS_TITLE', $moduleTitle);
Configuration::updateValue('CONF_COSMOS_DESC', $moduleDesc);
Configuration::updateValue('CONF_COSMOS_CHAINS', serialize($checkChains));
Configuration::updateValue('CONF_COSMOS_CHAINS_ADDR', serialize($input));
/*if (!$checkChains) {
Configuration::updateValue('CONF_COSMOS_CHAINS', '');
Configuration::updateValue('CONF_COSMOS_CHAINS_ADDR', '');
} else {
Configuration::updateValue('CONF_COSMOS_CHAINS', serialize($checkChains));
Configuration::updateValue('CONF_COSMOS_CHAINS_ADDR', serialize($input));
}*/
$this->context->smarty->assign('confirmation', 'ok');
}
}
public function assignConfiguration()
{
$CosmosPayDisclaimer = Configuration::get('COSMOS_PAY_DISCLAIMER');
$this->context->smarty->assign('disclaimer_check', $CosmosPayDisclaimer);
if ($CosmosPayDisclaimer === false) {
$getDisclamer = file_get_contents('https://store-api.bitcanna.io/disclaimer');
$this->context->smarty->assign('disclaimer_data', $getDisclamer);
}
$json = file_get_contents('https://store-api.bitcanna.io');
//$json = file_get_contents('https://raw.githubusercontent.com/BitCannaGlobal/cosmospay-api/main/cosmos.config.test.json');
//$array = (array)json_decode(json_encode($json));
$obj = json_decode($json);
$this->context->smarty->assign('configCosmos', $obj);
$moduleTitle = Configuration::get('CONF_COSMOS_TITLE');
$moduleDesc = Configuration::get('CONF_COSMOS_DESC');
$selectChain = Configuration::get('CONF_COSMOS_CHAINS');
$selectChainAddr = Configuration::get('CONF_COSMOS_CHAINS_ADDR');
$unserializeChains = unserialize($selectChain);
$unserializeChainsAddr = unserialize($selectChainAddr);
$this->context->smarty->assign('moduleTitle', $moduleTitle);
$this->context->smarty->assign('moduleDesc', $moduleDesc);
if (!$unserializeChains) {
$this->context->smarty->assign('selectChain', []);
} else
$this->context->smarty->assign('selectChain', $unserializeChains);
if (!$unserializeChainsAddr) {
$this->context->smarty->assign('inputActive', []);
} else
$this->context->smarty->assign('inputActive', $unserializeChainsAddr);
$this->context->smarty->assign('disclaimer_error', true);
}
public function getContent()
{
$this->processConfiguration();
$this->assignConfiguration();
//var_dump(Configuration::get('MYMOD_GRADES'));
return $this->display(__FILE__, 'getContent.tpl');
}
public function hookPaymentOptions($params)
{
if (!$this->active) {
return;
}
if (!$this->checkCurrency($params['cart'])) {
return;
}
$payment_options = [
$this->getOfflinePaymentOption(),
];
return $payment_options;
}
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;
}
public function getOfflinePaymentOption()
{
$moduleDesc = Configuration::get('CONF_COSMOS_DESC');
$this->context->smarty->assign('moduleDesc', $moduleDesc);
$offlineOption = new PaymentOption();
$offlineOption->setCallToActionText($this->l('Pay with Cosmos Pay'))
->setAction($this->context->link->getModuleLink($this->name, 'validation', array(), true))
->setAdditionalInformation($this->context->smarty->fetch('module:cosmospay/views/templates/front/payment_infos.tpl'));
// ->setLogo(Media::getMediaPath(_PS_MODULE_DIR_.$this->name.'/logo.png'));
return $offlineOption;
}
protected function generateForm()
{
$months = [];
for ($i = 1; $i <= 12; $i++) {
$months[] = sprintf("%02d", $i);
}
$years = [];
for ($i = 0; $i <= 10; $i++) {
$years[] = date('Y', strtotime('+'.$i.' years'));
}
$this->context->smarty->assign([
'action' => $this->context->link->getModuleLink($this->name, 'validation', array(), true),
'months' => $months,
'years' => $years,
]);
return $this->context->smarty->fetch('module:cosmospay/views/templates/front/payment_form.tpl');
}
}