Skip to content

Commit

Permalink
handle pending decline
Browse files Browse the repository at this point in the history
  • Loading branch information
zuk3975 committed Jun 27, 2024
1 parent 68fb81f commit 18a7df4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
54 changes: 38 additions & 16 deletions controllers/front/notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,7 @@ public function postProcess()
}

if ($cart->orderExists()) {
if (method_exists('Order', 'getIdByCartId')) {
$orderId = Order::getIdByCartId($cartId);
} else {
// For PrestaShop 1.6 use the alternative method
$orderId = Order::getOrderByCartId($cartId);
}

$orderId = $this->getOrderId($cartId);
$order = new Order($orderId);

$saferPayAuthorizedStatus = (int) Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_PAYMENT_AUTHORIZED);
Expand All @@ -92,24 +86,19 @@ public function postProcess()
}
}

/** @var SaferPayOrderRepository $saferPayOrderRepository */
$saferPayOrderRepository = $this->module->getService(SaferPayOrderRepository::class);

try {
$assertResponseBody = $this->assertTransaction($cartId);

/** @var SaferPayOrderRepository $saferPayOrderRepository */
$saferPayOrderRepository = $this->module->getService(SaferPayOrderRepository::class);
$saferPayOrderId = $saferPayOrderRepository->getIdByCartId($cartId);

/** @var UpdateSaferPayOrderAction $updateSaferPayOrderAction */
$updateSaferPayOrderAction = $this->module->getService(UpdateSaferPayOrderAction::class);
$updateSaferPayOrderAction->run(new SaferPayOrder($saferPayOrderId), self::SAFERPAY_ORDER_AUTHORIZE_ACTION);

// If order does not exist but assertion is valid that means order authorized or captured.
if (method_exists('Order', 'getIdByCartId')) {
$orderId = Order::getIdByCartId($cartId);
} else {
// For PrestaShop 1.6 use the alternative method
$orderId = Order::getOrderByCartId($cartId);
}
$orderId = $this->getOrderId($cartId);
if (!$orderId) {
/** @var CheckoutProcessor $checkoutProcessor **/
$checkoutProcessor = $this->module->getService(CheckoutProcessor::class);
Expand Down Expand Up @@ -163,6 +152,24 @@ public function postProcess()
$orderStatusService->capture($order);
}
} catch (Exception $e) {
// this might be executed after pending transaction is declined (e.g. with accountToAccount payment)
if (!isset($order)) {
$orderId = $this->getOrderId($cartId);
$order = new Order($orderId);
}

$saferPayOrderId = $saferPayOrderRepository->getIdByOrderId($order->id);
$saferPayOrder = new SaferPayOrder($saferPayOrderId);

if ($order->id && $saferPayOrder->id) {
// assuming order transaction was declined
$order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZATION_FAILED_);
$order->update();
$saferPayOrder->authorized = false;
$saferPayOrder->pending = false;
$saferPayOrder->update();
}

PrestaShopLogger::addLog(
sprintf(
'%s has caught an error: %s',
Expand All @@ -188,6 +195,21 @@ private function assertTransaction($cartId) {
return $transactionAssert->assert($cartId);
}

/**
* @param int $cartId
*
* @return bool|int
*/
private function getOrderId($cartId)
{
if (method_exists('Order', 'getIdByCartId')) {
return Order::getIdByCartId($cartId);
} else {
// For PrestaShop 1.6 use the alternative method
return Order::getOrderByCartId($cartId);
}
}

protected function displayMaintenancePage()
{
return true;
Expand Down
1 change: 1 addition & 0 deletions src/Api/Request/AssertService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Invertus\SaferPay\DTO\Response\Assert\AssertBody;
use Invertus\SaferPay\EntityBuilder\SaferPayAssertBuilder;
use Invertus\SaferPay\Exception\Api\SaferPayApiException;
use Invertus\SaferPay\Exception\Api\TransactionDeclinedException;
use Invertus\SaferPay\Service\Response\AssertResponseObjectCreator;
use SaferPayOrder;

Expand Down
3 changes: 1 addition & 2 deletions src/Processor/CheckoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Invertus\SaferPay\Repository\SaferPayOrderRepository;
use Invertus\SaferPay\Service\SaferPayInitialize;
use Order;
use PrestaShop\PrestaShop\Adapter\Entity\PrestaShopLogger;
use PrestaShopException;
use SaferPayOrder;

Expand Down Expand Up @@ -203,7 +204,6 @@ private function processAuthorizedOrder(CheckoutData $data, Cart $cart)
}

$saferPayOrder->id_order = $order->id;

if ($data->getOrderStatus() === 'AUTHORIZED') {
$order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZED_);
$saferPayOrder->authorized = 1;
Expand All @@ -212,7 +212,6 @@ private function processAuthorizedOrder(CheckoutData $data, Cart $cart)
$saferPayOrder->captured = 1;
} elseif ($data->getOrderStatus() === 'PENDING') {
$order->setCurrentState(_SAFERPAY_PAYMENT_PENDING_);
$saferPayOrder->authorized = 1;
$saferPayOrder->pending = 1;
}

Expand Down

0 comments on commit 18a7df4

Please sign in to comment.