From 8a3052fc2d2632759787a53ad749c7c6a912e1ea Mon Sep 17 00:00:00 2001 From: Julius Zukauskas Date: Wed, 3 Jul 2024 17:36:17 +0300 Subject: [PATCH] cleanup and fix payment authorization issues --- controllers/front/notify.php | 44 ++-- controllers/front/pendingNotify.php | 8 +- controllers/front/return.php | 57 +++-- saferpayofficial.php | 200 ++++++++++-------- src/Config/SaferPayConfig.php | 9 +- .../Order/Action/UpdateOrderStatusAction.php | 4 +- src/Processor/CheckoutProcessor.php | 59 +++--- src/Repository/SaferPayOrderRepository.php | 1 + src/Service/SaferPayInitialize.php | 3 - src/Service/SaferPayOrderStatusService.php | 25 +-- 10 files changed, 207 insertions(+), 203 deletions(-) diff --git a/controllers/front/notify.php b/controllers/front/notify.php index 0ac0d22f..cd4c2f5f 100755 --- a/controllers/front/notify.php +++ b/controllers/front/notify.php @@ -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; @@ -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. @@ -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 @@ -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); diff --git a/controllers/front/pendingNotify.php b/controllers/front/pendingNotify.php index 43c6639a..cf25f56d 100755 --- a/controllers/front/pendingNotify.php +++ b/controllers/front/pendingNotify.php @@ -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; @@ -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']); } } @@ -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); diff --git a/controllers/front/return.php b/controllers/front/return.php index 6d62e477..9db0e2ba 100755 --- a/controllers/front/return.php +++ b/controllers/front/return.php @@ -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; @@ -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); } } /** @@ -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( @@ -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 ])); @@ -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); diff --git a/saferpayofficial.php b/saferpayofficial.php index a89a33e1..8199671c 100755 --- a/saferpayofficial.php +++ b/saferpayofficial.php @@ -21,7 +21,29 @@ *@license SIX Payment Services */ +use Invertus\SaferPay\Builder\OrderConfirmationMessageTemplate; use Invertus\SaferPay\Config\SaferPayConfig; +use Invertus\SaferPay\Core\Order\Verification\CanSendOrderConfirmationEmail; +use Invertus\SaferPay\Exception\Api\SaferPayApiException; +use Invertus\SaferPay\Install\Installer; +use Invertus\SaferPay\Install\Uninstaller; +use Invertus\SaferPay\Presentation\Loader\PaymentFormAssetLoader; +use Invertus\SaferPay\Presenter\AdminOrderPagePresenter; +use Invertus\SaferPay\Presenter\AssertPresenter; +use Invertus\SaferPay\Provider\PaymentRedirectionProvider; +use Invertus\SaferPay\Provider\PaymentTypeProvider; +use Invertus\SaferPay\Repository\SaferPayCardAliasRepository; +use Invertus\SaferPay\Repository\SaferPayOrderRepository; +use Invertus\SaferPay\Repository\SaferPayPaymentRepository; +use Invertus\SaferPay\Service\LegacyTranslator; +use Invertus\SaferPay\Service\PaymentRestrictionValidation; +use Invertus\SaferPay\Service\SaferPayCartService; +use Invertus\SaferPay\Service\SaferPayErrorDisplayService; +use Invertus\SaferPay\Service\SaferPayMailService; +use Invertus\SaferPay\Service\SaferPayObtainPaymentMethods; +use Invertus\SaferPay\Service\SaferPayPaymentNotation; +use Invertus\SaferPay\ServiceProvider\LeagueServiceContainerProvider; +use PrestaShop\PrestaShop\Core\Payment\PaymentOption; if (!defined('_PS_VERSION_')) { exit; @@ -59,7 +81,7 @@ public function __construct($name = null) public function getContent() { - if (Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::USERNAME)) { + if (Configuration::get(SaferPayConfig::USERNAME)) { Tools::redirectAdmin($this->context->link->getAdminLink(self::ADMIN_PAYMENTS_CONTROLLER)); } Tools::redirectAdmin($this->context->link->getAdminLink(self::ADMIN_SETTINGS_CONTROLLER)); @@ -67,7 +89,7 @@ public function getContent() public function install() { - $installer = new \Invertus\SaferPay\Install\Installer($this); + $installer = new Installer($this); if (!parent::install()) { return false; @@ -82,7 +104,7 @@ public function install() public function uninstall() { - $uninstaller = new \Invertus\SaferPay\Install\Uninstaller($this); + $uninstaller = new Uninstaller($this); if (!$uninstaller->uninstall()) { $this->_errors += $uninstaller->getErrors(); return false; @@ -92,7 +114,7 @@ public function uninstall() public function getTabs() { - $installer = new \Invertus\SaferPay\Install\Installer($this); + $installer = new Installer($this); return $installer->tabs(); } @@ -111,7 +133,7 @@ private function loadConfig() } public function getService($service) { - $containerProvider = new \Invertus\SaferPay\ServiceProvider\LeagueServiceContainerProvider(); + $containerProvider = new LeagueServiceContainerProvider(); return $containerProvider->getService($service); } @@ -125,8 +147,8 @@ public function hookDisplayOrderConfirmation($params) /** @var Order $psOrder */ $psOrder = $params['order']; - /** @var \Invertus\SaferPay\Repository\SaferPayOrderRepository $repository */ - $repository = $this->getService(\Invertus\SaferPay\Repository\SaferPayOrderRepository::class); + /** @var SaferPayOrderRepository $repository */ + $repository = $this->getService(SaferPayOrderRepository::class); $sfOrder = $repository->getByOrderId((int) $psOrder->id); if (!$sfOrder->pending) { @@ -151,8 +173,8 @@ public function hookActionObjectOrderPaymentAddAfter($params) return; } - /** @var \Invertus\SaferPay\Repository\SaferPayOrderRepository $saferPayOrderRepository */ - $saferPayOrderRepository = $this->getService(\Invertus\SaferPay\Repository\SaferPayOrderRepository::class); + /** @var SaferPayOrderRepository $saferPayOrderRepository */ + $saferPayOrderRepository = $this->getService(SaferPayOrderRepository::class); $orders = Order::getByReference($orderPayment->order_reference); @@ -182,30 +204,30 @@ public function hookActionObjectOrderPaymentAddAfter($params) public function hookPaymentOptions($params) { /** @var Invertus\SaferPay\Service\SaferPayCartService $assertService */ - $cartService = $this->getService(\Invertus\SaferPay\Service\SaferPayCartService::class); + $cartService = $this->getService(SaferPayCartService::class); if (!$cartService->isCurrencyAvailable($params['cart'])) { return; } - /** @var \Invertus\SaferPay\Provider\PaymentTypeProvider $paymentTypeProvider */ - $paymentTypeProvider = $this->getService(\Invertus\SaferPay\Provider\PaymentTypeProvider::class); + /** @var PaymentTypeProvider $paymentTypeProvider */ + $paymentTypeProvider = $this->getService(PaymentTypeProvider::class); - /** @var \Invertus\SaferPay\Service\SaferPayObtainPaymentMethods $obtainPaymentMethods */ - $obtainPaymentMethods = $this->getService(\Invertus\SaferPay\Service\SaferPayObtainPaymentMethods::class); - /** @var \Invertus\SaferPay\Repository\SaferPayPaymentRepository $paymentRepository */ - $paymentRepository = $this->getService(\Invertus\SaferPay\Repository\SaferPayPaymentRepository::class); + /** @var SaferPayObtainPaymentMethods $obtainPaymentMethods */ + $obtainPaymentMethods = $this->getService(SaferPayObtainPaymentMethods::class); + /** @var SaferPayPaymentRepository $paymentRepository */ + $paymentRepository = $this->getService(SaferPayPaymentRepository::class); try { $paymentMethods = $obtainPaymentMethods->obtainPaymentMethods(); - } catch (\Invertus\SaferPay\Exception\Api\SaferPayApiException $exception) { + } catch (SaferPayApiException $exception) { return []; } $paymentOptions = []; - /** @var \Invertus\SaferPay\Service\PaymentRestrictionValidation $paymentRestrictionValidation */ + /** @var PaymentRestrictionValidation $paymentRestrictionValidation */ $paymentRestrictionValidation = $this->getService( - \Invertus\SaferPay\Service\PaymentRestrictionValidation::class + PaymentRestrictionValidation::class ); foreach ($paymentMethods as $paymentMethod) { @@ -220,20 +242,20 @@ public function hookPaymentOptions($params) $isCreditCard = in_array( $paymentMethod['paymentMethod'], - \Invertus\SaferPay\Config\SaferPayConfig::TRANSACTION_METHODS + SaferPayConfig::TRANSACTION_METHODS ); $isBusinessLicenseEnabled = Configuration::get( - \Invertus\SaferPay\Config\SaferPayConfig::BUSINESS_LICENSE - . \Invertus\SaferPay\Config\SaferPayConfig::getConfigSuffix() + SaferPayConfig::BUSINESS_LICENSE + . SaferPayConfig::getConfigSuffix() ); - /** @var \Invertus\SaferPay\Repository\SaferPayCardAliasRepository $cardAliasRep */ + /** @var SaferPayCardAliasRepository $cardAliasRep */ $cardAliasRep = $this->getService( - \Invertus\SaferPay\Repository\SaferPayCardAliasRepository::class + SaferPayCardAliasRepository::class ); $isCreditCardSavingEnabled = Configuration::get( - \Invertus\SaferPay\Config\SaferPayConfig::CREDIT_CARD_SAVE + SaferPayConfig::CREDIT_CARD_SAVE ); $selectedCard = 0; if ($this->context->customer->is_guest) { @@ -241,15 +263,15 @@ public function hookPaymentOptions($params) $selectedCard = -1; } - /** @var \Invertus\SaferPay\Provider\PaymentRedirectionProvider $paymentRedirectionProvider */ - $paymentRedirectionProvider = $this->getService(\Invertus\SaferPay\Provider\PaymentRedirectionProvider::class); + /** @var PaymentRedirectionProvider $paymentRedirectionProvider */ + $paymentRedirectionProvider = $this->getService(PaymentRedirectionProvider::class); - $newOption = new \PrestaShop\PrestaShop\Core\Payment\PaymentOption(); + $newOption = new PaymentOption(); $translator = $this->getService( - \Invertus\SaferPay\Service\LegacyTranslator::class + LegacyTranslator::class ); - /** @var \Invertus\SaferPay\Service\SaferPayPaymentNotation $saferPayPaymentNotation */ - $saferPayPaymentNotation = $this->getService(\Invertus\SaferPay\Service\SaferPayPaymentNotation::class); + /** @var SaferPayPaymentNotation $saferPayPaymentNotation */ + $saferPayPaymentNotation = $this->getService(SaferPayPaymentNotation::class); $paymentMethodName = $saferPayPaymentNotation->getForDisplay($paymentMethod['paymentMethod']); $inputs = [ @@ -316,7 +338,7 @@ public function hookPaymentOptions($params) public function hookDisplayAdminOrderTabContent(array $params) { - $isVersionAbove177 = \Invertus\SaferPay\Config\SaferPayConfig::isVersionAbove177(); + $isVersionAbove177 = SaferPayConfig::isVersionAbove177(); return !$isVersionAbove177 ? false : $this->displayInAdminOrderPage($params); } @@ -324,20 +346,20 @@ public function hookDisplayAdminOrderTabContent(array $params) public function hookDisplayAdminOrder(array $params) { - $isVersionAbove177 = \Invertus\SaferPay\Config\SaferPayConfig::isVersionAbove177(); + $isVersionAbove177 = SaferPayConfig::isVersionAbove177(); return $isVersionAbove177 ? false : $this->displayInAdminOrderPage($params); } public function hookActionFrontControllerSetMedia() { - /** @var \Invertus\SaferPay\Presentation\Loader\PaymentFormAssetLoader $paymentFormAssetsLoader */ - $paymentFormAssetsLoader = $this->getService(\Invertus\SaferPay\Presentation\Loader\PaymentFormAssetLoader::class); + /** @var PaymentFormAssetLoader $paymentFormAssetsLoader */ + $paymentFormAssetsLoader = $this->getService(PaymentFormAssetLoader::class); $paymentFormAssetsLoader->register($this->context->controller); if ($this->context->controller instanceof OrderController) { - if (\Invertus\SaferPay\Config\SaferPayConfig::isVersion17()) { + if (SaferPayConfig::isVersion17()) { $this->context->controller->registerJavascript( 'saved-card', 'modules/' . $this->name . '/views/js/front/saferpay_saved_card.js' @@ -347,13 +369,13 @@ public function hookActionFrontControllerSetMedia() } else { $this->context->controller->addCSS("{$this->getPathUri()}views/css/front/saferpay_checkout_16.css"); $this->context->controller->addJS("{$this->getPathUri()}views/js/front/saferpay_saved_card_16.js"); - $fieldsLibrary = \Invertus\SaferPay\Config\SaferPayConfig::FIELDS_LIBRARY; - $configSuffix = \Invertus\SaferPay\Config\SaferPayConfig::getConfigSuffix(); + $fieldsLibrary = SaferPayConfig::FIELDS_LIBRARY; + $configSuffix = SaferPayConfig::getConfigSuffix(); $this->context->controller->addJs(Configuration::get($fieldsLibrary . $configSuffix)); } - /** @var \Invertus\SaferPay\Service\SaferPayErrorDisplayService $errorDisplayService */ - $errorDisplayService = $this->getService(\Invertus\SaferPay\Service\SaferPayErrorDisplayService::class); + /** @var SaferPayErrorDisplayService $errorDisplayService */ + $errorDisplayService = $this->getService(SaferPayErrorDisplayService::class); $errorDisplayService->showCookieError('saferpay_payment_canceled_error'); } } @@ -361,11 +383,11 @@ public function hookActionFrontControllerSetMedia() public function hookDisplayCustomerAccount() { $isCreditCardSaveEnabled = - Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::CREDIT_CARD_SAVE); + Configuration::get(SaferPayConfig::CREDIT_CARD_SAVE); if (!$isCreditCardSaveEnabled) { return; } - if (\Invertus\SaferPay\Config\SaferPayConfig::isVersion17()) { + if (SaferPayConfig::isVersion17()) { return $this->context->smarty->fetch( $this->getLocalPath() . 'views/templates/hook/front/MyAccount.tpl' ); @@ -378,7 +400,7 @@ public function hookDisplayCustomerAccount() public function displayNavigationTop() { - if (\Invertus\SaferPay\Config\SaferPayConfig::isVersion17()) { + if (SaferPayConfig::isVersion17()) { return; } @@ -443,27 +465,27 @@ public function hookDisplayPayment($params) return; } - /** @var \Invertus\SaferPay\Service\SaferPayCartService $assertService */ - $cartService = $this->getService(\Invertus\SaferPay\Service\SaferPayCartService::class); + /** @var SaferPayCartService $assertService */ + $cartService = $this->getService(SaferPayCartService::class); if (!$cartService->isCurrencyAvailable($params['cart'])) { return; } - /** @var \Invertus\SaferPay\Service\PaymentRestrictionValidation $paymentRestrictionValidation */ - $paymentRepository = $this->getService(\Invertus\SaferPay\Repository\SaferPayPaymentRepository::class); + /** @var PaymentRestrictionValidation $paymentRestrictionValidation */ + $paymentRepository = $this->getService(SaferPayPaymentRepository::class); - /** @var \Invertus\SaferPay\Service\SaferPayObtainPaymentMethods $obtainPaymentMethods */ - $obtainPaymentMethods = $this->getService(\Invertus\SaferPay\Service\SaferPayObtainPaymentMethods::class); + /** @var SaferPayObtainPaymentMethods $obtainPaymentMethods */ + $obtainPaymentMethods = $this->getService(SaferPayObtainPaymentMethods::class); try { $paymentMethods = $obtainPaymentMethods->obtainPaymentMethods(); - } catch (\Invertus\SaferPay\Exception\Api\SaferPayApiException $exception) { + } catch (SaferPayApiException $exception) { return ''; } $paymentOptions = []; $paymentRestrictionValidation = $this->getService( - \Invertus\SaferPay\Service\PaymentRestrictionValidation::class + PaymentRestrictionValidation::class ); foreach ($paymentMethods as $paymentMethod) { @@ -475,20 +497,20 @@ public function hookDisplayPayment($params) $imageUrl = ($paymentRepository->isLogoEnabledByName($paymentMethod['paymentMethod'])) ? $paymentMethod['logoUrl'] : ''; - $isCreditCard = in_array($paymentMethod['paymentMethod'], \Invertus\SaferPay\Config\SaferPayConfig::TRANSACTION_METHODS); + $isCreditCard = in_array($paymentMethod['paymentMethod'], SaferPayConfig::TRANSACTION_METHODS); $isBusinessLicenseEnabled = Configuration::get( - \Invertus\SaferPay\Config\SaferPayConfig::BUSINESS_LICENSE - . \Invertus\SaferPay\Config\SaferPayConfig::getConfigSuffix() + SaferPayConfig::BUSINESS_LICENSE + . SaferPayConfig::getConfigSuffix() ); - /** @var \Invertus\SaferPay\Repository\SaferPayCardAliasRepository $cardAliasRep */ + /** @var SaferPayCardAliasRepository $cardAliasRep */ $cardAliasRep = $this->getService( - \Invertus\SaferPay\Repository\SaferPayCardAliasRepository::class + SaferPayCardAliasRepository::class ); $displayTpl = 'front/payment.tpl'; - $isCardSaveEnabled = Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::CREDIT_CARD_SAVE); + $isCardSaveEnabled = Configuration::get(SaferPayConfig::CREDIT_CARD_SAVE); $currentDate = date('Y-m-d h:i:s'); if ($isBusinessLicenseEnabled && $isCreditCard && $isCardSaveEnabled) { @@ -515,11 +537,11 @@ public function hookDisplayPayment($params) $displayTpl = 'front/payment_with_cards.tpl'; } - /** @var \Invertus\SaferPay\Provider\PaymentRedirectionProvider $paymentRedirectionProvider */ - $paymentRedirectionProvider = $this->getService(\Invertus\SaferPay\Provider\PaymentRedirectionProvider::class); + /** @var PaymentRedirectionProvider $paymentRedirectionProvider */ + $paymentRedirectionProvider = $this->getService(PaymentRedirectionProvider::class); - /** @var \Invertus\SaferPay\Provider\PaymentTypeProvider $paymentTypeProvider */ - $paymentTypeProvider = $this->getService(\Invertus\SaferPay\Provider\PaymentTypeProvider::class); + /** @var PaymentTypeProvider $paymentTypeProvider */ + $paymentTypeProvider = $this->getService(PaymentTypeProvider::class); $this->smarty->assign( [ @@ -544,13 +566,13 @@ public function hookDisplayPayment($params) public function hookPaymentReturn() { - if (\Invertus\SaferPay\Config\SaferPayConfig::isVersion17()) { + if (SaferPayConfig::isVersion17()) { return; } - /** @var \Invertus\SaferPay\Builder\OrderConfirmationMessageTemplate $OrderConfirmationMessageTemplate */ + /** @var OrderConfirmationMessageTemplate $OrderConfirmationMessageTemplate */ $OrderConfirmationMessageTemplate = $this->getService( - \Invertus\SaferPay\Builder\OrderConfirmationMessageTemplate::class + OrderConfirmationMessageTemplate::class ); $OrderConfirmationMessageTemplate->setSmarty($this->context->smarty); @@ -581,7 +603,7 @@ public function hookActionEmailSendBefore($params) } $cart = new Cart($params['cart']->id); - /** @var \Order $order */ + /** @var Order $order */ $order = Order::getByCartId($cart->id); if (!$order) { @@ -592,15 +614,15 @@ public function hookActionEmailSendBefore($params) return true; } - /** @var \Invertus\SaferPay\Core\Order\Verification\CanSendOrderConfirmationEmail $canSendOrderConfirmationEmail */ - $canSendOrderConfirmationEmail = $this->getService(\Invertus\SaferPay\Core\Order\Verification\CanSendOrderConfirmationEmail::class); + /** @var CanSendOrderConfirmationEmail $canSendOrderConfirmationEmail */ + $canSendOrderConfirmationEmail = $this->getService(CanSendOrderConfirmationEmail::class); if ($params['template'] === 'order_conf') { return $canSendOrderConfirmationEmail->verify((int) $order->current_state); } if ($params['template'] === 'new_order') { - if ((int) Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_SEND_NEW_ORDER_MAIL)) { + if ((int) Configuration::get(SaferPayConfig::SAFERPAY_SEND_NEW_ORDER_MAIL)) { return true; } @@ -633,20 +655,20 @@ public function hookActionOrderHistoryAddAfter($params = []) return; } - /** @var \Invertus\SaferPay\Service\SaferPayMailService $mailService */ - $mailService = $this->getService(\Invertus\SaferPay\Service\SaferPayMailService::class); + /** @var SaferPayMailService $mailService */ + $mailService = $this->getService(SaferPayMailService::class); - /** @var \Invertus\SaferPay\Core\Order\Verification\CanSendOrderConfirmationEmail $canSendOrderConfirmationEmail */ - $canSendOrderConfirmationEmail = $this->getService(\Invertus\SaferPay\Core\Order\Verification\CanSendOrderConfirmationEmail::class); + /** @var CanSendOrderConfirmationEmail $canSendOrderConfirmationEmail */ + $canSendOrderConfirmationEmail = $this->getService(CanSendOrderConfirmationEmail::class); if ($canSendOrderConfirmationEmail->verify((int) $orderStatus->id)) { try { $mailService->sendNewOrderMail($order, (int) $orderStatus->id); - } catch (\Exception $e) { + } catch (Exception $e) { // emailalert module sometimes throws error which leads into failed payment issue } - if ((int) \Configuration::get(SaferPayConfig::SAFERPAY_PAYMENT_AUTHORIZED) === (int) $orderStatus->id) { + if ((int) Configuration::get(SaferPayConfig::SAFERPAY_PAYMENT_AUTHORIZED) === (int) $orderStatus->id) { $mailService->sendOrderConfMail($order, (int) $orderStatus->id); } } @@ -662,8 +684,8 @@ public function hookActionAdminControllerSetMedia() $orderId = Tools::getValue('id_order'); $order = new Order($orderId); - /** @var \Invertus\SaferPay\Repository\SaferPayOrderRepository $orderRepo */ - $orderRepo = $this->getService(\Invertus\SaferPay\Repository\SaferPayOrderRepository::class); + /** @var SaferPayOrderRepository $orderRepo */ + $orderRepo = $this->getService(SaferPayOrderRepository::class); $saferPayOrderId = $orderRepo->getIdByOrderId($orderId); $saferPayOrder = new SaferPayOrder($saferPayOrderId); @@ -708,19 +730,20 @@ private function displayInAdminOrderPage(array $params) { $orderId = $params['id_order']; $order = new Order($orderId); - $orderRepo = $this->getService(\Invertus\SaferPay\Repository\SaferPayOrderRepository::class); + /** @var SaferPayOrderRepository $orderRepo */ + $orderRepo = $this->getService(SaferPayOrderRepository::class); $saferPayOrderId = $orderRepo->getIdByOrderId($orderId); $saferPayOrder = new SaferPayOrder($saferPayOrderId); if ($order->module !== $this->name) { - return; + return ''; } - if (!$saferPayOrder->authorized) { - return; + if (!$saferPayOrder->authorized && !$saferPayOrder->captured) { + return ''; } - if (\Invertus\SaferPay\Config\SaferPayConfig::isVersionAbove177()) { + if (SaferPayConfig::isVersionAbove177()) { $action = $this->context->link->getAdminLink( self::ADMIN_ORDER_CONTROLLER, true, @@ -729,16 +752,15 @@ private function displayInAdminOrderPage(array $params) ); } else { $action = $this->context->link->getAdminLink( - self::ADMIN_ORDER_CONTROLLER - ) . '&id_order=' . (int) $orderId; + self::ADMIN_ORDER_CONTROLLER + ) . '&id_order=' . (int) $orderId; } - $assertId = $orderRepo->getAssertIdBySaferPayOrderId($saferPayOrderId); $assertData = new SaferPayAssert($assertId); - $assertPresenter = new \Invertus\SaferPay\Presenter\AssertPresenter($this); + $assertPresenter = new AssertPresenter($this); $assertData = $assertPresenter->present($assertData); - $supported3DsPaymentMethods = \Invertus\SaferPay\Config\SaferPayConfig::SUPPORTED_3DS_PAYMENT_METHODS; + $supported3DsPaymentMethods = SaferPayConfig::SUPPORTED_3DS_PAYMENT_METHODS; // Note: This condition check or Payment method supports 3DS. // If payment method does not supports 3DS , when we change 'liability_shift' @@ -751,11 +773,11 @@ private function displayInAdminOrderPage(array $params) $this->context->smarty->assign($assertData); $currency = new Currency($order->id_currency); - $adminOrderPagePresenter = new \Invertus\SaferPay\Presenter\AdminOrderPagePresenter(); + $adminOrderPagePresenter = new AdminOrderPagePresenter(); $orderPageData = $adminOrderPagePresenter->present( $saferPayOrder, $action, - \Invertus\SaferPay\Config\SaferPayConfig::AMOUNT_MULTIPLIER_FOR_API, + SaferPayConfig::AMOUNT_MULTIPLIER_FOR_API, $currency->sign ); @@ -768,7 +790,7 @@ private function displayInAdminOrderPage(array $params) public function addFlash($msg, $type) { - if (\Invertus\SaferPay\Config\SaferPayConfig::isVersionAbove177()) { + if (SaferPayConfig::isVersionAbove177()) { return $this->get('session')->getFlashBag()->add($type, $msg); } diff --git a/src/Config/SaferPayConfig.php b/src/Config/SaferPayConfig.php index 37f6393d..5685688b 100755 --- a/src/Config/SaferPayConfig.php +++ b/src/Config/SaferPayConfig.php @@ -248,11 +248,6 @@ class SaferPayConfig const CREDIT_CARD_OPTION_SAVE = 0; const CREDIT_CARD_DONT_OPTION_SAVE = -1; - const TRANSACTION_STATUS_AUTHORIZED = 'AUTHORIZED'; - const TRANSACTION_STATUS_CAPTURED = 'CAPTURED'; - const TRANSACTION_STATUS_PENDING = 'PENDING'; - const TRANSACTION_STATUS_CANCELED = 'CANCELED'; - const FIELDS_ACCESS_TOKEN = 'SAFERPAY_FIELDS_ACCESS_TOKEN'; const FIELDS_LIBRARY = 'SAFERPAY_FIELDS_JAVASCRIPT_LIBRARY'; const FIELDS_LIBRARY_DEFAULT_VALUE = 'https://www.saferpay.com/Fields/lib/1/saferpay-fields.js'; @@ -268,7 +263,7 @@ class SaferPayConfig const PAYMENT_BEHAVIOR_WITHOUT_3D_CANCEL = 0; const PAYMENT_BEHAVIOR_WITHOUT_3D_AUTHORIZE = 1; - public static function supportsOrderCapture(string $paymentMethod): bool + public static function supportsOrderCapture(string $paymentMethod) { //payments that DOES NOT SUPPORT capture $unsupportedCapturePayments = [ @@ -279,7 +274,7 @@ public static function supportsOrderCapture(string $paymentMethod): bool return !in_array($paymentMethod, $unsupportedCapturePayments); } - public static function supportsOrderCancel(string $paymentMethod): bool + public static function supportsOrderCancel(string $paymentMethod) { //payments that DOES NOT SUPPORT order cancel $unsupportedCancelPayments = [ diff --git a/src/Core/Order/Action/UpdateOrderStatusAction.php b/src/Core/Order/Action/UpdateOrderStatusAction.php index 316bbd66..000ce93b 100644 --- a/src/Core/Order/Action/UpdateOrderStatusAction.php +++ b/src/Core/Order/Action/UpdateOrderStatusAction.php @@ -45,7 +45,7 @@ public function run($orderId, $orderStatusId) /** @var \Order|null $order */ $order = new Order($orderId); } catch (\Exception $exception) { - throw CouldNotChangeOrderStatus::unknownError($exception); + throw CouldNotChangeOrderStatus::unknownError(); } if (!$order) { @@ -58,7 +58,7 @@ public function run($orderId, $orderStatusId) $order->update(); } } catch (\Exception $exception) { - throw CouldNotChangeOrderStatus::unknownError($exception); + throw CouldNotChangeOrderStatus::unknownError(); } } diff --git a/src/Processor/CheckoutProcessor.php b/src/Processor/CheckoutProcessor.php index 24c31544..e7880663 100644 --- a/src/Processor/CheckoutProcessor.php +++ b/src/Processor/CheckoutProcessor.php @@ -28,6 +28,7 @@ } use Cart; +use Invertus\SaferPay\Api\Enum\TransactionStatus; use Invertus\SaferPay\Config\SaferPayConfig; use Invertus\SaferPay\Core\Payment\DTO\CheckoutData; use Invertus\SaferPay\EntityBuilder\SaferPayOrderBuilder; @@ -37,7 +38,6 @@ use Invertus\SaferPay\Repository\SaferPayOrderRepository; use Invertus\SaferPay\Service\SaferPayInitialize; use Order; -use PrestaShop\PrestaShop\Adapter\Entity\PrestaShopLogger; use PrestaShopException; use SaferPayOrder; @@ -68,24 +68,19 @@ public function __construct( } public function run(CheckoutData $data) { - $cart = new Cart($data->getCartId()); if (!$cart) { throw CouldNotProcessCheckout::failedToFindCart($data->getCartId()); } - if ($data->getIsAuthorizedOrder()) { - $this->processAuthorizedOrder($data, $cart); - return ''; + if (!$data->getCreateAfterAuthorization()) { + $this->processCreateOrder($cart, $data->getPaymentMethod()); } - try { - if (!$data->getCreateAfterAuthorization()) { - $this->processCreateOrder($cart, $data->getPaymentMethod()); - } - } catch (\Exception $exception) { - throw CouldNotProcessCheckout::failedToCreateOrder($data->getCartId()); + if ($data->getOrderStatus() === TransactionStatus::AUTHORIZED || $data->getOrderStatus() === TransactionStatus::CAPTURED) { + $this->processAuthorizedOrder($data, $cart); + return ''; } try { @@ -122,13 +117,13 @@ public function run(CheckoutData $data) { */ private function processCreateOrder(Cart $cart, $paymentMethod) { - $customer = new \Customer($cart->id_customer); - // Notify and return webhooks triggers together leading into order created previously if ($cart->orderExists()) { return; } + $customer = new \Customer($cart->id_customer); + $this->module->validateOrder( $cart->id, \Configuration::get(SaferPayConfig::SAFERPAY_ORDER_STATE_CHOICE_AWAITING_PAYMENT), @@ -189,36 +184,36 @@ private function processAuthorizedOrder(CheckoutData $data, Cart $cart) { try { $saferPayOrder = new SaferPayOrder($this->saferPayOrderRepository->getIdByCartId($cart->id)); - - if (!$saferPayOrder->id_order) { - $this->processCreateOrder($cart, $data->getPaymentMethod()); - } - - if (method_exists('Order', 'getIdByCartId')) { - $orderId = Order::getIdByCartId($cart->id); - $order = new Order($orderId); - } else { - // For PrestaShop 1.6 use the alternative method - $orderId = Order::getOrderByCartId($cart->id); - $order = new Order($orderId); - } + $this->processCreateOrder($cart, $data->getPaymentMethod()); + $order = $this->getOrder($cart->id); $saferPayOrder->id_order = $order->id; - if ($data->getOrderStatus() === 'AUTHORIZED') { + if ($data->getOrderStatus() === TransactionStatus::AUTHORIZED) { $order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZED_); $saferPayOrder->authorized = 1; - } elseif ($data->getOrderStatus() === 'CAPTURED') { + } elseif ($data->getOrderStatus() === TransactionStatus::CAPTURED) { $order->setCurrentState(_SAFERPAY_PAYMENT_COMPLETED_); $saferPayOrder->captured = 1; - } elseif ($data->getOrderStatus() === 'PENDING') { - $order->setCurrentState(_SAFERPAY_PAYMENT_PENDING_); - $saferPayOrder->pending = 1; } $saferPayOrder->update(); - return; } catch (\Exception $exception) { throw CouldNotProcessCheckout::failedToCreateOrder($data->getCartId()); } } + + /** + * @param int $cartId + * + * @return Order + */ + private function getOrder($cartId) + { + if (method_exists('Order', 'getIdByCartId')) { + return new Order(Order::getIdByCartId($cartId)); + } else { + // For PrestaShop 1.6 use the alternative method + return new Order(Order::getOrderByCartId($cartId)); + } + } } diff --git a/src/Repository/SaferPayOrderRepository.php b/src/Repository/SaferPayOrderRepository.php index b9d262e8..d6b25626 100755 --- a/src/Repository/SaferPayOrderRepository.php +++ b/src/Repository/SaferPayOrderRepository.php @@ -69,6 +69,7 @@ public function getAssertIdBySaferPayOrderId($saferPayOrderId) $query->select('`id_saferpay_assert`'); $query->from('saferpay_assert'); $query->where('id_saferPay_order = "' . (int) $saferPayOrderId . '"'); + $query->orderBy('id_saferpay_assert DESC'); return Db::getInstance()->getValue($query); } diff --git a/src/Service/SaferPayInitialize.php b/src/Service/SaferPayInitialize.php index 1c8186b2..e1cb6300 100755 --- a/src/Service/SaferPayInitialize.php +++ b/src/Service/SaferPayInitialize.php @@ -106,7 +106,6 @@ public function buildRequest( ) { $customerEmail = $this->context->customer->email; $cartId = $this->context->cart->id; - $creationAfterInitialization = $this->configuration->getAsBoolean(SaferPayConfig::SAFERPAY_ORDER_CREATION_AFTER_AUTHORIZATION); $alias = $this->saferPayCardAliasRepository->getSavedCardAliasFromId($selectedCard); $returnUrl = $this->context->link->getModuleLink( @@ -115,7 +114,6 @@ public function buildRequest( [ 'cartId' => $cartId, 'secureKey' => $this->context->cart->secure_key, - 'orderId' => $creationAfterInitialization ? 0 : Order::getOrderByCartId($cartId), 'moduleId' => $this->module->id, 'selectedCard' => $selectedCard, 'isBusinessLicence' => $isBusinessLicence, @@ -130,7 +128,6 @@ public function buildRequest( [ 'success' => 1, 'cartId' => $this->context->cart->id, - 'orderId' => $creationAfterInitialization ? 0 : Order::getOrderByCartId($cartId), 'secureKey' => $this->context->cart->secure_key, ], true diff --git a/src/Service/SaferPayOrderStatusService.php b/src/Service/SaferPayOrderStatusService.php index b1800f83..7a995f98 100755 --- a/src/Service/SaferPayOrderStatusService.php +++ b/src/Service/SaferPayOrderStatusService.php @@ -27,6 +27,7 @@ use Customer; use Exception; use Invertus\SaferPay\Adapter\LegacyContext; +use Invertus\SaferPay\Api\Enum\TransactionStatus; use Invertus\SaferPay\Api\Request\CancelService; use Invertus\SaferPay\Api\Request\CaptureService; use Invertus\SaferPay\Api\Request\RefundService; @@ -111,7 +112,7 @@ public function __construct( $this->module = $module->getModule(); } - public function pending(Order $order) + public function setPending(Order $order) { $saferPayOrder = $this->orderRepository->getByOrderId($order->id); $saferPayOrder->pending = 1; @@ -120,22 +121,6 @@ public function pending(Order $order) $order->setCurrentState(_SAFERPAY_PAYMENT_PENDING_); } - /** - * @param Order $order - * - * @throws \Exception - */ - public function authorize(Order $order) - { - $saferPayOrderId = $this->orderRepository->getIdByOrderId($order->id); - $saferPayOrder = new SaferPayOrder($saferPayOrderId); - $saferPayOrder->authorized = 1; - $order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZED_); - - $saferPayOrder->update(); - $order->update(); - } - /** TODO extract capture api code to different service like Assert for readability */ public function capture(Order $order, $refundedAmount = 0, $isRefund = false) { @@ -248,11 +233,11 @@ public function refund(Order $order, $refundedAmount) $saferPayOrder->refund_id = $refundResponse->Transaction->Id; $saferPayOrder->update(); - if ($refundResponse->Transaction->Status === SaferPayConfig::TRANSACTION_STATUS_AUTHORIZED) { + if ($refundResponse->Transaction->Status === TransactionStatus::AUTHORIZED) { $this->capture($order, $refundAmount, true); } - if ($refundResponse->Transaction->Status === SaferPayConfig::TRANSACTION_STATUS_CAPTURED) { + if ($refundResponse->Transaction->Status === TransactionStatus::CAPTURED) { $saferPayAssert->refunded_amount += $refundAmount; $saferPayAssert->update(); if ((int) $saferPayAssert->refunded_amount === (int) $saferPayAssert->amount) { @@ -263,7 +248,7 @@ public function refund(Order $order, $refundedAmount) } } - if ($refundResponse->Transaction->Status === SaferPayConfig::TRANSACTION_STATUS_PENDING) { + if ($refundResponse->Transaction->Status === TransactionStatus::PENDING) { $saferPayAssert->pending_refund_amount += $refundAmount; $saferPayAssert->update(); $orderState = $order->getCurrentState();