Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECP-9408] Fix handling Giftcard payments responses #2724

Merged
merged 35 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
82acd1d
[ECP-9408] Fix the payment response if one of the payments has failed…
Aug 26, 2024
6fa1a09
[ECP-9408] Add unit tests for multiple giftcard payment responses, up…
Aug 27, 2024
5679aee
[ECP-9408] Optimize the unit tests
Aug 27, 2024
88eaf45
[ECP-9408] Fix Spaces
Aug 27, 2024
6a4f7ea
[ECP-9408] Transaction payment returns array of responses, modified t…
Aug 28, 2024
e0a897e
[ECP-9408] Remove duplicate code
Aug 28, 2024
1dc9d1b
[ECP-9408] Remove error
Aug 28, 2024
40ea693
[ECP-9408] Update method return type
Aug 28, 2024
f49691e
[ECP-9408] Remove extra line
Aug 28, 2024
015e4c7
[ECP-9408] Clean up
Aug 28, 2024
a99f147
[ECP-9408] More Clean up
Aug 28, 2024
87f72f0
[ECP-9408] Fix the mistake in array handling
Aug 28, 2024
381a3e6
[ECP-9408] Fix code sniffer issue
Aug 28, 2024
063373e
[ECP-9408] Created a test file and wrote first two test cases
Sep 3, 2024
16cf0f1
[ECP-9408] Added all other test cases
Sep 3, 2024
7c02f89
[ECP-9408] Different creation of the ResultInterfaceFactory mock sinc…
Sep 3, 2024
73a602e
[ECP-9408] php cs fix
Sep 3, 2024
ac35247
[ECP-9408] Re-added call to 'setMethods'
Sep 3, 2024
d67adf1
[ECP-9408] Refactored CheckoutResponseValidator to reduce 'Cognitive …
Sep 3, 2024
78225e7
[ECP-9408] Update parameters on the dummy test values
Sep 4, 2024
b493dd0
[ECP-9408] Update parameters on the dummy test values
Sep 5, 2024
0a6e08e
[ECP-9408] Fix response collection array parameter
Sep 5, 2024
692c798
[ECP-9408] Remove hasOnlyGiftCards where not needed
Sep 5, 2024
9169ec3
[ECP-9408] Removed empty line
Sep 5, 2024
6aa4ee8
Merge branch 'main' into ECP-9408
SushmitaThakur Sep 9, 2024
bb3bdcc
[ECP-9408] FIx `hasOnlyGiftCards` bool being overwritten
Sep 9, 2024
fc599b6
Merge remote-tracking branch 'origin/ECP-9408' into ECP-9408
Sep 9, 2024
a9b3a8a
[ECP-9408] Add Unit test for CheckoutPaymentsDetailsHandler.php
Sep 9, 2024
6ba9ad2
Merge branch 'main' into ECP-9408
SushmitaThakur Sep 10, 2024
c818f2c
[ECP-9408] Add Unit test for VaultDetailsHandler.php and update from …
Sep 10, 2024
651e94f
Merge remote-tracking branch 'origin/ECP-9408' into ECP-9408
Sep 10, 2024
b6dd779
Merge branch 'main' into ECP-9408
SushmitaThakur Sep 10, 2024
e1fe656
[ECP-9408] Add Unit tests for CheckoutPaymentCommentHistoryHandler.php
Sep 10, 2024
a83fe2f
Merge remote-tracking branch 'origin/ECP-9408' into ECP-9408
Sep 10, 2024
04557f9
Merge branch 'main' into ECP-9408
SushmitaThakur Sep 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions Gateway/Http/Client/TransactionPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,20 @@ public function placeRequest(TransferInterface $transferObject): array

$client = $this->adyenHelper->initializeAdyenClientWithClientConfig($clientConfig);
$service = $this->adyenHelper->initializePaymentsApi($client);
$responseData = [];
$responseCollection = [];
$responseCollection['hasOnlyGiftCards'] = false;

try {
list($requestData, $giftcardResponse) = $this->processGiftcards($requestData, $service);
list($requestData, $giftcardResponseCollection) = $this->processGiftcards($requestData, $service);

/** @var PaymentResponse $giftcardResponse */
if (isset($giftcardResponse) && $this->remainingOrderAmount === 0) {
return $giftcardResponse->toArray();
/** @var array $responseCollection */
if (!empty($giftcardResponseCollection)) {
$responseCollection = array_merge($responseCollection, $giftcardResponseCollection);

if ($this->remainingOrderAmount === 0) {
$responseCollection['hasOnlyGiftCards'] = true;
return $responseCollection;
}
}

$requestData['applicationInfo'] = $this->adyenHelper->buildApplicationInfo($client);
Expand All @@ -151,21 +157,22 @@ public function placeRequest(TransferInterface $transferObject): array
$paymentResponse->setMerchantReference($requestData["reference"]);
$this->paymentResponseResourceModel->save($paymentResponse);
$responseData = $response->toArray();
$responseCollection[] = $responseData;

$this->adyenHelper->logResponse($responseData);
} catch (AdyenException $e) {
$this->adyenHelper->logAdyenException($e);
}

return $responseData;
return $responseCollection;
}

