Skip to content

Commit

Permalink
Fix installment payment when admin manually pay for order (#388)
Browse files Browse the repository at this point in the history
* [MIT-1562]: Total amount for payment is pulled from order instead of cart when the payment page is triggered from admin.

* Fixed the issue with Japanese language Support in the secure form.

* Added unit test for installment

* Added unit test for installment

* Updated test

* Fix indentation

* Fix the tests after code chagne.

* Fix the spelling mistake.

* Reverted secure form changes.

---------

Co-authored-by: Aashish <[email protected]>
  • Loading branch information
aashishgurung and Aashish authored Aug 7, 2023
1 parent e9fe347 commit 9e78008
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
23 changes: 22 additions & 1 deletion includes/gateway/class-omise-payment-installment.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public function payment_fields()
parent::payment_fields();

$currency = get_woocommerce_currency();
$cart_total = WC()->cart->total;
$cart_total = $this->getTotalAmount();

$capabilities = $this->backend->capabilities();
$installmentMinLimit = $capabilities->getInstallmentMinLimit();

Expand All @@ -84,6 +85,26 @@ public function payment_fields()
);
}

/**
* Get the total amount of an order
*/
public function getTotalAmount()
{
global $wp;

if (
isset($wp->query_vars['order-pay']) &&
(int)$wp->query_vars['order-pay'] > 0
) {
$order_id = (int)$wp->query_vars['order-pay'];
$order = wc_get_order( $order_id );
return $order->get_total();
}

// if not an order page then get total from the cart
return WC()->cart->total;
}

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

use PHPUnit\Framework\TestCase;
use Mockery;

class Omise_Payment_Installment_Test extends TestCase
{
public function setUp(): void
{
// Mocking the parent class
$offsite = Mockery::mock('overload:Omise_Payment_Offsite');
$offsite->shouldReceive('init_settings');
$offsite->shouldReceive('get_option');

// mocking WP built-in functions
if (!function_exists('wp_kses')) {
function wp_kses() {}
}

if (!function_exists('add_action')) {
function add_action() {}
}

require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-installment.php';
}

/**
* close mockery after tests are done
*/
public function teardown(): void
{
Mockery::close();
}

/**
* @test
*/
public function getTotalAmountFromAdminOrderpage()
{
// mocking built-in WooCommerce function
if (!function_exists('wc_get_order')) {
function wc_get_order() {
$class = new class {
public $property;

public function get_total() {
return 999999;
}
};
return $class;
}
}

// mocking the WP global variable $wp
$wp = new stdClass();
$wp->query_vars = ['order-pay' => 123];
$GLOBALS['wp'] = $wp;

$installment = new Omise_Payment_Installment();
$total = $installment->getTotalAmount();

$this->assertEquals($total, 999999);
}

/**
* @test
*/
public function getTotalAmountFromCart()
{
// mocking WC() method
if (!function_exists('WC')) {
function WC() {
$class = new stdClass();
$class->cart = new stdClass();
$class->cart->total = 999999;
return $class;
}
}

$installment = new Omise_Payment_Installment();
$total = $installment->getTotalAmount();

$this->assertEquals($total, 999999);
}
}

0 comments on commit 9e78008

Please sign in to comment.