Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SV-4] Api up 1.40;Introduce Wechatpay: handlepending accounttoaccount #168

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@
},
"author": "PrestaShop",
"license": "AFL-3.0"
}
}
3 changes: 2 additions & 1 deletion controllers/front/notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ public function postProcess()

//NOTE to get latest information possible and not override new information.
$order = new Order($orderId);
$paymentMethod = $assertResponseBody->getPaymentMeans()->getBrand()->getPaymentMethod();

if ((int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR) === SaferPayConfig::DEFAULT_PAYMENT_BEHAVIOR_CAPTURE &&
if (SaferPayConfig::supportsOrderCapture($paymentMethod) && (int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR) === SaferPayConfig::DEFAULT_PAYMENT_BEHAVIOR_CAPTURE &&
$assertResponseBody->getTransaction()->getStatus() !== TransactionStatus::CAPTURED
) {
/** @var SaferPayOrderStatusService $orderStatusService */
Expand Down
3 changes: 2 additions & 1 deletion controllers/front/return.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ public function postProcess()
true
));
}
$paymentMethod = $response->getPaymentMeans()->getBrand()->getPaymentMethod();

if ((int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR) === SaferPayConfig::DEFAULT_PAYMENT_BEHAVIOR_CAPTURE &&
if (SaferPayConfig::supportsOrderCapture($paymentMethod) && (int) Configuration::get(SaferPayConfig::PAYMENT_BEHAVIOR) === SaferPayConfig::DEFAULT_PAYMENT_BEHAVIOR_CAPTURE &&
$response->getTransaction()->getStatus() !== TransactionStatus::CAPTURED
) {
$orderStatusService->capture(new Order($orderId));
Expand Down
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
24 changes: 23 additions & 1 deletion saferpayofficial.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct($name = null)
{
$this->name = 'saferpayofficial';
$this->author = 'Invertus';
$this->version = '1.2.2';
$this->version = '1.2.3';
$this->module_key = '3d3506c3e184a1fe63b936b82bda1bdf';
$this->displayName = 'SaferpayOfficial';
$this->description = 'Saferpay Payment module';
Expand Down Expand Up @@ -116,6 +116,28 @@ public function getService($service)
return $containerProvider->getService($service);
}

public function hookDisplayOrderConfirmation($params)
{
if (empty($params['order'])) {
return '';
}

/** @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 if needed when requirements are clear
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sitas todo lieka. Reik issiaiskint ko mes norim is to pending status pag'o. Ar uztenka translatint sita hooka ar kazka daugiau daryt.

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!';
}

public function hookActionObjectOrderPaymentAddAfter($params)
{
if (!isset($params['object'])) {
Expand Down
35 changes: 32 additions & 3 deletions src/Config/SaferPayConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SaferPayConfig
const CONFIGURATION_NAME = 'SAFERPAY_CONFIGURATION_NAME';
const CSS_FILE = 'SAFERPAY_CSS_FILE';
const TEST_SUFFIX = '_TEST';
const API_VERSION = 1.37;
const API_VERSION = '1.40';
const PAYMENT_METHODS = [
self::PAYMENT_ALIPAY,
self::PAYMENT_AMEX,
Expand All @@ -77,6 +77,8 @@ class SaferPayConfig
self::PAYMENT_APPLEPAY,
self::PAYMENT_KLARNA,
self::PAYMENT_WLCRYPTOPAYMENTS,
self::PAYMENT_WECHATPAY,
self::PAYMENT_ACCOUNTTOACCOUNT,
];

const PAYMENT_ALIPAY = 'ALIPAY';
Expand Down Expand Up @@ -111,6 +113,7 @@ class SaferPayConfig
const PAYMENT_PAYCONIQ = 'PAYCONIQ';
const PAYMENT_CARD = 'CARD';
const PAYMENT_POSTFINANCE_PAY = 'POSTFINANCEPAY';
const PAYMENT_WECHATPAY = 'WECHATPAY';

const WALLET_PAYMENT_METHODS = [
self::PAYMENT_APPLEPAY,
Expand Down Expand Up @@ -141,6 +144,7 @@ class SaferPayConfig
'Payconiq' => self::PAYMENT_PAYCONIQ,
'Card' => self::PAYMENT_CARD,
'PostFinancePay' => self::PAYMENT_POSTFINANCE_PAY,
'WeChatPay' => self::PAYMENT_WECHATPAY,
];

const FIELD_SUPPORTED_PAYMENT_METHODS = [
Expand All @@ -152,6 +156,7 @@ class SaferPayConfig
self::PAYMENT_DINERS,
self::PAYMENT_JCB,
self::PAYMENT_MYONE,
self::PAYMENT_WECHATPAY,
];

const WLCRYPTOPAYMENTS_SUPPORTED_CURRENCIES = [
Expand Down Expand Up @@ -214,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 Expand Up @@ -262,6 +268,29 @@ class SaferPayConfig
const PAYMENT_BEHAVIOR_WITHOUT_3D_CANCEL = 0;
const PAYMENT_BEHAVIOR_WITHOUT_3D_AUTHORIZE = 1;

public static function supportsOrderCapture(string $paymentMethod): bool
{
//payments that DOES NOT SUPPORT capture
$unsupportedCapturePayments = [
self::PAYMENT_WECHATPAY,
self::PAYMENT_ACCOUNTTOACCOUNT,
];

return !in_array($paymentMethod, $unsupportedCapturePayments);
}

public static function supportsOrderCancel(string $paymentMethod): bool
{
//payments that DOES NOT SUPPORT order cancel
$unsupportedCancelPayments = [
self::PAYMENT_WECHATPAY,
self::PAYMENT_ACCOUNTTOACCOUNT,
];

return !in_array($paymentMethod, $unsupportedCancelPayments);
}


public static function getConfigSuffix()
{
if (Configuration::get(self::TEST_MODE)) {
Expand Down Expand Up @@ -333,8 +362,8 @@ public static function getBaseUrl()
public static function getDefaultConfiguration()
{
return [
RequestHeader::SPEC_VERSION => '1.37',
RequestHeader::SPEC_REFUND_VERSION => '1.37',
RequestHeader::SPEC_VERSION => SaferPayConfig::API_VERSION,
RequestHeader::SPEC_REFUND_VERSION => SaferPayConfig::API_VERSION,
RequestHeader::RETRY_INDICATOR => 0,
SaferPayConfig::PAYMENT_BEHAVIOR => 1,
SaferPayConfig::PAYMENT_BEHAVIOR_WITHOUT_3D => 1,
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);
}
}
6 changes: 6 additions & 0 deletions src/Entity/SaferPayOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ class SaferPayOrder extends ObjectModel
*/
public $authorized;

/**
* @var bool
*/
public $pending;

/**
* @var array
*/
Expand All @@ -109,6 +114,7 @@ class SaferPayOrder extends ObjectModel
'canceled' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'refunded' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'authorized' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
'pending' => ['type' => self::TYPE_BOOL, 'validate' => 'isBool'],
],
];
}
2 changes: 1 addition & 1 deletion src/EntityBuilder/SaferPayOrderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

