1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the official Pagantis module for PrestaShop. |
4
|
|
|
* |
5
|
|
|
* @author Pagantis <[email protected]> |
6
|
|
|
* @copyright 2019 Pagantis |
7
|
|
|
* @license proprietary |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
require_once('AbstractController.php'); |
11
|
|
|
|
12
|
|
|
use Pagantis\ModuleUtils\Exception\OrderNotFoundException; |
13
|
|
|
use Pagantis\ModuleUtils\Exception\UnknownException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class PagantisRedirectModuleFrontController |
17
|
|
|
*/ |
18
|
|
|
class PagantisPaymentModuleFrontController extends AbstractController |
19
|
|
|
{ |
20
|
|
|
/** @var string $language */ |
21
|
|
|
protected $language; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $customer |
25
|
|
|
* @param $exception |
26
|
|
|
*/ |
27
|
|
|
protected function addLog($customer, $exception) |
28
|
|
|
{ |
29
|
|
|
if (_PS_VERSION_ < 1.6) { |
|
|
|
|
30
|
|
|
Logger::addLog( |
|
|
|
|
31
|
|
|
'Pagantis Exception For user ' . |
32
|
|
|
$customer->email . |
33
|
|
|
' : ' . |
34
|
|
|
$exception->getMessage(), |
35
|
|
|
3, |
36
|
|
|
$exception->getCode(), |
37
|
|
|
null, |
38
|
|
|
null, |
39
|
|
|
true |
40
|
|
|
); |
41
|
|
|
} else { |
42
|
|
|
PrestaShopLogger::addLog( |
|
|
|
|
43
|
|
|
'Pagantis Exception For user ' . |
44
|
|
|
$customer->email . |
45
|
|
|
' : ' . |
46
|
|
|
$exception->getMessage(), |
47
|
|
|
3, |
48
|
|
|
$exception->getCode(), |
49
|
|
|
null, |
50
|
|
|
null, |
51
|
|
|
true |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Process Post Request |
58
|
|
|
* |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
|
|
public function postProcess() |
62
|
|
|
{ |
63
|
|
|
/** @var Cart $cart */ |
64
|
|
|
$cart = $this->context->cart; |
65
|
|
|
|
66
|
|
|
if (!$cart->id) { |
67
|
|
|
Tools::redirect('index.php?controller=order'); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @var Customer $customer */ |
71
|
|
|
$customer = $this->context->customer; |
72
|
|
|
$query = array( |
73
|
|
|
'id_cart' => $cart->id, |
74
|
|
|
'key' => $cart->secure_key, |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$koUrl = $this->context->link->getPageLink( |
78
|
|
|
'order', |
79
|
|
|
null, |
80
|
|
|
null, |
81
|
|
|
array('step'=>3) |
82
|
|
|
); |
83
|
|
|
$iframe = Pagantis::getExtraConfig('PAGANTIS_FORM_DISPLAY_TYPE'); |
84
|
|
|
$cancelUrl = (Pagantis::getExtraConfig('PAGANTIS_URL_KO') !== '') ? |
85
|
|
|
Pagantis::getExtraConfig('PAGANTIS_URL_KO') : $koUrl; |
86
|
|
|
$pagantisPublicKey = Configuration::get('pagantis_public_key'); |
|
|
|
|
87
|
|
|
$pagantisPrivateKey = Configuration::get('pagantis_private_key'); |
88
|
|
|
$okUrl = _PS_BASE_URL_SSL_.__PS_BASE_URI__ |
|
|
|
|
89
|
|
|
.'index.php?canonical=true&fc=module&module=pagantis&controller=notify&' |
90
|
|
|
.http_build_query($query) |
91
|
|
|
; |
92
|
|
|
|
93
|
|
|
$shippingAddress = new Address($cart->id_address_delivery); |
|
|
|
|
94
|
|
|
$billingAddress = new Address($cart->id_address_invoice); |
95
|
|
|
$curlInfo = curl_version(); |
96
|
|
|
$curlVersion = $curlInfo['version']; |
97
|
|
|
$metadata = array( |
98
|
|
|
'ps' => _PS_VERSION_, |
|
|
|
|
99
|
|
|
'pagantis' => $this->module->version, |
100
|
|
|
'php' => phpversion(), |
101
|
|
|
'curl' => $curlVersion, |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
try { |
105
|
|
|
$userAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address(); |
106
|
|
|
$userAddress |
107
|
|
|
->setZipCode($shippingAddress->postcode) |
108
|
|
|
->setFullName($shippingAddress->firstname . ' ' . $shippingAddress->lastname) |
109
|
|
|
->setCountryCode($this->language) |
110
|
|
|
->setCity($shippingAddress->city) |
111
|
|
|
->setAddress($shippingAddress->address1 . ' ' . $shippingAddress->address2) |
112
|
|
|
->setTaxId($this->getTaxId($customer, $shippingAddress, $billingAddress)) |
113
|
|
|
->setNationalId($this->getNationalId($customer, $shippingAddress, $billingAddress)) |
114
|
|
|
; |
115
|
|
|
|
116
|
|
|
$orderShippingAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address(); |
117
|
|
|
$orderShippingAddress |
118
|
|
|
->setZipCode($shippingAddress->postcode) |
119
|
|
|
->setFullName($shippingAddress->firstname . ' ' . $shippingAddress->lastname) |
120
|
|
|
->setCountryCode($this->language) |
121
|
|
|
->setCity($shippingAddress->city) |
122
|
|
|
->setAddress($shippingAddress->address1 . ' ' . $shippingAddress->address2) |
123
|
|
|
->setTaxId($this->getTaxId($customer, $shippingAddress, $billingAddress)) |
124
|
|
|
->setNationalId($this->getNationalId($customer, $shippingAddress, $billingAddress)) |
125
|
|
|
->setFixPhone($shippingAddress->phone) |
126
|
|
|
->setMobilePhone($shippingAddress->phone_mobile) |
127
|
|
|
; |
128
|
|
|
|
129
|
|
|
$orderBillingAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address(); |
130
|
|
|
$orderBillingAddress |
131
|
|
|
->setZipCode($billingAddress->postcode) |
132
|
|
|
->setFullName($billingAddress->firstname . ' ' . $billingAddress->lastname) |
133
|
|
|
->setCountryCode($this->language) |
134
|
|
|
->setCity($billingAddress->city) |
135
|
|
|
->setAddress($billingAddress->address1 . ' ' . $billingAddress->address2) |
136
|
|
|
->setTaxId($this->getTaxId($customer, $billingAddress, $shippingAddress)) |
137
|
|
|
->setNationalId($this->getNationalId($customer, $billingAddress, $shippingAddress)) |
138
|
|
|
->setFixPhone($billingAddress->phone) |
139
|
|
|
->setMobilePhone($billingAddress->phone_mobile) |
140
|
|
|
; |
141
|
|
|
|
142
|
|
|
$orderUser = new \Pagantis\OrdersApiClient\Model\Order\User(); |
143
|
|
|
$orderUser |
144
|
|
|
->setAddress($userAddress) |
145
|
|
|
->setFullName($orderShippingAddress->getFullName()) |
146
|
|
|
->setBillingAddress($orderBillingAddress) |
147
|
|
|
->setEmail($this->context->cookie->logged ? $this->context->cookie->email : $customer->email) |
148
|
|
|
->setFixPhone($shippingAddress->phone) |
149
|
|
|
->setMobilePhone($shippingAddress->phone_mobile) |
150
|
|
|
->setShippingAddress($orderShippingAddress) |
151
|
|
|
->setTaxId($this->getTaxId($customer, $shippingAddress, $billingAddress)) |
152
|
|
|
->setNationalId($this->getNationalId($customer, $shippingAddress, $billingAddress)) |
153
|
|
|
; |
154
|
|
|
|
155
|
|
|
if ($customer->birthday!='0000-00-00') { |
156
|
|
|
$orderUser->setDateOfBirth($customer->birthday); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$orders = Order::getCustomerOrders($customer->id); |
|
|
|
|
160
|
|
|
/** @var \PrestaShop\PrestaShop\Adapter\Entity\Order $order */ |
161
|
|
|
foreach ($orders as $order) { |
162
|
|
|
if ($order['valid']) { |
163
|
|
|
$orderHistory = new \Pagantis\OrdersApiClient\Model\Order\User\OrderHistory(); |
164
|
|
|
$orderHistory |
165
|
|
|
->setAmount((string) floor(100 * $order['total_paid'])) |
166
|
|
|
->setDate(new \DateTime($order['date_add'])) |
|
|
|
|
167
|
|
|
; |
168
|
|
|
$orderUser->addOrderHistory($orderHistory); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$metadataOrder = new \Pagantis\OrdersApiClient\Model\Order\Metadata(); |
173
|
|
|
foreach ($metadata as $key => $metadatum) { |
174
|
|
|
$metadataOrder->addMetadata($key, $metadatum); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$details = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details(); |
178
|
|
|
$details->setShippingCost((string) floor(100 * $cart->getTotalShippingCost())); |
179
|
|
|
$items = $cart->getProducts(); |
180
|
|
|
$promotedAmount = 0; |
181
|
|
|
foreach ($items as $key => $item) { |
182
|
|
|
$promotedProduct = $this->isPromoted($item['id_product']); |
183
|
|
|
$product = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product(); |
184
|
|
|
$product |
185
|
|
|
->setAmount((string) floor(100 * $item['price_wt'])) |
186
|
|
|
->setQuantity($item['quantity']) |
187
|
|
|
->setDescription($item['name']); |
188
|
|
|
if ($promotedProduct) { |
189
|
|
|
$promotedAmount+=$product->getAmount(); |
190
|
|
|
$productId = $item['id_product']; |
191
|
|
|
$finalPrice = Product::getPriceStatic($productId); |
|
|
|
|
192
|
|
|
$promotedMessage = 'Promoted Item: ' . $product->getDescription() . |
193
|
|
|
' Price: ' . $finalPrice . |
194
|
|
|
' Qty: ' . $product->getQuantity() . |
195
|
|
|
' Item ID: ' . $item['id_product']; |
196
|
|
|
$metadataOrder->addMetadata('promotedProduct', $promotedMessage); |
197
|
|
|
} |
198
|
|
|
$details->addProduct($product); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
$orderShoppingCart = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart(); |
203
|
|
|
$totalAmount = (string) floor(100 * $cart->getOrderTotal(true)); |
204
|
|
|
$orderShoppingCart |
205
|
|
|
->setDetails($details) |
206
|
|
|
->setOrderReference($cart->id) |
207
|
|
|
->setTotalAmount($totalAmount) |
208
|
|
|
->setPromotedAmount($promotedAmount) |
209
|
|
|
; |
210
|
|
|
|
211
|
|
|
$orderConfigurationUrls = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Urls(); |
212
|
|
|
$orderConfigurationUrls |
213
|
|
|
->setCancel($cancelUrl) |
214
|
|
|
->setKo($cancelUrl) |
215
|
|
|
->setAuthorizedNotificationCallback($okUrl) |
216
|
|
|
->setRejectedNotificationCallback($okUrl) |
217
|
|
|
->setOk($okUrl) |
218
|
|
|
; |
219
|
|
|
|
220
|
|
|
$orderChannel = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Channel(); |
221
|
|
|
$orderChannel |
222
|
|
|
->setAssistedSale(false) |
223
|
|
|
->setType(\Pagantis\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE) |
224
|
|
|
; |
225
|
|
|
|
226
|
|
|
$orderConfiguration = new \Pagantis\OrdersApiClient\Model\Order\Configuration(); |
227
|
|
|
$orderConfiguration |
228
|
|
|
->setChannel($orderChannel) |
229
|
|
|
->setUrls($orderConfigurationUrls) |
230
|
|
|
->setPurchaseCountry($this->language) |
231
|
|
|
; |
232
|
|
|
|
233
|
|
|
$order = new \Pagantis\OrdersApiClient\Model\Order(); |
234
|
|
|
$order |
235
|
|
|
->setConfiguration($orderConfiguration) |
236
|
|
|
->setMetadata($metadataOrder) |
237
|
|
|
->setShoppingCart($orderShoppingCart) |
238
|
|
|
->setUser($orderUser) |
239
|
|
|
; |
240
|
|
|
} catch (\Exception $exception) { |
241
|
|
|
$this->saveLog(array(), $exception); |
242
|
|
|
Tools::redirect($cancelUrl); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$url =''; |
246
|
|
|
try { |
247
|
|
|
$orderClient = new \Pagantis\OrdersApiClient\Client( |
248
|
|
|
$pagantisPublicKey, |
249
|
|
|
$pagantisPrivateKey |
250
|
|
|
); |
251
|
|
|
$order = $orderClient->createOrder($order); |
|
|
|
|
252
|
|
|
|
253
|
|
|
if ($order instanceof \Pagantis\OrdersApiClient\Model\Order) { |
|
|
|
|
254
|
|
|
$url = $order->getActionUrls()->getForm(); |
255
|
|
|
$orderId = $order->getId(); |
256
|
|
|
$sql = "INSERT INTO `" . _DB_PREFIX_ . "pagantis_order` (`id`, `order_id`) |
|
|
|
|
257
|
|
|
VALUES ('$cart->id','$orderId') |
258
|
|
|
ON DUPLICATE KEY UPDATE `order_id` = '$orderId'"; |
259
|
|
|
$result = Db::getInstance()->execute($sql); |
|
|
|
|
260
|
|
|
if (!$result) { |
261
|
|
|
throw new UnknownException('Unable to save pagantis-order-id in database: '. $sql); |
262
|
|
|
} |
263
|
|
|
} else { |
264
|
|
|
throw new OrderNotFoundException(); |
265
|
|
|
} |
266
|
|
|
} catch (\Exception $exception) { |
267
|
|
|
$this->saveLog(array(), $exception); |
268
|
|
|
Tools::redirect($cancelUrl); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
if (!$iframe) { |
272
|
|
|
Tools::redirect($url); |
273
|
|
|
} else { |
274
|
|
|
$this->context->smarty->assign(array( |
275
|
|
|
'url' => $url, |
276
|
|
|
'checkoutUrl' => $cancelUrl, |
277
|
|
|
)); |
278
|
|
|
|
279
|
|
|
try { |
280
|
|
|
if (_PS_VERSION_ < 1.7) { |
281
|
|
|
$this->setTemplate('payment-15.tpl'); |
282
|
|
|
} else { |
283
|
|
|
$this->setTemplate('module:pagantis/views/templates/front/payment-17.tpl'); |
284
|
|
|
} |
285
|
|
|
} catch (\Exception $exception) { |
286
|
|
|
$this->saveLog(array(), $exception); |
287
|
|
|
Tools::redirect($url); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @param null $customer |
|
|
|
|
294
|
|
|
* @param null $addressOne |
|
|
|
|
295
|
|
|
* @param null $addressTwo |
|
|
|
|
296
|
|
|
* @return mixed|null |
297
|
|
|
*/ |
298
|
|
|
private function getNationalId($customer = null, $addressOne = null, $addressTwo = null) |
299
|
|
|
{ |
300
|
|
|
if ($customer !== null && isset($customer->national_id)) { |
|
|
|
|
301
|
|
|
return $customer->national_id; |
302
|
|
|
} elseif ($addressOne !== null and isset($addressOne->national_id)) { |
|
|
|
|
303
|
|
|
return $addressOne->national_id; |
304
|
|
|
} elseif ($addressOne !== null and isset($addressOne->dni)) { |
|
|
|
|
305
|
|
|
return $addressOne->dni; |
306
|
|
|
} elseif ($addressTwo !== null and isset($addressTwo->national_id)) { |
|
|
|
|
307
|
|
|
return $addressTwo->national_id; |
308
|
|
|
} elseif ($addressTwo !== null and isset($addressTwo->dni)) { |
|
|
|
|
309
|
|
|
return $addressTwo->dni; |
310
|
|
|
} else { |
311
|
|
|
return null; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param null $customer |
|
|
|
|
317
|
|
|
* @param null $addressOne |
|
|
|
|
318
|
|
|
* @param null $addressTwo |
|
|
|
|
319
|
|
|
* @return mixed|null |
320
|
|
|
*/ |
321
|
|
|
private function getTaxId($customer = null, $addressOne = null, $addressTwo = null) |
322
|
|
|
{ |
323
|
|
|
if ($customer !== null && isset($customer->tax_id)) { |
|
|
|
|
324
|
|
|
return $customer->tax_id; |
325
|
|
|
} elseif ($customer !== null && isset($customer->fiscalcode)) { |
|
|
|
|
326
|
|
|
return $customer->fiscalcode; |
327
|
|
|
} elseif ($addressOne !== null and isset($addressOne->tax_id)) { |
|
|
|
|
328
|
|
|
return $addressOne->tax_id; |
329
|
|
|
} elseif ($addressTwo !== null and isset($addressTwo->tax_id)) { |
|
|
|
|
330
|
|
|
return $addressTwo->tax_id; |
331
|
|
|
} else { |
332
|
|
|
return null; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @param $item |
338
|
|
|
* |
339
|
|
|
* @return bool |
340
|
|
|
*/ |
341
|
|
|
private function isPromoted($itemId) |
342
|
|
|
{ |
343
|
|
|
$itemCategories = ProductCore::getProductCategoriesFull($itemId); |
|
|
|
|
344
|
|
|
if (in_array(PROMOTIONS_CATEGORY_NAME, $this->arrayColumn($itemCategories, 'name')) !== false) { |
345
|
|
|
return true; |
346
|
|
|
} |
347
|
|
|
return false; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @param array $input |
352
|
|
|
* @param $columnKey |
353
|
|
|
* @param null $indexKey |
|
|
|
|
354
|
|
|
* |
355
|
|
|
* @return array|bool |
356
|
|
|
*/ |
357
|
|
|
private function arrayColumn(array $input, $columnKey, $indexKey = null) |
358
|
|
|
{ |
359
|
|
|
$array = array(); |
360
|
|
|
foreach ($input as $value) { |
361
|
|
|
if (!array_key_exists($columnKey, $value)) { |
362
|
|
|
trigger_error("Key \"$columnKey\" does not exist in array"); |
363
|
|
|
return false; |
364
|
|
|
} |
365
|
|
|
if (is_null($indexKey)) { |
366
|
|
|
$array[] = $value[$columnKey]; |
367
|
|
|
} else { |
368
|
|
|
if (!array_key_exists($indexKey, $value)) { |
369
|
|
|
trigger_error("Key \"$indexKey\" does not exist in array"); |
370
|
|
|
return false; |
371
|
|
|
} |
372
|
|
|
if (!is_scalar($value[$indexKey])) { |
373
|
|
|
trigger_error("Key \"$indexKey\" does not contain scalar value"); |
374
|
|
|
return false; |
375
|
|
|
} |
376
|
|
|
$array[$value[$indexKey]] = $value[$columnKey]; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
return $array; |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|