/**
* @param array $request
* @param PaymentsApi $service
* @param array $redeemedGiftcards
* @param array $ordersResponse
* @return CheckoutPaymentResponse
* @return array
* @throws AdyenException
* @throws AlreadyExistsException
*/
Expand All @@ -174,7 +181,10 @@ private function handleGiftcardPayments(
PaymentsApi $service,
array $redeemedGiftcards,
array $ordersResponse
): CheckoutPaymentResponse {
): array {

$giftCardResponseCollection = [];

foreach ($redeemedGiftcards as $giftcard) {
$stateData = json_decode($giftcard['state_data'], true);

Expand Down Expand Up @@ -202,20 +212,21 @@ private function handleGiftcardPayments(
);

$response = $service->payments(new PaymentRequest($giftcardPaymentRequest));
SushmitaThakur marked this conversation as resolved.
Show resolved Hide resolved
$this->adyenHelper->logResponse($response->toArray());

/** @var PaymentResponse $paymentResponse */
$paymentResponse = $this->paymentResponseFactory->create();
$paymentResponse->setResponse(json_encode($response));
$paymentResponse->setResultCode($response['resultCode']);
$paymentResponse->setMerchantReference($request["reference"]);

$this->paymentResponseResourceModel->save($paymentResponse);

$this->remainingOrderAmount -= $deductedAmount;
SushmitaThakur marked this conversation as resolved.
Show resolved Hide resolved
$responseArray = $response->toArray();
$giftCardResponseCollection[] = $responseArray;
$this->adyenHelper->logResponse($responseArray);
}

return $response;
return $giftCardResponseCollection;
}

/**
Expand All @@ -239,17 +250,17 @@ public function processGiftcards(array $request, PaymentsApi $service): array
$this->storeManager->getStore()->getId()
);

$giftcardResponse = $this->handleGiftcardPayments($request, $service, $redeemedGiftcards, $ordersResponse);
$giftcardResponseCollection = $this->handleGiftcardPayments($request, $service, $redeemedGiftcards, $ordersResponse);

$request['amount']['value'] = $this->remainingOrderAmount;
$request['order'] = [
'pspReference' => $ordersResponse['pspReference'],
'orderData' => $ordersResponse['orderData']
];
} else {
$giftcardResponse = null;
$giftcardResponseCollection = [];
}

return array($request, $giftcardResponse);
return array($request, $giftcardResponseCollection);
}
}
42 changes: 14 additions & 28 deletions Gateway/Response/CheckoutPaymentCommentHistoryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,37 @@

namespace Adyen\Payment\Gateway\Response;

use Magento\Payment\Gateway\Helper\SubjectReader;
use Magento\Payment\Gateway\Response\HandlerInterface;

class CheckoutPaymentCommentHistoryHandler implements HandlerInterface
{
/**
* @param array $handlingSubject
* @param array $response
* @param array $responseCollection
* @return $this
*/
public function handle(array $handlingSubject, array $response)
public function handle(array $handlingSubject, array $responseCollection): self
{
$readPayment = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($handlingSubject);

$readPayment = SubjectReader::readPayment($handlingSubject);
$payment = $readPayment->getPayment();

$comment = __("Adyen Result response:");

if (isset($response['resultCode'])) {
$responseCode = $response['resultCode'];
} else {
// try to get response from response key (used for modifications
if (isset($response['response'])) {
$responseCode = $response['response'];
} else {
$responseCode = "";
}
}

if (isset($response['pspReference'])) {
$pspReference = $response['pspReference'];
} else {
$pspReference = "";
}
foreach ($responseCollection as $response) {
$responseCode = $response['resultCode'] ?? $response['response'] ?? '';

if ($responseCode) {
$comment .= '<br /> ' . __('authResult:') . ' ' . $responseCode;
$payment->getOrder()->setAdyenResulturlEventCode($responseCode);
}
if (!empty($responseCode)) {
$comment .= '<br /> ' . __('authResult:') . ' ' . $responseCode;
$payment->getOrder()->setAdyenResulturlEventCode($responseCode);
}

if ($pspReference) {
$comment .= '<br /> ' . __('pspReference:') . ' ' . $pspReference;
if (isset($response['pspReference'])) {
$comment .= '<br /> ' . __('pspReference:') . ' ' . $response['pspReference'];
}
$comment .= '<br /> ';
}

$payment->getOrder()->addStatusHistoryComment($comment, $payment->getOrder()->getStatus());

return $this;
}
}
7 changes: 5 additions & 2 deletions Gateway/Response/CheckoutPaymentsDetailsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
* This is being used for all checkout methods (adyen hpp payment method)
*
*/
public function handle(array $handlingSubject, array $response)
public function handle(array $handlingSubject, array $responseCollection)
{
$paymentDataObject = SubjectReader::readPayment($handlingSubject);

Expand All @@ -48,6 +48,9 @@ public function handle(array $handlingSubject, array $response)
$payment->getOrder()->setCanSendNewEmailFlag(false);
}

// for partial payments, non-giftcard payments will always be the last element in the collection
// for non-partial, there is only one response in the collection
$response = end($responseCollection);
if (!empty($response['pspReference'])) {
// set pspReference as transactionId
$payment->setCcTransId($response['pspReference']);
Expand All @@ -57,7 +60,7 @@ public function handle(array $handlingSubject, array $response)
$payment->setTransactionId($response['pspReference']);
}

// do not close transaction so you can do a cancel() and void
// do not close transaction, so you can do a cancel() and void
$payment->setIsTransactionClosed(false);
$payment->setShouldCloseParentTransaction(false);
}
Expand Down
11 changes: 7 additions & 4 deletions Gateway/Response/VaultDetailsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ public function __construct(Vault $vaultHelper)

/**
* @param array $handlingSubject
* @param array $response
* @param array $responseCollection
* @return void
* @throws LocalizedException
*/
public function handle(array $handlingSubject, array $response): void
public function handle(array $handlingSubject, array $responseCollection): void
{
if (empty($response['additionalData'])) {
// for (non-) partial payments, the non-giftcard payment is always last.
$response = end($responseCollection);

// payments without additional data or only giftcards should be ignored.
if (empty($response['additionalData']) || $responseCollection['hasOnlyGiftCards']) {
return;
}

Expand Down
Loading
Loading