Skip to content

Commit

Permalink
Fixed an issue with pintransactions and prevented uncatched exception…
Browse files Browse the repository at this point in the history
… errors
  • Loading branch information
woutse committed Jun 2, 2021
1 parent cbc278b commit f4120d4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 31 deletions.
36 changes: 22 additions & 14 deletions Controller/Checkout/Finish.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
Expand Down Expand Up @@ -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);
}
}
}
38 changes: 27 additions & 11 deletions Model/Paymentmethod/Instore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 20 additions & 6 deletions Model/Paymentmethod/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +33,6 @@ abstract class PaymentMethod extends AbstractMethod
{
protected $_code = 'paynl_payment_base';


protected $_isInitializeNeeded = true;

protected $_canRefund = true;
Expand All @@ -41,6 +42,11 @@ abstract class PaymentMethod extends AbstractMethod

protected $_canVoid = true;

/**
*
* @var Psr\Log\LoggerInterface
*/
protected $logger;

/**
* @var Config
Expand All @@ -58,32 +64,40 @@ abstract class PaymentMethod extends AbstractMethod

protected $helper;

/**
* @var Magento\Framework\Message\ManagerInterface
*/
protected $messageManager;

public function __construct(
Context $context,
Registry $registry,
ExtensionAttributesFactory $extensionFactory,
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)
Expand Down Expand Up @@ -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/';

Expand Down

0 comments on commit f4120d4

Please sign in to comment.