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

Cleanup and fix payment authorization issues #173

Merged
merged 1 commit into from
Jul 3, 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
44 changes: 14 additions & 30 deletions controllers/front/notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
use Invertus\SaferPay\Api\Enum\TransactionStatus;
use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\Core\Order\Action\UpdateOrderStatusAction;
use Invertus\SaferPay\Core\Order\Action\UpdateSaferPayOrderAction;
use Invertus\SaferPay\Core\Payment\DTO\CheckoutData;
use Invertus\SaferPay\Processor\CheckoutProcessor;
use Invertus\SaferPay\Repository\SaferPayOrderRepository;
Expand All @@ -39,7 +37,6 @@
class SaferPayOfficialNotifyModuleFrontController extends AbstractSaferPayController
{
const FILENAME = 'notify';
const SAFERPAY_ORDER_AUTHORIZE_ACTION = 'AUTHORIZE';

/**
* This code is being called by SaferPay by using NotifyUrl in InitializeRequest.
Expand Down Expand Up @@ -91,38 +88,24 @@ public function postProcess()

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

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

/** @var CheckoutProcessor $checkoutProcessor **/
$checkoutProcessor = $this->module->getService(CheckoutProcessor::class);
$checkoutData = CheckoutData::create(
(int) $cart->id,
$assertResponseBody->getPaymentMeans()->getBrand()->getPaymentMethod(),
(int) Configuration::get(SaferPayConfig::IS_BUSINESS_LICENCE)
);

// If order does not exist but assertion is valid that means order authorized or captured.
$checkoutData->setOrderStatus($transactionStatus);
$checkoutProcessor->run($checkoutData);
$orderId = $this->getOrderId($cartId);
if (!$orderId) {
/** @var CheckoutProcessor $checkoutProcessor **/
$checkoutProcessor = $this->module->getService(CheckoutProcessor::class);
$checkoutData = CheckoutData::create(
(int) $cart->id,
$assertResponseBody->getPaymentMeans()->getBrand()->getPaymentMethod(),
(int) Configuration::get(SaferPayConfig::IS_BUSINESS_LICENCE)
);

$checkoutData->setIsAuthorizedOrder(true);
$checkoutData->setOrderStatus($assertResponseBody->getTransaction()->getStatus());

$checkoutProcessor->run($checkoutData);
$orderId = $this->getOrderId($cartId);
}

//TODO look into pipeline design pattern to use when object is modified in multiple places to avoid this issue.
//NOTE must be left below assert action to get newest information.
$order = new Order($orderId);

/** @var UpdateOrderStatusAction $updateOrderStatusAction **/
$updateOrderStatusAction = $this->module->getService(UpdateOrderStatusAction::class);
$updateOrderStatusAction->run((int) $orderId, (int) Configuration::get('SAFERPAY_PAYMENT_AUTHORIZED'));

if (!$assertResponseBody->getLiability()->getLiabilityShift() &&
in_array($order->payment, SaferPayConfig::SUPPORTED_3DS_PAYMENT_METHODS) &&
(int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR_WITHOUT_3D) === SaferPayConfig::PAYMENT_BEHAVIOR_WITHOUT_3D_CANCEL
Expand All @@ -138,8 +121,9 @@ public function postProcess()
$order = new Order($orderId);
$paymentMethod = $assertResponseBody->getPaymentMeans()->getBrand()->getPaymentMethod();

if (SaferPayConfig::supportsOrderCapture($paymentMethod) && (int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR) === SaferPayConfig::DEFAULT_PAYMENT_BEHAVIOR_CAPTURE &&
$assertResponseBody->getTransaction()->getStatus() !== TransactionStatus::CAPTURED
if (SaferPayConfig::supportsOrderCapture($paymentMethod) &&
(int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR) === SaferPayConfig::DEFAULT_PAYMENT_BEHAVIOR_CAPTURE &&
$transactionStatus !== TransactionStatus::CAPTURED
) {
/** @var SaferPayOrderStatusService $orderStatusService */
$orderStatusService = $this->module->getService(SaferPayOrderStatusService::class);
Expand Down
8 changes: 4 additions & 4 deletions controllers/front/pendingNotify.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*@license SIX Payment Services
*/

use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Api\Enum\TransactionStatus;
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\DTO\Response\AssertRefund\AssertRefundBody;
use Invertus\SaferPay\Repository\SaferPayOrderRepository;
Expand Down Expand Up @@ -57,12 +57,12 @@ public function postProcess()

$orderRefunds = $saferPayOrderRepository->getOrderRefunds($saferPayOrderId);
foreach ($orderRefunds as $orderRefund) {
if ($orderRefund['status'] === SaferPayConfig::TRANSACTION_STATUS_CAPTURED) {
if ($orderRefund['status'] === TransactionStatus::CAPTURED) {
continue;
}

$assertRefundResponse = $this->assertRefundTransaction($orderRefund['transaction_id']);
if ($assertRefundResponse->getStatus() === SaferPayConfig::TRANSACTION_STATUS_CAPTURED) {
if ($assertRefundResponse->getStatus() === TransactionStatus::CAPTURED) {
$this->handleCapturedRefund($orderRefund['id_saferpay_order_refund']);
}
}
Expand Down Expand Up @@ -90,7 +90,7 @@ private function handleCapturedRefund($orderRefundId)
$saferPayOrderRepository = $this->module->getService(SaferPayOrderRepository::class);

$orderRefund = new SaferPayOrderRefund($orderRefundId);
$orderRefund->status = SaferPayConfig::TRANSACTION_STATUS_CAPTURED;
$orderRefund->status = TransactionStatus::CAPTURED;
$orderRefund->update();

$orderAssertId = $saferPayOrderRepository->getAssertIdBySaferPayOrderId($orderRefund->id_saferpay_order);
Expand Down
57 changes: 41 additions & 16 deletions controllers/front/return.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
*@license SIX Payment Services
*/

use Invertus\SaferPay\Api\Enum\TransactionStatus;
use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\DTO\Response\Assert\AssertBody;
use Invertus\SaferPay\Enum\ControllerName;
use Invertus\SaferPay\Repository\SaferPayOrderRepository;
use Invertus\SaferPay\Service\SaferPayOrderStatusService;
use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAssertion;
use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAuthorization;

if (!defined('_PS_VERSION_')) {
exit;
Expand All @@ -38,18 +41,25 @@ class SaferPayOfficialReturnModuleFrontController extends AbstractSaferPayContro

public function postProcess()
{
$cartId = Tools::getValue('cartId');
$cartId = (int) Tools::getValue('cartId');
$isBusinessLicence = (int) Tools::getValue(SaferPayConfig::IS_BUSINESS_LICENCE);
$selectedCard = (int) Tools::getValue('selectedCard');
$order = new Order($this->getOrderId($cartId));

$transactionResponse = $this->assertTransaction($cartId);
if ($transactionResponse->getTransaction()->getStatus() === 'PENDING') {
$orderId = $this->getOrderId($cartId);
if (!$orderId) {
return;
}
if (!$order->id) {
return;
}

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

/** @var SaferPayOrderStatusService $orderStatusService */
$orderStatusService = $this->module->getService(SaferPayOrderStatusService::class);
$orderStatusService->pending(new \Order($orderId));
/** @var SaferPayOrderStatusService $orderStatusService */
$orderStatusService = $this->module->getService(SaferPayOrderStatusService::class);
if ($response->getTransaction()->getStatus() === TransactionStatus::PENDING) {
$orderStatusService->setPending($order);
}
}
/**
Expand Down Expand Up @@ -96,8 +106,8 @@ public function initContent()

$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);
$saferPayAuthorizedStatus = (int) Configuration::get(SaferPayConfig::SAFERPAY_PAYMENT_AUTHORIZED);
$saferPayCapturedStatus = (int) Configuration::get(SaferPayConfig::SAFERPAY_PAYMENT_COMPLETED);

if ((int) $order->current_state === $saferPayAuthorizedStatus || (int) $order->current_state === $saferPayCapturedStatus) {
Tools::redirect($this->context->link->getModuleLink(
Expand Down Expand Up @@ -191,10 +201,6 @@ protected function processGetStatus()
}

$this->ajaxDie(json_encode([
'authorized' => $saferPayOrder->authorized,
'captured' => $saferPayOrder->captured,
'canceled' => $saferPayOrder->canceled,
'pending' => $saferPayOrder->pending,
'isFinished' => $saferPayOrder->authorized || $saferPayOrder->captured || $saferPayOrder->canceled || $saferPayOrder->pending,
'href' => $href
]));
Expand All @@ -215,6 +221,25 @@ private function getSuccessControllerName($isBusinessLicence, $fieldToken)
return $successController;
}

/**
* @param int $orderId
* @param int $selectedCard
*
* @return AssertBody
* @throws Exception
*/
private function executeTransaction($orderId, $selectedCard)
{
/** @var SaferPayTransactionAuthorization $saferPayTransactionAuthorization */
$saferPayTransactionAuthorization = $this->module->getService(SaferPayTransactionAuthorization::class);

return $saferPayTransactionAuthorization->authorize(
$orderId,
$selectedCard === SaferPayConfig::CREDIT_CARD_OPTION_SAVE,
$selectedCard
);
}

private function assertTransaction($cartId) {
/** @var SaferPayTransactionAssertion $transactionAssert */
$transactionAssert = $this->module->getService(SaferPayTransactionAssertion::class);
Expand Down
Loading
Loading