From 7cbacc1e8e1cb1fd8c663193057beed76e4452be Mon Sep 17 00:00:00 2001 From: woutse Date: Tue, 28 Feb 2023 14:12:16 +0100 Subject: [PATCH 1/4] Fixed unhappy path auto-capture --- Observer/ShipmentSaveAfter.php | 60 ++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/Observer/ShipmentSaveAfter.php b/Observer/ShipmentSaveAfter.php index 20b32fd1..3da58843 100644 --- a/Observer/ShipmentSaveAfter.php +++ b/Observer/ShipmentSaveAfter.php @@ -2,17 +2,17 @@ namespace Paynl\Payment\Observer; +use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Store\Model\Store; use Paynl\Result\Transaction\Transaction; use Paynl\Payment\Model\Config; use Magento\Sales\Model\Order; -use \Paynl\Payment\Helper\PayHelper; -use \Paynl\Payment\Model\PayPayment; +use Paynl\Payment\Helper\PayHelper; +use Paynl\Payment\Model\PayPayment; class ShipmentSaveAfter implements ObserverInterface { - /** * * @var Magento\Store\Model\Store; @@ -21,7 +21,7 @@ class ShipmentSaveAfter implements ObserverInterface /** * - * @var \Paynl\Payment\Model\Config + * @var Config */ private $config; @@ -30,17 +30,26 @@ class ShipmentSaveAfter implements ObserverInterface */ private $payPayment; + /** + * @param Config $config + * @param Store $store + * @param PayPayment $payPayment + */ public function __construct( - Config $config, - Store $store, - PayPayment $payPayment + Config $config, + Store $store, + PayPayment $payPayment ) { $this->config = $config; $this->store = $store; $this->payPayment = $payPayment; } - public function execute(\Magento\Framework\Event\Observer $observer) + /** + * @param Observer $observer + * @return void + */ + public function execute(Observer $observer) { $order = $observer->getEvent()->getShipment()->getOrder(); $this->config->setStore($order->getStore()); @@ -60,6 +69,7 @@ public function execute(\Magento\Framework\Event\Observer $observer) if ($bHasAmountAuthorized && $amountPaidCheck === true && $amountRefunded === null) { payHelper::logDebug('AUTO-CAPTURING(shipment-save-after) ' . $payOrderId, [], $order->getStore()); + $bCaptureResult = false; try { # Handles Wuunder # Handles Picqer @@ -67,20 +77,36 @@ public function execute(\Magento\Framework\Event\Observer $observer) # Handles Manual made shipment $this->config->configureSDK(); $bCaptureResult = \Paynl\Transaction::capture($payOrderId); - if ($bCaptureResult) { - $transaction = \Paynl\Transaction::get($payOrderId); - $this->payPayment->processPaidOrder($transaction, $order); - $strResult = 'Success'; - } else { + + if (!$bCaptureResult) { throw new \Exception('Capture failed'); } } catch (\Exception $e) { - payHelper::logDebug('Order PAY error(rest): ' . $e->getMessage() . ' EntityId: ' . $order->getEntityId(), [], $order->getStore()); - $strResult = 'Failed. Errorcode: PAY-MAGENTO2-004. See docs.pay.nl for more information'; + $strMessage = $e->getMessage(); + payHelper::logDebug('Order PAY error(rest): ' . $strMessage . ' EntityId: ' . $order->getEntityId(), [], $order->getStore()); + + $strFriendlyMessage = 'Failed. Errorcode: PAY-MAGENTO2-004. See docs.pay.nl for more information'; + + if (stripos($strMessage, 'Transaction not found') !== false) { + $strFriendlyMessage = 'Transaction seems to be already captured/paid'; + } + } + + $order->addStatusHistoryComment( + __('PAY. - Performed auto-capture. Result: ') . ($bCaptureResult ? 'Success' : 'Failed') . (empty($strFriendlyMessage) ? '' : '. ' . $strFriendlyMessage) + )->save(); + + # Whether capture failed or succeeded, we still might have to process paid order + $transaction = \Paynl\Transaction::get($payOrderId); + if ($transaction->isPaid()) { + $this->payPayment->processPaidOrder($transaction, $order); } - $order->addStatusHistoryComment(__('PAY. - Performed auto-capture. Result: ') . $strResult, false)->save(); } else { - payHelper::logDebug('Auto-Capture conditions not met (yet). Amountpaid:' . $amountPaid . ' bHasAmountAuthorized: ' . ($bHasAmountAuthorized ? '1' : '0'), [], $order->getStore()); + payHelper::logDebug( + 'Auto-Capture conditions not met (yet). Amountpaid:' . $amountPaid . ' bHasAmountAuthorized: ' . ($bHasAmountAuthorized ? '1' : '0'), + [], + $order->getStore() + ); } } else { payHelper::logDebug('Auto-Capture conditions not met (yet). No PAY-Order-id.', [], $order->getStore()); From 8c83f137a3052bab810582cabf190ad73cba61bd Mon Sep 17 00:00:00 2001 From: woutse Date: Tue, 28 Feb 2023 14:13:13 +0100 Subject: [PATCH 2/4] Fixed unhappy path auto-capture --- Model/Config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/Config.php b/Model/Config.php index ffc780a5..4a77f31b 100755 --- a/Model/Config.php +++ b/Model/Config.php @@ -293,7 +293,7 @@ public function ignoreB2BInvoice(string $methodCode) */ public function ignoreManualCapture() { - return $this->store->getConfig('payment/paynl/auto_capture') != 0 && $this->store->getConfig('payment/paynl/auto_capture') != 1; + return $this->store->getConfig('payment/paynl/auto_capture') != 0; } /** From 69aaacfd99d548c151829be08ab4c0a6ac821fb0 Mon Sep 17 00:00:00 2001 From: woutse Date: Tue, 28 Feb 2023 14:14:01 +0100 Subject: [PATCH 3/4] Code Polish --- Observer/ShipmentSaveAfter.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Observer/ShipmentSaveAfter.php b/Observer/ShipmentSaveAfter.php index 3da58843..29c6c106 100644 --- a/Observer/ShipmentSaveAfter.php +++ b/Observer/ShipmentSaveAfter.php @@ -103,9 +103,9 @@ public function execute(Observer $observer) } } else { payHelper::logDebug( - 'Auto-Capture conditions not met (yet). Amountpaid:' . $amountPaid . ' bHasAmountAuthorized: ' . ($bHasAmountAuthorized ? '1' : '0'), - [], - $order->getStore() + 'Auto-Capture conditions not met (yet). Amountpaid:' . $amountPaid . ' bHasAmountAuthorized: ' . ($bHasAmountAuthorized ? '1' : '0'), + [], + $order->getStore() ); } } else { From 05d64b9778a5e18340610043f01f45ce09f538c1 Mon Sep 17 00:00:00 2001 From: woutse Date: Tue, 28 Feb 2023 14:17:47 +0100 Subject: [PATCH 4/4] Code Polish --- Observer/ShipmentSaveAfter.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Observer/ShipmentSaveAfter.php b/Observer/ShipmentSaveAfter.php index 29c6c106..ee7c82f5 100644 --- a/Observer/ShipmentSaveAfter.php +++ b/Observer/ShipmentSaveAfter.php @@ -36,9 +36,9 @@ class ShipmentSaveAfter implements ObserverInterface * @param PayPayment $payPayment */ public function __construct( - Config $config, - Store $store, - PayPayment $payPayment + Config $config, + Store $store, + PayPayment $payPayment ) { $this->config = $config; $this->store = $store; @@ -93,7 +93,7 @@ public function execute(Observer $observer) } $order->addStatusHistoryComment( - __('PAY. - Performed auto-capture. Result: ') . ($bCaptureResult ? 'Success' : 'Failed') . (empty($strFriendlyMessage) ? '' : '. ' . $strFriendlyMessage) + __('PAY. - Performed auto-capture. Result: ') . ($bCaptureResult ? 'Success' : 'Failed') . (empty($strFriendlyMessage) ? '' : '. ' . $strFriendlyMessage) )->save(); # Whether capture failed or succeeded, we still might have to process paid order @@ -104,7 +104,7 @@ public function execute(Observer $observer) } else { payHelper::logDebug( 'Auto-Capture conditions not met (yet). Amountpaid:' . $amountPaid . ' bHasAmountAuthorized: ' . ($bHasAmountAuthorized ? '1' : '0'), - [], + [], $order->getStore() ); }