-
Notifications
You must be signed in to change notification settings - Fork 1
/
paybear.php
executable file
·417 lines (363 loc) · 14.8 KB
/
paybear.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
include_once _PS_MODULE_DIR_.'paybear/sdk/PayBearSDK.php';
include_once 'classes/PaybearData.php';
/**
* @property int is_eu_compatible
* @property bool bootstrap
*/
class PayBear extends PaymentModule
{
protected $_html = '';
protected $_postErrors = array();
public $details;
public $owner;
public $address;
public $extra_mail_vars;
public function __construct()
{
$this->name = 'paybear';
$this->tab = 'payments_gateways';
$this->version = '0.7.5';
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->author = 'PayBear';
$this->controllers = array('validation', 'currencies', 'payment', 'callback', 'status');
$this->is_eu_compatible = 1;
$this->currencies = true;
$this->currencies_mode = 'checkbox';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Crypto Payments by PayBear.io');
$this->description = $this->l('Allows to accept crypto payments such as Bitcoin (BTC), Ethereum (ETH) and other crypto currencies');
if (!count(Currency::checkPaymentCurrencies($this->id))) {
$this->warning = $this->l('No currency has been set for this module.');
}
if (!Configuration::get('PAYBEAR_API_SECRET') || !Configuration::get('PAYBEAR_API_PUBLIC')) {
$this->warning = $this->l('Please set your API keys');
}
}
/**
* @return bool
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
public function install()
{
if (!$this->installOrderState()) {
return false;
}
if (!$this->installSQL()) {
return false;
}
if ((float) _PS_VERSION_ < 1.7) {
// hooks for 1.6
if (!parent::install() || !$this->registerHook('payment') || !$this->registerHook('header')) {
return false;
}
} else {
// hooks for 1.7
if (!parent::install()
|| !$this->registerHook('paymentOptions')
|| !$this->registerHook('paymentReturn')
|| !$this->registerHook('header')
) {
return false;
}
}
Configuration::updateValue('PAYBEAR_TITLE', 'Pay with Crypto via PayBear (BTC/ETH/LTC and more)');
Configuration::updateValue('PAYBEAR_DESCRIPTION', '');
Configuration::updateValue('PAYBEAR_EXCHANGE_LOCKTIME', '15');
Configuration::updateValue('PAYBEAR_MAX_UNDERPAYMENT', '0.01');
Configuration::updateValue('PAYBEAR_MIN_OVERPAYMENT', '1');
return true;
}
public function hookHeader()
{
if ((float) _PS_VERSION_ >= 1.7 && Tools::getValue('controller') === 'pay') {
$this->context->controller->registerStylesheet($this->name . '-css', 'modules/' . $this->name . '/views/css/paybear.css');
$this->context->controller->registerJavascript($this->name . '-lib-js', 'modules/' . $this->name . '/views/js/paybear.js');
$this->context->controller->registerJavascript($this->name . '-js', 'modules/' . $this->name . '/views/js/payment.js');
}
}
public function hookPayment($params)
{
if (!$this->active) {
return null;
}
if (!$this->checkCurrency($params['cart'])) {
return null;
}
if (!Configuration::get('PAYBEAR_API_SECRET')) {
return null;
}
$this->smarty->assign(array(
'this_path' => $this->_path,
'this_path_bw' => $this->_path,
));
return $this->display(__FILE__, 'payment.tpl');
}
public function hookPaymentOptions($params)
{
if (!$this->active) {
return [];
}
if (!$this->checkCurrency($params['cart'])) {
return [];
}
if (!Configuration::get('PAYBEAR_API_SECRET')) {
return [];
}
return [
$this->getEmbeddedPaymentOption(),
];
}
public function checkCurrency($cart)
{
$currency_order = new Currency($cart->id_currency);
$currencies_module = $this->getCurrency($cart->id_currency);
$sdk = new PayBearSDK($this->context);
$availableCurrencies = $sdk->getCurrencies();
if (empty($availableCurrencies)) {
return false;
}
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 getEmbeddedPaymentOption()
{
libxml_use_internal_errors(true);
$embeddedOption = new \PrestaShop\PrestaShop\Core\Payment\PaymentOption();
$embeddedOption
->setModuleName($this->name)
->setCallToActionText(Configuration::get('PAYBEAR_TITLE'))
->setAction($this->context->link->getModuleLink($this->name, 'validation', array(), true))
->setAdditionalInformation(Configuration::get('PAYBEAR_DESCRIPTION'))
// ->setLogo(Media::getMediaPath(_PS_MODULE_DIR_.$this->name.'/crypto.png'))
;
return $embeddedOption;
}
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit'.$this->name))
{
/** @var array $values */
$values = Tools::getAllValues();
foreach ($values as $name => $value) {
if (strpos($name, 'paybear_') !== false) {
Configuration::updateValue(strtoupper($name), $value);
}
}
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
return $output.$this->displayForm();
}
public function displayForm()
{
// Get default language
$defaultLang = (int) Configuration::get('PS_LANG_DEFAULT');
$defaultCurrency = new Currency((int) Configuration::get('PS_CURRENCY_DEFAULT'));
// Init Fields form array
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
)
);
$fields_form[0]['form']['input'] = array(
array(
'type' => 'text',
'required' => true,
'label' => 'Title',
'name' => 'paybear_title',
),
array(
'type' => 'textarea',
'label' => 'Description',
'name' => 'paybear_description'
),
array(
'type' => 'text',
'required' => true,
'label' => 'API Key (Secret)',
'name' => 'paybear_api_secret'
),
array(
'type' => 'text',
'required' => true,
'label' => 'API Key (Public)',
'name' => 'paybear_api_public'
),
array(
'type' => 'text',
'label' => 'Exchange Rate Lock Time',
'name' => 'paybear_exchange_locktime',
'desc' => 'Lock Fiat to Crypto exchange rate for this long (in minutes, 15 is the recommended minimum)'
),
array(
'type' => 'text',
'label' => sprintf('Underpayment (%s)', $defaultCurrency->iso_code),
'name' => 'paybear_max_underpayment',
'desc' => 'The client will be notified and required to pay the balance owed for underpayments greater than this specified amount'
),
array(
'type' => 'text',
'label' => sprintf('Overpayment (%s)', $defaultCurrency->iso_code),
'name' => 'paybear_min_overpayment',
'desc' => ' The client will be notified about their overpayment if it is greater than this amount. You will then need to issue the overpayment refund.'
)
);
$helper = new HelperForm();
// Module, token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
// Language
$helper->default_form_language = $defaultLang;
$helper->allow_employee_form_lang = $defaultLang;
// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true; // false -> remove toolbar
$helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen.
$helper->submit_action = 'submit'.$this->name;
$helper->toolbar_btn = array(
'save' =>
array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
),
'back' => array(
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
// Load current value
$helper->fields_value['paybear_title'] = Configuration::get('PAYBEAR_TITLE');
$helper->fields_value['paybear_description'] = Configuration::get('PAYBEAR_DESCRIPTION');
$helper->fields_value['paybear_exchange_locktime'] = Configuration::get('PAYBEAR_EXCHANGE_LOCKTIME');
$helper->fields_value['paybear_api_secret'] = Configuration::get('PAYBEAR_API_SECRET');
$helper->fields_value['paybear_api_public'] = Configuration::get('PAYBEAR_API_PUBLIC');
$helper->fields_value['paybear_max_underpayment'] = Configuration::get('PAYBEAR_MAX_UNDERPAYMENT');
$helper->fields_value['paybear_min_overpayment'] = Configuration::get('PAYBEAR_MIN_OVERPAYMENT');
return $helper->generateForm($fields_form);
}
private function installSQL()
{
$sql = array();
$sql[] = 'CREATE TABLE IF NOT EXISTS `' ._DB_PREFIX_. 'paybear_data` (
`id_paybear` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`order_reference` VARCHAR(9) NOT NULL,
`token` VARCHAR(256) NULL DEFAULT NULL,
`address` VARCHAR(256),
`invoice` VARCHAR(256),
`amount` DECIMAL(20, 8),
`confirmations` INT(2) NULL DEFAULT NULL,
`max_confirmations` INT(2) NULL DEFAULT NULL,
`date_add` DATETIME NULL DEFAULT NULL,
`date_upd` DATETIME NULL DEFAULT NULL,
KEY `order_reference` (`order_reference`),
KEY `token` (`token`)
) ENGINE = ' ._MYSQL_ENGINE_;
$sql[] = 'CREATE TABLE IF NOT EXISTS `' ._DB_PREFIX_. 'paybear_transaction` (
`id_paybear_transaction` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`order_reference` VARCHAR(9) NOT NULL,
`invoice` VARCHAR(255) NULL DEFAULT NULL,
`blockchain` VARCHAR(255) NULL DEFAULT NULL,
`address` VARCHAR(255) NULL DEFAULT NULL,
`amount` DECIMAL(20, 8),
`currency` VARCHAR(255) NULL DEFAULT NULL,
`rate` DECIMAL(20, 8) NULL DEFAULT NULL,
`transaction_hash` VARCHAR(255) NULL DEFAULT NULL,
`confirmations` INT(2) NULL DEFAULT NULL,
`max_confirmations` INT(2) NULL DEFAULT NULL,
`date_add` DATETIME NULL DEFAULT NULL,
`date_upd` DATETIME NULL DEFAULT NULL,
KEY `order_reference` (`order_reference`),
KEY `blockchain` (`blockchain`),
KEY `transaction_hash` (`transaction_hash`)
) ENGINE = ' ._MYSQL_ENGINE_;
foreach ($sql as $q) {
if (!DB::getInstance()->execute($q)) {
return false;
}
}
return true;
}
/**
* Create order state
*
* @return boolean
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
public function installOrderState()
{
$states = array(
array(
'name' => 'PAYBEAR_OS_WAITING',
'color' => '#4775de',
'title' => 'Awaiting for PayBear payment'
),
array(
'name' => 'PAYBEAR_OS_WAITING_CONFIRMATIONS',
'color' => '#4775de',
'title' => 'Awaiting for PayBear payment confirmations'
),
array(
'name' => 'PAYBEAR_OS_MISPAID',
'color' => '#8f0621',
'title' => 'Mispaid'
),
array(
'name' => 'PAYBEAR_OS_LATE_PAYMENT_RATE_CHANGED',
'color' => '#8f0621',
'title' => 'Late Payment/Rate changed'
),
);
$allOrderStates = OrderState::getOrderStates($this->context->language->id);
$allOrderStatesOrdered = [];
foreach ($allOrderStates as $orderState) {
$allOrderStatesOrdered[$orderState['name']] = $orderState;
}
foreach ($states as $state) {
if (!Configuration::get($state['name'])
|| !Validate::isLoadedObject(new OrderState(Configuration::get($state['name'])))) {
if (!isset($allOrderStatesOrdered[$state['title']])) {
$orderState = new OrderState();
$orderState->name = array();
foreach (Language::getLanguages() as $language) {
$orderState->name[$language['id_lang']] = $state['title'];
}
$orderState->send_email = false;
$orderState->color = $state['color'];
$orderState->hidden = false;
$orderState->delivery = false;
$orderState->logable = false;
$orderState->invoice = false;
if ($orderState->add()) {
$source = _PS_MODULE_DIR_.'paybear/logo_os.png';
$destination = _PS_ROOT_DIR_.'/img/os/'.(int) $orderState->id.'.gif';
copy($source, $destination);
}
} else {
$orderState = new OrderState($allOrderStatesOrdered[$state['title']]['id_order_state']);
}
Configuration::updateValue($state['name'], (int) $orderState->id);
}
}
return true;
}
}