Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix return failure and set complete for uncapturable payments #174

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions controllers/front/notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function postProcess()
{
$cartId = Tools::getValue('cartId');
$secureKey = Tools::getValue('secureKey');

$cart = new Cart($cartId);

if (!Validate::isLoadedObject($cart)) {
Expand All @@ -71,18 +70,6 @@ public function postProcess()
die($this->module->l('Lock already exist', self::FILENAME));
}

if ($cart->orderExists()) {
$orderId = $this->getOrderId($cartId);
$order = new Order($orderId);

$saferPayAuthorizedStatus = (int) Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_PAYMENT_AUTHORIZED);
$saferPayCapturedStatus = (int) Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_PAYMENT_COMPLETED);

if ((int) $order->current_state === $saferPayAuthorizedStatus || (int) $order->current_state === $saferPayCapturedStatus) {
die($this->module->l('Order already created', self::FILENAME));
}
}

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

Expand Down Expand Up @@ -121,6 +108,18 @@ public function postProcess()
$order = new Order($orderId);
$paymentMethod = $assertResponseBody->getPaymentMeans()->getBrand()->getPaymentMethod();

// if payment does not support order capture, it means it always auto-captures it (at least with accountToAccount payment),
// so in this case if status comes back "captured" we just update the order state accordingly
if (!SaferPayConfig::supportsOrderCapture($paymentMethod) &&
$transactionStatus === TransactionStatus::CAPTURED
) {
/** @var SaferPayOrderStatusService $orderStatusService */
$orderStatusService = $this->module->getService(SaferPayOrderStatusService::class);
$orderStatusService->setComplete($order);

return;
}

if (SaferPayConfig::supportsOrderCapture($paymentMethod) &&
(int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR) === SaferPayConfig::DEFAULT_PAYMENT_BEHAVIOR_CAPTURE &&
$transactionStatus !== TransactionStatus::CAPTURED
Expand All @@ -137,15 +136,20 @@ public function postProcess()
$order = new Order($orderId);
}

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

if ($order->id && $saferPayOrder->id) {
if ($order->id) {
// assuming order transaction was declined
$order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZATION_FAILED_);
$order->update();
$saferPayOrder = new SaferPayOrder($saferPayOrderRepository->getIdByOrderId($order->id));
} else {
// assuming order transaction was cancelled before ps order was even made
$saferPayOrder = new SaferPayOrder($saferPayOrderRepository->getIdByCartId($cartId));
}

if ($saferPayOrder->id) {
$saferPayOrder->authorized = false;
$saferPayOrder->pending = false;
$saferPayOrder->canceled = true;
$saferPayOrder->update();
}

Expand Down
70 changes: 42 additions & 28 deletions controllers/front/return.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\DTO\Response\Assert\AssertBody;
use Invertus\SaferPay\Enum\ControllerName;
use Invertus\SaferPay\Exception\Api\SaferPayApiException;
use Invertus\SaferPay\Repository\SaferPayOrderRepository;
use Invertus\SaferPay\Service\SaferPayOrderStatusService;
use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAssertion;
Expand All @@ -50,16 +51,23 @@ public function postProcess()
return;
}

if ($isBusinessLicence) {
$response = $this->executeTransaction($cartId, $selectedCard);
} else {
$response = $this->assertTransaction($cartId);
}
try {
if ($isBusinessLicence) {
$response = $this->executeTransaction($cartId, $selectedCard);
} else {
$response = $this->assertTransaction($cartId);
}

\PrestaShopLogger::addLog($response->getTransaction()->getStatus());

/** @var SaferPayOrderStatusService $orderStatusService */
$orderStatusService = $this->module->getService(SaferPayOrderStatusService::class);
if ($response->getTransaction()->getStatus() === TransactionStatus::PENDING) {
$orderStatusService->setPending($order);
/** @var SaferPayOrderStatusService $orderStatusService */
$orderStatusService = $this->module->getService(SaferPayOrderStatusService::class);
if ($response->getTransaction()->getStatus() === TransactionStatus::PENDING) {
$orderStatusService->setPending($order);
}
} catch (SaferPayApiException $e) {
\PrestaShopLogger::addLog($e->getMessage());
// we only care if we have a response with pending status, else we skip further actions
}
}
/**
Expand Down Expand Up @@ -175,19 +183,16 @@ protected function processGetStatus()
$saferPayOrderId = $saferPayOrderRepository->getIdByCartId($cartId);
$saferPayOrder = new SaferPayOrder($saferPayOrderId);

if ($saferPayOrder->canceled || !$saferPayOrder->id_order) {
$href = $this->context->link->getModuleLink(
$this->module->name,
ControllerName::FAIL,
[
'cartId' => $cartId,
'secureKey' => $secureKey,
'moduleId' => $moduleId,
],
true
);
} else {
$href = $this->context->link->getModuleLink(
if (!$saferPayOrder->id || $saferPayOrder->canceled) {
$this->ajaxDie(json_encode([
'isFinished' => true,
'href' => $this->getFailControllerLink($cartId, $secureKey, $moduleId)
]));
}

$this->ajaxDie(json_encode([
'isFinished' => $saferPayOrder->authorized || $saferPayOrder->captured || $saferPayOrder->pending,
'href' => $this->context->link->getModuleLink(
$this->module->name,
$this->getSuccessControllerName($isBusinessLicence, $fieldToken),
[
Expand All @@ -197,12 +202,7 @@ protected function processGetStatus()
'secureKey' => $secureKey,
'selectedCard' => $selectedCard,
]
);
}

$this->ajaxDie(json_encode([
'isFinished' => $saferPayOrder->authorized || $saferPayOrder->captured || $saferPayOrder->canceled || $saferPayOrder->pending,
'href' => $href
)
]));
}

Expand All @@ -221,6 +221,20 @@ private function getSuccessControllerName($isBusinessLicence, $fieldToken)
return $successController;
}

private function getFailControllerLink($cartId, $secureKey, $moduleId)
{
return $this->context->link->getModuleLink(
$this->module->name,
ControllerName::FAIL,
[
'cartId' => $cartId,
'secureKey' => $secureKey,
'moduleId' => $moduleId,
],
true
);
}

/**
* @param int $orderId
* @param int $selectedCard
Expand Down
9 changes: 9 additions & 0 deletions src/Service/SaferPayOrderStatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ public function setPending(Order $order)
$order->setCurrentState(_SAFERPAY_PAYMENT_PENDING_);
}

public function setComplete(Order $order)
{
$saferPayOrder = $this->orderRepository->getByOrderId($order->id);
$saferPayOrder->captured = 1;

$saferPayOrder->update();
$order->setCurrentState(_SAFERPAY_PAYMENT_COMPLETED_);
}

/** TODO extract capture api code to different service like Assert for readability */
public function capture(Order $order, $refundedAmount = 0, $isRefund = false)
{
Expand Down
Loading