1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the official Paylater module for PrestaShop. |
4
|
|
|
* |
5
|
|
|
* @author Paga+Tarde <[email protected]> |
6
|
|
|
* @copyright 2019 Paga+Tarde |
7
|
|
|
* @license proprietary |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
require_once('AbstractController.php'); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class PaylaterRedirectModuleFrontController |
14
|
|
|
*/ |
15
|
|
|
class PaylaterPaymentModuleFrontController extends AbstractController |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param $customer |
19
|
|
|
* @param $exception |
20
|
|
|
*/ |
21
|
|
|
protected function addLog($customer, $exception) |
22
|
|
|
{ |
23
|
|
|
if (_PS_VERSION_ < 1.6) { |
|
|
|
|
24
|
|
|
Logger::addLog( |
|
|
|
|
25
|
|
|
'PagaMasTarde Exception For user ' . |
26
|
|
|
$customer->email . |
27
|
|
|
' : ' . |
28
|
|
|
$exception->getMessage(), |
29
|
|
|
3, |
30
|
|
|
$exception->getCode(), |
31
|
|
|
null, |
32
|
|
|
null, |
33
|
|
|
true |
34
|
|
|
); |
35
|
|
|
} else { |
36
|
|
|
PrestaShopLogger::addLog( |
|
|
|
|
37
|
|
|
'PagaMasTarde Exception For user ' . |
38
|
|
|
$customer->email . |
39
|
|
|
' : ' . |
40
|
|
|
$exception->getMessage(), |
41
|
|
|
3, |
42
|
|
|
$exception->getCode(), |
43
|
|
|
null, |
44
|
|
|
null, |
45
|
|
|
true |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Process Post Request |
52
|
|
|
* |
53
|
|
|
* @throws \Exception |
54
|
|
|
*/ |
55
|
|
|
public function postProcess() |
56
|
|
|
{ |
57
|
|
|
/** @var Cart $cart */ |
58
|
|
|
$cart = $this->context->cart; |
59
|
|
|
|
60
|
|
|
if (!$cart->id) { |
61
|
|
|
Tools::redirect('index.php?controller=order'); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @var Customer $customer */ |
65
|
|
|
$customer = $this->context->customer; |
66
|
|
|
$query = array( |
67
|
|
|
'id_cart' => $cart->id, |
68
|
|
|
'key' => $cart->secure_key, |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$koUrl = $this->context->link->getPageLink( |
72
|
|
|
'order', |
73
|
|
|
null, |
74
|
|
|
null, |
75
|
|
|
array('step'=>3) |
76
|
|
|
); |
77
|
|
|
$iframe = getenv('PMT_FORM_DISPLAY_TYPE'); |
78
|
|
|
$cancelUrl = (getenv('PMT_URL_KO') !== '') ? getenv('PMT_URL_KO') : $koUrl; |
79
|
|
|
$paylaterPublicKey = Configuration::get('pmt_public_key'); |
|
|
|
|
80
|
|
|
$paylaterPrivateKey = Configuration::get('pmt_private_key'); |
81
|
|
|
$okUrl = _PS_BASE_URL_.__PS_BASE_URI__ |
|
|
|
|
82
|
|
|
.'index.php?canonical=true&fc=module&module=paylater&controller=notify&' |
83
|
|
|
.http_build_query($query) |
84
|
|
|
; |
85
|
|
|
|
86
|
|
|
$shippingAddress = new Address($cart->id_address_delivery); |
|
|
|
|
87
|
|
|
$billingAddress = new Address($cart->id_address_invoice); |
88
|
|
|
$curlInfo = curl_version(); |
89
|
|
|
$curlVersion = $curlInfo['version']; |
90
|
|
|
$metadata = array( |
91
|
|
|
'ps' => _PS_VERSION_, |
|
|
|
|
92
|
|
|
'pmt' => $this->module->version, |
93
|
|
|
'php' => phpversion(), |
94
|
|
|
'curl' => $curlVersion, |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
$userAddress = new \PagaMasTarde\OrdersApiClient\Model\Order\User\Address(); |
99
|
|
|
$userAddress |
100
|
|
|
->setZipCode($shippingAddress->postcode) |
101
|
|
|
->setFullName($shippingAddress->firstname . ' ' . $shippingAddress->lastname) |
102
|
|
|
->setCountryCode('ES') |
103
|
|
|
->setCity($shippingAddress->city) |
104
|
|
|
->setAddress($shippingAddress->address1 . ' ' . $shippingAddress->address2) |
105
|
|
|
; |
106
|
|
|
|
107
|
|
|
$orderShippingAddress = new \PagaMasTarde\OrdersApiClient\Model\Order\User\Address(); |
108
|
|
|
$orderShippingAddress |
109
|
|
|
->setZipCode($shippingAddress->postcode) |
110
|
|
|
->setFullName($shippingAddress->firstname . ' ' . $shippingAddress->lastname) |
111
|
|
|
->setCountryCode('ES') |
112
|
|
|
->setCity($shippingAddress->city) |
113
|
|
|
->setAddress($shippingAddress->address1 . ' ' . $shippingAddress->address2) |
114
|
|
|
->setDni($shippingAddress->dni) |
115
|
|
|
->setFixPhone($shippingAddress->phone) |
116
|
|
|
->setMobilePhone($shippingAddress->phone_mobile) |
117
|
|
|
; |
118
|
|
|
|
119
|
|
|
$orderBillingAddress = new \PagaMasTarde\OrdersApiClient\Model\Order\User\Address(); |
120
|
|
|
$orderBillingAddress |
121
|
|
|
->setZipCode($billingAddress->postcode) |
122
|
|
|
->setFullName($billingAddress->firstname . ' ' . $billingAddress->lastname) |
123
|
|
|
->setCountryCode('ES') |
124
|
|
|
->setCity($billingAddress->city) |
125
|
|
|
->setAddress($billingAddress->address1 . ' ' . $billingAddress->address2) |
126
|
|
|
->setDni($billingAddress->dni) |
127
|
|
|
->setFixPhone($billingAddress->phone) |
128
|
|
|
->setMobilePhone($billingAddress->phone_mobile) |
129
|
|
|
; |
130
|
|
|
|
131
|
|
|
$orderUser = new \PagaMasTarde\OrdersApiClient\Model\Order\User(); |
132
|
|
|
$orderUser |
133
|
|
|
->setAddress($userAddress) |
134
|
|
|
->setFullName($orderShippingAddress->getFullName()) |
135
|
|
|
->setBillingAddress($orderBillingAddress) |
136
|
|
|
->setEmail($this->context->cookie->logged ? $this->context->cookie->email : $customer->email) |
137
|
|
|
->setFixPhone($shippingAddress->phone) |
138
|
|
|
->setMobilePhone($shippingAddress->phone_mobile) |
139
|
|
|
->setShippingAddress($orderShippingAddress) |
140
|
|
|
->setDni($shippingAddress->dni) |
141
|
|
|
; |
142
|
|
|
|
143
|
|
|
if ($customer->birthday!='0000-00-00') { |
144
|
|
|
$orderUser->setDateOfBirth($customer->birthday); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$orders = Order::getCustomerOrders($customer->id); |
|
|
|
|
148
|
|
|
/** @var \PrestaShop\PrestaShop\Adapter\Entity\Order $order */ |
149
|
|
|
foreach ($orders as $order) { |
150
|
|
|
if ($order['valid']) { |
151
|
|
|
$orderHistory = new \PagaMasTarde\OrdersApiClient\Model\Order\User\OrderHistory(); |
152
|
|
|
$orderHistory |
153
|
|
|
->setAmount((int) (100 * $order['total_paid'])) |
154
|
|
|
->setDate(new \DateTime($order['date_add'])) |
|
|
|
|
155
|
|
|
; |
156
|
|
|
$orderUser->addOrderHistory($orderHistory); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$details = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details(); |
161
|
|
|
$details->setShippingCost((int) (100 * $cart->getTotalShippingCost())); |
162
|
|
|
$items = $cart->getProducts(); |
163
|
|
|
foreach ($items as $key => $item) { |
164
|
|
|
$product = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details\Product(); |
165
|
|
|
$product |
166
|
|
|
->setAmount((int) (100 * $item['price_wt'])) |
167
|
|
|
->setQuantity($item['quantity']) |
168
|
|
|
->setDescription($item['name']); |
169
|
|
|
$details->addProduct($product); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$orderShoppingCart = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart(); |
173
|
|
|
$orderShoppingCart |
174
|
|
|
->setDetails($details) |
175
|
|
|
->setOrderReference($cart->id) |
176
|
|
|
->setPromotedAmount(0) |
177
|
|
|
->setTotalAmount((int) (100 * $cart->getOrderTotal(true))) |
178
|
|
|
; |
179
|
|
|
|
180
|
|
|
$orderConfigurationUrls = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Urls(); |
181
|
|
|
$orderConfigurationUrls |
182
|
|
|
->setCancel($cancelUrl) |
183
|
|
|
->setKo($cancelUrl) |
184
|
|
|
->setAuthorizedNotificationCallback($okUrl) |
185
|
|
|
->setRejectedNotificationCallback($okUrl) |
186
|
|
|
->setOk($okUrl) |
187
|
|
|
; |
188
|
|
|
|
189
|
|
|
$orderChannel = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel(); |
190
|
|
|
$orderChannel |
191
|
|
|
->setAssistedSale(false) |
192
|
|
|
->setType(\PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE) |
193
|
|
|
; |
194
|
|
|
|
195
|
|
|
$orderConfiguration = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration(); |
196
|
|
|
$orderConfiguration |
197
|
|
|
->setChannel($orderChannel) |
198
|
|
|
->setUrls($orderConfigurationUrls) |
199
|
|
|
; |
200
|
|
|
|
201
|
|
|
$metadataOrder = new \PagaMasTarde\OrdersApiClient\Model\Order\Metadata(); |
202
|
|
|
foreach ($metadata as $key => $metadatum) { |
203
|
|
|
$metadataOrder |
204
|
|
|
->addMetadata($key, $metadatum); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$order = new \PagaMasTarde\OrdersApiClient\Model\Order(); |
208
|
|
|
$order |
209
|
|
|
->setConfiguration($orderConfiguration) |
210
|
|
|
->setMetadata($metadataOrder) |
211
|
|
|
->setShoppingCart($orderShoppingCart) |
212
|
|
|
->setUser($orderUser) |
213
|
|
|
; |
214
|
|
|
} catch (\Exception $exception) { |
215
|
|
|
$this->saveLog( |
216
|
|
|
array( |
217
|
|
|
'exception' => 'Exception for user ' . $customer->email . ' : ' . $exception->getMessage() |
218
|
|
|
) |
219
|
|
|
); |
220
|
|
|
Tools::redirect($cancelUrl); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$url =''; |
224
|
|
|
try { |
225
|
|
|
$orderClient = new \PagaMasTarde\OrdersApiClient\Client( |
226
|
|
|
$paylaterPublicKey, |
227
|
|
|
$paylaterPrivateKey |
228
|
|
|
); |
229
|
|
|
$order = $orderClient->createOrder($order); |
|
|
|
|
230
|
|
|
if ($order instanceof \PagaMasTarde\OrdersApiClient\Model\Order) { |
|
|
|
|
231
|
|
|
$url = $order->getActionUrls()->getForm(); |
232
|
|
|
$orderId = $order->getId(); |
233
|
|
|
$result = Db::getInstance()->execute( |
|
|
|
|
234
|
|
|
"INSERT INTO `" . _DB_PREFIX_ . "pmt_order` (`id`, `order_id`) |
|
|
|
|
235
|
|
|
VALUES ('$cart->id','$orderId') |
236
|
|
|
ON DUPLICATE KEY UPDATE `order_id` = '$orderId'" |
237
|
|
|
); |
238
|
|
|
if (!$result) { |
239
|
|
|
throw new \Exception('Unable to save pmt-order-id'); |
240
|
|
|
} |
241
|
|
|
} else { |
242
|
|
|
throw new \Exception('Order not created'); |
243
|
|
|
} |
244
|
|
|
} catch (\Exception $exception) { |
245
|
|
|
$this->saveLog( |
246
|
|
|
array( |
247
|
|
|
'exception' => 'Exception for user ' . $customer->email . ' : ' . $exception->getMessage() |
248
|
|
|
) |
249
|
|
|
); |
250
|
|
|
Tools::redirect($cancelUrl); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
if (!$iframe) { |
254
|
|
|
Tools::redirect($url); |
255
|
|
|
} else { |
256
|
|
|
$this->context->smarty->assign(array( |
257
|
|
|
'url' => $url, |
258
|
|
|
'checkoutUrl' => $cancelUrl, |
259
|
|
|
)); |
260
|
|
|
|
261
|
|
|
try { |
262
|
|
|
if (_PS_VERSION_ < 1.7) { |
263
|
|
|
$this->setTemplate('payment-15.tpl'); |
264
|
|
|
} else { |
265
|
|
|
$this->setTemplate('module:paylater/views/templates/front/payment-17.tpl'); |
266
|
|
|
} |
267
|
|
|
} catch (\Exception $exception) { |
268
|
|
|
$this->saveLog( |
269
|
|
|
array( |
270
|
|
|
'exception' => 'Exception for user ' . $customer->email . ' : ' . $exception->getMessage() |
271
|
|
|
) |
272
|
|
|
); |
273
|
|
|
Tools::redirect($url); |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|