diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ee21fc..16597f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +2.8.4, 2024-02-13: +- Improve IPN errors management. + 2.8.3, 2024-01-03: - Set return mode to POST by default. - [technical] Improve features management by plugin variant. diff --git a/Controller/Adminhtml/Payment/Check.php b/Controller/Adminhtml/Payment/Check.php index b180a8d..e039605 100644 --- a/Controller/Adminhtml/Payment/Check.php +++ b/Controller/Adminhtml/Payment/Check.php @@ -51,6 +51,11 @@ public function execute() $response = $data['response']; $case = $this->checkProcessor->execute($order, $response); + if ($case === 'payment_ko_on_order_ok') { + header(\Lyranetwork\Payzen\Helper\Payment::HEADER_ERROR_500); + die($response->getOutputForGateway($case)); + } + return $this->renderResponse($response->getOutputForGateway($case)); } catch (\Lyranetwork\Payzen\Model\ResponseException $e) { return $this->renderResponse($e->getMessage()); diff --git a/Controller/Payment/Check.php b/Controller/Payment/Check.php index a23b051..c6134f7 100644 --- a/Controller/Payment/Check.php +++ b/Controller/Payment/Check.php @@ -51,6 +51,11 @@ public function execute() $response = $data['response']; $case = $this->checkProcessor->execute($order, $response); + if ($case === 'payment_ko_on_order_ok') { + header(\Lyranetwork\Payzen\Helper\Payment::HEADER_ERROR_500); + die($response->getOutputForGateway($case)); + } + return $this->renderResponse($response->getOutputForGateway($case)); } catch (\Lyranetwork\Payzen\Model\ResponseException $e) { return $this->renderResponse($e->getMessage()); diff --git a/Controller/Payment/Rest/Check.php b/Controller/Payment/Rest/Check.php index 2931f0a..626c0d0 100644 --- a/Controller/Payment/Rest/Check.php +++ b/Controller/Payment/Rest/Check.php @@ -9,6 +9,7 @@ */ namespace Lyranetwork\Payzen\Controller\Payment\Rest; +use \Lyranetwork\Payzen\Helper\Payment as PayzenPaymentHelper; use Lyranetwork\Payzen\Model\ResponseException; use Magento\Framework\DataObject; @@ -89,13 +90,15 @@ protected function prepareResponse($params) // Check the validity of the request. if (! $this->restHelper->checkResponseFormat($params)) { $this->dataHelper->log('Invalid response received. Content: ' . json_encode($params), \Psr\Log\LogLevel::ERROR); - throw new ResponseException('KO-Invalid IPN request received.'."\n".''); + header(PayzenPaymentHelper::HEADER_ERROR_500); + die('KO-Invalid IPN request received.'."\n".''); } $answer = json_decode($params['kr-answer'], true); if (! is_array($answer)) { $this->dataHelper->log('Invalid response received. Content: ' . json_encode($params), \Psr\Log\LogLevel::ERROR); - throw new ResponseException('KO-Invalid IPN request received.' . "\n" . ''); + header(PayzenPaymentHelper::HEADER_ERROR_500); + die('KO-Invalid IPN request received.' . "\n" . ''); } // Wrap payment result to use traditional order creation tunnel. @@ -115,7 +118,8 @@ protected function prepareResponse($params) $orderId = (int) $response->get('order_id'); if (! $orderId) { $this->dataHelper->log("Received empty Order ID.", \Psr\Log\LogLevel::ERROR); - throw new ResponseException('Order ID is empty.'); + header(PayzenPaymentHelper::HEADER_ERROR_500); + die('Order ID is empty.'); } $order = $this->orderFactory->create(); @@ -126,7 +130,8 @@ protected function prepareResponse($params) if ($quoteId = (int) $response->getExtInfo('quote_id')) { if ($this->quoteRepository->get($quoteId)->getId()) { $this->dataHelper->log("Quote not found with ID #{$quoteId}.", \Psr\Log\LogLevel::ERROR); - throw new ResponseException($response->getOutputForGateway('order_not_found')); + header(PayzenPaymentHelper::HEADER_ERROR_500); + die($response->getOutputForGateway('order_not_found')); } $quote = $this->quoteRepository->get($quoteId); @@ -149,11 +154,13 @@ protected function prepareResponse($params) $order->loadByIncrementId($quote->getReservedOrderId()); if (! $order->getId()) { $this->dataHelper->log("Order cannot be created. Quote ID: #{$quoteId}, reserved order ID: #{$quote->getReservedOrderId()}.", \Psr\Log\LogLevel::ERROR); - throw new ResponseException($response->getOutputForGateway('ko', 'Error when trying to create order.')); + header(PayzenPaymentHelper::HEADER_ERROR_500); + die($response->getOutputForGateway('ko', 'Error when trying to create order.')); } } else { $this->dataHelper->log("Order not found with ID #{$orderId}.", \Psr\Log\LogLevel::ERROR); - throw new ResponseException("Order not found with ID #{$orderId}."); + header(PayzenPaymentHelper::HEADER_ERROR_500); + die("Order not found with ID #{$orderId}."); } } @@ -186,7 +193,8 @@ protected function prepareResponse($params) \Psr\Log\LogLevel::ERROR ); - throw new ResponseException($response->getOutputForGateway('auth_fail')); + header(PayzenPaymentHelper::HEADER_ERROR_500); + die($response->getOutputForGateway('auth_fail')); } return [ @@ -206,7 +214,8 @@ protected function saveOrderForQuote($quote) $this->onepage->saveOrder(); } catch (Exception $e) { $this->dataHelper->log("Order cannot be created. Quote ID: #{$quote->getId()}, reserved order ID: #{$quote->getReservedOrderId()}.", \Psr\Log\LogLevel::ERROR); - throw new ResponseException('Error when trying to create order.'); + header(PayzenPaymentHelper::HEADER_ERROR_500); + die('Error when trying to create order.'); } } } diff --git a/Controller/Processor/CheckProcessor.php b/Controller/Processor/CheckProcessor.php index b380c9f..aefef7f 100644 --- a/Controller/Processor/CheckProcessor.php +++ b/Controller/Processor/CheckProcessor.php @@ -9,8 +9,8 @@ */ namespace Lyranetwork\Payzen\Controller\Processor; +use \Lyranetwork\Payzen\Helper\Payment as PayzenPaymentHelper; use \Lyranetwork\Payzen\Model\Api\Form\Api as PayzenApi; -use Lyranetwork\Payzen\Model\ResponseException; class CheckProcessor { @@ -60,7 +60,7 @@ class CheckProcessor * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Store\Model\App\Emulation $emulation * @param \Lyranetwork\Payzen\Helper\Data $dataHelper - * @param \Lyranetwork\Payzen\Helper\Payment $paymentHelper + * @param PayzenPaymentHelper $paymentHelper * @param \Magento\Sales\Model\OrderFactory $orderFactory * @param \Lyranetwork\Payzen\Model\Api\Form\ResponseFactory $payzenResponseFactory * @param \Magento\Sales\Model\Order\CreditmemoFactory $creditmemoFactory @@ -70,7 +70,7 @@ public function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Store\Model\App\Emulation $emulation, \Lyranetwork\Payzen\Helper\Data $dataHelper, - \Lyranetwork\Payzen\Helper\Payment $paymentHelper, + PayzenPaymentHelper $paymentHelper, \Magento\Sales\Model\OrderFactory $orderFactory, \Lyranetwork\Payzen\Model\Api\Form\ResponseFactory $payzenResponseFactory, \Magento\Sales\Model\Order\CreditmemoFactory $creditmemoFactory, @@ -275,7 +275,8 @@ public function prepareResponse($params) \Psr\Log\LogLevel::ERROR ); - throw new ResponseException($response->getOutputforGateway('auth_fail')); + header(PayzenPaymentHelper::HEADER_ERROR_500); + die($response->getOutputforGateway('auth_fail')); } return [ @@ -290,7 +291,8 @@ private function findOrder($params) $orderId = key_exists('vads_order_id', $params) ? $params['vads_order_id'] : null; if (! $orderId) { $this->dataHelper->log('Order ID is empty. Content: ' . json_encode($params), \Psr\Log\LogLevel::ERROR); - throw new ResponseException('KO-Invalid IPN request received.'."\n".''); + header(PayzenPaymentHelper::HEADER_ERROR_500); + die('KO-Invalid IPN request received.'."\n".''); } // Loading order. @@ -299,7 +301,8 @@ private function findOrder($params) if (! $order->getId()) { $this->dataHelper->log("Order not found with ID #{$orderId}.", \Psr\Log\LogLevel::ERROR); - throw new ResponseException('KO-Order not found.' . "\n" . ''); + header(PayzenPaymentHelper::HEADER_ERROR_500); + die('KO-Order not found.' . "\n" . ''); } return $order; diff --git a/Helper/Payment.php b/Helper/Payment.php index 572847a..34ec0a5 100644 --- a/Helper/Payment.php +++ b/Helper/Payment.php @@ -54,6 +54,8 @@ class Payment const BRAND_USER_CHOICE = 'payzen_brand_user_choice'; + const HEADER_ERROR_500 = 'HTTP/1.1 500 Internal Server Error'; + const SUCCESS = 1; const FAILURE = 2; const CANCEL = 3; diff --git a/Model/Api/Form/Api.php b/Model/Api/Form/Api.php index df9274a..993b7bf 100644 --- a/Model/Api/Form/Api.php +++ b/Model/Api/Form/Api.php @@ -258,7 +258,6 @@ public static function getSuccessStatuses() { return array( 'AUTHORISED', - 'AUTHORISED_TO_VALIDATE', // TODO is this a pending status? 'CAPTURED', 'ACCEPTED', 'PARTIALLY_AUTHORISED' @@ -279,6 +278,8 @@ public static function getPendingStatuses() 'UNDER_VERIFICATION', 'PRE_AUTHORISED', 'WAITING_FOR_PAYMENT', + 'AUTHORISED_TO_VALIDATE', + 'SUSPENDED', 'PENDING' ); } @@ -289,7 +290,11 @@ public static function getPendingStatuses() */ public static function getCancelledStatuses() { - return array('ABANDONED'); + return array( + 'ABANDONED', + 'NOT_CREATED', + 'CANCELLED' + ); } /** @@ -298,7 +303,10 @@ public static function getCancelledStatuses() */ public static function getToValidateStatuses() { - return array('WAITING_AUTHORISATION_TO_VALIDATE', 'AUTHORISED_TO_VALIDATE'); + return array( + 'WAITING_AUTHORISATION_TO_VALIDATE', + 'AUTHORISED_TO_VALIDATE' + ); } /** diff --git a/composer.json b/composer.json index ebd3bcb..bc4c2cd 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "php" : "~7|~8" }, "type" : "magento2-module", - "version" : "2.8.3", + "version" : "2.8.4", "license" : "OSL-3.0", "autoload" : { "files" : [ diff --git a/etc/config.xml b/etc/config.xml index 6f22e43..39b66bc 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -25,7 +25,7 @@ - 2.8.3 + 2.8.4 V2 Magento_2.x 1