-
Notifications
You must be signed in to change notification settings - Fork 2
/
PlaceOrder.php
232 lines (199 loc) · 7.26 KB
/
PlaceOrder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Mollie\Multishipping;
use Magento\Multishipping\Model\Checkout\Type\Multishipping\PlaceOrderInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderManagementInterface;
use Mollie\Multishipping\Service\CheckoutUrl;
use Mollie\Multishipping\Service\Mollie\TransactionDescription;
use Mollie\Multishipping\Service\Order\MultishippingTransaction;
use Mollie\Payment\Helper\General;
use Mollie\Payment\Model\Client\Payments;
use Mollie\Payment\Model\Mollie;
use Mollie\Payment\Service\Order\BuildTransaction;
use Mollie\Payment\Service\Order\MethodCode;
use Mollie\Payment\Service\Order\Transaction;
use Mollie\Payment\Service\PaymentToken\PaymentTokenForOrder;
class PlaceOrder implements PlaceOrderInterface
{
/**
* @var OrderManagementInterface
*/
private $orderManagement;
/**
* @var Mollie
*/
private $mollieModel;
/**
* @var Payments
*/
private $molliePaymentsApi;
/**
* @var array
*/
private $errorList = [];
/**
* @var General
*/
private $mollieHelper;
/**
* @var Transaction
*/
private $transaction;
/**
* @var MultishippingTransaction
*/
private $multishippingTransaction;
/**
* @var BuildTransaction
*/
private $buildTransaction;
/**
* @var CheckoutUrl
*/
private $checkoutUrl;
/**
* @var TransactionDescription
*/
private $transactionDescription;
/**
* @var PaymentTokenForOrder
*/
private $paymentTokenForOrder;
/**
* @var MethodCode
*/
private $methodCode;
public function __construct(
OrderManagementInterface $orderManagement,
Mollie $mollieModel,
Payments $molliePaymentsApi,
General $mollieHelper,
Transaction $transaction,
MultishippingTransaction $multishippingTransaction,
BuildTransaction $buildTransaction,
CheckoutUrl $checkoutUrl,
TransactionDescription $transactionDescription,
PaymentTokenForOrder $paymentTokenForOrder,
MethodCode $methodCode
) {
$this->orderManagement = $orderManagement;
$this->mollieModel = $mollieModel;
$this->molliePaymentsApi = $molliePaymentsApi;
$this->mollieHelper = $mollieHelper;
$this->transaction = $transaction;
$this->multishippingTransaction = $multishippingTransaction;
$this->buildTransaction = $buildTransaction;
$this->checkoutUrl = $checkoutUrl;
$this->transactionDescription = $transactionDescription;
$this->paymentTokenForOrder = $paymentTokenForOrder;
$this->methodCode = $methodCode;
}
/**
* @param OrderInterface[] $orderList
* @return array
*/
public function place(array $orderList): array
{
try {
$mollieOrders = [];
foreach ($orderList as $order) {
$this->orderManagement->place($order);
if (substr($order->getPayment()->getMethod(), 0, 6) === 'mollie') {
// Only process Mollie orders; some orders _could_ have been paid with 'free' method.
$mollieOrders[] = $order;
}
}
if (count($mollieOrders) === 0) {
// This situation should not happen, as the quote would then have 'free' payment method.
// This class will then never be called. But to be sure...
return $this->errorList;
}
$firstOrder = reset($mollieOrders);
$storeId = $firstOrder->getStoreId();
$paymentData = $this->buildPaymentData($mollieOrders, $storeId);
$paymentData = $this->mollieHelper->validatePaymentData($paymentData);
$this->mollieHelper->addTolog('request', $paymentData);
$mollieApi = $this->mollieModel->getMollieApi($storeId);
$paymentResponse = $mollieApi->payments->create($paymentData);
if ($url = $paymentResponse->getCheckoutUrl()) {
$this->checkoutUrl->setUrl($url);
}
} catch (\Exception $exception) {
$errorList = [];
foreach ($orderList as $order) {
$errorList[$order->getIncrementId()] = $exception;
}
return $errorList;
}
foreach ($mollieOrders as $order) {
try {
$this->molliePaymentsApi->processResponse($order, $paymentResponse);
} catch (\Exception $exception) {
$this->errorList[$order->getIncrementId()] = $exception;
}
}
return $this->errorList;
}
private function getTotalAmount(array $orderList)
{
$amount = 0;
$currencyCode = null;
foreach ($orderList as $order) {
if ($this->mollieHelper->useBaseCurrency($order->getStoreId())) {
$amount += $order->getBaseGrandTotal();
$currencyCode = $order->getBaseCurrencyCode();
continue;
}
$amount += $order->getBaseGrandTotal();
$currencyCode = $order->getBaseCurrencyCode();
}
return $this->mollieHelper->getAmountArray($currencyCode, $amount);
}
/**
* @param OrderInterface[] $orderList
* @param $storeId
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @return array
*/
private function buildPaymentData(array $orderList, $storeId): array
{
$firstOrder = reset($orderList);
$paymentTokens = $this->getPaymentTokens($orderList);
$method = $this->methodCode->execute($firstOrder);
$orderIds = array_map(function (OrderInterface $order) { return $order->getEntityId(); }, $orderList);
$paymentData = [
'amount' => $this->getTotalAmount($orderList),
'description' => $this->transactionDescription->forMultishippingTransaction($storeId),
'billingAddress' => $this->molliePaymentsApi->getAddressLine($firstOrder->getBillingAddress()),
'redirectUrl' => $this->multishippingTransaction->getRedirectUrl($orderList, $paymentTokens),
'webhookUrl' => $this->transaction->getWebhookUrl($orderList),
'method' => $method,
'metadata' => [
'order_ids' => implode(', ', $orderIds),
'store_id' => $storeId,
'payment_token' => implode(', ', $paymentTokens),
],
'locale' => $this->mollieHelper->getLocaleCode($storeId, Payments::CHECKOUT_TYPE),
];
if ($method == 'banktransfer') {
$paymentData['billingEmail'] = $firstOrder->getCustomerEmail();
$paymentData['dueDate'] = $this->mollieHelper->getBanktransferDueDate($storeId);
}
if ($method == 'przelewy24') {
$paymentData['billingEmail'] = $firstOrder->getCustomerEmail();
}
return $this->buildTransaction->execute($firstOrder, Payments::CHECKOUT_TYPE, $paymentData);
}
public function getPaymentTokens(array $orders): array
{
$output = [];
foreach ($orders as $order) {
$output[] = $this->paymentTokenForOrder->execute($order);
}
return $output;
}
}