Skip to content

Commit 3e2a4b8

Browse files
author
Mario Pando
committed
fix order updating
1 parent e87e9c5 commit 3e2a4b8

File tree

3 files changed

+42
-52
lines changed

3 files changed

+42
-52
lines changed

cryptomarket/cryptomarket.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ public function execPayment($cart) {
201201

202202
$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';
203203

204+
205+
204206
try {
205207
$result = $client->getTicker(array('market' => 'ETH' . $currency->iso_code));
206208
if($result->status === 'error'){
@@ -212,18 +214,28 @@ public function execPayment($cart) {
212214

213215
//Min value validation
214216
$min_value = (float) $result->data[0]->bid * 0.001;
215-
$total_order = (float) $cart->getOrderTotal();
217+
$total_order = (float)$cart->getOrderTotal(true, Cart::BOTH);
216218

217-
if ($total_order > $min_value) {
219+
if ($total_order > $min_value) {
218220
try {
221+
//create order
222+
$customer = new Customer($cart->id_customer);
223+
$this->validateOrder($cart->id, Configuration::get('PS_OS_PREPARATION'), $total_order, $this->displayName, NULL, NULL, (int)$currency->id, false, $customer->secure_key);
224+
225+
if (_PS_VERSION_ <= '1.5') {
226+
$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;
227+
} else {
228+
$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;
229+
}
230+
219231
$payment = array(
220232
'payment_receiver' => Configuration::get('payment_receiver'),
221233
'to_receive_currency' => $currency->iso_code,
222234
'to_receive' => $total_order,
223-
'external_id' => $cart->id,
235+
'external_id' => $this->currentOrder,
224236
'callback_url' => $callback_url,
225237
'error_url' => $this->context->link->getPagelink('order'),
226-
'success_url' => $this->context->link->getPagelink('order'),
238+
'success_url' => $success_url,
227239
'refund_email' => $this->context->customer->email,
228240
'language' => strtolower(Configuration::get('PS_LOCALE_LANGUAGE'))
229241
);

cryptomarket/updater.php

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public function updateOrderStates(){
3232
}
3333

3434
if (false === array_key_exists('id', $payload)) {
35-
error_log('[Error] Plugin did not receive an Order ID present in payload: ' . var_export($payload, true));
35+
error_log('[Error] Plugin did not receive an Pay Order ID present in payload: ' . var_export($payload, true));
3636
exit;
3737
} else {
38-
error_log('[Info] Order ID present in payload...');
38+
error_log('[Info] Pay Order ID present in payload...');
3939
}
4040

4141
if (false === array_key_exists('signature', $payload) && $payload->signature === (string) $this->getHash('sha384', $payload->id . $payload->status, Configuration::get('apisecret')) ) {
@@ -52,86 +52,64 @@ public function updateOrderStates(){
5252
error_log('[Info] Order ID present in JSON payload...');
5353
}
5454

55-
$cart_id = $payload->external_id; error_log('[Info] Cart ID:' . $cart_id);
56-
57-
if ( $order = (int)Order::getOrderByCartId($cart_id)) {
58-
error_log('[Error] The Plugin was called but could not retrieve the cart details for cart_id: "' . $cart_id . '". If you use an alternative order numbering system, please see class-wc-gateway-cryptomarket.php to apply a search filter.');
59-
throw new \Exception('The Plugin was called but could not retrieve the cart details for cart_id ' . $cart_id . '. Cannot continue!');
60-
} else {
61-
error_log('[Info] Order details retrieved successfully...');
62-
}
55+
$order_id = $payload->external_id; error_log('[Info] Order ID:' . $order_id);
6356

6457
switch ($payload->status) {
6558
case "-4":
66-
error_log('[Info] Pago múltiple. Cart ID:'.$cart_id);
67-
$status_cryptomarket = Configuration::get('PS_OS_PREPARATION');
59+
error_log('[Info] Pago múltiple. Order ID:'.$order_id);
60+
$status_cryptomarket = Configuration::get('PS_OS_ERROR');
6861

6962
break;
7063
case "-3":
71-
error_log('[Info] Monto pagado no concuerda. Cart ID:'.$cart_id);
64+
error_log('[Info] Monto pagado no concuerda. Order ID:'.$order_id);
7265
$status_cryptomarket = Configuration::get('PS_OS_ERROR');
7366

7467
break;
7568
case "-2":
76-
error_log('[Info] Falló conversión. Cart ID:'.$cart_id);
69+
error_log('[Info] Falló conversión. Order ID:'.$order_id);
7770
$status_cryptomarket = Configuration::get('PS_OS_ERROR');
7871

7972
break;
8073
case "-1":
81-
error_log('[Info] Expiró orden de pago. Cart ID:'.$cart_id);
74+
error_log('[Info] Expiró orden de pago. Order ID:'.$order_id);
8275
$status_cryptomarket = Configuration::get('PS_OS_ERROR');
8376

8477
break;
8578
case "0":
86-
error_log('[Info] Esperando pago. Cart ID:'.$cart_id);
79+
error_log('[Info] Esperando pago. Order ID:'.$order_id);
8780
$status_cryptomarket = Configuration::get('PS_OS_PREPARATION');
8881

8982
break;
9083
case "1":
91-
error_log('[Info] Esperando bloque. Cart ID:'.$cart_id);
84+
error_log('[Info] Esperando bloque. Order ID:'.$order_id);
9285
$status_cryptomarket = Configuration::get('PS_OS_PREPARATION');
9386

9487
break;
9588
case "2":
96-
error_log('[Info] Esperando procesamiento. Cart ID:'.$cart_id);
89+
error_log('[Info] Esperando procesamiento. Order ID:'.$order_id);
9790
$status_cryptomarket = Configuration::get('PS_OS_PREPARATION');
9891

9992
break;
10093
case "3":
101-
error_log('[Info] Pago exitoso. Cart ID:'.$cart_id);
94+
error_log('[Info] Pago exitoso. Order ID:'.$order_id);
10295
$status_cryptomarket = Configuration::get('PS_OS_PAYMENT');
10396
break;
10497

10598
default:
106-
error_log('[Error] No status payment defined:'.$payload->status.'. Cart ID:'.$cart_id);
99+
error_log('[Error] No status payment defined:'.$payload->status.'. Order ID:'.$order_id);
107100
break;
108101
}
109102

110-
if ($order == 0){
111-
$cryptomarket = new cryptomarket();
112-
113-
$cart = new Cart($cart_id);
114-
115-
$customer = new Customer($cart->id_customer);
116-
117-
$currency = new Currency($cart->id_currency);
118-
119-
$total = (float)$cart->getOrderTotal(true, Cart::BOTH);
120-
121-
$cryptomarket->validateOrder($cart->id, $status_cryptomarket, $total, $cryptomarket->displayName, NULL, NULL, (int)$currency->id, false, $customer->secure_key);
122-
}
123-
else{
124-
if (empty(Context::getContext()->link)){
125-
Context::getContext()->link = new Link(); // workaround a prestashop bug so email is sent
126-
}
127-
128-
$order = new Order((int)Order::getOrderByCartId($cart_id));
129-
$new_history = new OrderHistory();
130-
$new_history->id_order = (int)$order->id;
131-
$order_status = (int)$status_cryptomarket;
132-
$new_history->changeIdOrderState((int)$order_status, $order, true);
133-
$new_history->addWithemail(true);
103+
if (empty(Context::getContext()->link)){
104+
Context::getContext()->link = new Link(); // workaround a prestashop bug so email is sent
134105
}
106+
107+
$order = new Order((int)$order_id);
108+
$new_history = new OrderHistory();
109+
$new_history->id_order = (int)$order->id;
110+
$order_status = (int)$status_cryptomarket;
111+
$new_history->changeIdOrderState((int)$order_status, $order, true);
112+
$new_history->addWithemail(true);
135113

136114
exit;
137115
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{if $state == '2'}
2-
<p>{l s='Your order on %s is complete.' sprintf=$shop_name mod='cryptomarket'}
3-
<br /><br /> <strong>{l s='Your order will be sent as soon as your payment is confirmed by the Ethereum network.' mod='bitpay'}</strong>
4-
<br /><br />{l s='If you have questions, comments or concerns, please contact our' mod='cryptomarket'} <a href="{$link->getPageLink('contact', true)|escape:'html'}">{l s='expert customer support team. ' mod='cryptomarket'}</a>
2+
<p>{l s='Your order is complete.' sprintf=$shop_name mod='cryptomarket'}
3+
<br /><br /> <strong>{l s='Your order will be sent as soon as your payment is confirmed by the Ethereum|Bitcoin|Stellar network.' mod='cryptomarket'}</strong>
4+
<br /><br />{l s='If you have questions, comments or concerns, please contact our to' mod='cryptomarket'} <a href="{$link->getPageLink('contact', true)|escape:'html'}">{l s='Contact us' mod='cryptomarket'}</a>
55
</p>
66
{else}
77
<p class="warning">
88
{l s="We noticed a problem with your order. If you think this is an error, feel free to contact our" mod='cryptomarket'}
9-
<a href="{$link->getPageLink('contact', true)|escape:'html'}">{l s='expert customer support team. ' mod='cryptomarket'}</a>.
9+
<a href="{$link->getPageLink('contact', true)|escape:'html'}">{l s='Contact us' mod='cryptomarket'}</a>.
1010
</p>
1111
{/if}

0 commit comments

Comments
 (0)