From f4120d4e6cb3f648ff23863a454252b9eb04070a Mon Sep 17 00:00:00 2001 From: woutse Date: Wed, 2 Jun 2021 20:40:08 +0200 Subject: [PATCH] Fixed an issue with pintransactions and prevented uncatched exception errors --- Controller/Checkout/Finish.php | 36 +++++++++++++++---------- Model/Paymentmethod/Instore.php | 38 +++++++++++++++++++-------- Model/Paymentmethod/PaymentMethod.php | 26 +++++++++++++----- 3 files changed, 69 insertions(+), 31 deletions(-) diff --git a/Controller/Checkout/Finish.php b/Controller/Checkout/Finish.php index 5e99eae7..79a4db82 100644 --- a/Controller/Checkout/Finish.php +++ b/Controller/Checkout/Finish.php @@ -97,9 +97,8 @@ public function execute() $magOrderId = empty($params['entityid']) ? null : $params['entityid']; $bSuccess = $orderStatusId === Config::ORDERSTATUS_PAID; $bDenied = $orderStatusId === Config::ORDERSTATUS_DENIED; - $pinStatus = null; - $pinCanceled = null; - + $isPinTransaction = false; + try { $this->checkEmpty($payOrderId, 'payOrderid', 101); $this->checkEmpty($magOrderId, 'magOrderId', 1012); @@ -112,19 +111,12 @@ public function execute() $this->checkEmpty($information['transactionId'] == $payOrderId, '', 1014, 'transaction mismatch'); - if (!empty($information['terminal_hash'])) { - $status = \Paynl\Instore::status(['hash' => $information['terminal_hash']]); - $pinStatus = $status->getTransactionState(); - $pinCanceled = false; - if (in_array($pinStatus, ['cancelled', 'expired', 'error'])) { - # Instore does not send a canceled exchange message, so cancel it here - $order->cancel(); - $this->orderRepository->save($order); - $pinCanceled = true; - } + if (!empty($information['terminal_hash']) && !$bSuccess) { + $isPinTransaction = true; + $this->handlePin($information['terminal_hash'], $order); } - if (empty($bSuccess) || $pinCanceled == false) { + if (empty($bSuccess) && !$isPinTransaction) { $transaction = \Paynl\Transaction::status($payOrderId); $orderNumber = $transaction->getOrderNumber(); $this->checkEmpty($order->getIncrementId() == $orderNumber, '', 104, 'order mismatch'); @@ -168,4 +160,20 @@ public function execute() return $resultRedirect; } + /** + * @param $hash + * @param $order + * @throws \Magento\Framework\Exception\AlreadyExistsException + * @throws \Magento\Framework\Exception\InputException + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + private function handlePin($hash, $order) + { + $status = \Paynl\Instore::status(['hash' => $hash]); + if (in_array($status->getTransactionState(), ['cancelled', 'expired', 'error'])) { + # Instore does not send a canceled exchange message, so cancel it here + $order->cancel(); + $this->orderRepository->save($order); + } + } } diff --git a/Model/Paymentmethod/Instore.php b/Model/Paymentmethod/Instore.php index a0e1b734..49467700 100644 --- a/Model/Paymentmethod/Instore.php +++ b/Model/Paymentmethod/Instore.php @@ -22,27 +22,43 @@ protected function getDefaultPaymentOptionId() public function startTransaction(Order $order) { + $store = $order->getStore(); + $url = $store->getBaseUrl() . 'checkout/cart/'; $additionalData = $order->getPayment()->getAdditionalInformation(); - $bankId = null; + $terminalId = null; if (isset($additionalData['bank_id'])) { - $bankId = $additionalData['bank_id']; + $terminalId = $additionalData['bank_id']; } unset($additionalData['bank_id']); - $transaction = $this->doStartTransaction($order); + try { + if (empty($terminalId)) { + throw new \Exception(__('Please select a pin-terminal'), 201); + } + $transaction = $this->doStartTransaction($order); + + $instorePayment = \Paynl\Instore::payment(['transactionId' => $transaction->getTransactionId(), 'terminalId' => $terminalId]); + + $additionalData['transactionId'] = $transaction->getTransactionId(); + $additionalData['terminal_hash'] = $instorePayment->getHash(); - $instorePayment = \Paynl\Instore::payment([ - 'transactionId' => $transaction->getTransactionId(), - 'terminalId' => $bankId - ]); + $order->getPayment()->setAdditionalInformation($additionalData); + $order->save(); - $additionalData['terminal_hash'] = $instorePayment->getHash(); + $url = $instorePayment->getRedirectUrl(); - $order->getPayment()->setAdditionalInformation($additionalData); - $order->save(); + } catch (\Exception $e) { + $this->logger->critical($e->getMessage(), array()); + + if ($e->getCode() == 201) { + $this->messageManager->addNoticeMessage($e->getMessage()); + } else { + $this->messageManager->addNoticeMessage(__('Pin transaction could not be started')); + } + } - return $instorePayment->getRedirectUrl(); + return $url; } public function assignData(\Magento\Framework\DataObject $data) diff --git a/Model/Paymentmethod/PaymentMethod.php b/Model/Paymentmethod/PaymentMethod.php index 632c4774..40c609f7 100644 --- a/Model/Paymentmethod/PaymentMethod.php +++ b/Model/Paymentmethod/PaymentMethod.php @@ -12,11 +12,13 @@ use Magento\Framework\Data\Collection\AbstractDb; use Magento\Framework\Model\Context; use Magento\Framework\Model\ResourceModel\AbstractResource; +use Magento\Framework\Message\ManagerInterface; use Magento\Framework\Registry; use Magento\Payment\Helper\Data; use Magento\Payment\Model\InfoInterface; use Magento\Payment\Model\Method\AbstractMethod; use Magento\Payment\Model\Method\Logger; +use Psr\Log\LoggerInterface; use Magento\Sales\Model\Order; use Magento\Sales\Model\OrderRepository; use Paynl\Payment\Model\Config; @@ -31,7 +33,6 @@ abstract class PaymentMethod extends AbstractMethod { protected $_code = 'paynl_payment_base'; - protected $_isInitializeNeeded = true; protected $_canRefund = true; @@ -41,6 +42,11 @@ abstract class PaymentMethod extends AbstractMethod protected $_canVoid = true; + /** + * + * @var Psr\Log\LoggerInterface + */ + protected $logger; /** * @var Config @@ -58,6 +64,11 @@ abstract class PaymentMethod extends AbstractMethod protected $helper; + /** + * @var Magento\Framework\Message\ManagerInterface + */ + protected $messageManager; + public function __construct( Context $context, Registry $registry, @@ -65,25 +76,28 @@ public function __construct( AttributeValueFactory $customAttributeFactory, Data $paymentData, ScopeConfigInterface $scopeConfig, - Logger $logger, + Logger $methodLogger, \Magento\Sales\Model\Order\Config $orderConfig, OrderRepository $orderRepository, Config $paynlConfig, AbstractResource $resource = null, AbstractDb $resourceCollection = null, - array $data = [] + array $data = [], + ManagerInterface $messageManager, + LoggerInterface $logger ) { parent::__construct( $context, $registry, $extensionFactory, $customAttributeFactory, - $paymentData, $scopeConfig, $logger, $resource, $resourceCollection, $data); + $paymentData, $scopeConfig, $methodLogger, $resource, $resourceCollection, $data); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); - + $this->messageManager = $messageManager; $this->helper = $objectManager->create('Paynl\Payment\Helper\PayHelper'); $this->paynlConfig = $paynlConfig; $this->orderRepository = $orderRepository; $this->orderConfig = $orderConfig; + $this->logger = $logger; } protected function getState($status) @@ -294,7 +308,7 @@ protected function doStartTransaction(Order $order) $store = $order->getStore(); $baseUrl = $store->getBaseUrl(); - + $returnUrl = $baseUrl . 'paynl/checkout/finish/?entityid=' . $order->getEntityId(); $exchangeUrl = $baseUrl . 'paynl/checkout/exchange/';