class SaferPayOrderBuilder
{
//TODO to pass $body as InitializeBody.
public function create($body, $cartId, $customerId, $isTransaction)
{
if (method_exists('Order', 'getIdByCartId')) {
Expand All @@ -51,6 +50,7 @@ public function create($body, $cartId, $customerId, $isTransaction)
$saferPayOrder->id_customer = $customerId;
$saferPayOrder->redirect_url = $this->getRedirectionUrl($body);
$saferPayOrder->is_transaction = $isTransaction;

$saferPayOrder->add();

return $saferPayOrder;
Expand Down
16 changes: 16 additions & 0 deletions src/Install/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private function registerHooks()
$this->module->registerHook('actionAdminControllerSetMedia');
$this->module->registerHook('actionOrderHistoryAddAfter');
$this->module->registerHook('actionObjectOrderPaymentAddAfter');
$this->module->registerHook('displayOrderConfirmation');
}

private function installConfiguration()
Expand Down Expand Up @@ -321,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 @@ -343,6 +358,7 @@ public function createAllOrderStatus()
true,
true
);
$success &= $this->createPendingOrderStatus();
$success &= $this->createOrderStatus(
SaferPayConfig::SAFERPAY_PAYMENT_REJECTED,
'Payment rejected by Saferpay',
Expand Down
7 changes: 6 additions & 1 deletion src/Presenter/AssertPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace Invertus\SaferPay\Presenter;

use Invertus\SaferPay\Config\SaferPayConfig;
use SaferPayAssert;
use SaferPayOfficial;

Expand All @@ -46,6 +47,8 @@ public function __construct(SaferPayOfficial $saferPay)

public function present(SaferPayAssert $assert)
{
$paymentMethod = $assert->payment_method;

return [
'authAmount' => $assert->amount,
'transactionAuth' => $assert->authorized ?
Expand All @@ -58,10 +61,12 @@ public function present(SaferPayAssert $assert)
'currency' => $assert->currency_code,
'transactionUncertain' => $assert->uncertain,
'brand' => $assert->brand,
'paymentMethod' => $assert->payment_method,
'paymentMethod' => $paymentMethod,
'transactionPaid' => $assert->status,
'merchantReference' => $assert->merchant_reference,
'paymentId' => $assert->payment_id,
'supportsOrderCapture' => SaferPayConfig::supportsOrderCapture($paymentMethod),
'supportsOrderCancel' => SaferPayConfig::supportsOrderCancel($paymentMethod),
'acceptance' => '????',
'liability_entity' => $assert->liability_entity,
'cardNumber' => $assert->card_number,
Expand Down
4 changes: 4 additions & 0 deletions src/Processor/CheckoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ private function processAuthorizedOrder(CheckoutData $data, Cart $cart)
} elseif ($data->getOrderStatus() === 'CAPTURED') {
$order->setCurrentState(_SAFERPAY_PAYMENT_COMPLETED_);
$saferPayOrder->captured = 1;
} elseif ($data->getOrderStatus() === 'PENDING') {
$order->setCurrentState(_SAFERPAY_PAYMENT_PENDING_);
$saferPayOrder->authorized = 1;
$saferPayOrder->pending = 1;
}

$saferPayOrder->update();
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
1 change: 1 addition & 0 deletions src/Service/LegacyTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private function getTranslations()
SaferPayConfig::PAYMENT_PAYCONIQ => $this->module->l('Payconiq', self::FILE_NAME),
SaferPayConfig::PAYMENT_CARD => $this->module->l('Card', self::FILE_NAME),
SaferPayConfig::PAYMENT_POSTFINANCE_PAY => $this->module->l('PostFinancePay', self::FILE_NAME),
SaferPayConfig::PAYMENT_WECHATPAY => $this->module->l('WeChatPay', self::FILE_NAME),
];
}
}
41 changes: 41 additions & 0 deletions upgrade/install-1.2.3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
*NOTICE OF LICENSE
*
*This source file is subject to the Open Software License (OSL 3.0)
*that is bundled with this package in the file LICENSE.txt.
*It is also available through the world-wide-web at this URL:
*http://opensource.org/licenses/osl-3.0.php
*If you did not receive a copy of the license and are unable to
*obtain it through the world-wide-web, please send an email
*to [email protected] so we can send you a copy immediately.
*
*DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
*versions in the future. If you wish to customize PrestaShop for your
*needs please refer to http://www.prestashop.com for more information.
*
*@author INVERTUS UAB www.invertus.eu <[email protected]>
*@copyright SIX Payment Services
*@license SIX Payment Services
*/

use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\DTO\Request\RequestHeader;

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

function upgrade_module_1_2_3(SaferPayOfficial $module)
{
$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);
}
Loading