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&origin=redirect&' |
90
|
|
|
.http_build_query($query) |
91
|
|
|
; |
92
|
|
|
$notificationOkUrl = _PS_BASE_URL_SSL_.__PS_BASE_URI__ |
93
|
|
|
.'index.php?canonical=true&fc=module&module=pagantis&controller=notify&origin=notification&' |
94
|
|
|
.http_build_query($query) |
95
|
|
|
; |
96
|
|
|
|
97
|
|
|
$shippingAddress = new Address($cart->id_address_delivery); |
|
|
|
|
98
|
|
|
$billingAddress = new Address($cart->id_address_invoice); |
99
|
|
|
$curlInfo = curl_version(); |
100
|
|
|
$curlVersion = $curlInfo['version']; |
101
|
|
|
$metadata = array( |
102
|
|
|
'ps' => _PS_VERSION_, |
|
|
|
|
103
|
|
|
'pagantis' => $this->module->version, |
104
|
|
|
'php' => phpversion(), |
105
|
|
|
'curl' => $curlVersion, |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
try { |
109
|
|
|
$shippingCountry = Country::getIsoById($shippingAddress->id_country); |
|
|
|
|
110
|
|
|
$userAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address(); |
111
|
|
|
$userAddress |
112
|
|
|
->setZipCode($shippingAddress->postcode) |
113
|
|
|
->setFullName($shippingAddress->firstname . ' ' . $shippingAddress->lastname) |
114
|
|
|
->setCountryCode($shippingCountry) |
115
|
|
|
->setCity($shippingAddress->city) |
116
|
|
|
->setAddress($shippingAddress->address1 . ' ' . $shippingAddress->address2) |
117
|
|
|
->setTaxId($this->getTaxId($customer, $shippingAddress, $billingAddress)) |
118
|
|
|
->setNationalId($this->getNationalId($customer, $shippingAddress, $billingAddress)) |
119
|
|
|
; |
120
|
|
|
|
121
|
|
|
$orderShippingAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address(); |
122
|
|
|
$orderShippingAddress |
123
|
|
|
->setZipCode($shippingAddress->postcode) |
124
|
|
|
->setFullName($shippingAddress->firstname . ' ' . $shippingAddress->lastname) |
125
|
|
|
->setCountryCode($shippingCountry) |
126
|
|
|
->setCity($shippingAddress->city) |
127
|
|
|
->setAddress($shippingAddress->address1 . ' ' . $shippingAddress->address2) |
128
|
|
|
->setTaxId($this->getTaxId($customer, $shippingAddress, $billingAddress)) |
129
|
|
|
->setNationalId($this->getNationalId($customer, $shippingAddress, $billingAddress)) |
130
|
|
|
->setFixPhone($shippingAddress->phone) |
131
|
|
|
->setMobilePhone($shippingAddress->phone_mobile) |
132
|
|
|
; |
133
|
|
|
|
134
|
|
|
$billingCountry = Country::getIsoById($billingAddress->id_country); |
135
|
|
|
$orderBillingAddress = new \Pagantis\OrdersApiClient\Model\Order\User\Address(); |
136
|
|
|
$orderBillingAddress |
137
|
|
|
->setZipCode($billingAddress->postcode) |
138
|
|
|
->setFullName($billingAddress->firstname . ' ' . $billingAddress->lastname) |
139
|
|
|
->setCountryCode($billingCountry) |
140
|
|
|
->setCity($billingAddress->city) |
141
|
|
|
->setAddress($billingAddress->address1 . ' ' . $billingAddress->address2) |
142
|
|
|
->setTaxId($this->getTaxId($customer, $billingAddress, $shippingAddress)) |
143
|
|
|
->setNationalId($this->getNationalId($customer, $billingAddress, $shippingAddress)) |
144
|
|
|
->setFixPhone($billingAddress->phone) |
145
|
|
|
->setMobilePhone($billingAddress->phone_mobile) |
146
|
|
|
; |
147
|
|
|
|
148
|
|
|
$orderUser = new \Pagantis\OrdersApiClient\Model\Order\User(); |
149
|
|
|
$orderUser |
150
|
|
|
->setAddress($userAddress) |
151
|
|
|
->setFullName($orderShippingAddress->getFullName()) |
152
|
|
|
->setBillingAddress($orderBillingAddress) |
153
|
|
|
->setEmail($this->context->cookie->logged ? $this->context->cookie->email : $customer->email) |
154
|
|
|
->setFixPhone($shippingAddress->phone) |
155
|
|
|
->setMobilePhone($shippingAddress->phone_mobile) |
156
|
|
|
->setShippingAddress($orderShippingAddress) |
157
|
|
|
->setTaxId($this->getTaxId($customer, $shippingAddress, $billingAddress)) |
158
|
|
|
->setNationalId($this->getNationalId($customer, $shippingAddress, $billingAddress)) |
159
|
|
|
; |
160
|
|
|
|
161
|
|
|
if ($customer->birthday!='0000-00-00') { |
162
|
|
|
$orderUser->setDateOfBirth($customer->birthday); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$orders = Order::getCustomerOrders($customer->id); |
|
|
|
|
166
|
|
|
/** @var \PrestaShop\PrestaShop\Adapter\Entity\Order $order */ |
167
|
|
|
foreach ($orders as $order) { |
168
|
|
|
if ($order['valid']) { |
169
|
|
|
$orderHistory = new \Pagantis\OrdersApiClient\Model\Order\User\OrderHistory(); |
170
|
|
|
$orderHistory |
171
|
|
|
->setAmount((string) floor(100 * $order['total_paid'])) |
172
|
|
|
->setDate(new \DateTime($order['date_add'])) |
|
|
|
|
173
|
|
|
; |
174
|
|
|
$orderUser->addOrderHistory($orderHistory); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$metadataOrder = new \Pagantis\OrdersApiClient\Model\Order\Metadata(); |
179
|
|
|
foreach ($metadata as $key => $metadatum) { |
180
|
|
|
$metadataOrder->addMetadata($key, $metadatum); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$details = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details(); |
184
|
|
|
$details->setShippingCost((string) floor(100 * $cart->getTotalShippingCost())); |
185
|
|
|
$items = $cart->getProducts(); |
186
|
|
|
$promotedAmount = 0; |
187
|
|
|
foreach ($items as $key => $item) { |
188
|
|
|
$promotedProduct = $this->isPromoted($item['id_product']); |
189
|
|
|
$product = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product(); |
190
|
|
|
$product |
191
|
|
|
->setAmount((string) floor(100 * $item['price_wt'])) |
192
|
|
|
->setQuantity($item['quantity']) |
193
|
|
|
->setDescription($item['name']); |
194
|
|
|
if ($promotedProduct) { |
195
|
|
|
$promotedAmount+=$product->getAmount(); |
196
|
|
|
$productId = $item['id_product']; |
197
|
|
|
$finalPrice = Product::getPriceStatic($productId); |
|
|
|
|
198
|
|
|
$promotedMessage = 'Promoted Item: ' . $product->getDescription() . |
199
|
|
|
' Price: ' . $finalPrice . |
200
|
|
|
' Qty: ' . $product->getQuantity() . |
201
|
|
|
' Item ID: ' . $item['id_product']; |
202
|
|
|
$metadataOrder->addMetadata('promotedProduct', $promotedMessage); |
203
|
|
|
} |
204
|
|
|
$details->addProduct($product); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
$orderShoppingCart = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart(); |
209
|
|
|
$totalAmount = (string) floor(100 * $cart->getOrderTotal(true)); |
210
|
|
|
$orderShoppingCart |
211
|
|
|
->setDetails($details) |
212
|
|
|
->setOrderReference($cart->id) |
213
|
|
|
->setTotalAmount($totalAmount) |
214
|
|
|
->setPromotedAmount($promotedAmount) |
215
|
|
|
; |
216
|
|
|
|
217
|
|
|
$orderConfigurationUrls = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Urls(); |
218
|
|
|
$orderConfigurationUrls |
219
|
|
|
->setCancel($cancelUrl) |
220
|
|
|
->setKo($cancelUrl) |
221
|
|
|
->setAuthorizedNotificationCallback($notificationOkUrl) |
222
|
|
|
->setRejectedNotificationCallback(null) |
223
|
|
|
->setOk($okUrl) |
224
|
|
|
; |
225
|
|
|
|
226
|
|
|
$orderChannel = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Channel(); |
227
|
|
|
$orderChannel |
228
|
|
|
->setAssistedSale(false) |
229
|
|
|
->setType(\Pagantis\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE) |
230
|
|
|
; |
231
|
|
|
|
232
|
|
|
$purchaseCountry = $this->getUserLanguage($shippingAddress, $billingAddress); |
233
|
|
|
$orderConfiguration = new \Pagantis\OrdersApiClient\Model\Order\Configuration(); |
234
|
|
|
$orderConfiguration |
235
|
|
|
->setChannel($orderChannel) |
236
|
|
|
->setUrls($orderConfigurationUrls) |
237
|
|
|
->setPurchaseCountry($purchaseCountry) |
238
|
|
|
; |
239
|
|
|
|
240
|
|
|
$order = new \Pagantis\OrdersApiClient\Model\Order(); |
241
|
|
|
$order |
242
|
|
|
->setConfiguration($orderConfiguration) |
243
|
|
|
->setMetadata($metadataOrder) |
244
|
|
|
->setShoppingCart($orderShoppingCart) |
245
|
|
|
->setUser($orderUser) |
246
|
|
|
; |
247
|
|
|
} catch (\Exception $exception) { |
248
|
|
|
$this->saveLog(array(), $exception); |
249
|
|
|
Tools::redirect($cancelUrl); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$url =''; |
253
|
|
|
try { |
254
|
|
|
$orderClient = new \Pagantis\OrdersApiClient\Client( |
255
|
|
|
$pagantisPublicKey, |
256
|
|
|
$pagantisPrivateKey |
257
|
|
|
); |
258
|
|
|
$order = $orderClient->createOrder($order); |
|
|
|
|
259
|
|
|
|
260
|
|
|
if ($order instanceof \Pagantis\OrdersApiClient\Model\Order) { |
|
|
|
|
261
|
|
|
$url = $order->getActionUrls()->getForm(); |
262
|
|
|
/** |
263
|
|
|
* @var string $orderId Pagantis Order id |
264
|
|
|
*/ |
265
|
|
|
$orderId = $order->getId(); |
266
|
|
|
$sql = "INSERT INTO `" . _DB_PREFIX_ . "pagantis_order` (`id`, `order_id`) |
|
|
|
|
267
|
|
|
VALUES ('$cart->id','$orderId') |
268
|
|
|
ON DUPLICATE KEY UPDATE `order_id` = '$orderId'"; |
269
|
|
|
$result = Db::getInstance()->execute($sql); |
|
|
|
|
270
|
|
|
if (!$result) { |
271
|
|
|
throw new UnknownException('Unable to save pagantis-order-id in database: '. $sql); |
272
|
|
|
} |
273
|
|
|
} else { |
274
|
|
|
throw new OrderNotFoundException(); |
275
|
|
|
} |
276
|
|
|
} catch (\Exception $exception) { |
277
|
|
|
$this->saveLog(array(), $exception); |
278
|
|
|
Tools::redirect($cancelUrl); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
if (!$iframe) { |
282
|
|
|
Tools::redirect($url); |
283
|
|
|
} else { |
284
|
|
|
$this->context->smarty->assign(array( |
285
|
|
|
'url' => $url, |
286
|
|
|
'checkoutUrl' => $cancelUrl, |
287
|
|
|
)); |
288
|
|
|
|
289
|
|
|
try { |
290
|
|
|
if (_PS_VERSION_ < 1.7) { |
291
|
|
|
$this->setTemplate('payment-15.tpl'); |
292
|
|
|
} else { |
293
|
|
|
$this->setTemplate('module:pagantis/views/templates/front/payment-17.tpl'); |
294
|
|
|
} |
295
|
|
|
} catch (\Exception $exception) { |
296
|
|
|
$this->saveLog(array(), $exception); |
297
|
|
|
Tools::redirect($url); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param null $customer |
|
|
|
|
304
|
|
|
* @param null $addressOne |
|
|
|
|
305
|
|
|
* @param null $addressTwo |
|
|
|
|
306
|
|
|
* @return mixed|null |
307
|
|
|
*/ |
308
|
|
|
private function getNationalId($customer = null, $addressOne = null, $addressTwo = null) |
309
|
|
|
{ |
310
|
|
|
if ($customer !== null && isset($customer->national_id)) { |
|
|
|
|
311
|
|
|
return $customer->national_id; |
312
|
|
|
} elseif ($addressOne !== null and isset($addressOne->national_id)) { |
|
|
|
|
313
|
|
|
return $addressOne->national_id; |
314
|
|
|
} elseif ($addressOne !== null and isset($addressOne->dni)) { |
|
|
|
|
315
|
|
|
return $addressOne->dni; |
316
|
|
|
} elseif ($addressTwo !== null and isset($addressTwo->national_id)) { |
|
|
|
|
317
|
|
|
return $addressTwo->national_id; |
318
|
|
|
} elseif ($addressTwo !== null and isset($addressTwo->dni)) { |
|
|
|
|
319
|
|
|
return $addressTwo->dni; |
320
|
|
|
} else { |
321
|
|
|
return null; |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param null $customer |
|
|
|
|
327
|
|
|
* @param null $addressOne |
|
|
|
|
328
|
|
|
* @param null $addressTwo |
|
|
|
|
329
|
|
|
* @return mixed|null |
330
|
|
|
*/ |
331
|
|
|
private function getTaxId($customer = null, $addressOne = null, $addressTwo = null) |
332
|
|
|
{ |
333
|
|
|
if ($customer !== null && isset($customer->tax_id)) { |
|
|
|
|
334
|
|
|
return $customer->tax_id; |
335
|
|
|
} elseif ($customer !== null && isset($customer->fiscalcode)) { |
|
|
|
|
336
|
|
|
return $customer->fiscalcode; |
337
|
|
|
} elseif ($addressOne !== null and isset($addressOne->tax_id)) { |
|
|
|
|
338
|
|
|
return $addressOne->tax_id; |
339
|
|
|
} elseif ($addressTwo !== null and isset($addressTwo->tax_id)) { |
|
|
|
|
340
|
|
|
return $addressTwo->tax_id; |
341
|
|
|
} else { |
342
|
|
|
return null; |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @param $item |
348
|
|
|
* |
349
|
|
|
* @return bool |
350
|
|
|
*/ |
351
|
|
|
private function isPromoted($itemId) |
352
|
|
|
{ |
353
|
|
|
$itemCategories = ProductCore::getProductCategoriesFull($itemId); |
|
|
|
|
354
|
|
|
if (in_array(PROMOTIONS_CATEGORY_NAME, $this->arrayColumn($itemCategories, 'name')) !== false) { |
355
|
|
|
return true; |
356
|
|
|
} |
357
|
|
|
return false; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @param array $input |
362
|
|
|
* @param $columnKey |
363
|
|
|
* @param null $indexKey |
|
|
|
|
364
|
|
|
* |
365
|
|
|
* @return array|bool |
366
|
|
|
*/ |
367
|
|
|
private function arrayColumn(array $input, $columnKey, $indexKey = null) |
368
|
|
|
{ |
369
|
|
|
$array = array(); |
370
|
|
|
foreach ($input as $value) { |
371
|
|
|
if (!array_key_exists($columnKey, $value)) { |
372
|
|
|
trigger_error("Key \"$columnKey\" does not exist in array"); |
373
|
|
|
return false; |
374
|
|
|
} |
375
|
|
|
if (is_null($indexKey)) { |
376
|
|
|
$array[] = $value[$columnKey]; |
377
|
|
|
} else { |
378
|
|
|
if (!array_key_exists($indexKey, $value)) { |
379
|
|
|
trigger_error("Key \"$indexKey\" does not exist in array"); |
380
|
|
|
return false; |
381
|
|
|
} |
382
|
|
|
if (!is_scalar($value[$indexKey])) { |
383
|
|
|
trigger_error("Key \"$indexKey\" does not contain scalar value"); |
384
|
|
|
return false; |
385
|
|
|
} |
386
|
|
|
$array[$value[$indexKey]] = $value[$columnKey]; |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
return $array; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
private function getUserLanguage($shippingAddress = null, $billingAddress = null) |
393
|
|
|
{ |
394
|
|
|
$allowedCountries = unserialize(Pagantis::getExtraConfig('PAGANTIS_ALLOWED_COUNTRIES')); |
395
|
|
|
$lang = Language::getLanguage($this->context->language->id); |
|
|
|
|
396
|
|
|
$langArray = explode("-", $lang['language_code']); |
397
|
|
|
if (count($langArray) != 2 && isset($lang['locale'])) { |
398
|
|
|
$langArray = explode("-", $lang['locale']); |
399
|
|
|
} |
400
|
|
|
$language = Tools::strtoupper($langArray[count($langArray)-1]); |
401
|
|
|
// Prevent null language detection |
402
|
|
|
if (in_array(Tools::strtolower($language), $allowedCountries)) { |
403
|
|
|
return $language; |
404
|
|
|
} |
405
|
|
|
if ($shippingAddress) { |
406
|
|
|
$language = Country::getIsoById($shippingAddress->id_country); |
407
|
|
|
if (in_array(Tools::strtolower($language), $allowedCountries)) { |
408
|
|
|
return $language; |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
if ($billingAddress) { |
412
|
|
|
$language = Country::getIsoById($billingAddress->id_country); |
413
|
|
|
if (in_array(Tools::strtolower($language), $allowedCountries)) { |
414
|
|
|
return $language; |
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
return 'ES'; |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
|