-
Notifications
You must be signed in to change notification settings - Fork 8
/
Index.php
executable file
·190 lines (164 loc) · 6.85 KB
/
Index.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
<?php
namespace Paytrail\PaymentService\Controller\Redirect;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Gateway\Command\CommandManagerPoolInterface;
use Magento\Sales\Api\OrderManagementInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use Paytrail\PaymentService\Exceptions\CheckoutException;
use Paytrail\PaymentService\Gateway\Config\Config;
use Paytrail\PaymentService\Model\Email\Order\PendingOrderEmailConfirmation;
use Paytrail\PaymentService\Model\ProviderForm;
use Paytrail\PaymentService\Model\Receipt\ProcessService;
use Paytrail\PaymentService\Model\Ui\DataProvider\PaymentProvidersData;
use Psr\Log\LoggerInterface;
class Index implements ActionInterface
{
private $errorMsg = null;
/**
* Index constructor.
*
* @param PendingOrderEmailConfirmation $pendingOrderEmailConfirmation
* @param Session $checkoutSession
* @param OrderRepositoryInterface $orderRepositoryInterface
* @param OrderManagementInterface $orderManagementInterface
* @param LoggerInterface $logger
* @param Config $gatewayConfig
* @param ResultFactory $resultFactory
* @param RequestInterface $request
* @param CommandManagerPoolInterface $commandManagerPool
* @param ProcessService $processService
* @param PaymentProvidersData $paymentProvidersData
* @param ProviderForm $providerForm
*/
public function __construct(
private readonly PendingOrderEmailConfirmation $pendingOrderEmailConfirmation,
private readonly Session $checkoutSession,
private readonly OrderRepositoryInterface $orderRepositoryInterface,
private readonly OrderManagementInterface $orderManagementInterface,
private readonly LoggerInterface $logger,
private readonly Config $gatewayConfig,
private readonly ResultFactory $resultFactory,
private readonly RequestInterface $request,
private readonly CommandManagerPoolInterface $commandManagerPool,
private readonly ProcessService $processService,
private readonly PaymentProvidersData $paymentProvidersData,
private readonly ProviderForm $providerForm
) {
}
/**
* Execute function
*
* @return mixed
*/
public function execute()
{
/** @var Json $resultJson */
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$order = null;
try {
if ($this->request->getParam('is_ajax')) {
$selectedPaymentMethodRaw = $this->request->getParam(
'preselected_payment_method_id'
);
$selectedPaymentMethodId = $this->paymentProvidersData->getIdWithoutIncrement(
$selectedPaymentMethodRaw
);
$cardType = $this->paymentProvidersData->getCardType(
$selectedPaymentMethodRaw
);
if (empty($selectedPaymentMethodId)) {
$this->errorMsg = __('No payment method selected');
throw new LocalizedException(__('No payment method selected'));
}
$order = $this->checkoutSession->getLastRealOrder();
$paytrailPayment = $this->getPaytrailPayment($order, $selectedPaymentMethodId);
if ($this->gatewayConfig->getSkipBankSelection()) {
$redirect_url = $paytrailPayment->getHref();
return $resultJson->setData(
[
'success' => true,
'data' => 'redirect',
'redirect' => $redirect_url
]
);
}
$formParams = $this->providerForm->getFormParams($paytrailPayment, $selectedPaymentMethodId, $cardType);
// send order confirmation for pending order
if ($paytrailPayment) {
$this->pendingOrderEmailConfirmation->pendingOrderEmailSend($order);
}
$block = $this->resultFactory->create(ResultFactory::TYPE_PAGE)
->getLayout()
->createBlock(\Paytrail\PaymentService\Block\Redirect\Paytrail::class)
->setUrl($formParams['action'])
->setParams($this->getInputs($formParams['inputs']));
return $resultJson->setData([
'success' => true,
'data' => $block->toHtml(),
]);
}
} catch (\Exception $e) {
// Error will be handled below
$this->logger->error($e->getMessage());
}
if ($order && $order->getId()) {
$this->orderManagementInterface->cancel($order->getId());
$order->addCommentToStatusHistory(
__('Order canceled. Failed to redirect to Paytrail Payment Service.')
);
$this->orderRepositoryInterface->save($order);
}
$this->checkoutSession->restoreQuote();
return $resultJson->setData([
'success' => false,
'message' => $this->errorMsg
]);
}
/**
* GetResponseData function
*
* @param Order $order
* @param string $paymentMethod
*
* @return mixed
* @throws CheckoutException
* @throws \Magento\Framework\Exception\NotFoundException
* @throws \Magento\Payment\Gateway\Command\CommandException
*/
private function getPaytrailPayment(Order $order, string $paymentMethod)
{
$commandExecutor = $this->commandManagerPool->get('paytrail');
$response = $commandExecutor->executeByCode(
'payment',
null,
[
'order' => $order,
'payment_method' => $paymentMethod
]
);
if ($response['error']) {
$this->errorMsg = ($response['error']);
$this->processService->processError($response['error']);
}
return $response["data"];
}
/**
* @param $inputs
*
* @return mixed
*/
public function getInputs($inputs)
{
$formFields = [];
foreach ($inputs as $input) {
$formFields[$input['name']] = $input['value'];
}
return $formFields;
}
}