Skip to content

Commit

Permalink
[ECP-9408] Fix handling Giftcard payments responses (#2724)
Browse files Browse the repository at this point in the history
* [ECP-9408] Fix the payment response if one of the payments has failed in case of partial payments, also fix the processGiftcard response to include all the giftcard processings

* [ECP-9408] Add unit tests for multiple giftcard payment responses, update the existing one for when there is no gift cars and the array is empty

* [ECP-9408] Optimize the unit tests

* [ECP-9408] Fix Spaces

* [ECP-9408] Transaction payment returns array of responses, modified tests accordingly, remove class type assert for multiple giftcard response test, add the foreach for verifying payments in CheckoutResponseValidator

* [ECP-9408] Remove duplicate code

* [ECP-9408] Remove error

* [ECP-9408] Update method return type

* [ECP-9408] Remove extra line

* [ECP-9408] Clean up

* [ECP-9408] More Clean up

* [ECP-9408] Fix the mistake in array handling

* [ECP-9408] Fix code sniffer issue

* [ECP-9408] Created a test file and wrote first two test cases

* [ECP-9408] Added all other test cases

* [ECP-9408] Different creation of the ResultInterfaceFactory mock since it throws a 'class or interface does not exist' exception.

* [ECP-9408] php cs fix

* [ECP-9408] Re-added call to 'setMethods'

* [ECP-9408] Refactored CheckoutResponseValidator to reduce 'Cognitive Complexity' from 35 to below 15

* [ECP-9408] Update parameters on the dummy test values

* [ECP-9408] Update parameters on the dummy test values

* [ECP-9408] Fix response collection array parameter

* [ECP-9408] Remove hasOnlyGiftCards where not needed

* [ECP-9408] Removed empty line

* [ECP-9408] FIx `hasOnlyGiftCards` bool being overwritten

* [ECP-9408] Add Unit test for CheckoutPaymentsDetailsHandler.php

* [ECP-9408] Add Unit test for VaultDetailsHandler.php and update from array_last() to end()

* [ECP-9408] Add Unit tests for CheckoutPaymentCommentHistoryHandler.php

---------

Co-authored-by: sushmita <[email protected]>
  • Loading branch information
SushmitaThakur and sushmita authored Sep 11, 2024
1 parent a525118 commit 5511ac0
Show file tree
Hide file tree
Showing 10 changed files with 851 additions and 139 deletions.
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));
$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;
$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

0 comments on commit 5511ac0

Please sign in to comment.