Skip to content

Commit

Permalink
Add truemoney jumpapp (#466)
Browse files Browse the repository at this point in the history
* Added Truemoney jumpapp

* Resolve M2 coding check issues.

* Added tests.

* Reverting magento version in dev to 2.4.4

* Added more tests.

* Added missing @Covers annotation

* Refactored CapabilitiesConfigProvider and added tests.

* Resolve M2 coding standard issue and update tests.

* Refactored APMBuildter tests.

* Removed Wallet keyword from admin html and translation.

* Created new variables to hold the merged values from two different arrays.

---------

Co-authored-by: Aashish <[email protected]>
  • Loading branch information
aashishgurung and Aashish authored Jan 19, 2024
1 parent fda3305 commit d9a0c8b
Show file tree
Hide file tree
Showing 16 changed files with 395 additions and 139 deletions.
39 changes: 32 additions & 7 deletions Gateway/Request/APMBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

class APMBuilder implements BuilderInterface
{

/**
* @var string
*/
Expand Down Expand Up @@ -122,6 +121,16 @@ class APMBuilder implements BuilderInterface
*/
protected $money;

/**
* @var Capabilities
*/
protected $capabilities;

/**
* @var Config
*/
protected $config;

/**
* @param $helper \Omise\Payment\Helper\OmiseHelper
* @param $returnUrl \Omise\Payment\Helper\ReturnUrl
Expand Down Expand Up @@ -183,12 +192,7 @@ public function build(array $buildSubject)
];
break;
case Truemoney::CODE:
$paymentInfo[self::SOURCE] = [
self::SOURCE_TYPE => 'truemoney',
self::SOURCE_PHONE_NUMBER => $method->getAdditionalInformation(
TruemoneyDataAssignObserver::PHONE_NUMBER
),
];
$paymentInfo[self::SOURCE] = $this->getTruemoneySourceData($method);
break;
case Conveniencestore::CODE:
$paymentInfo[self::SOURCE] = [
Expand Down Expand Up @@ -404,4 +408,25 @@ private function getOrderItems($order)
}
return $itemArray;
}

public function getTruemoneySourceData($method)
{
$isJumpAppEnabled = $this->capabilities->isBackendEnabled(Truemoney::JUMPAPP_ID);
$isWalletEnabled = $this->capabilities->isBackendEnabled(Truemoney::ID);

if (!$isJumpAppEnabled && $isWalletEnabled) {
return [
self::SOURCE_TYPE => Truemoney::ID,
self::SOURCE_PHONE_NUMBER => $method->getAdditionalInformation(
TruemoneyDataAssignObserver::PHONE_NUMBER
)
];
}

// Returning JUMP APP for the following cases:
// Case 1: Both jumpapp and wallet are enabled
// Case 2: jumpapp is enabled and wallet is disabled
// Case 3: Both are disabled.
return [ self::SOURCE_TYPE => Truemoney::JUMPAPP_ID ];
}
}
3 changes: 2 additions & 1 deletion Helper/OmiseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class OmiseHelper extends AbstractHelper
// offsite payment
Alipay::ID => Alipay::CODE,
Truemoney::ID => Truemoney::CODE,
Truemoney::JUMPAPP_ID => Truemoney::CODE,
Pointsciti::ID => Pointsciti::CODE,
Fpx::ID => Fpx::CODE,
Alipayplus::ALIPAY_ID => Alipayplus::ALIPAY_CODE,
Expand Down Expand Up @@ -187,7 +188,7 @@ class OmiseHelper extends AbstractHelper
Alipay::CODE => "Alipay",
Internetbanking::CODE => "Internet Banking Payment",
Installment::CODE => "Installment Payment",
Truemoney::CODE => "TrueMoney Wallet Payment",
Truemoney::CODE => "TrueMoney Payment",
Pointsciti::CODE => "Citi Pay with Points",
Fpx::CODE => "FPX Payment",
Alipayplus::ALIPAY_CODE => "Alipay (Alipay+ Partner) Payment",
Expand Down
2 changes: 2 additions & 0 deletions Model/Config/Truemoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ class Truemoney extends Config
* @var string
*/
const ID = 'truemoney';

const JUMPAPP_ID = 'truemoney_jumpapp';
}
71 changes: 57 additions & 14 deletions Model/Ui/CapabilitiesConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Omise\Payment\Helper\OmiseHelper;
use Omise\Payment\Model\Capabilities;
use Omise\Payment\Model\Config\Shopeepay;
use Omise\Payment\Model\Config\Truemoney;
use Omise\Payment\Model\Config\CcGooglePay;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Checkout\Model\ConfigProviderInterface;
Expand Down Expand Up @@ -33,7 +34,7 @@ public function __construct(
$this->capabilities = $capabilities;
$this->_paymentLists = $paymentLists;
$this->_storeManager = $storeManager;
$this->helper = $helper;
$this->helper = $helper;
}

/**
Expand All @@ -46,12 +47,8 @@ public function getConfig()
$listOfActivePaymentMethods = $this->_paymentLists->getActiveList($this->_storeManager->getStore()->getId());
$currency = $this->_storeManager->getStore()->getCurrentCurrencyCode();
$configs = [];

// Retrieve available backends & methods from capabilities api
$backends = $this->capabilities->getBackendsWithOmiseCode();
$tokenization_methods = $this->capabilities->getTokenizationMethodsWithOmiseCode();
$backends = array_merge($backends, $tokenization_methods);
$configs['omise_installment_min_limit'] = $this->capabilities->getInstallmentMinLimit($currency);
$configs['omise_payment_list'] = [];

foreach ($listOfActivePaymentMethods as $method) {
$code = $method->getCode();
Expand All @@ -62,19 +59,40 @@ public function getConfig()
$configs['card_brands'] = $this->capabilities->getCardBrands();
}

// filter only active backends
if (array_key_exists($code, $backends)) {
if ($code === 'omise_offsite_shopeepay') {
$configs['omise_payment_list'][$code] = $this->getShopeeBackendByType($backends[$code]);
} else {
$configs['omise_payment_list'][$code] = $backends[$code];
}
}
$this->filterActiveBackends($code, $configs['omise_payment_list']);
}

return $configs;
}

/**
* filter only active backends
* @param $code Payment method code
* @param $paymentList Reference of the payment list
*/
private function filterActiveBackends($code, &$paymentList)
{
// Retrieve available backends & methods from capabilities api
$paymentBackends = $this->capabilities->getBackendsWithOmiseCode();
$tokenizationMethods = $this->capabilities->getTokenizationMethodsWithOmiseCode();
$mergedBackends = array_merge($paymentBackends, $tokenizationMethods);

// filter only active backends
if (!array_key_exists($code, $mergedBackends)) {
return;
}

if ($code === Shopeepay::CODE) {
$backend = $this->getShopeeBackendByType($mergedBackends[$code]);
} elseif ($code === Truemoney::CODE) {
$backend = $this->getTruemoneyBackendByType($mergedBackends[$code]);
} else {
$backend = $configs['omise_payment_list'][$code] = $mergedBackends[$code];
}

$paymentList[$code] = $backend;
}

/**
* Return the right ShopeePay backend depending on the platform and availability of
* the backend in the capability
Expand Down Expand Up @@ -116,4 +134,29 @@ private function getShopeeBackendByType($shopeeBackends)
// execute if none of the shopee backends were disabled.
return $isShopeepayEnabled ? $mpmBackend : $jumpAppBackend;
}

private function getTruemoneyBackendByType($truemoneyBackends)
{
$jumpAppBackend = [];
$walletBackend = [];

// Since Truemoney will have two types i.e truemoney and truemoney_jumpapp,
// we split and store the type in separate variables.
foreach ($truemoneyBackends as $backend) {
if ($backend->type === Truemoney::JUMPAPP_ID) {
$jumpAppBackend[] = $backend;
} else {
$walletBackend[] = $backend;
}
}

$isJumpAppEnabled = $this->capabilities->isBackendEnabled(Truemoney::JUMPAPP_ID);
$isWalletEnabled = $this->capabilities->isBackendEnabled(Truemoney::ID);

if (!$isJumpAppEnabled && $isWalletEnabled) {
return $walletBackend;
}

return $jumpAppBackend;
}
}
32 changes: 32 additions & 0 deletions Test/Unit/Gateway/Request/APMBuilders/APMBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Omise\Payment\Test\Unit\Gateway\Request\APMBuilders;

use Magento\Payment\Gateway\Data\OrderAdapterInterface;
use Omise\Payment\Helper\OmiseHelper;
use Omise\Payment\Helper\ReturnUrlHelper;
use Omise\Payment\Model\Capabilities;
use Omise\Payment\Model\Config\Config;
use PHPUnit\Framework\TestCase;
use Omise\Payment\Test\Mock\InfoMock;

abstract class APMBuilderTest extends TestCase
{
protected $builder;
protected $helper;
protected $returnUrlHelper;
protected $config;
protected $capabilities;
protected $orderMock;
protected $infoMock;

protected function setUp(): void
{
$this->helper = $this->getMockBuilder(OmiseHelper::class)->disableOriginalConstructor()->getMock();
$this->returnUrlHelper = $this->getMockBuilder(ReturnUrlHelper::class)->disableOriginalConstructor()->getMock();
$this->config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
$this->capabilities = $this->getMockBuilder(Capabilities::class)->disableOriginalConstructor()->getMock();
$this->orderMock = $this->getMockBuilder(OrderAdapterInterface::class)->getMock();
$this->infoMock = $this->getMockBuilder(InfoMock::class)->getMock();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Omise\Payment\Test\Unit;
namespace Omise\Payment\Test\Unit\Gateway\Request\APMBuilders;

use PHPUnit\Framework\TestCase;
use Omise\Payment\Model\Config\Atome;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,15 @@
<?php

namespace Omise\Payment\Test\Unit;
namespace Omise\Payment\Test\Unit\Gateway\Request\APMBuilders;

use Magento\Payment\Gateway\Data\OrderAdapterInterface;
use Magento\Payment\Gateway\Data\PaymentDataObject;
use Omise\Payment\Gateway\Request\APMBuilder;
use Omise\Payment\Helper\OmiseHelper;
use Omise\Payment\Helper\OmiseMoney;
use Omise\Payment\Helper\ReturnUrlHelper;
use Omise\Payment\Model\Capabilities;
use Omise\Payment\Model\Config\Alipay;
use Omise\Payment\Model\Config\Config;
use PHPUnit\Framework\TestCase;
use Omise\Payment\Test\Mock\InfoMock;
use Omise\Payment\Test\Unit\Gateway\Request\APMBuilders\APMBuilderTest;

class AlipayAPMBuilderTest extends TestCase
class AlipayAPMBuilderTest extends APMBuilderTest
{
private $builder;
private $helper;
private $returnUrlHelper;
private $config;
private $capabilities;
private $orderMock;
private $infoMock;

protected function setUp(): void
{
$this->helper = $this->getMockBuilder(OmiseHelper::class)->disableOriginalConstructor()->getMock();
$this->returnUrlHelper = $this->getMockBuilder(ReturnUrlHelper::class)->disableOriginalConstructor()->getMock();
$this->config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
$this->capabilities = $this->getMockBuilder(Capabilities::class)->disableOriginalConstructor()->getMock();
$this->orderMock = $this->getMockBuilder(OrderAdapterInterface::class)->getMock();
$this->infoMock = $this->getMockBuilder(InfoMock::class)->getMock();
}

/**
* @covers Omise\Payment\Gateway\Request\APMBuilder
* @covers Omise\Payment\Model\Config\Alipay
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,26 @@
<?php

namespace Omise\Payment\Test\Unit;
namespace Omise\Payment\Test\Unit\Gateway\Request\APMBuilders;

use PHPUnit\Framework\TestCase;
use Omise\Payment\Helper\OmiseMoney;
use Omise\Payment\Helper\OmiseHelper;
use Omise\Payment\Model\Capabilities;
use Omise\Payment\Model\Config\Atome;
use Omise\Payment\Test\Mock\InfoMock;
use Omise\Payment\Model\Config\Config;
use Omise\Payment\Helper\ReturnUrlHelper;
use Omise\Payment\Gateway\Request\APMBuilder;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Payment\Gateway\Data\PaymentDataObject;
use Magento\Payment\Gateway\Data\OrderAdapterInterface;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Payment\Gateway\Data\AddressAdapterInterface;
use Omise\Payment\Test\Unit\Gateway\Request\APMBuilders\APMBuilderTest;

class AtomeAPMBuilderTest extends TestCase
class AtomeAPMBuilderTest extends APMBuilderTest
{
private $builder;
private $helper;
private $returnUrlHelper;
private $config;
private $capabilities;
private $orderMock;
private $infoMock;
private $addressMock;
private $itemMock;

protected function setUp(): void
{
parent::setup();

$this->itemMock = $this->getMockBuilder(OrderItemInterface::class)->getMock();
$this->addressMock = $this->getMockBuilder(AddressAdapterInterface::class)->getMock();
$this->helper = $this->getMockBuilder(OmiseHelper::class)->disableOriginalConstructor()->getMock();
$this->returnUrlHelper = $this->getMockBuilder(ReturnUrlHelper::class)->disableOriginalConstructor()->getMock();
$this->config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
$this->capabilities = $this->getMockBuilder(Capabilities::class)->disableOriginalConstructor()->getMock();
$this->orderMock = $this->getMockBuilder(OrderAdapterInterface::class)->getMock();
$this->orderMock->method('getShippingAddress')->willReturn($this->addressMock);
$this->orderMock->method('getItems')->willReturn([$this->itemMock]);
$this->orderMock->method('getCurrencyCode')->willReturn('THB');
$this->infoMock = $this->getMockBuilder(InfoMock::class)->getMock();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,15 @@
<?php

namespace Omise\Payment\Test\Unit;
namespace Omise\Payment\Test\Unit\Gateway\Request\APMBuilders;

use Magento\Payment\Gateway\Data\OrderAdapterInterface;
use Magento\Payment\Gateway\Data\PaymentDataObject;
use Omise\Payment\Gateway\Request\APMBuilder;
use Omise\Payment\Helper\OmiseHelper;
use Omise\Payment\Helper\OmiseMoney;
use Omise\Payment\Helper\ReturnUrlHelper;
use Omise\Payment\Model\Capabilities;
use Omise\Payment\Model\Config\Config;
use PHPUnit\Framework\TestCase;
use Omise\Payment\Model\Config\PayPay;
use Omise\Payment\Test\Mock\InfoMock;
use Omise\Payment\Test\Unit\Gateway\Request\APMBuilders\APMBuilderTest;

class PayPayAPMBuilderTest extends TestCase
class PayPayAPMBuilderTest extends APMBuilderTest
{
private $builder;
private $helper;
private $returnUrlHelper;
private $config;
private $capabilities;
private $orderMock;
private $infoMock;

protected function setUp(): void
{
$this->helper = $this->getMockBuilder(OmiseHelper::class)->disableOriginalConstructor()->getMock();
$this->returnUrlHelper = $this->getMockBuilder(ReturnUrlHelper::class)->disableOriginalConstructor()->getMock();
$this->config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
$this->capabilities = $this->getMockBuilder(Capabilities::class)->disableOriginalConstructor()->getMock();
$this->orderMock = $this->getMockBuilder(OrderAdapterInterface::class)->getMock();
$this->infoMock = $this->getMockBuilder(InfoMock::class)->getMock();
}

/**
* @covers Omise\Payment\Gateway\Request\APMBuilder
* @covers Omise\Payment\Model\Config\PayPay
Expand Down
Loading

0 comments on commit d9a0c8b

Please sign in to comment.