From c83fe8ee8522c424ca2a44e7bf12e8a7f54112dc Mon Sep 17 00:00:00 2001 From: Aashish Gurung <101558497+aashishgurung@users.noreply.github.com> Date: Mon, 4 Mar 2024 14:00:23 +0700 Subject: [PATCH 1/3] Add wechat pay (#434) * Implemented WeChat Pay. * Added tests * Added tests for get_client_ip * Add missing require of wechat in omise-woocommerce. --------- Co-authored-by: Aashish --- assets/images/wechat_pay.svg | 3 + includes/class-omise-callback.php | 3 +- includes/class-omise-payment-factory.php | 3 +- .../class-omise-payment-wechat-pay.php | 83 +++++++++++++++++++ .../omise-plugin/helpers/request.php | 51 ++++++++++-- omise-woocommerce.php | 1 + phpunit.xml | 8 ++ .../includes/gateway/bootstrap-test-setup.php | 1 - .../class-omise-payment-wechat-pay-test.php | 48 +++++++++++ .../omise-plugin/helpers/request-test.php | 51 ++++++++++++ 10 files changed, 243 insertions(+), 9 deletions(-) create mode 100644 assets/images/wechat_pay.svg create mode 100644 includes/gateway/class-omise-payment-wechat-pay.php create mode 100644 tests/unit/includes/gateway/class-omise-payment-wechat-pay-test.php create mode 100644 tests/unit/includes/libraries/omise-plugin/helpers/request-test.php diff --git a/assets/images/wechat_pay.svg b/assets/images/wechat_pay.svg new file mode 100644 index 00000000..01775569 --- /dev/null +++ b/assets/images/wechat_pay.svg @@ -0,0 +1,3 @@ + + + diff --git a/includes/class-omise-callback.php b/includes/class-omise-callback.php index 736a001a..7a636f83 100644 --- a/includes/class-omise-callback.php +++ b/includes/class-omise-callback.php @@ -31,7 +31,7 @@ public static function execute() $order_id = isset( $_GET['order_id'] ) ? sanitize_text_field( $_GET['order_id'] ) : null; $order = wc_get_order( $order_id ); - if(!RequestHelper::validateRequest($order->get_meta('token'))) { + if(!RequestHelper::validate_request($order->get_meta('token'))) { return wp_redirect( wc_get_checkout_url() ); } @@ -39,7 +39,6 @@ public static function execute() $callback->validate(); } - /** * Sometimes cancelling a transaction does not updates the status on the Omise backend * which causes the status to be pending even thought the transaction was cancelled. diff --git a/includes/class-omise-payment-factory.php b/includes/class-omise-payment-factory.php index 9607a4d9..d4ec0930 100644 --- a/includes/class-omise-payment-factory.php +++ b/includes/class-omise-payment-factory.php @@ -41,7 +41,8 @@ class Omise_Payment_Factory { 'Omise_Payment_DuitNow_QR', 'Omise_Payment_DuitNow_OBW', 'Omise_Payment_Atome', - 'Omise_Payment_PayPay' + 'Omise_Payment_PayPay', + 'Omise_Payment_Wechat_Pay', ); /** diff --git a/includes/gateway/class-omise-payment-wechat-pay.php b/includes/gateway/class-omise-payment-wechat-pay.php new file mode 100644 index 00000000..5b2a56be --- /dev/null +++ b/includes/gateway/class-omise-payment-wechat-pay.php @@ -0,0 +1,83 @@ +id = 'omise_wechat_pay'; + $this->has_fields = true; + $this->method_title = __( 'Opn Payments WeChat Pay', 'omise' ); + $this->method_description = wp_kses( + __( 'Accept payment through WeChat Pay via Opn Payments payment gateway.', 'omise' ), + ['strong' => []] + ); + + $this->init_form_fields(); + $this->init_settings(); + + $this->title = $this->get_option( 'title' ); + $this->description = $this->get_option( 'description' ); + $this->restricted_countries = [ 'TH' ]; + $this->source_type = 'wechat_pay'; + + add_action( 'woocommerce_api_' . $this->id . '_callback', 'Omise_Callback::execute' ); + add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); + add_action( 'woocommerce_order_action_' . $this->id . '_sync_payment', array( $this, 'sync_payment' ) ); + } + + /** + * @see WC_Settings_API::init_form_fields() + * @see woocommerce/includes/abstracts/abstract-wc-settings-api.php + */ + public function init_form_fields() + { + $this->form_fields = array( + 'enabled' => array( + 'title' => __('Enable/Disable', 'omise'), + 'type' => 'checkbox', + 'label' => __('Enable Opn Payments WeChat Pay', 'omise'), + 'default' => 'no' + ), + + 'title' => array( + 'title' => __('Title', 'omise'), + 'type' => 'text', + 'description' => __('This controls the title the user sees during checkout.', 'omise'), + 'default' => __('WeChat Pay', 'omise'), + ), + + 'description' => array( + 'title' => __('Description', 'omise'), + 'type' => 'textarea', + 'description' => __('This controls the description the user sees during checkout.', 'omise') + ), + ); + } + + /** + * Get icons + * + * @see WC_Payment_Gateway::get_icon() + */ + public function get_icon() + { + $icon = Omise_Image::get_image([ + 'file' => 'wechat_pay.svg', + 'alternate_text' => 'WeChat Pay', + ]); + + return apply_filters('woocommerce_gateway_icon', $icon, $this->id); + } + + public function charge($order_id, $order) + { + $requestData = $this->build_charge_request( + $order_id, $order, $this->source_type, $this->id . "_callback" + ); + $requestData['source']['ip'] = RequestHelper::get_client_ip(); + return OmiseCharge::create($requestData); + } +} diff --git a/includes/libraries/omise-plugin/helpers/request.php b/includes/libraries/omise-plugin/helpers/request.php index 0bbbc31b..976d1851 100644 --- a/includes/libraries/omise-plugin/helpers/request.php +++ b/includes/libraries/omise-plugin/helpers/request.php @@ -1,5 +1,5 @@ vendor + + + + + + + + diff --git a/tests/unit/includes/gateway/bootstrap-test-setup.php b/tests/unit/includes/gateway/bootstrap-test-setup.php index 952ba0a6..6bdec48f 100644 --- a/tests/unit/includes/gateway/bootstrap-test-setup.php +++ b/tests/unit/includes/gateway/bootstrap-test-setup.php @@ -2,7 +2,6 @@ use PHPUnit\Framework\TestCase; - abstract class Bootstrap_Test_Setup extends TestCase { public $sourceType; diff --git a/tests/unit/includes/gateway/class-omise-payment-wechat-pay-test.php b/tests/unit/includes/gateway/class-omise-payment-wechat-pay-test.php new file mode 100644 index 00000000..648f96ad --- /dev/null +++ b/tests/unit/includes/gateway/class-omise-payment-wechat-pay-test.php @@ -0,0 +1,48 @@ +sourceType = 'wechat_pay'; + + Brain\Monkey\setUp(); + Brain\Monkey\Functions\stubs([ + 'apply_filters' => function () { + return Omise_Image::get_image([ + 'file' => 'wechat_pay.svg', + 'alternate_text' => 'WeChat Pay', + ]); + }, + ]); + + require_once __DIR__ . '/../../../../includes/libraries/omise-plugin/helpers/request.php'; + require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-wechat-pay.php'; + } + + public function test_restricted_countries_field_has_required_countries() + { + $obj = new Omise_Payment_Wechat_Pay(); + $expectedCountries = ['TH']; + + $this->assertEqualsCanonicalizing($expectedCountries, $obj->restricted_countries); + unset($expectedCountries); + } + + public function test_charge() + { + $obj = new Omise_Payment_Wechat_Pay(); + $this->getChargeTest($obj); + } + + public function test_get_icon() + { + $obj = new Omise_Payment_Wechat_Pay(); + $res = $obj->get_icon(); + $expected = "WeChat Pay"; + $this->assertEquals($expected, trim($res)); + } +} diff --git a/tests/unit/includes/libraries/omise-plugin/helpers/request-test.php b/tests/unit/includes/libraries/omise-plugin/helpers/request-test.php new file mode 100644 index 00000000..b235a8a7 --- /dev/null +++ b/tests/unit/includes/libraries/omise-plugin/helpers/request-test.php @@ -0,0 +1,51 @@ +assertEquals($_SERVER[$serverArrKeyToTest], $res); + } + + /** + * Data provider for toSubunitReturnCorrectFormat + */ + public function get_client_ip_data_provider() + { + return [ + ['HTTP_CLIENT_IP'], + ['HTTP_X_FORWARDED_FOR'], + ['HTTP_X_FORWARDED'], + ['HTTP_FORWARDED_FOR'], + ['HTTP_FORWARDED'], + ['REMOTE_ADDR'], + ]; + } +} From a5354f3659f5522b8d4228f234668335db3ca9b9 Mon Sep 17 00:00:00 2001 From: Aashish Gurung <101558497+aashishgurung@users.noreply.github.com> Date: Mon, 4 Mar 2024 14:00:39 +0700 Subject: [PATCH 2/3] Use wc order number metadata (#435) * Order ID sent in charge is replaced with order number * Add missing mock function --------- Co-authored-by: Aashish --- includes/gateway/abstract-omise-payment-base-card.php | 5 +++-- .../gateway/traits/charge-request-builder-trait.php | 11 ++++++----- .../gateway/abstract-omise-payment-base-card-test.php | 2 ++ .../gateway/traits/charge-request-builder-test.php | 2 ++ 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/includes/gateway/abstract-omise-payment-base-card.php b/includes/gateway/abstract-omise-payment-base-card.php index 2707d17d..d61402a4 100644 --- a/includes/gateway/abstract-omise-payment-base-card.php +++ b/includes/gateway/abstract-omise-payment-base-card.php @@ -62,13 +62,14 @@ private function getOmiseCustomerId($user) { private function prepareChargeData($order_id, $order, $omise_customer_id, $card_id, $token) { $currency = $order->get_currency(); + $order_number = $order->get_order_number(); $data = [ 'amount' => Omise_Money::to_subunit($order->get_total(), $currency), 'currency' => $currency, - 'description' => 'WooCommerce Order id ' . $order_id, + 'description' => 'WooCommerce Order id ' . $order_number, 'return_uri' => $this->get_redirect_url('omise_callback', $order_id, $order), 'metadata' => $this->get_metadata( - $order_id, + $order_number, [ 'secure_form_enabled' => $this->getSecureFormState()] ), ]; diff --git a/includes/gateway/traits/charge-request-builder-trait.php b/includes/gateway/traits/charge-request-builder-trait.php index 3ddd1e11..bdc04476 100644 --- a/includes/gateway/traits/charge-request-builder-trait.php +++ b/includes/gateway/traits/charge-request-builder-trait.php @@ -10,13 +10,14 @@ public function build_charge_request( ) { $currency = $order->get_currency(); - $description = 'WooCommerce Order id ' . $order_id; + $order_number = $order->get_order_number(); + $description = 'WooCommerce Order id ' . $order_number; $request = [ 'amount' => Omise_Money::to_subunit($order->get_total(), $currency), 'currency' => $currency, 'description' => $description, - 'metadata' => $this->get_metadata($order_id), + 'metadata' => $this->get_metadata($order_number), 'source' => [ 'type' => $source_type ] ]; @@ -40,13 +41,13 @@ public function build_charge_request( } /** - * @param string $order_id + * @param string $order_number * @param array $additionalData */ - public function get_metadata($order_id, $additionalData = []) + public function get_metadata($order_number, $additionalData = []) { // override order_id as a reference for webhook handlers. - $orderId = [ 'order_id' => $order_id ]; + $orderId = [ 'order_id' => $order_number ]; return array_merge($orderId, $additionalData); } diff --git a/tests/unit/includes/gateway/abstract-omise-payment-base-card-test.php b/tests/unit/includes/gateway/abstract-omise-payment-base-card-test.php index cff4a717..3a70ebdf 100644 --- a/tests/unit/includes/gateway/abstract-omise-payment-base-card-test.php +++ b/tests/unit/includes/gateway/abstract-omise-payment-base-card-test.php @@ -62,6 +62,8 @@ public function getOrderMock($expectedAmount, $expectedCurrency) ->andReturn($expectedAmount); // in units $orderMock->shouldReceive('add_meta_data') ->andReturn(['order_id' => 'order_123']); + $orderMock->shouldReceive('get_order_number') + ->andReturn(1234); $orderMock->shouldReceive('get_user') ->andReturn((object)[ 'ID' => 'user_123', 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 3ffe5cc4..3366dcea 100644 --- a/tests/unit/includes/gateway/traits/charge-request-builder-test.php +++ b/tests/unit/includes/gateway/traits/charge-request-builder-test.php @@ -34,6 +34,8 @@ public function getOrderMock($expectedAmount, $expectedCurrency) $orderMock->shouldReceive('get_total') ->andReturn($expectedAmount); // in units $orderMock->shouldReceive('add_meta_data'); + $orderMock->shouldReceive('get_order_number') + ->andReturn(1234); return $orderMock; } From 78b383c46a433d656fdd6872c474e3b61e7be12a Mon Sep 17 00:00:00 2001 From: Aashish Date: Tue, 5 Mar 2024 09:55:06 +0700 Subject: [PATCH 3/3] Update metadata for v5.8.0. --- CHANGELOG.md | 4 ++++ omise-woocommerce.php | 4 ++-- readme.txt | 7 ++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d5072d9..3817c622 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## [v5.8.0 _(Mar 5, 2024)_](https://github.com/omise/omise-woocommerce/releases/tag/v5.8.0) +- Add WeChat Pay. (PR [#434](https://github.com/omise/omise-woocommerce/pull/434)) +- Use WC order number metadata. (PR [#435](https://github.com/omise/omise-woocommerce/pull/435)) + ## [v5.7.0 _(Jan 11, 2024)_](https://github.com/omise/omise-woocommerce/releases/tag/v5.7.0) - Added TrueMoney jumpapp. (PR [#431](https://github.com/omise/omise-woocommerce/pull/431)) - Updated README.md. (PR [#429](https://github.com/omise/omise-woocommerce/pull/429)) diff --git a/omise-woocommerce.php b/omise-woocommerce.php index fd495f3d..d97c1d9d 100644 --- a/omise-woocommerce.php +++ b/omise-woocommerce.php @@ -4,7 +4,7 @@ * Plugin Name: Opn Payments * Plugin URI: https://www.omise.co/woocommerce * Description: Opn Payments is a WordPress plugin designed specifically for WooCommerce. The plugin adds support for Opn Payments Payment Gateway's payment methods to WooCommerce. - * Version: 5.7.0 + * Version: 5.8.0 * Author: Opn Payments and contributors * Author URI: https://github.com/omise/omise-woocommerce/graphs/contributors * Text Domain: omise @@ -22,7 +22,7 @@ class Omise * * @var string */ - public $version = '5.7.0'; + public $version = '5.8.0'; /** * The Omise Instance. diff --git a/readme.txt b/readme.txt index 3b460e29..1de7f2b1 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: Opn Payments 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.2 -Stable tag: 5.7.0 +Stable tag: 5.8.0 License: MIT License URI: https://opensource.org/licenses/MIT @@ -34,6 +34,11 @@ From there: == Changelog == += 5.8.0 = + +- Add WeChat Pay. (PR [#434](https://github.com/omise/omise-woocommerce/pull/434)) +- Use WC order number metadata. (PR [#435](https://github.com/omise/omise-woocommerce/pull/435)) + = 5.7.0 = - Added TrueMoney jumpapp. (PR [#431](https://github.com/omise/omise-woocommerce/pull/431))