-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathTransactionCapture.php
More file actions
199 lines (172 loc) · 6.75 KB
/
TransactionCapture.php
File metadata and controls
199 lines (172 loc) · 6.75 KB
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
<?php
/**
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2023 Adyen N.V. (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Gateway\Http\Client;
use Adyen\AdyenException;
use Adyen\Client;
use Adyen\Model\Checkout\PaymentCaptureRequest;
use Adyen\Payment\Api\Data\OrderPaymentInterface;
use Adyen\Payment\Helper\Data;
use Adyen\Payment\Helper\Idempotency;
use Adyen\Payment\Helper\Requests;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Service\Checkout\ModificationsApi;
use Adyen\Payment\Helper\PlatformInfo;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Payment\Gateway\Http\ClientInterface;
use Magento\Payment\Gateway\Http\TransferInterface;
class TransactionCapture implements ClientInterface
{
const MULTIPLE_AUTHORIZATIONS = 'multiple_authorizations';
const FORMATTED_CAPTURE_AMOUNT = 'formatted_capture_amount';
const CAPTURE_AMOUNT = 'capture_amount';
const ORIGINAL_REFERENCE = 'paymentPspReference';
const CAPTURE_RECEIVED = 'received';
/**
* @var Data
*/
private Data $adyenHelper;
/**
* @var AdyenLogger
*/
private AdyenLogger $adyenLogger;
/**
* @var Idempotency
*/
private Idempotency $idempotencyHelper;
/**
* @var PlatformInfo
*/
private PlatformInfo $platformInfo;
/**
* @param Data $adyenHelper
* @param AdyenLogger $adyenLogger
* @param Idempotency $idempotencyHelper
* @param PlatformInfo $platformInfo
*/
public function __construct(
Data $adyenHelper,
AdyenLogger $adyenLogger,
Idempotency $idempotencyHelper,
PlatformInfo $platformInfo
) {
$this->adyenHelper = $adyenHelper;
$this->adyenLogger = $adyenLogger;
$this->idempotencyHelper = $idempotencyHelper;
$this->platformInfo = $platformInfo;
}
/**
* @param TransferInterface $transferObject
* @return array
* @throws AdyenException
* @throws NoSuchEntityException
*/
public function placeRequest(TransferInterface $transferObject): array
{
$request = $transferObject->getBody();
$headers = $transferObject->getHeaders();
$clientConfig = $transferObject->getClientConfig();
$client = $this->adyenHelper->initializeAdyenClientWithClientConfig($clientConfig);
$service = $this->adyenHelper->initializeModificationsApi($client);
$requestOptions['headers'] = $headers;
$request['applicationInfo'] = $this->platformInfo->buildApplicationInfo($client);
if (array_key_exists(self::MULTIPLE_AUTHORIZATIONS, $request)) {
return $this->placeMultipleCaptureRequests($service, $request, $requestOptions);
}
$idempotencyKeyExtraData = $request['idempotencyExtraData'];
unset($request['idempotencyExtraData']);
$idempotencyKey = $this->idempotencyHelper->generateIdempotencyKey(
$request,
$idempotencyKeyExtraData ?? null
);
$requestOptions['idempotencyKey'] = $idempotencyKey;
$this->adyenHelper->logRequest($request, Client::API_CHECKOUT_VERSION, '/captures');
$paymentCaptureRequest = new PaymentCaptureRequest($request);
$responseData = [];
try {
$response = $service->captureAuthorisedPayment(
$request['paymentPspReference'],
$paymentCaptureRequest,
$requestOptions
);
$responseData = $response->toArray();
$responseData = $this->copyParamsToResponse($responseData, $request);
$this->adyenHelper->logResponse($responseData);
} catch (AdyenException $e) {
$this->adyenHelper->logAdyenException($e);
$responseData['error'] = $e->getMessage();
}
return $responseData;
}
/**
* @param ModificationsApi $service
* @param array $requestContainer
* @param array $requestOptions
* @return array
*/
private function placeMultipleCaptureRequests(
ModificationsApi $service,
array $requestContainer,
array $requestOptions
): array {
$response = [];
$applicationInfo = $requestContainer['applicationInfo'];
foreach ($requestContainer[self::MULTIPLE_AUTHORIZATIONS] as $request) {
$idempotencyKeyExtraData = $request['idempotencyExtraData'];
unset($request['idempotencyExtraData']);
$idempotencyKey = $this->idempotencyHelper->generateIdempotencyKey(
$request,
$idempotencyKeyExtraData ?? null
);
$requestOptions['idempotencyKey'] = $idempotencyKey;
try {
// Copy merchant account from parent array to every request array
$request[Requests::MERCHANT_ACCOUNT] = $requestContainer[Requests::MERCHANT_ACCOUNT];
$request['applicationInfo'] = $applicationInfo;
$paymentCaptureRequest = new PaymentCaptureRequest($request);
$singleResponseObj = $service->captureAuthorisedPayment(
$request['paymentPspReference'],
$paymentCaptureRequest,
$requestOptions
);
$singleResponse = $singleResponseObj->toArray();
$singleResponse[self::FORMATTED_CAPTURE_AMOUNT] = $request['amount']['currency'] . ' ' .
$this->adyenHelper->originalAmount(
$request['amount']['value'],
$request['amount']['currency']
);
$singleResponse = $this->copyParamsToResponse($singleResponse, $request);
$response[self::MULTIPLE_AUTHORIZATIONS][] = $singleResponse;
} catch (AdyenException $e) {
$pspReference = $request[OrderPaymentInterface::PSPREFRENCE] ?? 'pspReference not set';
$message = sprintf(
'Exception occurred when attempting to capture multiple authorizations.
Authorization with pspReference %s: %s',
$pspReference,
$e->getMessage()
);
$this->adyenLogger->error($message);
$response[self::MULTIPLE_AUTHORIZATIONS]['error'] = $message;
}
}
return $response;
}
/**
* @param array $response
* @param array $request
* @return array
*/
private function copyParamsToResponse(array $response, array $request): array
{
$response[self::CAPTURE_AMOUNT] = $request['amount']['value'];
$response[self::ORIGINAL_REFERENCE] = $request[self::ORIGINAL_REFERENCE];
return $response;
}
}