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