-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptomarket.php
352 lines (303 loc) · 12.3 KB
/
cryptomarket.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
<?php
/**
* NOTICE OF LICENSE.
*
* This file is licenced under the Software License Agreement.
* With the purchase or the installation of the software in your application
* you accept the licence agreement.
*
* You must not modify, adapt or create derivative works of this source code
*
* @author CryptoMarket Dev Team
* @copyright 2010-2015 CryptoMarket Inc
* @license LICENSE.txt
*/
if (!defined('_PS_VERSION_')) {
exit;
}
//composer autoload
$loader = require dirname(__FILE__).'/vendor/autoload.php';
$loader->add('Cryptomkt\\Exchange\\Client\\', dirname(__FILE__));
$loader->add('Cryptomkt\\Exchange\\Configuration\\', dirname(__FILE__));
$loader->add('Cryptomkt\\Exchange\\Authentication\\', dirname(__FILE__));
class Cryptomarket extends PaymentModule
{
private $htmloutput = '';
/**
* [__construct define details of module].
*/
public function __construct()
{
$this->name = 'cryptomarket';
$this->tab = 'payments_gateways';
$this->version = '0.1.2';
$this->author = 'CryptoMarket Dev Team';
$this->className = 'cryptomarket';
$this->ps_versions_compliancy = array('min' => '1.5.x.x', 'max' => _PS_VERSION_);
$this->bootstrap = false;
$this->controllers = array('payment', 'validation');
$this->sslport = 443;
$this->verifypeer = 1;
$this->verifyhost = 2;
$this->currencies = true;
$this->module_key = 'cf627c37d30e1768e5ab1640e20be045';
$this->displayName = $this->l('CryptoCompra by CryptoMarket');
$this->description = $this->l('Accept multiple cryptocurrencies and turn into local currency as EUR, CLP, BRL and ARS. Welcome to CryptoCompra a new way for payments: simple, free and totally secure.');
$this->confirmUninstall = $this->l('Would you like uninstall this plugin?');
$this->default_min_value = 1000;
parent::__construct();
}
public function install()
{
if (!function_exists('curl_version')) {
$this->_errors[] = $this->l('Sorry, this module requires the cURL PHP extension but it is not enabled
on your server.');
return false;
}
if (!parent::install() || !$this->registerHook('invoice') || !$this->registerHook('paymentReturn')
|| !$this->registerHook('payment') || !$this->registerHook('paymentOptions')
|| !$this->addOrderState($this->l('Procesando CryptoCompra'))) {
return false;
}
return true;
}
/**
* getContent Return a valid html form.
*
* @return string html formatted
*/
public function getContent()
{
$this->_postProcess();
$this->smarty->assign(array(
'this_path' => $this->_path,
'request_uri' => htmlentities($_SERVER['REQUEST_URI']),
'js_dir' => _PS_BASE_URL_._PS_JS_DIR_,
'css_dir' => _PS_BASE_URL_._PS_CSS_DIR_,
'cryptomkt_payment_receiver' => htmlentities(
Tools::getValue(
'cryptomkt_payment_receiver',
Configuration::get('cryptomkt_payment_receiver')
),
ENT_COMPAT,
'UTF-8'
),
'cryptomkt_apikey' => htmlentities(
Tools::getValue(
'cryptomkt_apikey',
Configuration::get('cryptomkt_apikey')
),
ENT_COMPAT,
'UTF-8'
),
'cryptomkt_apisecret' => htmlentities(
Tools::getValue(
'cryptomkt_apisecret',
Configuration::get('cryptomkt_apisecret')
),
ENT_COMPAT,
'UTF-8'
), ));
$this->htmloutput = $this->display(__FILE__, 'settings.tpl');
return $this->htmloutput;
}
public function uninstall()
{
if (!parent::uninstall() || !$this->unregisterHook('displayHome')) {
return false;
}
return true;
}
public function hookPayment($params)
{
if (!$this->active) {
return;
}
$this->smarty->assign(array(
'this_path' => $this->_path,
'this_path_bw' => $this->_path,
'this_path_ssl' => Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this->name.'/',
));
return $this->display(__FILE__, 'payment.tpl');
}
public function hookPaymentReturn($params)
{
$order = ($params['objOrder']);
$state = $order->current_state;
$this->smarty->assign(array(
'state' => $state,
'this_path' => $this->_path,
'this_path_ssl' => Configuration::get('PS_FO_PROTOCOL').$_SERVER['HTTP_HOST'].__PS_BASE_URI__."modules/{$this->name}/", ));
return $this->display(__FILE__, 'payment_return.tpl');
}
public function hookPaymentOptions($params)
{
if (!$this->active) {
return false;
}
return array(0 => $this->linkToCryptoMkt());
}
/**
* getCryptoMarketClient Return a Cryptomarket PHP Client.
*
* @return Cryptomkt\Exchange\Client Object
*/
public function getCryptoMarketClient()
{
$configuration = Cryptomkt\Exchange\Configuration::apiKey(
Configuration::get('cryptomkt_apikey'),
Configuration::get('cryptomkt_apisecret')
);
return Cryptomkt\Exchange\Client::create($configuration);
}
/**
* execPayment Main function.
*
* @param PSCart $cart Default cart
*
* @return array Smarty success return
*/
public function execPayment($cart)
{
$client = $this->getCryptoMarketClient();
$currency = Currency::getCurrencyInstance((int) $cart->id_currency);
$callback_url = (
Configuration::get('PS_SSL_ENABLED') ? 'https://' : 'http://'
)
.htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8')
.__PS_BASE_URI__.'modules/'.$this->name.'/updater.php';
try {
$result = $client->getTicker(array('market' => 'ETH'.$currency->iso_code));
if ('error' === $result->status) {
return array(
'success' => false,
'message' => $this->l('Currency does not supported: '.$currency->iso_code), );
}
} catch (Exception $e) {
return array(
'success' => false,
'message' => $this->l('Currency does not supported: '.$currency->iso_code), );
}
//Min value validation
if(isset($result->data[0])){
$this->default_min_value = (float) $result->data[0]->bid * 0.001;
}
$total_order = (float) $cart->getOrderTotal(true, Cart::BOTH);
if ($total_order > $this->default_min_value) {
try {
//create order
$customer = new Customer($cart->id_customer);
$order_state = $this->getOrderState('Procesando CryptoCompra');
$this->validateOrder($cart->id, $order_state['id_order_state'], $total_order, $this->displayName, null, null, (int) $currency->id, false, $customer->secure_key);
if (_PS_VERSION_ <= '1.5') {
$success_url = (Configuration::get('PS_SSL_ENABLED') ? 'https://' : 'http://').htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8').__PS_BASE_URI__.'order-confirmation.php?id_cart='.$cart->id.'&id_module='.$this->id.'&id_order='.$this->currentOrder;
} else {
$success_url = (Configuration::get('PS_SSL_ENABLED') ? 'https://' : 'http://').htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8').__PS_BASE_URI__.'index.php?controller=order-confirmation&id_cart='.(int) $cart->id.'&id_module='.(int) $this->id.'&id_order='.$this->currentOrder.'&key='.$customer->secure_key;
}
$payment = array(
'payment_receiver' => Configuration::get('cryptomkt_payment_receiver'),
'to_receive_currency' => $currency->iso_code,
'to_receive' => $total_order,
'external_id' => $this->currentOrder,
'callback_url' => $callback_url,
'error_url' => $this->context->link->getPagelink('order'),
'success_url' => $success_url,
'refund_email' => $this->context->customer->email,
'language' => Tools::strtolower(Configuration::get('PS_LOCALE_LANGUAGE')),
);
$payload = $client->createPayOrder($payment);
if ('error' === $payload->status) {
return array('success' => false, 'message' => $payload->message);
}
\ob_clean();
Tools::redirect($payload->data->payment_url);
exit;
} catch (Exception $e) {
return array('success' => false, 'message' => $e->getMessage());
}
} else {
return array('success' => false, 'message' => $this->l('Total order must be greater than ').$currency->iso_code.' '.$min_value);
}
}
public function linkToCryptoMkt()
{
$cryptomarket_option = new PrestaShop\PrestaShop\Core\Payment\PaymentOption();
$paymentGateway = $this->fetch('module:cryptomarket/views/templates/front/payment_execution.tpl');
$cryptomarket_option->setCallToActionText($this->l('CryptoMarket'))
->setForm($paymentGateway)
->setAction(Configuration::get('PS_FO_PROTOCOL').__PS_BASE_URI__."modules/{$this->name}/payment.php");
return $cryptomarket_option;
}
/**
* addOrderState Add a order state.
*
* @param string $name Order name
*/
public function addOrderState($name)
{
$state_exist = false;
$state_exist = $this->getOrderState($name);
// If the state does not exist, we create it.
if (false !== $state_exist) {
// create new order state
$order_state = new OrderState();
$order_state->color = '#00ffff';
$order_state->send_email = true;
$order_state->module_name = $this->name;
$order_state->template = 'preparation';
$order_state->name = array();
$languages = Language::getLanguages(false);
foreach ($languages as $language) {
$order_state->name[$language['id_lang']] = $name;
}
// Update object
$order_state->add();
}
return true;
}
/**
* getOrderState get a object order state.
*
* @param string $name Order name
*
* @return object State order object
*/
public function getOrderState($name)
{
$state = false;
$states = OrderState::getOrderStates((int) $this->context->language->id);
// check if order state exist
foreach ($states as $state) {
if (in_array($name, $state, true)) {
$state = $state;
break;
}
}
return $state;
}
public function checkResponseSignature($hash, $id, $status)
{
return Cryptomkt\Exchange\Authentication\ApiKeyAuthentication::checkHash($hash, $id.$status, Configuration::get('cryptomkt_apisecret'));
}
private function _postProcess()
{
if (Tools::isSubmit('submitcryptomarket')) {
$this->_errors = array();
if (null === Tools::getValue('cryptomkt_apikey')) {
$this->_errors[] = $this->l('Missing API Key');
}
if (count($this->_errors) > 0) {
$error_msg = '';
foreach ($this->_errors as $error) {
$error_msg .= $error.'<br />';
}
$this->htmloutput = $this->displayError($error_msg);
} else {
Configuration::updateValue('cryptomkt_payment_receiver', trim(Tools::getValue('cryptomkt_payment_receiver')));
Configuration::updateValue('cryptomkt_apikey', trim(Tools::getValue('cryptomkt_apikey')));
Configuration::updateValue('cryptomkt_apisecret', trim(Tools::getValue('cryptomkt_apisecret')));
$this->htmloutput = $this->displayConfirmation($this->l('Settings updated'));
}
}
}
}