Skip to content

Commit

Permalink
Merge pull request #98 from paynl/feature/PLUG-1806
Browse files Browse the repository at this point in the history
PLUG-1806 - FIxed unhappy path auto capture
  • Loading branch information
woutse authored Mar 1, 2023
2 parents 6082f9b + 05d64b9 commit 494059d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
54 changes: 40 additions & 14 deletions Observer/ShipmentSaveAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +21,7 @@ class ShipmentSaveAfter implements ObserverInterface

/**
*
* @var \Paynl\Payment\Model\Config
* @var Config
*/
private $config;

Expand All @@ -30,6 +30,11 @@ class ShipmentSaveAfter implements ObserverInterface
*/
private $payPayment;

/**
* @param Config $config
* @param Store $store
* @param PayPayment $payPayment
*/
public function __construct(
Config $config,
Store $store,
Expand All @@ -40,7 +45,11 @@ public function __construct(
$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());
Expand All @@ -60,27 +69,44 @@ 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
# Handles Sherpa
# 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());
Expand Down

0 comments on commit 494059d

Please sign in to comment.