Skip to content

Commit

Permalink
force create order before checkout for a2a. add upgrade and new pend…
Browse files Browse the repository at this point in the history
…ing status
  • Loading branch information
zuk3975 committed Jun 26, 2024
1 parent f4aaea2 commit f35ac76
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 5 deletions.
4 changes: 4 additions & 0 deletions saferpay.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
13 changes: 12 additions & 1 deletion saferpayofficial.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!';
}
Expand Down
1 change: 1 addition & 0 deletions src/Config/SaferPayConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
24 changes: 22 additions & 2 deletions src/Core/Payment/DTO/CheckoutData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -184,4 +184,24 @@ public function setOrderStatus($status)
{
$this->status = $status;
}
}

/**
* @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);
}
}
15 changes: 15 additions & 0 deletions src/Install/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -344,6 +358,7 @@ public function createAllOrderStatus()
true,
true
);
$success &= $this->createPendingOrderStatus();
$success &= $this->createOrderStatus(
SaferPayConfig::SAFERPAY_PAYMENT_REJECTED,
'Payment rejected by Saferpay',
Expand Down
2 changes: 1 addition & 1 deletion src/Processor/CheckoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Repository/SaferPayOrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,25 @@

use Db;
use DbQuery;
use SaferPayOrder;

if (!defined('_PS_VERSION_')) {
exit;
}

class SaferPayOrderRepository
{

/**
* @param int $orderId
*
* @return SaferPayOrder
*/
public function getByOrderId($orderId)
{
return new SaferPayOrder($this->getIdByOrderId($orderId));
}

public function getIdByOrderId($orderId)
{
$query = new DbQuery();
Expand Down
6 changes: 5 additions & 1 deletion upgrade/install-1.2.3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f35ac76

Please sign in to comment.