diff --git a/Helper/PaymentResponseHandler.php b/Helper/PaymentResponseHandler.php index e804e167b..7529379a5 100644 --- a/Helper/PaymentResponseHandler.php +++ b/Helper/PaymentResponseHandler.php @@ -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 { @@ -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 @@ -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 ''; } }