Skip to content

Commit

Permalink
Updating logic of canceling the already authorised the giftcards orders
Browse files Browse the repository at this point in the history
  • Loading branch information
khushboo-singhvi committed Oct 23, 2024
1 parent 7c409a7 commit 258f342
Showing 1 changed file with 55 additions and 57 deletions.
112 changes: 55 additions & 57 deletions Helper/PaymentResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Magento\Sales\Model\Order as OrderModel;
use Adyen\Payment\Helper\Data as Data;
use Magento\Framework\Mail\Exception\InvalidArgumentException;
use Adyen\Client;

class PaymentResponseHandler
{
Expand Down Expand Up @@ -290,61 +291,10 @@ public function handlePaymentsDetailsResponse(
break;
case self::REFUSED:
case self::CANCELLED:
$getGiftcardDetails = $this->hasActiveGiftCardPayments(
$paymentsDetailsResponse['merchantReference']
$this->hasActiveGiftCardPayments(
$paymentsDetailsResponse['merchantReference'], $order
);
if (!empty($getGiftcardDetails)) {
//Cancel the Authorised Payments
$storeId = $order->getStoreId();
$client = $this->dataHelper->initializeAdyenClient($storeId);
$service = $this->dataHelper->initializeOrdersApi($client);
foreach ($getGiftcardDetails as $giftcardData) {
try {
// Decode JSON response and validate it
$response = json_decode($giftcardData['response'], true);
if (json_last_error() !== JSON_ERROR_NONE || !isset($response['order'])) {
throw new InvalidArgumentException('Invalid giftcard response data');
}

// Extract order data and PSPRef
$orderData = $response['order']['orderData'] ?? null;
$pspReference = $response['order']['pspReference'] ?? null;

if (!$orderData || !$pspReference) {
throw new InvalidArgumentException('Missing orderData or pspReference in the response');
}

// Prepare cancel request
$merchantAccount = $this->configHelper->getAdyenAbstractConfigData("merchant_account", $storeId);
$cancelRequest = [
'order' => [
'pspReference' => $pspReference,
'orderData' => $orderData,
],
'merchantAccount' => $merchantAccount,
];

// Call the cancel service
$cancelResponse = $service->cancelOrder(new CancelOrderRequest($cancelRequest));
$response = $cancelResponse->toArray();

if (is_null($response['resultCode'])) {
// In case the result is unknown we log the request and don't update the history
$this->adyenLogger->error(
"Unexpected result query parameter for cancel order request. Response: " . json_encode($response)
);

return false;
}
} catch (\Exception $e) {
// Log the error with relevant information for debugging
$this->adyenLogger->error('Error canceling partial payments', [
'exception' => $e->getMessage(),
'giftcardData' => $giftcardData,
]);
}
}
}

// Cancel order in case result is refused
if (null !== $order) {
// Check if the current state allows for changing to new for cancellation
Expand Down Expand Up @@ -409,15 +359,63 @@ private function isValidMerchantReference(array $paymentsDetailsResponse, OrderI
}

// Method to check for existing Gift Card payments
private function hasActiveGiftCardPayments($merchantReference): array|string|null
private function hasActiveGiftCardPayments($merchantReference, $order)
{
$paymentResponseCollection = $this->paymentResponseCollectionFactory->create()
->addFieldToFilter('merchant_reference', $merchantReference)
->addFieldToFilter('result_code', 'Authorised');

if ($paymentResponseCollection->getSize() > 0) {
return $paymentResponseCollection->getData();
$getGiftcardDetails = $paymentResponseCollection->getData();

//Cancel the Authorised Payments
$storeId = $order->getStoreId();
$client = $this->dataHelper->initializeAdyenClient($storeId);
$service = $this->dataHelper->initializeOrdersApi($client);
foreach ($getGiftcardDetails as $giftcardData) {
try {
// Decode JSON response and validate it
$response = json_decode($giftcardData['response'], true);
if (json_last_error() !== JSON_ERROR_NONE || !isset($response['order'])) {
throw new InvalidArgumentException('Invalid giftcard response data');
}

// Extract order data and PSPRef
$orderData = $response['order']['orderData'] ?? null;
$pspReference = $response['order']['pspReference'] ?? null;

if (!$orderData || !$pspReference) {
throw new InvalidArgumentException('Missing orderData or pspReference in the response');
}

// Prepare cancel request
$merchantAccount = $this->configHelper->getAdyenAbstractConfigData("merchant_account", $storeId);
$cancelRequest = [
'order' => [
'pspReference' => $pspReference,
'orderData' => $orderData,
],
'merchantAccount' => $merchantAccount,
];
$this->dataHelper->logRequest($cancelRequest, Client::API_CHECKOUT_VERSION, '/orders/cancel');
// Call the cancel service
$cancelResponse = $service->cancelOrder(new CancelOrderRequest($cancelRequest));
$response = $cancelResponse->toArray();
$this->dataHelper->logResponse($response);
if (is_null($response['resultCode'])) {
// In case the result is unknown we log the request and don't update the history
$this->adyenLogger->error(
"Unexpected result query parameter for cancel order request. Response: " . json_encode($response)
);
}
} catch (\Exception $e) {
// Log the error with relevant information for debugging
$this->adyenLogger->error('Error canceling partial payments', [
'exception' => $e->getMessage(),
'giftcardData' => $giftcardData,
]);
}
}
}
return '';
}
}

0 comments on commit 258f342

Please sign in to comment.