From f35ac76f4d2923ecc34ada6ea54fe34ca68065ac Mon Sep 17 00:00:00 2001 From: Julius Zukauskas Date: Wed, 26 Jun 2024 15:25:06 +0300 Subject: [PATCH] force create order before checkout for a2a. add upgrade and new pending status --- saferpay.config.php | 4 ++++ saferpayofficial.php | 13 +++++++++++- src/Config/SaferPayConfig.php | 1 + src/Core/Payment/DTO/CheckoutData.php | 24 ++++++++++++++++++++-- src/Install/Installer.php | 15 ++++++++++++++ src/Processor/CheckoutProcessor.php | 2 +- src/Repository/SaferPayOrderRepository.php | 12 +++++++++++ upgrade/install-1.2.3.php | 6 +++++- 8 files changed, 72 insertions(+), 5 deletions(-) diff --git a/saferpay.config.php b/saferpay.config.php index 94c75db3..b3c3553e 100755 --- a/saferpay.config.php +++ b/saferpay.config.php @@ -35,6 +35,10 @@ /** @var URL to module IMG files directory */ define('_SAFERPAY_PAYMENT_AUTHORIZED_', Configuration::get(SaferPayConfig::SAFERPAY_PAYMENT_AUTHORIZED)); } +if (!defined('_SAFERPAY_PAYMENT_PENDING_')) { + /** @var URL to module IMG files directory */ + define('_SAFERPAY_PAYMENT_PENDING_', Configuration::get(SaferPayConfig::SAFERPAY_PAYMENT_PENDING)); +} if (!defined('_SAFERPAY_PAYMENT_REJECTED_')) { /** @var URL to module IMG files directory */ define('_SAFERPAY_PAYMENT_REJECTED_', Configuration::get(SaferPayConfig::SAFERPAY_PAYMENT_REJECTED)); diff --git a/saferpayofficial.php b/saferpayofficial.php index c37f9f52..c8f1a8ad 100755 --- a/saferpayofficial.php +++ b/saferpayofficial.php @@ -122,8 +122,19 @@ public function hookDisplayOrderConfirmation($params) return ''; } - //@todo: get saferpay order, check if pending and only then show custom message + /** @var Order $psOrder */ + $psOrder = $params['order']; + + /** @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 when requirements are clear + //@todo: order confirmation is already shown and confirmation email is already sent (altough the payment is pending), is that ok? 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!'; } diff --git a/src/Config/SaferPayConfig.php b/src/Config/SaferPayConfig.php index c6649a1a..37f6393d 100755 --- a/src/Config/SaferPayConfig.php +++ b/src/Config/SaferPayConfig.php @@ -219,6 +219,7 @@ class SaferPayConfig const SAFERPAY_PAYMENT_COMPLETED = 'SAFERPAY_PAYMENT_COMPLETED'; const SAFERPAY_PAYMENT_AUTHORIZED = 'SAFERPAY_PAYMENT_AUTHORIZED'; + const SAFERPAY_PAYMENT_PENDING = 'SAFERPAY_PAYMENT_PENDING'; const SAFERPAY_PAYMENT_REJECTED = 'SAFERPAY_PAYMENT_REJECTED'; const SAFERPAY_PAYMENT_AWAITING = 'SAFERPAY_PAYMENT_AWAITING'; const SAFERPAY_PAYMENT_REFUNDED = 'SAFERPAY_PAYMENT_REFUNDED'; diff --git a/src/Core/Payment/DTO/CheckoutData.php b/src/Core/Payment/DTO/CheckoutData.php index 7281cfe9..7a48165d 100644 --- a/src/Core/Payment/DTO/CheckoutData.php +++ b/src/Core/Payment/DTO/CheckoutData.php @@ -60,8 +60,8 @@ public function __construct( $this->fieldToken = $fieldToken; $this->successController = $successController; $this->isTransaction = $isTransaction; - $this->createAfterAuthorization = Configuration::get(SaferPayConfig::SAFERPAY_ORDER_CREATION_AFTER_AUTHORIZATION); $this->isAuthorizedOrder = false; + $this->setCreateAfterAuthorization($paymentMethod); } public static function create( @@ -184,4 +184,24 @@ public function setOrderStatus($status) { $this->status = $status; } -} \ No newline at end of file + + /** + * @param string $paymentMethod + * + * @return void + */ + private function setCreateAfterAuthorization($paymentMethod) + { + $methodsToForceBeforeAuthorization = [ + SaferPayConfig::PAYMENT_ACCOUNTTOACCOUNT, + ]; + + if (in_array($paymentMethod, $methodsToForceBeforeAuthorization, true)) { + $this->createAfterAuthorization = false; + + return; + } + + $this->createAfterAuthorization = Configuration::get(SaferPayConfig::SAFERPAY_ORDER_CREATION_AFTER_AUTHORIZATION); + } +} diff --git a/src/Install/Installer.php b/src/Install/Installer.php index 00974406..81e21ca8 100755 --- a/src/Install/Installer.php +++ b/src/Install/Installer.php @@ -322,6 +322,20 @@ private function installOrderRefundTable() ); } + public function createPendingOrderStatus() + { + return $this->createOrderStatus( + SaferPayConfig::SAFERPAY_PAYMENT_PENDING, + 'Payment pending by Saferpay', + '#ec730a', + false, + true, + false, + false, + true + ); + } + public function createAllOrderStatus() { $success = true; @@ -344,6 +358,7 @@ public function createAllOrderStatus() true, true ); + $success &= $this->createPendingOrderStatus(); $success &= $this->createOrderStatus( SaferPayConfig::SAFERPAY_PAYMENT_REJECTED, 'Payment rejected by Saferpay', diff --git a/src/Processor/CheckoutProcessor.php b/src/Processor/CheckoutProcessor.php index 061f66bb..478520eb 100644 --- a/src/Processor/CheckoutProcessor.php +++ b/src/Processor/CheckoutProcessor.php @@ -211,7 +211,7 @@ private function processAuthorizedOrder(CheckoutData $data, Cart $cart) $order->setCurrentState(_SAFERPAY_PAYMENT_COMPLETED_); $saferPayOrder->captured = 1; } elseif ($data->getOrderStatus() === 'PENDING') { - $order->setCurrentState(_SAFERPAY_PAYMENT_AUTHORIZED_); + $order->setCurrentState(_SAFERPAY_PAYMENT_PENDING_); $saferPayOrder->authorized = 1; $saferPayOrder->pending = 1; } diff --git a/src/Repository/SaferPayOrderRepository.php b/src/Repository/SaferPayOrderRepository.php index ae51aa3f..b9d262e8 100755 --- a/src/Repository/SaferPayOrderRepository.php +++ b/src/Repository/SaferPayOrderRepository.php @@ -25,6 +25,7 @@ use Db; use DbQuery; +use SaferPayOrder; if (!defined('_PS_VERSION_')) { exit; @@ -32,6 +33,17 @@ class SaferPayOrderRepository { + + /** + * @param int $orderId + * + * @return SaferPayOrder + */ + public function getByOrderId($orderId) + { + return new SaferPayOrder($this->getIdByOrderId($orderId)); + } + public function getIdByOrderId($orderId) { $query = new DbQuery(); diff --git a/upgrade/install-1.2.3.php b/upgrade/install-1.2.3.php index bac6152c..b5ce3a0b 100644 --- a/upgrade/install-1.2.3.php +++ b/upgrade/install-1.2.3.php @@ -30,7 +30,11 @@ function upgrade_module_1_2_3(SaferPayOfficial $module) { - return Db::getInstance()->execute('ALTER TABLE ' . _DB_PREFIX_ . 'saferpay_order ADD COLUMN `pending` TINYINT(1) DEFAULT 0') && + $installer = new \Invertus\SaferPay\Install\Installer($module); + + return + $installer->createPendingOrderStatus() && + Db::getInstance()->execute('ALTER TABLE ' . _DB_PREFIX_ . 'saferpay_order ADD COLUMN `pending` TINYINT(1) DEFAULT 0') && $module->registerHook('displayOrderConfirmation') && Configuration::updateValue(RequestHeader::SPEC_VERSION, SaferPayConfig::API_VERSION) && Configuration::updateValue(RequestHeader::SPEC_REFUND_VERSION, SaferPayConfig::API_VERSION);