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

[MIT-1695] Add TrueMoney jump app #431

Merged
merged 6 commits into from Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions includes/class-omise-capabilities.php
Expand Up @@ -251,4 +251,20 @@ public function getInstallmentMinLimit()
{
return $this->capabilities['limits']['installment_amount']['min'];
}

/**
* Retrieves details of TrueMoney from capabilities.
*
* @param string $source_type
*/
public function get_truemoney_backend($source_type)
{
$truemoney_source_types = [Omise_Payment_Truemoney::WALLET, Omise_Payment_Truemoney::JUMPAPP];

if (!in_array($source_type, $truemoney_source_types)) {
return null;
}

return $this->getBackendByType($source_type);
}
}
72 changes: 55 additions & 17 deletions includes/gateway/class-omise-payment-truemoney.php
Expand Up @@ -6,15 +6,22 @@
*/
class Omise_Payment_Truemoney extends Omise_Payment_Offsite
{
/**
* Backends identifier
* @var string
*/
const WALLET = 'truemoney';
const JUMPAPP = 'truemoney_jumpapp';

public function __construct()
{
parent::__construct();

$this->id = 'omise_truemoney';
$this->has_fields = true;
$this->method_title = __( 'Opn Payments TrueMoney Wallet', 'omise' );
$this->method_title = __( 'Opn Payments TrueMoney', 'omise' );
$this->method_description = wp_kses(
__( 'Accept payments through <strong>TrueMoney Wallet</strong> via Opn Payments payment gateway (only available in Thailand).', 'omise' ),
__( 'Accept payments through <strong>TrueMoney</strong> via Opn Payments payment gateway (only available in Thailand).', 'omise' ),
array( 'strong' => array() )
);

Expand All @@ -26,7 +33,7 @@ public function __construct()
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->restricted_countries = array( 'TH' );
$this->source_type = 'truemoney';
$this->source_type = $this->get_source();

add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_api_' . $this->id . '_callback', 'Omise_Callback::execute' );
Expand All @@ -43,15 +50,15 @@ public function init_form_fields()
'enabled' => array(
'title' => __( 'Enable/Disable', 'omise' ),
'type' => 'checkbox',
'label' => __( 'Enable Opn Payments TrueMoney Wallet Payment', 'omise' ),
'label' => __( 'Enable Opn Payments TrueMoney Payment', 'omise' ),
'default' => 'no'
),

'title' => array(
'title' => __( 'Title', 'omise' ),
'type' => 'text',
'description' => __( 'This controls the title the user sees during checkout.', 'omise' ),
'default' => __( 'TrueMoney Wallet', 'omise' ),
'default' => __( 'TrueMoney', 'omise' ),
),

'description' => array(
Expand All @@ -68,7 +75,9 @@ public function init_form_fields()
public function payment_fields()
{
parent::payment_fields();
Omise_Util::render_view( 'templates/payment/form-truemoney.php', array() );
if (self::WALLET === $this->source_type) {
Omise_Util::render_view( 'templates/payment/form-truemoney.php', [] );
}
}

/**
Expand All @@ -82,22 +91,51 @@ public function charge($order_id, $order)

public function get_charge_request($order_id, $order)
{
$phoneOption = $_POST['omise_phone_number_default'];
$isPhoneOptionChecked = isset($phoneOption) && 1 == $phoneOption;
$phone_number = $isPhoneOptionChecked ?
$order->get_billing_phone() :
sanitize_text_field( $_POST['omise_phone_number'] );

$requestData = $this->build_charge_request(
$request_data = $this->build_charge_request(
$order_id,
$order,
$this->source_type,
$this->id . '_callback'
);
$requestData['source'] = array_merge($requestData['source'], [
'phone_number' => $phone_number
]);

return $requestData;
if (self::WALLET === $this->source_type) {
$phone_option = $_POST['omise_phone_number_default'];
$is_phone_option_checked = isset($phone_option) && 1 == $phone_option;
$phone_number = $is_phone_option_checked ?
$order->get_billing_phone() :
sanitize_text_field( $_POST['omise_phone_number'] );

$request_data['source'] = array_merge($request_data['source'], [
'phone_number' => $phone_number
]);
}

return $request_data;
}

/**
* Return the right ShopeePay backend depending on the platform and availability of
* the backend in the capability
*/
public function get_source()
{
$capabilities = Omise_Capabilities::retrieve();

if (!$capabilities) {
return self::JUMPAPP;
}

$is_jumpapp_enabled = $capabilities->get_truemoney_backend(self::JUMPAPP);
$is_wallet_enabled = $capabilities->get_truemoney_backend(self::WALLET);

if (!empty($is_wallet_enabled) && empty($is_jumpapp_enabled)) {
return self::WALLET;
}

// Return 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::JUMPAPP;
}
}
2 changes: 1 addition & 1 deletion readme.txt
@@ -1,6 +1,6 @@
=== Opn Payments ===
Contributors: Opn Payments
Tags: opn payments, payment, payment gateway, woocommerce plugin, omise, opn, installment, internet banking, alipay, paynow, truemoney wallet, woocommerce payment
Tags: opn payments, payment, payment gateway, woocommerce plugin, omise, opn, installment, internet banking, alipay, paynow, truemoney, woocommerce payment
Requires at least: 4.3.1
Tested up to: 6.4.0
Stable tag: 5.6.1
Expand Down
81 changes: 61 additions & 20 deletions tests/unit/includes/class-omise-capabilities-test.php
@@ -1,8 +1,11 @@
<?php

use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/gateway/bootstrap-test-setup.php';

class Omise_Capabilities_Test extends TestCase
/**
* @runTestsInSeparateProcesses
*/
class Omise_Capabilities_Test extends Bootstrap_Test_Setup
{
private $omiseSettingMock;

Expand All @@ -13,26 +16,21 @@ class Omise_Capabilities_Test extends TestCase
*/
public function setUp(): void
{
parent::setUp();

Mockery::mock('Omise_Payment_Offsite');
require_once __DIR__ . '/../../../includes/class-omise-capabilities.php';
require_once __DIR__ . '/../../../includes/gateway/class-omise-payment-truemoney.php';
$this->omiseSettingMock = Mockery::mock('alias:Omise_Setting');
$this->omiseCapabilitiesMock = Mockery::mock('alias:OmiseCapabilities');
}

/**
* close mockery after test cases are done
*/
public function tearDown(): void
{
Mockery::close();
}

/**
* @dataProvider retrieve_data_provider
* @runInSeparateProcess
* @covers Omise_Capabilities
*/
public function test_retrieve_should_return_value_when_it_should_call_api($isCheckout, $isThankYouPage, $isAdmin, $adminPageName, $expected)
{
{
// assigning to global variable, so that we can use in child functions
$GLOBALS['isCheckout'] = $isCheckout;
$GLOBALS['isThankYouPage'] = $isThankYouPage;
Expand All @@ -41,9 +39,15 @@ public function test_retrieve_should_return_value_when_it_should_call_api($isChe
// mocking page name
$_GET['page'] = $adminPageName;

function is_checkout() { return $GLOBALS['isCheckout']; }
function is_wc_endpoint_url($page) { return $GLOBALS['isThankYouPage']; }
function is_admin() { return $GLOBALS['isAdmin']; }
Brain\Monkey\Functions\expect('is_admin')
->with('123')
->andReturn($GLOBALS['isAdmin']);
Brain\Monkey\Functions\expect('is_checkout')
->with('123')
->andReturn($GLOBALS['isCheckout']);
Brain\Monkey\Functions\expect('is_wc_endpoint_url')
->with('123')
->andReturn($GLOBALS['isThankYouPage']);

if ($expected) {
$this->omiseSettingMock->shouldReceive('instance')->andReturn($this->omiseSettingMock);
Expand All @@ -66,16 +70,53 @@ public function retrieve_data_provider()
return [
// checkout page and not thank you page
[true, false, false, '', true],
// // checkout page and also thank you page
// checkout page and also thank you page
[true, true, false, '', false],
// // omise setting page
// omise setting page
[true, true, true, 'omise', true],
// // other admin page
// other admin page
[true, true, true, 'other-page', false],
// // non checkout page and also no-admin page
// non checkout page and also no-admin page
[false, false, false, 'other-page', false],
// // non checkout page, non admin page
// non checkout page, non admin page
[false, false, false, '', false],
];
}

/**
* @test
*/
public function test_get_truemoney_backend_returns_null_when_invalid_payment_is_passed()
{
Brain\Monkey\Functions\expect('is_admin')
->with('123')
->andReturn(true);

Brain\Monkey\Functions\expect('is_checkout')
->with('123')
->andReturn(true);

Brain\Monkey\Functions\expect('is_wc_endpoint_url')
->with('123')
->andReturn(false);

$capabilities = new Omise_Capabilities;
$is_enabled = $capabilities->get_truemoney_backend('abc');
$this->assertNull($is_enabled);
}

public function truemoney_source_provider()
{
return [ ['abc', false], ['truemoney', true], ['truemoney_jumpapp', true] ];
}
}

class Omise_Payment_Truemoney_Stub
{
/**
* Backends identifier
* @var string
*/
const WALLET = 'truemoney';
const JUMPAPP = 'truemoney_jumpapp';
}
2 changes: 1 addition & 1 deletion tests/unit/includes/gateway/bootstrap-test-setup.php
@@ -1,7 +1,7 @@
<?php

use PHPUnit\Framework\TestCase;
use Brain;


abstract class Bootstrap_Test_Setup extends TestCase
{
Expand Down
Expand Up @@ -2,6 +2,9 @@

require_once __DIR__ . '/class-omise-offsite-test.php';

/**
* @runTestsInSeparateProcesses
*/
class Omise_Payment_Alipay_Hk_Test extends Omise_Offsite_Test
{
public function setUp(): void
Expand Down
Expand Up @@ -2,6 +2,9 @@

require_once __DIR__ . '/class-omise-offsite-test.php';

/**
* @runTestsInSeparateProcesses
*/
class Omise_Payment_Kakaopay_Test extends Omise_Offsite_Test
{
public function setUp(): void
Expand Down
@@ -1,7 +1,6 @@
<?php

use PHPUnit\Framework\TestCase;
use Brain;

class Omise_Payment_CreditCard_Test extends TestCase
{
Expand Down
Expand Up @@ -13,6 +13,9 @@ public function setUp(): void

public function testCharge()
{
Brain\Monkey\Functions\expect('wc_get_user_agent')
->with('123')
->andReturn('Chrome Web');
$_POST['omise-offsite'] = 'mobile_banking_bbl';
$obj = new Omise_Payment_Mobilebanking();
$this->getChargeTest($obj);
Expand Down
Expand Up @@ -20,12 +20,6 @@ function plugins_url() {
return "http://localhost";
}
}

if (!function_exists('wc_get_user_agent')) {
function wc_get_user_agent() {
return "Chrome Web";
}
}
}

public function tearDown(): void
Expand Down Expand Up @@ -85,6 +79,9 @@ public function getIconReturnsCorrectImageLink()
*/
public function testCharge()
{
Brain\Monkey\Functions\expect('wc_get_user_agent')
->with('123')
->andReturn('Chrome Web');
$this->getChargeTest($this->obj);
}
}
Expand Up @@ -26,6 +26,9 @@ public function tearDown(): void
*/
public function testCharge()
{
Brain\Monkey\Functions\expect('wc_get_user_agent')
->with('123')
->andReturn('Chrome Web');
$this->getChargeTest($this->obj);
}
}