diff --git a/controllers/front/ajax.php b/controllers/front/ajax.php index 270c768ce..f221d68ec 100755 --- a/controllers/front/ajax.php +++ b/controllers/front/ajax.php @@ -25,6 +25,7 @@ use Invertus\SaferPay\Controller\Front\CheckoutController; use Invertus\SaferPay\Core\Payment\DTO\CheckoutData; use Invertus\SaferPay\Enum\ControllerName; +use Invertus\SaferPay\Repository\SaferPayOrderRepository; if (!defined('_PS_VERSION_')) { exit; @@ -43,7 +44,81 @@ public function postProcess() case 'submitHostedFields': $this->submitHostedFields(); break; + case 'getStatus': + $this->processGetStatus(); + break; + } + } + + /** + * @throws PrestaShopDatabaseException + * @throws PrestaShopException + */ + protected function processGetStatus() + { + header('Content-Type: application/json;charset=UTF-8'); + /** @var SaferPayOrderRepository $saferPayOrderRepository */ + $saferPayOrderRepository = $this->module->getService(SaferPayOrderRepository::class); + $cartId = Tools::getValue('cartId'); + $secureKey = Tools::getValue('secureKey'); + $isBusinessLicence = (int) Tools::getValue(SaferPayConfig::IS_BUSINESS_LICENCE); + $fieldToken = Tools::getValue('fieldToken'); + $moduleId = $this->module->id; + $selectedCard = Tools::getValue('selectedCard'); + $saferPayOrderId = $saferPayOrderRepository->getIdByCartId($cartId); + $saferPayOrder = new SaferPayOrder($saferPayOrderId); + + if (!$saferPayOrder->id || $saferPayOrder->canceled) { + $this->ajaxDie(json_encode([ + 'isFinished' => true, + 'href' => $this->getFailControllerLink($cartId, $secureKey, $moduleId) + ])); + } + + $this->ajaxDie(json_encode([ + 'saferpayOrder' => json_encode($saferPayOrder), + 'isFinished' => $saferPayOrder->authorized || $saferPayOrder->captured || $saferPayOrder->pending, + 'href' => $this->context->link->getModuleLink( + $this->module->name, + $this->getSuccessControllerName($isBusinessLicence, $fieldToken), + [ + 'cartId' => $cartId, + 'orderId' => $saferPayOrder->id_order, + 'moduleId' => $moduleId, + 'secureKey' => $secureKey, + 'selectedCard' => $selectedCard, + ] + ) + ])); + } + + private function getFailControllerLink($cartId, $secureKey, $moduleId) + { + return $this->context->link->getModuleLink( + $this->module->name, + ControllerName::FAIL, + [ + 'cartId' => $cartId, + 'secureKey' => $secureKey, + 'moduleId' => $moduleId, + ], + true + ); + } + + private function getSuccessControllerName($isBusinessLicence, $fieldToken) + { + $successController = ControllerName::SUCCESS; + + if ($isBusinessLicence) { + $successController = ControllerName::SUCCESS_IFRAME; } + + if ($fieldToken) { + $successController = ControllerName::SUCCESS_HOSTED; + } + + return $successController; } private function submitHostedFields() diff --git a/controllers/front/notify.php b/controllers/front/notify.php index fa2e64422..d9fcaf5ad 100755 --- a/controllers/front/notify.php +++ b/controllers/front/notify.php @@ -66,8 +66,23 @@ public function postProcess() $secureKey )); - if (!$lockResult->isSuccessful()) { - die($this->module->l('Lock already exist', self::FILENAME)); + if (!SaferPayConfig::isVersion17()) { + if ($lockResult > 200) { + die($this->module->l('Lock already exists', self::FILENAME)); + } + } else { + if (!$lockResult->isSuccessful()) { + die($this->module->l('Lock already exists', self::FILENAME)); + } + } + + if ($cart->orderExists()) { + $order = new Order($this->getOrderId($cartId)); + $completed = (int) Configuration::get(SaferPayConfig::SAFERPAY_PAYMENT_COMPLETED); + + if ((int) $order->current_state === $completed) { + die($this->module->l('Order already complete', self::FILENAME)); + } } /** @var SaferPayOrderRepository $saferPayOrderRepository */ @@ -87,6 +102,7 @@ public function postProcess() $checkoutData->setOrderStatus($transactionStatus); $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. @@ -129,28 +145,40 @@ public function postProcess() $orderStatusService->capture($order); } } catch (Exception $e) { - $this->releaseLock(); // 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); + $order = new Order($this->getOrderId($cartId)); } - if ($order->id) { + $orderId = (int) $order->id; + + if ($orderId) { + $saferPayCapturedStatus = (int) Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_PAYMENT_COMPLETED); + + if ((int) $order->current_state === $saferPayCapturedStatus) { + die($this->module->l('Order already created', self::FILENAME)); + } + // 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)); } + // using cartId, because ps order might not be assigned yet + $saferPayOrder = new SaferPayOrder($saferPayOrderRepository->getIdByCartId($cartId)); + if ($saferPayOrder->id) { $saferPayOrder->authorized = false; $saferPayOrder->pending = false; $saferPayOrder->canceled = true; + + if ($orderId) { + // assign ps order to saferpay order id in case it was not assigned previously + $saferPayOrder->id_order = $orderId; + } + $saferPayOrder->update(); + $this->releaseLock(); + die('canceled'); } PrestaShopLogger::addLog( @@ -166,6 +194,7 @@ public function postProcess() true ); $this->releaseLock(); + die($this->module->l($e->getMessage(), self::FILENAME)); } diff --git a/controllers/front/return.php b/controllers/front/return.php index 7d2dd1959..1ce79fd00 100755 --- a/controllers/front/return.php +++ b/controllers/front/return.php @@ -27,7 +27,6 @@ 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; use Invertus\SaferPay\Service\TransactionFlow\SaferPayTransactionAuthorization; @@ -43,8 +42,6 @@ class SaferPayOfficialReturnModuleFrontController extends AbstractSaferPayContro public function postProcess() { $cartId = (int) Tools::getValue('cartId'); - $isBusinessLicence = (int) Tools::getValue(SaferPayConfig::IS_BUSINESS_LICENCE); - $selectedCard = (int) Tools::getValue('selectedCard'); $order = new Order($this->getOrderId($cartId)); if (!$order->id) { @@ -52,17 +49,13 @@ public function postProcess() } try { - if ($isBusinessLicence) { - $response = $this->executeTransaction($cartId, $selectedCard); - } else { - $response = $this->assertTransaction($cartId); - } - - \PrestaShopLogger::addLog($response->getTransaction()->getStatus()); + /** @var SaferPayTransactionAssertion $transactionAssert */ + $transactionAssert = $this->module->getService(SaferPayTransactionAssertion::class); + $transactionResponse = $transactionAssert->assert($cartId, false); /** @var SaferPayOrderStatusService $orderStatusService */ $orderStatusService = $this->module->getService(SaferPayOrderStatusService::class); - if ($response->getTransaction()->getStatus() === TransactionStatus::PENDING) { + if ($transactionResponse->getTransaction()->getStatus() === TransactionStatus::PENDING) { $orderStatusService->setPending($order); } } catch (SaferPayApiException $e) { @@ -75,13 +68,6 @@ public function postProcess() */ public function initContent() { - if (Tools::getValue('ajax')) { - $this->processAjax(); - exit; - } - - parent::initContent(); - $cartId = Tools::getValue('cartId'); $secureKey = Tools::getValue('secureKey'); $isBusinessLicence = (int) Tools::getValue(SaferPayConfig::IS_BUSINESS_LICENCE); @@ -136,7 +122,7 @@ public function initContent() 'checkStatusEndpoint', $this->context->link->getModuleLink( $this->module->name, - 'return', + 'ajax', [ 'ajax' => 1, 'action' => 'getStatus', @@ -155,62 +141,6 @@ public function initContent() $this->setTemplate('saferpay_wait_16.tpl'); } - protected function processAjax() - { - if (empty($this->context->customer->id)) { - return; - } - - switch (Tools::getValue('action')) { - case 'getStatus': - $this->processGetStatus(); - break; - } - - exit; - } - - /** - * @throws PrestaShopDatabaseException - * @throws PrestaShopException - */ - protected function processGetStatus() - { - header('Content-Type: application/json;charset=UTF-8'); - /** @var SaferPayOrderRepository $saferPayOrderRepository */ - $saferPayOrderRepository = $this->module->getService(SaferPayOrderRepository::class); - $cartId = Tools::getValue('cartId'); - $secureKey = Tools::getValue('secureKey'); - $isBusinessLicence = (int) Tools::getValue(SaferPayConfig::IS_BUSINESS_LICENCE); - $fieldToken = Tools::getValue('fieldToken'); - $moduleId = $this->module->id; - $selectedCard = Tools::getValue('selectedCard'); - $saferPayOrderId = $saferPayOrderRepository->getIdByCartId($cartId); - $saferPayOrder = new SaferPayOrder($saferPayOrderId); - - 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), - [ - 'cartId' => $cartId, - 'orderId' => $saferPayOrder->id_order, - 'moduleId' => $moduleId, - 'secureKey' => $secureKey, - 'selectedCard' => $selectedCard, - ] - ) - ])); - } - private function getSuccessControllerName($isBusinessLicence, $fieldToken) { $successController = ControllerName::SUCCESS; @@ -226,20 +156,6 @@ 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 @@ -259,13 +175,6 @@ private function executeTransaction($orderId, $selectedCard) ); } - private function assertTransaction($cartId) { - /** @var SaferPayTransactionAssertion $transactionAssert */ - $transactionAssert = $this->module->getService(SaferPayTransactionAssertion::class); - - return $transactionAssert->assert($cartId); - } - /** * @param int $cartId * diff --git a/saferpayofficial.php b/saferpayofficial.php index 31276a0be..cfd291d6c 100755 --- a/saferpayofficial.php +++ b/saferpayofficial.php @@ -21,30 +21,6 @@ *@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; } @@ -81,7 +57,7 @@ public function __construct($name = null) public function getContent() { - if (Configuration::get(SaferPayConfig::USERNAME)) { + if (Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::USERNAME)) { Tools::redirectAdmin($this->context->link->getAdminLink(self::ADMIN_PAYMENTS_CONTROLLER)); } Tools::redirectAdmin($this->context->link->getAdminLink(self::ADMIN_SETTINGS_CONTROLLER)); @@ -89,7 +65,7 @@ public function getContent() public function install() { - $installer = new Installer($this); + $installer = new \Invertus\SaferPay\Install\Installer($this); if (!parent::install()) { return false; @@ -104,7 +80,7 @@ public function install() public function uninstall() { - $uninstaller = new Uninstaller($this); + $uninstaller = new \Invertus\SaferPay\Install\Uninstaller($this); if (!$uninstaller->uninstall()) { $this->_errors += $uninstaller->getErrors(); return false; @@ -114,7 +90,7 @@ public function uninstall() public function getTabs() { - $installer = new Installer($this); + $installer = new \Invertus\SaferPay\Install\Installer($this); return $installer->tabs(); } @@ -133,7 +109,7 @@ private function loadConfig() } public function getService($service) { - $containerProvider = new LeagueServiceContainerProvider(); + $containerProvider = new \Invertus\SaferPay\ServiceProvider\LeagueServiceContainerProvider(); return $containerProvider->getService($service); } @@ -147,17 +123,17 @@ public function hookDisplayOrderConfirmation($params) /** @var Order $psOrder */ $psOrder = $params['order']; - /** @var SaferPayOrderRepository $repository */ - $repository = $this->getService(SaferPayOrderRepository::class); + /** @var \Invertus\SaferPay\Repository\SaferPayOrderRepository $repository */ + $repository = $this->getService(\Invertus\SaferPay\Repository\SaferPayOrderRepository::class); + $sfOrder = $repository->getByOrderId((int) $psOrder->id); if (!$sfOrder->pending) { return ''; } - //@todo: translate and move to template if needed when requirements are clear - return 'Your payment is still being processed by your bank. This can take up to 5 days (120 hours). Once we receive the final status, we will notify you immediately. -Thank you for your patience!'; + return $this->l('Your payment is still being processed by your bank. This can take up to 5 days (120 hours). Once we receive the final status, we will notify you immediately. +Thank you for your patience!'); } public function hookActionObjectOrderPaymentAddAfter($params) @@ -173,8 +149,8 @@ public function hookActionObjectOrderPaymentAddAfter($params) return; } - /** @var SaferPayOrderRepository $saferPayOrderRepository */ - $saferPayOrderRepository = $this->getService(SaferPayOrderRepository::class); + /** @var \Invertus\SaferPay\Repository\SaferPayOrderRepository $saferPayOrderRepository */ + $saferPayOrderRepository = $this->getService(\Invertus\SaferPay\Repository\SaferPayOrderRepository::class); $orders = Order::getByReference($orderPayment->order_reference); @@ -204,32 +180,33 @@ public function hookActionObjectOrderPaymentAddAfter($params) public function hookPaymentOptions($params) { /** @var Invertus\SaferPay\Service\SaferPayCartService $assertService */ - $cartService = $this->getService(SaferPayCartService::class); + $cartService = $this->getService(\Invertus\SaferPay\Service\SaferPayCartService::class); if (!$cartService->isCurrencyAvailable($params['cart'])) { return; } - /** @var PaymentTypeProvider $paymentTypeProvider */ - $paymentTypeProvider = $this->getService(PaymentTypeProvider::class); + /** @var \Invertus\SaferPay\Provider\PaymentTypeProvider $paymentTypeProvider */ + $paymentTypeProvider = $this->getService(\Invertus\SaferPay\Provider\PaymentTypeProvider::class); - /** @var SaferPayObtainPaymentMethods $obtainPaymentMethods */ - $obtainPaymentMethods = $this->getService(SaferPayObtainPaymentMethods::class); - /** @var SaferPayPaymentRepository $paymentRepository */ - $paymentRepository = $this->getService(SaferPayPaymentRepository::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); try { $paymentMethods = $obtainPaymentMethods->obtainPaymentMethods(); - } catch (SaferPayApiException $exception) { + } catch (\Invertus\SaferPay\Exception\Api\SaferPayApiException $exception) { return []; } $paymentOptions = []; - /** @var PaymentRestrictionValidation $paymentRestrictionValidation */ + /** @var \Invertus\SaferPay\Service\PaymentRestrictionValidation $paymentRestrictionValidation */ $paymentRestrictionValidation = $this->getService( - PaymentRestrictionValidation::class + \Invertus\SaferPay\Service\PaymentRestrictionValidation::class ); + foreach ($paymentMethods as $paymentMethod) { $paymentMethod['paymentMethod'] = str_replace(' ', '', $paymentMethod['paymentMethod']); @@ -242,20 +219,20 @@ public function hookPaymentOptions($params) $isCreditCard = in_array( $paymentMethod['paymentMethod'], - SaferPayConfig::TRANSACTION_METHODS + \Invertus\SaferPay\Config\SaferPayConfig::TRANSACTION_METHODS ); $isBusinessLicenseEnabled = Configuration::get( - SaferPayConfig::BUSINESS_LICENSE - . SaferPayConfig::getConfigSuffix() + \Invertus\SaferPay\Config\SaferPayConfig::BUSINESS_LICENSE + . \Invertus\SaferPay\Config\SaferPayConfig::getConfigSuffix() ); - /** @var SaferPayCardAliasRepository $cardAliasRep */ + /** @var \Invertus\SaferPay\Repository\SaferPayCardAliasRepository $cardAliasRep */ $cardAliasRep = $this->getService( - SaferPayCardAliasRepository::class + \Invertus\SaferPay\Repository\SaferPayCardAliasRepository::class ); $isCreditCardSavingEnabled = Configuration::get( - SaferPayConfig::CREDIT_CARD_SAVE + \Invertus\SaferPay\Config\SaferPayConfig::CREDIT_CARD_SAVE ); $selectedCard = 0; if ($this->context->customer->is_guest) { @@ -263,15 +240,15 @@ public function hookPaymentOptions($params) $selectedCard = -1; } - /** @var PaymentRedirectionProvider $paymentRedirectionProvider */ - $paymentRedirectionProvider = $this->getService(PaymentRedirectionProvider::class); + /** @var \Invertus\SaferPay\Provider\PaymentRedirectionProvider $paymentRedirectionProvider */ + $paymentRedirectionProvider = $this->getService(\Invertus\SaferPay\Provider\PaymentRedirectionProvider::class); - $newOption = new PaymentOption(); + $newOption = new \PrestaShop\PrestaShop\Core\Payment\PaymentOption(); $translator = $this->getService( - LegacyTranslator::class + \Invertus\SaferPay\Service\LegacyTranslator::class ); - /** @var SaferPayPaymentNotation $saferPayPaymentNotation */ - $saferPayPaymentNotation = $this->getService(SaferPayPaymentNotation::class); + /** @var \Invertus\SaferPay\Service\SaferPayPaymentNotation $saferPayPaymentNotation */ + $saferPayPaymentNotation = $this->getService(\Invertus\SaferPay\Service\SaferPayPaymentNotation::class); $paymentMethodName = $saferPayPaymentNotation->getForDisplay($paymentMethod['paymentMethod']); $inputs = [ @@ -286,7 +263,7 @@ public function hookPaymentOptions($params) 'value' => $selectedCard, ], ]; - + if ($isCreditCardSavingEnabled && $isCreditCard && $isBusinessLicenseEnabled) { $currentDate = date('Y-m-d h:i:s'); @@ -338,7 +315,7 @@ public function hookPaymentOptions($params) public function hookDisplayAdminOrderTabContent(array $params) { - $isVersionAbove177 = SaferPayConfig::isVersionAbove177(); + $isVersionAbove177 = \Invertus\SaferPay\Config\SaferPayConfig::isVersionAbove177(); return !$isVersionAbove177 ? false : $this->displayInAdminOrderPage($params); } @@ -346,20 +323,20 @@ public function hookDisplayAdminOrderTabContent(array $params) public function hookDisplayAdminOrder(array $params) { - $isVersionAbove177 = SaferPayConfig::isVersionAbove177(); + $isVersionAbove177 = \Invertus\SaferPay\Config\SaferPayConfig::isVersionAbove177(); return $isVersionAbove177 ? false : $this->displayInAdminOrderPage($params); } public function hookActionFrontControllerSetMedia() { - /** @var PaymentFormAssetLoader $paymentFormAssetsLoader */ - $paymentFormAssetsLoader = $this->getService(PaymentFormAssetLoader::class); + /** @var \Invertus\SaferPay\Presentation\Loader\PaymentFormAssetLoader $paymentFormAssetsLoader */ + $paymentFormAssetsLoader = $this->getService(\Invertus\SaferPay\Presentation\Loader\PaymentFormAssetLoader::class); $paymentFormAssetsLoader->register($this->context->controller); if ($this->context->controller instanceof OrderController) { - if (SaferPayConfig::isVersion17()) { + if (\Invertus\SaferPay\Config\SaferPayConfig::isVersion17()) { $this->context->controller->registerJavascript( 'saved-card', 'modules/' . $this->name . '/views/js/front/saferpay_saved_card.js' @@ -369,13 +346,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 = SaferPayConfig::FIELDS_LIBRARY; - $configSuffix = SaferPayConfig::getConfigSuffix(); + $fieldsLibrary = \Invertus\SaferPay\Config\SaferPayConfig::FIELDS_LIBRARY; + $configSuffix = \Invertus\SaferPay\Config\SaferPayConfig::getConfigSuffix(); $this->context->controller->addJs(Configuration::get($fieldsLibrary . $configSuffix)); } - /** @var SaferPayErrorDisplayService $errorDisplayService */ - $errorDisplayService = $this->getService(SaferPayErrorDisplayService::class); + /** @var \Invertus\SaferPay\Service\SaferPayErrorDisplayService $errorDisplayService */ + $errorDisplayService = $this->getService(\Invertus\SaferPay\Service\SaferPayErrorDisplayService::class); $errorDisplayService->showCookieError('saferpay_payment_canceled_error'); } } @@ -383,11 +360,11 @@ public function hookActionFrontControllerSetMedia() public function hookDisplayCustomerAccount() { $isCreditCardSaveEnabled = - Configuration::get(SaferPayConfig::CREDIT_CARD_SAVE); + Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::CREDIT_CARD_SAVE); if (!$isCreditCardSaveEnabled) { return; } - if (SaferPayConfig::isVersion17()) { + if (\Invertus\SaferPay\Config\SaferPayConfig::isVersion17()) { return $this->context->smarty->fetch( $this->getLocalPath() . 'views/templates/hook/front/MyAccount.tpl' ); @@ -400,7 +377,7 @@ public function hookDisplayCustomerAccount() public function displayNavigationTop() { - if (SaferPayConfig::isVersion17()) { + if (\Invertus\SaferPay\Config\SaferPayConfig::isVersion17()) { return; } @@ -465,27 +442,27 @@ public function hookDisplayPayment($params) return; } - /** @var SaferPayCartService $assertService */ - $cartService = $this->getService(SaferPayCartService::class); + /** @var \Invertus\SaferPay\Service\SaferPayCartService $assertService */ + $cartService = $this->getService(\Invertus\SaferPay\Service\SaferPayCartService::class); if (!$cartService->isCurrencyAvailable($params['cart'])) { return; } - /** @var PaymentRestrictionValidation $paymentRestrictionValidation */ - $paymentRepository = $this->getService(SaferPayPaymentRepository::class); + /** @var \Invertus\SaferPay\Service\PaymentRestrictionValidation $paymentRestrictionValidation */ + $paymentRepository = $this->getService(\Invertus\SaferPay\Repository\SaferPayPaymentRepository::class); - /** @var SaferPayObtainPaymentMethods $obtainPaymentMethods */ - $obtainPaymentMethods = $this->getService(SaferPayObtainPaymentMethods::class); + /** @var \Invertus\SaferPay\Service\SaferPayObtainPaymentMethods $obtainPaymentMethods */ + $obtainPaymentMethods = $this->getService(\Invertus\SaferPay\Service\SaferPayObtainPaymentMethods::class); try { $paymentMethods = $obtainPaymentMethods->obtainPaymentMethods(); - } catch (SaferPayApiException $exception) { + } catch (\Invertus\SaferPay\Exception\Api\SaferPayApiException $exception) { return ''; } $paymentOptions = []; $paymentRestrictionValidation = $this->getService( - PaymentRestrictionValidation::class + \Invertus\SaferPay\Service\PaymentRestrictionValidation::class ); foreach ($paymentMethods as $paymentMethod) { @@ -497,20 +474,20 @@ public function hookDisplayPayment($params) $imageUrl = ($paymentRepository->isLogoEnabledByName($paymentMethod['paymentMethod'])) ? $paymentMethod['logoUrl'] : ''; - $isCreditCard = in_array($paymentMethod['paymentMethod'], SaferPayConfig::TRANSACTION_METHODS); + $isCreditCard = in_array($paymentMethod['paymentMethod'], \Invertus\SaferPay\Config\SaferPayConfig::TRANSACTION_METHODS); $isBusinessLicenseEnabled = Configuration::get( - SaferPayConfig::BUSINESS_LICENSE - . SaferPayConfig::getConfigSuffix() + \Invertus\SaferPay\Config\SaferPayConfig::BUSINESS_LICENSE + . \Invertus\SaferPay\Config\SaferPayConfig::getConfigSuffix() ); - /** @var SaferPayCardAliasRepository $cardAliasRep */ + /** @var \Invertus\SaferPay\Repository\SaferPayCardAliasRepository $cardAliasRep */ $cardAliasRep = $this->getService( - SaferPayCardAliasRepository::class + \Invertus\SaferPay\Repository\SaferPayCardAliasRepository::class ); $displayTpl = 'front/payment.tpl'; - $isCardSaveEnabled = Configuration::get(SaferPayConfig::CREDIT_CARD_SAVE); + $isCardSaveEnabled = Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::CREDIT_CARD_SAVE); $currentDate = date('Y-m-d h:i:s'); if ($isBusinessLicenseEnabled && $isCreditCard && $isCardSaveEnabled) { @@ -537,11 +514,11 @@ public function hookDisplayPayment($params) $displayTpl = 'front/payment_with_cards.tpl'; } - /** @var PaymentRedirectionProvider $paymentRedirectionProvider */ - $paymentRedirectionProvider = $this->getService(PaymentRedirectionProvider::class); + /** @var \Invertus\SaferPay\Provider\PaymentRedirectionProvider $paymentRedirectionProvider */ + $paymentRedirectionProvider = $this->getService(\Invertus\SaferPay\Provider\PaymentRedirectionProvider::class); - /** @var PaymentTypeProvider $paymentTypeProvider */ - $paymentTypeProvider = $this->getService(PaymentTypeProvider::class); + /** @var \Invertus\SaferPay\Provider\PaymentTypeProvider $paymentTypeProvider */ + $paymentTypeProvider = $this->getService(\Invertus\SaferPay\Provider\PaymentTypeProvider::class); $this->smarty->assign( [ @@ -566,13 +543,13 @@ public function hookDisplayPayment($params) public function hookPaymentReturn() { - if (SaferPayConfig::isVersion17()) { + if (\Invertus\SaferPay\Config\SaferPayConfig::isVersion17()) { return; } - /** @var OrderConfirmationMessageTemplate $OrderConfirmationMessageTemplate */ + /** @var \Invertus\SaferPay\Builder\OrderConfirmationMessageTemplate $OrderConfirmationMessageTemplate */ $OrderConfirmationMessageTemplate = $this->getService( - OrderConfirmationMessageTemplate::class + \Invertus\SaferPay\Builder\OrderConfirmationMessageTemplate::class ); $OrderConfirmationMessageTemplate->setSmarty($this->context->smarty); @@ -603,7 +580,7 @@ public function hookActionEmailSendBefore($params) } $cart = new Cart($params['cart']->id); - /** @var Order $order */ + /** @var \Order $order */ $order = Order::getByCartId($cart->id); if (!$order) { @@ -614,15 +591,15 @@ public function hookActionEmailSendBefore($params) return true; } - /** @var CanSendOrderConfirmationEmail $canSendOrderConfirmationEmail */ - $canSendOrderConfirmationEmail = $this->getService(CanSendOrderConfirmationEmail::class); + /** @var \Invertus\SaferPay\Core\Order\Verification\CanSendOrderConfirmationEmail $canSendOrderConfirmationEmail */ + $canSendOrderConfirmationEmail = $this->getService(\Invertus\SaferPay\Core\Order\Verification\CanSendOrderConfirmationEmail::class); if ($params['template'] === 'order_conf') { return $canSendOrderConfirmationEmail->verify((int) $order->current_state); } if ($params['template'] === 'new_order') { - if ((int) Configuration::get(SaferPayConfig::SAFERPAY_SEND_NEW_ORDER_MAIL)) { + if ((int) Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_SEND_NEW_ORDER_MAIL)) { return true; } @@ -655,11 +632,11 @@ public function hookActionOrderHistoryAddAfter($params = []) return; } - /** @var SaferPayMailService $mailService */ - $mailService = $this->getService(SaferPayMailService::class); + /** @var \Invertus\SaferPay\Service\SaferPayMailService $mailService */ + $mailService = $this->getService(\Invertus\SaferPay\Service\SaferPayMailService::class); - /** @var CanSendOrderConfirmationEmail $canSendOrderConfirmationEmail */ - $canSendOrderConfirmationEmail = $this->getService(CanSendOrderConfirmationEmail::class); + /** @var \Invertus\SaferPay\Core\Order\Verification\CanSendOrderConfirmationEmail $canSendOrderConfirmationEmail */ + $canSendOrderConfirmationEmail = $this->getService(\Invertus\SaferPay\Core\Order\Verification\CanSendOrderConfirmationEmail::class); if ($canSendOrderConfirmationEmail->verify((int) $orderStatus->id)) { try { @@ -668,8 +645,8 @@ public function hookActionOrderHistoryAddAfter($params = []) // emailalert module sometimes throws error which leads into failed payment issue } - if ((int) Configuration::get(SaferPayConfig::SAFERPAY_PAYMENT_AUTHORIZED) === (int) $orderStatus->id) { - $mailService->sendOrderConfMail($order, (int) $orderStatus->id); + if ((int) \Configuration::get(\Invertus\SaferPay\Config\SaferPayConfig::SAFERPAY_PAYMENT_AUTHORIZED) === (int) $orderStatus->id) { + $mailService->sendOrderConfMail($order, (int) $orderStatus->id); } } } @@ -684,8 +661,9 @@ public function hookActionAdminControllerSetMedia() $orderId = Tools::getValue('id_order'); $order = new Order($orderId); - /** @var SaferPayOrderRepository $orderRepo */ - $orderRepo = $this->getService(SaferPayOrderRepository::class); + /** @var \Invertus\SaferPay\Repository\SaferPayOrderRepository $orderRepo */ + $orderRepo = $this->getService(\Invertus\SaferPay\Repository\SaferPayOrderRepository::class); + $saferPayOrderId = $orderRepo->getIdByOrderId($orderId); $saferPayOrder = new SaferPayOrder($saferPayOrderId); @@ -731,7 +709,7 @@ private function displayInAdminOrderPage(array $params) $orderId = $params['id_order']; $order = new Order($orderId); /** @var SaferPayOrderRepository $orderRepo */ - $orderRepo = $this->getService(SaferPayOrderRepository::class); + $orderRepo = $this->getService(\Invertus\SaferPay\Repository\SaferPayOrderRepository::class); $saferPayOrderId = $orderRepo->getIdByOrderId($orderId); $saferPayOrder = new SaferPayOrder($saferPayOrderId); @@ -743,7 +721,7 @@ private function displayInAdminOrderPage(array $params) return ''; } - if (SaferPayConfig::isVersionAbove177()) { + if (\Invertus\SaferPay\Config\SaferPayConfig::isVersionAbove177()) { $action = $this->context->link->getAdminLink( self::ADMIN_ORDER_CONTROLLER, true, @@ -758,9 +736,9 @@ private function displayInAdminOrderPage(array $params) $assertId = $orderRepo->getAssertIdBySaferPayOrderId($saferPayOrderId); $assertData = new SaferPayAssert($assertId); - $assertPresenter = new AssertPresenter($this); + $assertPresenter = new \Invertus\SaferPay\Presenter\AssertPresenter($this); $assertData = $assertPresenter->present($assertData); - $supported3DsPaymentMethods = SaferPayConfig::SUPPORTED_3DS_PAYMENT_METHODS; + $supported3DsPaymentMethods = \Invertus\SaferPay\Config\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' @@ -773,11 +751,11 @@ private function displayInAdminOrderPage(array $params) $this->context->smarty->assign($assertData); $currency = new Currency($order->id_currency); - $adminOrderPagePresenter = new AdminOrderPagePresenter(); + $adminOrderPagePresenter = new \Invertus\SaferPay\Presenter\AdminOrderPagePresenter(); $orderPageData = $adminOrderPagePresenter->present( $saferPayOrder, $action, - SaferPayConfig::AMOUNT_MULTIPLIER_FOR_API, + \Invertus\SaferPay\Config\SaferPayConfig::AMOUNT_MULTIPLIER_FOR_API, $currency->sign ); @@ -790,7 +768,7 @@ private function displayInAdminOrderPage(array $params) public function addFlash($msg, $type) { - if (SaferPayConfig::isVersionAbove177()) { + if (\Invertus\SaferPay\Config\SaferPayConfig::isVersionAbove177()) { return $this->get('session')->getFlashBag()->add($type, $msg); } diff --git a/src/Controller/AbstractSaferPayController.php b/src/Controller/AbstractSaferPayController.php index d62b5e465..cac8b4a27 100755 --- a/src/Controller/AbstractSaferPayController.php +++ b/src/Controller/AbstractSaferPayController.php @@ -82,6 +82,10 @@ protected function applyLock($resource) $this->lock->create($resource); if (!$this->lock->acquire()) { + + if (!SaferPayConfig::isVersion17()) { + return http_response_code(409); + } return Response::respond( $this->module->l('Resource conflict', self::FILE_NAME), Response::HTTP_CONFLICT @@ -93,12 +97,20 @@ protected function applyLock($resource) $logger->payload = $resource; $logger->save(); + if (!SaferPayConfig::isVersion17()) { + return http_response_code(500); + } + return Response::respond( $this->module->l('Internal error', self::FILE_NAME), Response::HTTP_INTERNAL_SERVER_ERROR ); } + if (!SaferPayConfig::isVersion17()) { + return http_response_code(200); + } + return Response::respond( '', Response::HTTP_OK diff --git a/src/Install/Installer.php b/src/Install/Installer.php index 81e21ca85..d005c53be 100755 --- a/src/Install/Installer.php +++ b/src/Install/Installer.php @@ -241,7 +241,8 @@ private function installSaferPayOrderTable() `captured` tinyint(1) DEFAULT 0, `refunded` tinyint(1) DEFAULT 0, `canceled` tinyint(1) DEFAULT 0, - `authorized` tinyint(1) DEFAULT 0 + `authorized` tinyint(1) DEFAULT 0, + `pending` tinyint(1) DEFAULT 0 ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci' ); } diff --git a/src/Processor/CheckoutProcessor.php b/src/Processor/CheckoutProcessor.php index e7880663d..41826f577 100644 --- a/src/Processor/CheckoutProcessor.php +++ b/src/Processor/CheckoutProcessor.php @@ -40,6 +40,7 @@ use Order; use PrestaShopException; use SaferPayOrder; +use Validate; class CheckoutProcessor { @@ -78,7 +79,12 @@ public function run(CheckoutData $data) { $this->processCreateOrder($cart, $data->getPaymentMethod()); } - if ($data->getOrderStatus() === TransactionStatus::AUTHORIZED || $data->getOrderStatus() === TransactionStatus::CAPTURED) { + $authorizedStates = [ + TransactionStatus::AUTHORIZED, + TransactionStatus::CAPTURED, + ]; + + if (in_array($data->getOrderStatus(), $authorizedStates)) { $this->processAuthorizedOrder($data, $cart); return ''; } @@ -183,19 +189,19 @@ private function processCreateSaferPayOrder($initializeBody, $cartId, $customerI private function processAuthorizedOrder(CheckoutData $data, Cart $cart) { try { - $saferPayOrder = new SaferPayOrder($this->saferPayOrderRepository->getIdByCartId($cart->id)); $this->processCreateOrder($cart, $data->getPaymentMethod()); $order = $this->getOrder($cart->id); + $saferPayOrder = new SaferPayOrder($this->saferPayOrderRepository->getIdByCartId($cart->id)); - $saferPayOrder->id_order = $order->id; if ($data->getOrderStatus() === TransactionStatus::AUTHORIZED) { + $saferPayOrder->authorized = true; $order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZED_); - $saferPayOrder->authorized = 1; - } elseif ($data->getOrderStatus() === TransactionStatus::CAPTURED) { + } else { + $saferPayOrder->captured = true; $order->setCurrentState(_SAFERPAY_PAYMENT_COMPLETED_); - $saferPayOrder->captured = 1; } + $saferPayOrder->id_order = $order->id; $saferPayOrder->update(); } catch (\Exception $exception) { throw CouldNotProcessCheckout::failedToCreateOrder($data->getCartId()); diff --git a/src/Service/SaferPayOrderStatusService.php b/src/Service/SaferPayOrderStatusService.php index 38dd60f05..711b837cb 100755 --- a/src/Service/SaferPayOrderStatusService.php +++ b/src/Service/SaferPayOrderStatusService.php @@ -127,6 +127,12 @@ public function setComplete(Order $order) $saferPayOrder->captured = 1; $saferPayOrder->update(); + + //NOTE: Older PS versions does not handle same state change, so we need to check if state is already set + if ($order->getCurrentState() === _SAFERPAY_PAYMENT_COMPLETED_) { + return; + } + $order->setCurrentState(_SAFERPAY_PAYMENT_COMPLETED_); } diff --git a/src/Service/TransactionFlow/SaferPayTransactionAssertion.php b/src/Service/TransactionFlow/SaferPayTransactionAssertion.php index 006fcd094..5fcc35496 100755 --- a/src/Service/TransactionFlow/SaferPayTransactionAssertion.php +++ b/src/Service/TransactionFlow/SaferPayTransactionAssertion.php @@ -66,9 +66,10 @@ public function __construct( * @return AssertBody * @throws \Exception */ - public function assert($cartId) + public function assert($cartId, $update = true) { $saferPayOrder = new SaferPayOrder($this->orderRepository->getIdByCartId($cartId)); + \PrestaShopLogger::addLog('saferpayOrderId:' . $saferPayOrder->id); $assertRequest = $this->assertRequestCreator->create($saferPayOrder->token); $assertResponse = $this->assertionService->assert($assertRequest, $saferPayOrder->id); @@ -82,9 +83,12 @@ public function assert($cartId) $saferPayOrder->id ); - $saferPayOrder->transaction_id = $assertBody->getTransaction()->getId(); - $saferPayOrder->id_cart = $cartId; - $saferPayOrder->update(); + // assertion shouldn't update, this is quickfix for what seems to be a general flaw in structure + if ($update) { + $saferPayOrder->transaction_id = $assertBody->getTransaction()->getId(); + $saferPayOrder->id_cart = $cartId; + $saferPayOrder->update(); + } return $assertBody; }