diff --git a/includes/gateway/class-omise-payment-ocbc-digital.php b/includes/gateway/class-omise-payment-ocbc-digital.php index e1431bfb..c15ad96e 100644 --- a/includes/gateway/class-omise-payment-ocbc-digital.php +++ b/includes/gateway/class-omise-payment-ocbc-digital.php @@ -56,15 +56,6 @@ public function init_form_fields() { * @inheritdoc */ public function charge($order_id, $order) - { - return OmiseCharge::create($this->get_charge_request($order_id, $order)); - } - - /** - * @order_id integer - * @order object - */ - public function get_charge_request($order_id, $order) { $requestData = $this->build_charge_request( $order_id, @@ -76,6 +67,7 @@ public function get_charge_request($order_id, $order) $requestData['source'] = array_merge($requestData['source'], [ 'platform_type' => Omise_Util::get_platform_type(wc_get_user_agent()) ]); + return OmiseCharge::create($requestData); } diff --git a/includes/gateway/traits/charge-request-builder.php b/includes/gateway/traits/charge-request-builder.php index 20df176d..1d5fe43b 100644 --- a/includes/gateway/traits/charge-request-builder.php +++ b/includes/gateway/traits/charge-request-builder.php @@ -38,6 +38,8 @@ public function getMetadata($order_id, $order, $additionalData = []) // override order_id as a reference for webhook handlers. $orderId = [ 'order_id' => $order_id ]; + echo var_dump(apply_filters('omise_charge_params_metadata', [], $order)); + return array_merge( apply_filters('omise_charge_params_metadata', [], $order), array_merge($orderId, $additionalData) diff --git a/includes/libraries/omise-php/lib/omise/res/OmiseApiResource.php b/includes/libraries/omise-php/lib/omise/res/OmiseApiResource.php index 45953b6e..2258ee88 100644 --- a/includes/libraries/omise-php/lib/omise/res/OmiseApiResource.php +++ b/includes/libraries/omise-php/lib/omise/res/OmiseApiResource.php @@ -1,8 +1,8 @@ shouldReceive('init_settings'); $offsite->shouldReceive('get_option'); $offsite->shouldReceive('get_provider'); + $offsite->shouldReceive('build_charge_request') + ->andReturn([ + 'source' => [ 'type' => 'source_123' ] + ]); // mocking WP built-in functions if (!function_exists('wp_kses')) { diff --git a/tests/unit/includes/gateway/class-omise-payment-ocbc-digital-test.php b/tests/unit/includes/gateway/class-omise-payment-ocbc-digital-test.php index 5aef9042..54fd8e75 100644 --- a/tests/unit/includes/gateway/class-omise-payment-ocbc-digital-test.php +++ b/tests/unit/includes/gateway/class-omise-payment-ocbc-digital-test.php @@ -80,12 +80,25 @@ function apply_filters() { /** * @test */ - public function getChargeRequestReturnsCorrectData() + public function testCharge() { - // Create a mock of the $order object - $orderMock = Mockery::mock('WC_Order'); $expectedCurrency = 'SGD'; $expectedAmount = 1000000; // in subunits + $expectedAmount = 1000000; // in subunits + $expectedRequest = [ + "object" => "charge", + "id" => "chrg_test_no1t4tnemucod0e51mo", + "location" => "/charges/chrg_test_no1t4tnemucod0e51mo", + "amount" => $expectedAmount, + "currency" => $expectedCurrency + ]; + + // Create a mock for OmiseCharge + $chargeMock = Mockery::mock('overload:OmiseCharge'); + $chargeMock->shouldReceive('create')->once()->andReturn($expectedRequest); + + // Create a mock of the $order object + $orderMock = Mockery::mock('WC_Order'); // Define expectations for the mock $orderMock->shouldReceive('get_currency') @@ -101,9 +114,8 @@ function wc_get_user_agent() { $expectedSourceType = 'mobile_banking_ocbc'; $order_id = "123"; - $result = $this->obj->get_charge_request($order_id, $orderMock); + $result = $this->obj->charge($order_id, $orderMock); $this->assertEquals($expectedAmount, $result['amount']); $this->assertEquals($expectedCurrency, $result['currency']); - $this->assertEquals($expectedSourceType, $result['source']['type']); } } diff --git a/tests/unit/includes/gateway/traits/charge-request-builder-test.php b/tests/unit/includes/gateway/traits/charge-request-builder-test.php index f4350335..2390ac7c 100644 --- a/tests/unit/includes/gateway/traits/charge-request-builder-test.php +++ b/tests/unit/includes/gateway/traits/charge-request-builder-test.php @@ -38,10 +38,12 @@ public function add_meta_data() {} $source_type = 'alipay'; $callback_url = 'omise_alipay_callback'; - if (!function_exists('apply_filters')) { - function apply_filters() { - return []; - } + // removed function_exists('apply_filters') check because "apply_filters" + // returns different data type such as string or array depending how it's + // implemented. So, even if "apply_filters" is already declared somewhere, + // it might not return the desired result. So, we are overriding it. + function apply_filters() { + return []; } $result = $mock->build_charge_request(