1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DigitalOrigin\Pmt\Controller\Payment; |
4
|
|
|
|
5
|
|
|
use Magento\Framework\App\Action\Action; |
6
|
|
|
use Magento\Framework\App\Action\Context; |
7
|
|
|
use Magento\Quote\Model\QuoteRepository; |
8
|
|
|
use Magento\Sales\Model\ResourceModel\Order\Collection as OrderCollection; |
9
|
|
|
use Magento\Checkout\Model\Session; |
10
|
|
|
use DigitalOrigin\Pmt\Helper\Config; |
11
|
|
|
use Magento\Framework\App\ResourceConnection; |
12
|
|
|
use Magento\Framework\App\ProductMetadataInterface; |
13
|
|
|
use Magento\Framework\Module\ModuleList; |
14
|
|
|
use PagaMasTarde\OrdersApiClient\Model\Order\User\Address; |
15
|
|
|
use Magento\Framework\DB\Ddl\Table; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Index |
19
|
|
|
* @package DigitalOrigin\Pmt\Controller\Payment |
20
|
|
|
*/ |
21
|
|
|
class Index extends Action |
22
|
|
|
{ |
23
|
|
|
/** Orders tablename */ |
24
|
|
|
const ORDERS_TABLE = 'cart_process'; |
25
|
|
|
|
26
|
|
|
/** Concurrency tablename */ |
27
|
|
|
const LOGS_TABLE = 'pmt_logs'; |
28
|
|
|
|
29
|
|
|
/** @var Context $context */ |
30
|
|
|
protected $context; |
31
|
|
|
|
32
|
|
|
/** @var QuoteRepository $quoteRepository */ |
33
|
|
|
protected $quoteRepository; |
34
|
|
|
|
35
|
|
|
/** @var OrderCollection $orderCollection */ |
36
|
|
|
protected $orderCollection; |
37
|
|
|
|
38
|
|
|
/** @var Session $session */ |
39
|
|
|
protected $session; |
40
|
|
|
|
41
|
|
|
/** @var mixed $config */ |
42
|
|
|
protected $config; |
43
|
|
|
|
44
|
|
|
/** @var ResourceConnection $dbObject */ |
45
|
|
|
protected $dbObject; |
46
|
|
|
|
47
|
|
|
/** @var ProductMetadataInterface $productMetadataInterface */ |
48
|
|
|
protected $productMetadataInterface; |
49
|
|
|
|
50
|
|
|
/** @var ModuleList $moduleList */ |
51
|
|
|
protected $moduleList; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Index constructor. |
55
|
|
|
* |
56
|
|
|
* @param Context $context |
57
|
|
|
* @param Session $session |
58
|
|
|
* @param Config $config |
59
|
|
|
* @param QuoteRepository $quoteRepository |
60
|
|
|
* @param OrderCollection $orderCollection |
61
|
|
|
* @param ResourceConnection $dbObject |
62
|
|
|
* @param ModuleList $moduleList |
63
|
|
|
* @param ProductMetadataInterface $productMetadataInterface |
64
|
|
|
* |
65
|
|
|
*/ |
66
|
|
|
public function __construct( |
67
|
|
|
Context $context, |
68
|
|
|
QuoteRepository $quoteRepository, |
69
|
|
|
OrderCollection $orderCollection, |
70
|
|
|
Session $session, |
71
|
|
|
Config $config, |
72
|
|
|
ResourceConnection $dbObject, |
73
|
|
|
ProductMetadataInterface $productMetadataInterface, |
74
|
|
|
ModuleList $moduleList |
75
|
|
|
) { |
76
|
|
|
parent::__construct($context); |
77
|
|
|
$this->session = $session; |
78
|
|
|
$this->context = $context; |
79
|
|
|
$this->config = $config->getConfig(); |
80
|
|
|
$this->quoteRepository = $quoteRepository; |
81
|
|
|
$this->orderCollection = $orderCollection; |
82
|
|
|
$this->dbObject = $dbObject; |
83
|
|
|
$this->moduleList = $moduleList; |
84
|
|
|
$this->productMetadataInterface = $productMetadataInterface; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Main function |
89
|
|
|
* |
90
|
|
|
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void |
91
|
|
|
* @throws \Magento\Framework\Exception\NoSuchEntityException |
92
|
|
|
*/ |
93
|
|
|
public function execute() |
94
|
|
|
{ |
95
|
|
|
try { |
96
|
|
|
$cancelUrl = $this->_url->getUrl('checkout', ['_fragment' => 'payment']); |
97
|
|
|
$quote = $this->session->getQuote(); |
98
|
|
|
/** @var Order $order */ |
99
|
|
|
$lastOrder = $this->session->getLastRealOrder(); |
|
|
|
|
100
|
|
|
$params = $this->getRequest()->getParams(); |
101
|
|
|
$customer = $quote->getCustomer(); |
102
|
|
|
$shippingAddress = $quote->getShippingAddress(); |
103
|
|
|
|
104
|
|
|
if (isset($params['email']) && $params['email']!='') { |
105
|
|
|
$this->session->setEmail($params['email']); //Get guest email after refresh page |
106
|
|
|
$customer->setEmail($params['email']); |
|
|
|
|
107
|
|
|
$quote->setCheckoutMethod('guest'); |
108
|
|
|
$quote->getBillingAddress()->setEmail($params['email']); |
109
|
|
|
} elseif ($customer->getEmail()=='') { |
|
|
|
|
110
|
|
|
$customer->setEmail($this->session->getEmail()); |
|
|
|
|
111
|
|
|
$quote->setCheckoutMethod('guest'); |
112
|
|
|
$quote->getBillingAddress()->setEmail($this->session->getEmail()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** @var Quote $currentQuote */ |
116
|
|
|
$currentQuote = $this->quoteRepository->get($quote->getId()); |
117
|
|
|
$currentQuote->setCustomerEmail($customer->getEmail()); |
118
|
|
|
$this->quoteRepository->save($currentQuote); |
119
|
|
|
|
120
|
|
|
$userAddress = new Address(); |
121
|
|
|
$userAddress |
122
|
|
|
->setZipCode($shippingAddress->getPostcode()) |
123
|
|
|
->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
124
|
|
|
->setCountryCode('ES') |
125
|
|
|
->setCity($shippingAddress->getCity()) |
126
|
|
|
->setAddress($shippingAddress->getStreetFull()) |
127
|
|
|
; |
128
|
|
|
|
129
|
|
|
$orderShippingAddress = new Address(); |
130
|
|
|
$orderShippingAddress |
131
|
|
|
->setZipCode($shippingAddress->getPostcode()) |
132
|
|
|
->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
133
|
|
|
->setCountryCode('ES') |
134
|
|
|
->setCity($shippingAddress->getCity()) |
135
|
|
|
->setAddress($shippingAddress->getStreetFull()) |
136
|
|
|
->setFixPhone($shippingAddress->getTelephone()) |
137
|
|
|
->setMobilePhone($shippingAddress->getTelephone()) |
138
|
|
|
; |
139
|
|
|
|
140
|
|
|
$orderBillingAddress = new Address(); |
141
|
|
|
$billingAddress = $quote->getBillingAddress(); |
142
|
|
|
$orderBillingAddress |
143
|
|
|
->setZipCode($billingAddress->getPostcode()) |
144
|
|
|
->setFullName($billingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
145
|
|
|
->setCountryCode('ES') |
146
|
|
|
->setCity($billingAddress->getCity()) |
147
|
|
|
->setAddress($billingAddress->getStreetFull()) |
148
|
|
|
->setFixPhone($billingAddress->getTelephone()) |
149
|
|
|
->setMobilePhone($billingAddress->getTelephone()) |
150
|
|
|
; |
151
|
|
|
|
152
|
|
|
$orderUser = new \PagaMasTarde\OrdersApiClient\Model\Order\User(); |
153
|
|
|
$billingAddress->setEmail($customer->getEmail()); |
154
|
|
|
$orderUser |
155
|
|
|
->setAddress($userAddress) |
156
|
|
|
->setFullName($shippingAddress->getFirstname()." ".$shippingAddress->getLastname()) |
157
|
|
|
->setBillingAddress($orderBillingAddress) |
158
|
|
|
->setEmail($customer->getEmail()) |
159
|
|
|
->setFixPhone($shippingAddress->getTelephone()) |
160
|
|
|
->setMobilePhone($shippingAddress->getTelephone()) |
161
|
|
|
->setShippingAddress($orderShippingAddress) |
162
|
|
|
; |
163
|
|
|
|
164
|
|
|
if ($customer->getDob()) { |
|
|
|
|
165
|
|
|
$orderUser->setDateOfBirth($customer->getDob()); |
166
|
|
|
} |
167
|
|
|
if ($customer->getTaxvat()!='') { |
|
|
|
|
168
|
|
|
$orderUser->setDni($customer->getTaxvat()); |
169
|
|
|
$orderBillingAddress->setDni($customer->getTaxvat()); |
170
|
|
|
$orderShippingAddress->setDni($customer->getTaxvat()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$previousOrders = $this->getOrders($customer->getId()); |
|
|
|
|
174
|
|
|
foreach ($previousOrders as $orderElement) { |
175
|
|
|
$orderHistory = new \PagaMasTarde\OrdersApiClient\Model\Order\User\OrderHistory(); |
176
|
|
|
$orderHistory |
177
|
|
|
->setAmount(intval(100 * $orderElement['grand_total'])) |
178
|
|
|
->setDate(new \DateTime($orderElement['created_at'])) |
|
|
|
|
179
|
|
|
; |
180
|
|
|
$orderUser->addOrderHistory($orderHistory); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$details = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details(); |
184
|
|
|
$shippingCost = $quote->collectTotals()->getTotals()['shipping']->getData('value'); |
185
|
|
|
$details->setShippingCost(intval(strval(100 * $shippingCost))); |
186
|
|
|
$items = $quote->getAllVisibleItems(); |
187
|
|
|
foreach ($items as $key => $item) { |
188
|
|
|
$product = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart\Details\Product(); |
189
|
|
|
$product |
190
|
|
|
->setAmount(intval(100 * $item->getPrice())) |
191
|
|
|
->setQuantity($item->getQty()) |
192
|
|
|
->setDescription($item->getName()); |
193
|
|
|
$details->addProduct($product); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$orderShoppingCart = new \PagaMasTarde\OrdersApiClient\Model\Order\ShoppingCart(); |
197
|
|
|
$orderShoppingCart |
198
|
|
|
->setDetails($details) |
199
|
|
|
->setOrderReference($quote->getId()) |
200
|
|
|
->setPromotedAmount(0) |
201
|
|
|
->setTotalAmount(intval(strval(100 * $quote->getGrandTotal()))) |
202
|
|
|
; |
203
|
|
|
|
204
|
|
|
$orderConfigurationUrls = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Urls(); |
205
|
|
|
$quoteId = $quote->getId(); |
206
|
|
|
$okUrl = $this->_url->getUrl('paylater/notify', ['_query' => ['quoteId'=>$quoteId]]); |
207
|
|
|
$orderConfigurationUrls |
208
|
|
|
->setCancel($cancelUrl) |
209
|
|
|
->setKo($okUrl) |
210
|
|
|
->setNotificationCallback($okUrl) |
211
|
|
|
->setOk($okUrl) |
212
|
|
|
; |
213
|
|
|
|
214
|
|
|
$orderChannel = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel(); |
215
|
|
|
$orderChannel |
216
|
|
|
->setAssistedSale(false) |
217
|
|
|
->setType(\PagaMasTarde\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE) |
218
|
|
|
; |
219
|
|
|
$orderConfiguration = new \PagaMasTarde\OrdersApiClient\Model\Order\Configuration(); |
220
|
|
|
$orderConfiguration |
221
|
|
|
->setChannel($orderChannel) |
222
|
|
|
->setUrls($orderConfigurationUrls) |
223
|
|
|
; |
224
|
|
|
|
225
|
|
|
$metadataOrder = new \PagaMasTarde\OrdersApiClient\Model\Order\Metadata(); |
226
|
|
|
$metadata = $this->getMetadata(); |
227
|
|
|
foreach ($metadata as $key => $metadatum) { |
228
|
|
|
$metadataOrder->addMetadata($key, $metadatum); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$order = new \PagaMasTarde\OrdersApiClient\Model\Order(); |
232
|
|
|
$order |
233
|
|
|
->setConfiguration($orderConfiguration) |
234
|
|
|
->setMetadata($metadataOrder) |
235
|
|
|
->setShoppingCart($orderShoppingCart) |
236
|
|
|
->setUser($orderUser) |
237
|
|
|
; |
238
|
|
|
|
239
|
|
|
if ($this->config['public_key']=='' || $this->config['secret_key']=='') { |
240
|
|
|
throw new \Exception('Public and Secret Key not found'); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$orderClient = new \PagaMasTarde\OrdersApiClient\Client( |
244
|
|
|
$this->config['public_key'], |
245
|
|
|
$this->config['secret_key'] |
246
|
|
|
); |
247
|
|
|
|
248
|
|
|
$order = $orderClient->createOrder($order); |
249
|
|
|
if ($order instanceof \PagaMasTarde\OrdersApiClient\Model\Order) { |
|
|
|
|
250
|
|
|
$url = $order->getActionUrls()->getForm(); |
251
|
|
|
$result = $this->insertRow($quote->getId(), $order->getId()); |
252
|
|
|
if (!$result) { |
253
|
|
|
throw new \Exception('Unable to save pmt-order-id'); |
254
|
|
|
} |
255
|
|
|
} else { |
256
|
|
|
throw new \Exception('Order not created'); |
257
|
|
|
} |
258
|
|
|
} catch (\Exception $exception) { |
259
|
|
|
$this->insertLog($exception); |
260
|
|
|
echo $cancelUrl; |
261
|
|
|
exit; |
|
|
|
|
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$displayMode = $this->config['display_mode']; |
265
|
|
|
if (!$displayMode) { |
266
|
|
|
echo $url; |
267
|
|
|
exit; |
|
|
|
|
268
|
|
|
} else { |
269
|
|
|
$iframeUrl = $this->_url->getUrl( |
270
|
|
|
"paylater/Payment/iframe", |
271
|
|
|
['_query' => ["orderId"=>$order->getId()]] |
272
|
|
|
); |
273
|
|
|
echo $iframeUrl; |
274
|
|
|
exit; |
|
|
|
|
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Get the orders of a customer |
280
|
|
|
* @param $customerId |
281
|
|
|
* |
282
|
|
|
* @return array |
283
|
|
|
*/ |
284
|
|
|
private function getOrders($customerId) |
285
|
|
|
{ |
286
|
|
|
$orderCollection = array(); |
287
|
|
|
if ($customerId!='') { |
288
|
|
|
$this->orderCollection->addAttributeToFilter('customer_id', $customerId) |
289
|
|
|
->addAttributeToFilter( |
290
|
|
|
'status', |
291
|
|
|
['in' => ['processing','pending','complete']] |
292
|
|
|
) |
293
|
|
|
->load(); |
294
|
|
|
$orderCollection = $this->orderCollection->getData(); |
295
|
|
|
} |
296
|
|
|
return $orderCollection; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @return void|\Zend_Db_Statement_Interface |
301
|
|
|
* @throws \Zend_Db_Exception |
302
|
|
|
*/ |
303
|
|
|
private function checkDbTable() |
304
|
|
|
{ |
305
|
|
|
$dbConnection = $this->dbObject->getConnection(); |
306
|
|
|
$tableName = $this->dbObject->getTableName(self::ORDERS_TABLE); |
307
|
|
|
if (!$dbConnection->isTableExists($tableName)) { |
308
|
|
|
$table = $dbConnection |
309
|
|
|
->newTable($tableName) |
310
|
|
|
->addColumn('id', Table::TYPE_SMALLINT, null, array('primary'=>true)) |
311
|
|
|
->addColumn('order_id', Table::TYPE_TEXT, 50) |
312
|
|
|
->addColumn('mg_order_id', Table::TYPE_TEXT, 50); |
313
|
|
|
return $dbConnection->createTable($table); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Create relationship between quote_id & pmt_order_id |
321
|
|
|
* @param $quoteId |
322
|
|
|
* @param $pmtOrderId |
323
|
|
|
* |
324
|
|
|
* @return int |
325
|
|
|
* @throws \Zend_Db_Exception |
326
|
|
|
*/ |
327
|
|
|
private function insertRow($quoteId, $pmtOrderId) |
328
|
|
|
{ |
329
|
|
|
$this->checkDbTable(); |
330
|
|
|
$dbConnection = $this->dbObject->getConnection(); |
331
|
|
|
$tableName = $this->dbObject->getTableName(self::ORDERS_TABLE); |
332
|
|
|
return $dbConnection->insertOnDuplicate( |
333
|
|
|
$tableName, |
334
|
|
|
array('id'=>$quoteId,'order_id'=>$pmtOrderId), |
335
|
|
|
array('order_id') |
336
|
|
|
); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @return array |
341
|
|
|
*/ |
342
|
|
|
private function getMetadata() |
343
|
|
|
{ |
344
|
|
|
$curlInfo = curl_version(); |
345
|
|
|
$curlVersion = $curlInfo['version']; |
346
|
|
|
$magentoVersion = $this->productMetadataInterface->getVersion(); |
347
|
|
|
$moduleInfo = $this->moduleList->getOne('DigitalOrigin_Pmt'); |
348
|
|
|
return array( 'magento' => $magentoVersion, |
349
|
|
|
'pmt' => $moduleInfo['setup_version'], |
350
|
|
|
'php' => phpversion(), |
351
|
|
|
'curl' => $curlVersion); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Check if log table exists, otherwise create it |
356
|
|
|
* |
357
|
|
|
* @return void|\Zend_Db_Statement_Interface |
358
|
|
|
* @throws \Zend_Db_Exception |
359
|
|
|
*/ |
360
|
|
|
private function checkDbLogTable() |
361
|
|
|
{ |
362
|
|
|
/** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
363
|
|
|
$dbConnection = $this->dbObject->getConnection(); |
364
|
|
|
$tableName = $this->dbObject->getTableName(self::LOGS_TABLE); |
365
|
|
|
if (!$dbConnection->isTableExists($tableName)) { |
366
|
|
|
$table = $dbConnection |
367
|
|
|
->newTable($tableName) |
368
|
|
|
->addColumn('id', Table::TYPE_SMALLINT, null, array('nullable'=>false, 'auto_increment'=>true, 'primary'=>true)) |
369
|
|
|
->addColumn('log', Table::TYPE_TEXT, null, array('nullable'=>false)) |
370
|
|
|
->addColumn('createdAt', Table::TYPE_TIMESTAMP, null, array('nullable'=>false, 'default'=>Table::TIMESTAMP_INIT)); |
371
|
|
|
return $dbConnection->createTable($table); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
return; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @param $exceptionMessage |
379
|
|
|
* |
380
|
|
|
* @throws \Zend_Db_Exception |
381
|
|
|
*/ |
382
|
|
|
private function insertLog($exceptionMessage) |
383
|
|
|
{ |
384
|
|
|
if ($exceptionMessage instanceof \Exception) { |
385
|
|
|
$this->checkDbLogTable(); |
386
|
|
|
$logObject = new \stdClass(); |
387
|
|
|
$logObject->message = $exceptionMessage->getMessage(); |
388
|
|
|
$logObject->code = $exceptionMessage->getCode(); |
389
|
|
|
$logObject->line = $exceptionMessage->getLine(); |
390
|
|
|
$logObject->file = $exceptionMessage->getFile(); |
391
|
|
|
$logObject->trace = $exceptionMessage->getTraceAsString(); |
392
|
|
|
|
393
|
|
|
/** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
394
|
|
|
$dbConnection = $this->dbObject->getConnection(); |
395
|
|
|
$tableName = $this->dbObject->getTableName(self::LOGS_TABLE); |
396
|
|
|
$dbConnection->insert($tableName, array('log' => json_encode($logObject))); |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
|