From 0ea8da6d9456538a4738d6409550f3657f83db04 Mon Sep 17 00:00:00 2001 From: guzzilar Date: Wed, 29 May 2019 13:49:32 +0700 Subject: [PATCH 1/9] Implementing Backend classes to handle each individual payment method's information. --- composer.json | 9 ++ .../class-omise-backend-installment.php | 123 ++++++++++++++ includes/backends/class-omise-backend.php | 25 +++ includes/class-omise-capabilities.php | 7 + omise-woocommerce.php | 2 + phpunit.xml | 18 +++ tests/unit/class-omise-unit-test.php | 16 ++ .../class-omise-backend-installment-test.php | 151 ++++++++++++++++++ 8 files changed, 351 insertions(+) create mode 100644 composer.json create mode 100644 includes/backends/class-omise-backend-installment.php create mode 100644 includes/backends/class-omise-backend.php create mode 100644 phpunit.xml create mode 100644 tests/unit/class-omise-unit-test.php create mode 100644 tests/unit/includes/backends/class-omise-backend-installment-test.php diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..704bb843 --- /dev/null +++ b/composer.json @@ -0,0 +1,9 @@ +{ + "name": "omise/omise-woocommerce", + "description": "An official payment extension which provides support for Omise payment gateway for store builders working on the WooCommerce platform.", + "homepage": "https://www.omise.co/", + "license": "MIT", + "require-dev": { + "phpunit/phpunit": "^8.1" + } +} diff --git a/includes/backends/class-omise-backend-installment.php b/includes/backends/class-omise-backend-installment.php new file mode 100644 index 00000000..672a20a0 --- /dev/null +++ b/includes/backends/class-omise-backend-installment.php @@ -0,0 +1,123 @@ + array( + 'bank_code' => 'first_choice', + 'title' => __( 'Krungsri First Choice', 'omise' ), + 'interest_rate' => 0.013, + 'min_allowed_amount' => 300.00, + ), + + 'installment_bay' => array( + 'bank_code' => 'bay', + 'title' => __( 'Krungsri', 'omise' ), + 'interest_rate' => 0.008, + 'min_allowed_amount' => 300.00, + ), + + 'installment_ktc' => array( + 'bank_code' => 'ktc', + 'title' => __( 'Krungthai Card (KTC)', 'omise' ), + 'interest_rate' => 0.008, + 'min_allowed_amount' => 300.00, + ), + + 'installment_bbl' => array( + 'bank_code' => 'bbl', + 'title' => __( 'Bangkok Bank', 'omise' ), + 'interest_rate' => 0.008, + 'min_allowed_amount' => 500.00, + ), + + 'installment_kbank' => array( + 'bank_code' => 'kbank', + 'title' => __( 'Kasikorn Bank', 'omise' ), + 'interest_rate' => 0.0065, + 'min_allowed_amount' => 500.00, + ), + ); + } + + /** + * @param string $currency + * @param float $purchase_amount + * + * @return array of an available installment providers + */ + public function get_available_providers( $currency, $purchase_amount ) { + // Note: as installment payment at the moment only supports for THB currency, so the + // $purchase_amount is multiplied with 100 to convert the amount into subunit (satang). + $providers = $this->capabilities()->getInstallmentBackends( $currency, ( $purchase_amount * 100 ) ); + + foreach ( $providers as &$provider ) { + $provider->provider_code = str_replace( 'installment_', '', $provider->_id ); + $provider->provider_name = isset( self::$providers[ $provider->_id ] ) ? self::$providers[ $provider->_id ]['title'] : strtoupper( $provider->code ); + $provider->allowed_installment_terms = $this->filter_available_terms( $provider->allowed_installment_terms, self::$providers[ $provider->_id ], $purchase_amount ); + } + + return $providers; + } + + /** + * @param array $available_terms + * @param array $provider_detail + * @param float $purchase_amount + * + * @return array of an filtered available terms + */ + public function filter_available_terms( $available_terms, $provider_detail, $purchase_amount ) { + $filtered_available_terms = array(); + + sort( $available_terms ); + + for ( $i = 0; $i < count( $available_terms ); $i++ ) { + $term = $available_terms[ $i ]; + $monthly_amount = $this->calculate_monthly_payment_amount( + $purchase_amount, + $term, + $this->capabilities()->is_zero_interest() ? 0 : $provider_detail['interest_rate'] + ); + + if ( $monthly_amount < $provider_detail['min_allowed_amount'] ) { + break; + } + + $filtered_available_terms[ $i ] = array( + 'term' => $term, + 'monthly_amount' => $monthly_amount + ); + } + + return $filtered_available_terms; + } + + /** + * @param float $purchase_amount + * @param int $term installment term. + * @param float $interest_rate its value can be '0' if merchant absorbs an interest. + * + * @return float of a installment monthly payment (round up to 2 decimals). + */ + public function calculate_monthly_payment_amount( $purchase_amount, $term, $interest_rate ) { + $interest = $purchase_amount * $term * $interest_rate; + return round( ( $purchase_amount + $interest ) / $term, 2 ); + } +} diff --git a/includes/backends/class-omise-backend.php b/includes/backends/class-omise-backend.php new file mode 100644 index 00000000..fabf9356 --- /dev/null +++ b/includes/backends/class-omise-backend.php @@ -0,0 +1,25 @@ +initiate(); + } + + /** + * Class initiation. + * + * @return void + */ + public function initiate() { + return; + } + + /** + * @return Omise_Capabilities Instant. + */ + public function capabilities() { + return Omise_Capabilities::retrieve(); + } +} diff --git a/includes/class-omise-capabilities.php b/includes/class-omise-capabilities.php index 218f50cb..41e9eaee 100644 --- a/includes/class-omise-capabilities.php +++ b/includes/class-omise-capabilities.php @@ -45,4 +45,11 @@ public function getInstallmentBackends( $currency = '', $amount = null ) { return $this->capabilities->getBackends( $params ); } + + /** + * @return bool True if merchant absorbs an interest or else, false. + */ + public function is_zero_interest() { + return $this->capabilities['zero_interest_installments']; + } } diff --git a/omise-woocommerce.php b/omise-woocommerce.php index 49dcf163..146a3b3e 100644 --- a/omise-woocommerce.php +++ b/omise-woocommerce.php @@ -117,6 +117,8 @@ private function define_constants() { private function include_classes() { defined( 'OMISE_WOOCOMMERCE_PLUGIN_PATH' ) || define( 'OMISE_WOOCOMMERCE_PLUGIN_PATH', __DIR__ ); + require_once OMISE_WOOCOMMERCE_PLUGIN_PATH . '/includes/backends/class-omise-backend.php'; + require_once OMISE_WOOCOMMERCE_PLUGIN_PATH . '/includes/backends/class-omise-backend-installment.php'; require_once OMISE_WOOCOMMERCE_PLUGIN_PATH . '/includes/classes/class-omise-charge.php'; require_once OMISE_WOOCOMMERCE_PLUGIN_PATH . '/includes/classes/class-omise-card-image.php'; require_once OMISE_WOOCOMMERCE_PLUGIN_PATH . '/includes/events/class-omise-event-charge-capture.php'; diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 00000000..3e35e659 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,18 @@ + + + + tests/unit + + + diff --git a/tests/unit/class-omise-unit-test.php b/tests/unit/class-omise-unit-test.php new file mode 100644 index 00000000..85a59684 --- /dev/null +++ b/tests/unit/class-omise-unit-test.php @@ -0,0 +1,16 @@ + 'bbl', + 'title' => __( 'Bangkok Bank', 'omise' ), + 'interest_rate' => 0.008, + 'min_allowed_amount' => 500.00, + ); + + $result = $installment_backend->filter_available_terms( $available_terms, $provider_detail, $purchase_amount ); + + $this->assertEquals( 3, count( $result ) ); + $this->assertEquals( array( + array( 'term' => 3, 'monthly_amount' => 1024.00 ), + array( 'term' => 4, 'monthly_amount' => 774.00 ), + array( 'term' => 6, 'monthly_amount' => 524.00 ), + ), $result ); + } + + /** + * @test + */ + public function making_sure_that_we_can_filter_terms_properly_even_available_terms_is_not_in_a_proper_order() { + $installment_backend = new Omise_Backend_Installment(); + $purchase_amount = 3000.00; + $available_terms = array( 3, 10, 4, 8, 6 ); + $provider_detail = array( + 'bank_code' => 'bbl', + 'title' => __( 'Bangkok Bank', 'omise' ), + 'interest_rate' => 0.008, + 'min_allowed_amount' => 500.00, + ); + + $result = $installment_backend->filter_available_terms( $available_terms, $provider_detail, $purchase_amount ); + + $this->assertEquals( 3, count( $result ) ); + $this->assertEquals( array( + array( 'term' => 3, 'monthly_amount' => 1024.00 ), + array( 'term' => 4, 'monthly_amount' => 774.00 ), + array( 'term' => 6, 'monthly_amount' => 524.00 ), + ), $result ); + } + + /** + * @test + */ + public function correctly_calculating_monthly_payment_amount_as_buyer_absorbs_case_1() { + $installment_backend = new Omise_Backend_Installment(); + $purchase_amount = 10000.00; + $term = 10; + $interest_rate = 0.008; + + $result = $installment_backend->calculate_monthly_payment_amount( $purchase_amount, $term, $interest_rate ); + + $this->assertEquals( 1080.00, $result ); + } + + /** + * @test + */ + public function correctly_calculating_monthly_payment_amount_as_buyer_absorbs_case_2() { + $installment_backend = new Omise_Backend_Installment(); + $purchase_amount = 17900.00; + $term = 6; + $interest_rate = 0.0069; + + $result = $installment_backend->calculate_monthly_payment_amount( $purchase_amount, $term, $interest_rate ); + + $this->assertEquals( 3106.84, $result ); + } + + /** + * @test + */ + public function correctly_calculating_monthly_payment_amount_as_buyer_absorbs_case_3() { + $installment_backend = new Omise_Backend_Installment(); + $purchase_amount = 5000.00; + $term = 10; + $interest_rate = 0.008; + + $result = $installment_backend->calculate_monthly_payment_amount( $purchase_amount, $term, $interest_rate ); + + $this->assertEquals( 540.00, $result ); + } + + /** + * @test + */ + public function correctly_calculating_monthly_payment_amount_as_merchant_absorbs_case_1() { + $installment_backend = new Omise_Backend_Installment(); + $purchase_amount = 10000.00; + $term = 10; + $interest_rate = 0; + + $result = $installment_backend->calculate_monthly_payment_amount( $purchase_amount, $term, $interest_rate ); + + $this->assertEquals( 1000.00, $result ); + } + + /** + * @test + */ + public function correctly_calculating_monthly_payment_amount_as_merchant_absorbs_case_2() { + $installment_backend = new Omise_Backend_Installment(); + $purchase_amount = 5000.00; + $term = 6; + $interest_rate = 0; + + $result = $installment_backend->calculate_monthly_payment_amount( $purchase_amount, $term, $interest_rate ); + + $this->assertEquals( 833.33, $result ); + } +} + +/** + * Mock Omise_Capabilities class. + * NOTE: This might not be an ideal way to mock a class, + * feel free to enhance the test or the core code. + * + * @see includes/class-omise-capabilities + */ +class Omise_Capabilities { + /** + * @var self + */ + protected static $the_instance = null; + + public static function retrieve() { + self::$the_instance = new self(); + return self::$the_instance; + } + + public function is_zero_interest() { + return false; + } +} From 6df8c7b33563da784e7fca0cc5d0e523828274c7 Mon Sep 17 00:00:00 2001 From: guzzilar Date: Wed, 29 May 2019 15:21:24 +0700 Subject: [PATCH 2/9] Implementing Backend class to Omise_Payment_Installment. --- .../class-omise-payment-installment.php | 25 ++++++------------- includes/gateway/class-omise-payment.php | 7 ++++++ templates/payment/form-installment.php | 4 +-- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/includes/gateway/class-omise-payment-installment.php b/includes/gateway/class-omise-payment-installment.php index 90ae2a3a..287669ab 100644 --- a/includes/gateway/class-omise-payment-installment.php +++ b/includes/gateway/class-omise-payment-installment.php @@ -33,6 +33,8 @@ public function __construct() { $this->title = $this->get_option( 'title' ); $this->description = $this->get_option( 'description' ); + $this->backend = new Omise_Backend_Installment; + add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); add_action( 'woocommerce_api_' . $this->id . '_callback', array( $this, 'callback' ) ); } @@ -69,27 +71,14 @@ public function init_form_fields() { * @inheritdoc */ public function payment_fields() { - $provider_names = array( - 'installment_bay' => __( 'Krungsri', 'omise' ), - 'installment_first_choice' => __( 'Krungsri First Choice', 'omise' ), - 'installment_kbank' => __( 'Kasikorn Bank', 'omise' ), - 'installment_bbl' => __( 'Bangkok Bank', 'omise' ), - 'installment_ktc' => __( 'Krungthai Card (KTC)', 'omise' ), - ); - - $currency = get_woocommerce_currency(); - $cart_total = WC()->cart->total; - $capabilities = Omise_Capabilities::retrieve(); - $installment_backends = $capabilities->getInstallmentBackends( $currency, $this->format_amount_subunit( $cart_total, $currency ) ); - - foreach ( $installment_backends as &$backend ) { - $backend->provider_code = str_replace( 'installment_', '', $backend->_id ); - $backend->provider_name = isset( $provider_names[ $backend->_id ] ) ? $provider_names[ $backend->_id ] : strtoupper( $backend->provider_code ); - } + $currency = get_woocommerce_currency(); + $cart_total = WC()->cart->total; Omise_Util::render_view( 'templates/payment/form-installment.php', - array( 'installment_backends' => $installment_backends ) + array( + 'installment_backends' => $this->backend->get_available_providers( $currency, $cart_total ) + ) ); } diff --git a/includes/gateway/class-omise-payment.php b/includes/gateway/class-omise-payment.php index a2c823c4..97817d38 100644 --- a/includes/gateway/class-omise-payment.php +++ b/includes/gateway/class-omise-payment.php @@ -27,6 +27,13 @@ abstract class Omise_Payment extends WC_Payment_Gateway { */ public $id = 'omise'; + /** + * @since 3.4 + * + * @var \Omise_Backend + */ + protected $backend; + /** * @see omise/includes/class-omise-setting.php * diff --git a/templates/payment/form-installment.php b/templates/payment/form-installment.php index 56f8fef6..66f5d779 100644 --- a/templates/payment/form-installment.php +++ b/templates/payment/form-installment.php @@ -10,8 +10,8 @@ provider_name; ?>
From b227959066fc81fb830a28bbb6aa8727b6ff71f0 Mon Sep 17 00:00:00 2001 From: guzzilar Date: Wed, 29 May 2019 16:33:29 +0700 Subject: [PATCH 3/9] Displaing installment interest fee at the checkout page (for Installment payment method). --- assets/css/omise-css.css | 23 +++++++++++++++++-- .../class-omise-backend-installment.php | 1 + .../class-omise-payment-installment.php | 3 ++- templates/payment/form-installment.php | 10 +++++++- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/assets/css/omise-css.css b/assets/css/omise-css.css index bf89f39c..9f494ee2 100644 --- a/assets/css/omise-css.css +++ b/assets/css/omise-css.css @@ -37,6 +37,16 @@ fieldset.card-exists { font-size: 80%; } +#payment .payment_methods li .payment_box fieldset#omise-form-installment .omise-buttom-note { + margin-top: 4em; +} + +#payment .payment_methods li .payment_box fieldset#omise-form-installment .omise-installment-interest-rate { + color: #aaa; + font-size: 80%; + line-height: 1; +} + /** * 2). Components @@ -51,7 +61,16 @@ fieldset.card-exists { } /** - * 2.2). Form, bank list components + * 2.2). Components + */ +.omise-buttom-note { + margin-top: 2em; + color: #aaa; + font-size: 80%; +} + +/** + * 2.3). Form, bank list components */ ul.omise-banks-list { margin: 0; @@ -149,7 +168,7 @@ ul.omise-banks-list { } /** - * 2.3). Bank logos + * 2.4). Bank logos */ /** BAY **/ .bank-logo.bay { diff --git a/includes/backends/class-omise-backend-installment.php b/includes/backends/class-omise-backend-installment.php index 672a20a0..d68a0d80 100644 --- a/includes/backends/class-omise-backend-installment.php +++ b/includes/backends/class-omise-backend-installment.php @@ -71,6 +71,7 @@ public function get_available_providers( $currency, $purchase_amount ) { $provider->provider_code = str_replace( 'installment_', '', $provider->_id ); $provider->provider_name = isset( self::$providers[ $provider->_id ] ) ? self::$providers[ $provider->_id ]['title'] : strtoupper( $provider->code ); $provider->allowed_installment_terms = $this->filter_available_terms( $provider->allowed_installment_terms, self::$providers[ $provider->_id ], $purchase_amount ); + $provider->interest = $this->capabilities()->is_zero_interest() ? 0 : ( self::$providers[ $provider->_id ]['interest_rate'] * 100 ); } return $providers; diff --git a/includes/gateway/class-omise-payment-installment.php b/includes/gateway/class-omise-payment-installment.php index 287669ab..7e9ae95d 100644 --- a/includes/gateway/class-omise-payment-installment.php +++ b/includes/gateway/class-omise-payment-installment.php @@ -77,7 +77,8 @@ public function payment_fields() { Omise_Util::render_view( 'templates/payment/form-installment.php', array( - 'installment_backends' => $this->backend->get_available_providers( $currency, $cart_total ) + 'installment_backends' => $this->backend->get_available_providers( $currency, $cart_total ), + 'is_zero_interest' => $this->backend->capabilities()->is_zero_interest() ) ); } diff --git a/templates/payment/form-installment.php b/templates/payment/form-installment.php index 66f5d779..1a3b6eac 100644 --- a/templates/payment/form-installment.php +++ b/templates/payment/form-installment.php @@ -11,14 +11,22 @@ + +
( interest; ?>% ) + +
+

+ +

+

From 312029c4fbf3ec59dc623381047174739474657e Mon Sep 17 00:00:00 2001 From: guzzilar Date: Wed, 5 Jun 2019 18:39:09 +0800 Subject: [PATCH 4/9] Applying & addressing all issues from the feedbacks at the code review, PR #115. --- .../class-omise-backend-installment.php | 12 +++++------ templates/payment/form-installment.php | 20 +++++++++++++++++-- .../class-omise-backend-installment-test.php | 12 +++++------ 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/includes/backends/class-omise-backend-installment.php b/includes/backends/class-omise-backend-installment.php index d68a0d80..16c32783 100644 --- a/includes/backends/class-omise-backend-installment.php +++ b/includes/backends/class-omise-backend-installment.php @@ -8,7 +8,7 @@ * * @method public initiate * @method public get_available_providers - * @method public filter_available_terms + * @method public get_valid_terms * @method public calculate_monthly_payment_amount */ class Omise_Backend_Installment extends Omise_Backend { @@ -70,7 +70,7 @@ public function get_available_providers( $currency, $purchase_amount ) { foreach ( $providers as &$provider ) { $provider->provider_code = str_replace( 'installment_', '', $provider->_id ); $provider->provider_name = isset( self::$providers[ $provider->_id ] ) ? self::$providers[ $provider->_id ]['title'] : strtoupper( $provider->code ); - $provider->allowed_installment_terms = $this->filter_available_terms( $provider->allowed_installment_terms, self::$providers[ $provider->_id ], $purchase_amount ); + $provider->allowed_installment_terms = $this->get_valid_terms( $provider->allowed_installment_terms, self::$providers[ $provider->_id ], $purchase_amount ); $provider->interest = $this->capabilities()->is_zero_interest() ? 0 : ( self::$providers[ $provider->_id ]['interest_rate'] * 100 ); } @@ -84,13 +84,13 @@ public function get_available_providers( $currency, $purchase_amount ) { * * @return array of an filtered available terms */ - public function filter_available_terms( $available_terms, $provider_detail, $purchase_amount ) { + public function get_valid_terms( $available_terms, $provider_detail, $purchase_amount ) { $filtered_available_terms = array(); sort( $available_terms ); - for ( $i = 0; $i < count( $available_terms ); $i++ ) { - $term = $available_terms[ $i ]; + foreach ( $available_terms as $key => $available_term ) { + $term = $available_terms[ $key ]; $monthly_amount = $this->calculate_monthly_payment_amount( $purchase_amount, $term, @@ -101,7 +101,7 @@ public function filter_available_terms( $available_terms, $provider_detail, $pur break; } - $filtered_available_terms[ $i ] = array( + $filtered_available_terms[ $key ] = array( 'term' => $term, 'monthly_amount' => $monthly_amount ); diff --git a/templates/payment/form-installment.php b/templates/payment/form-installment.php index 1a3b6eac..76da4705 100644 --- a/templates/payment/form-installment.php +++ b/templates/payment/form-installment.php @@ -11,11 +11,27 @@ -
( interest; ?>% ) +
+ interest ); ?> + diff --git a/tests/unit/includes/backends/class-omise-backend-installment-test.php b/tests/unit/includes/backends/class-omise-backend-installment-test.php index e7176d76..f92719de 100644 --- a/tests/unit/includes/backends/class-omise-backend-installment-test.php +++ b/tests/unit/includes/backends/class-omise-backend-installment-test.php @@ -11,10 +11,10 @@ public static function setUpBeforeClass(): void { /** * @test */ - public function filtering_bbl_installment_term() { + public function get_only_valid_terms_from_given_bbl_allowed_installment_terms() { $installment_backend = new Omise_Backend_Installment(); $purchase_amount = 3000.00; - $available_terms = array( 3, 4, 6, 8, 10 ); + $allowed_terms = array( 3, 4, 6, 8, 10 ); $provider_detail = array( 'bank_code' => 'bbl', 'title' => __( 'Bangkok Bank', 'omise' ), @@ -22,7 +22,7 @@ public function filtering_bbl_installment_term() { 'min_allowed_amount' => 500.00, ); - $result = $installment_backend->filter_available_terms( $available_terms, $provider_detail, $purchase_amount ); + $result = $installment_backend->get_valid_terms( $allowed_terms, $provider_detail, $purchase_amount ); $this->assertEquals( 3, count( $result ) ); $this->assertEquals( array( @@ -35,7 +35,7 @@ public function filtering_bbl_installment_term() { /** * @test */ - public function making_sure_that_we_can_filter_terms_properly_even_available_terms_is_not_in_a_proper_order() { + public function get_only_valid_terms_from_given_bbl_unsorted_allowed_installment_terms() { $installment_backend = new Omise_Backend_Installment(); $purchase_amount = 3000.00; $available_terms = array( 3, 10, 4, 8, 6 ); @@ -46,7 +46,7 @@ public function making_sure_that_we_can_filter_terms_properly_even_available_ter 'min_allowed_amount' => 500.00, ); - $result = $installment_backend->filter_available_terms( $available_terms, $provider_detail, $purchase_amount ); + $result = $installment_backend->get_valid_terms( $available_terms, $provider_detail, $purchase_amount ); $this->assertEquals( 3, count( $result ) ); $this->assertEquals( array( @@ -141,7 +141,7 @@ class Omise_Capabilities { protected static $the_instance = null; public static function retrieve() { - self::$the_instance = new self(); + self::$the_instance = self::$the_instance ?: new self(); return self::$the_instance; } From 810520a7bc33adbaf2b17fbc98c065da75fc9fdd Mon Sep 17 00:00:00 2001 From: guzzilar Date: Thu, 6 Jun 2019 14:16:51 +0800 Subject: [PATCH 5/9] Omise_Backend_Installment::get_valid_terms(), leaning the method (more efficient). --- includes/backends/class-omise-backend-installment.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/includes/backends/class-omise-backend-installment.php b/includes/backends/class-omise-backend-installment.php index 16c32783..936bf2e5 100644 --- a/includes/backends/class-omise-backend-installment.php +++ b/includes/backends/class-omise-backend-installment.php @@ -85,12 +85,11 @@ public function get_available_providers( $currency, $purchase_amount ) { * @return array of an filtered available terms */ public function get_valid_terms( $available_terms, $provider_detail, $purchase_amount ) { - $filtered_available_terms = array(); + $valid_terms = array(); sort( $available_terms ); - foreach ( $available_terms as $key => $available_term ) { - $term = $available_terms[ $key ]; + foreach ( $available_terms as $term ) { $monthly_amount = $this->calculate_monthly_payment_amount( $purchase_amount, $term, @@ -101,13 +100,13 @@ public function get_valid_terms( $available_terms, $provider_detail, $purchase_a break; } - $filtered_available_terms[ $key ] = array( + $valid_terms[] = array( 'term' => $term, 'monthly_amount' => $monthly_amount ); } - return $filtered_available_terms; + return $valid_terms; } /** From 24cb3855ad524b74ab63bfe6563e10ba98ee42ad Mon Sep 17 00:00:00 2001 From: guzzilar Date: Thu, 6 Jun 2019 17:57:19 +0800 Subject: [PATCH 6/9] Applying & addressing all issues from the second feedbacks at the code review, PR #115. --- .../class-omise-backend-installment.php | 54 ++++++++++--------- includes/class-omise-capabilities.php | 2 +- templates/payment/form-installment.php | 10 ++-- .../class-omise-backend-installment-test.php | 40 ++++++-------- 4 files changed, 51 insertions(+), 55 deletions(-) diff --git a/includes/backends/class-omise-backend-installment.php b/includes/backends/class-omise-backend-installment.php index 936bf2e5..40ca95f8 100644 --- a/includes/backends/class-omise-backend-installment.php +++ b/includes/backends/class-omise-backend-installment.php @@ -1,6 +1,6 @@ capabilities()->getInstallmentBackends( $currency, ( $purchase_amount * 100 ) ); foreach ( $providers as &$provider ) { - $provider->provider_code = str_replace( 'installment_', '', $provider->_id ); - $provider->provider_name = isset( self::$providers[ $provider->_id ] ) ? self::$providers[ $provider->_id ]['title'] : strtoupper( $provider->code ); - $provider->allowed_installment_terms = $this->get_valid_terms( $provider->allowed_installment_terms, self::$providers[ $provider->_id ], $purchase_amount ); - $provider->interest = $this->capabilities()->is_zero_interest() ? 0 : ( self::$providers[ $provider->_id ]['interest_rate'] * 100 ); + $provider_detail = self::$providers[ $provider->_id ]; + + $provider->provider_code = str_replace( 'installment_', '', $provider->_id ); + $provider->provider_name = isset( $provider_detail ) ? $provider_detail['title'] : strtoupper( $provider->code ); + $provider->interest_rate = $this->capabilities()->is_zero_interest() ? 0 : ( $provider_detail['interest_rate'] * 100 ); + $provider->available_plans = $this->get_available_plans( + $purchase_amount, + $provider->allowed_installment_terms, + $provider->interest_rate, + $provider_detail['min_allowed_amount'] + ); } return $providers; } /** - * @param array $available_terms - * @param array $provider_detail * @param float $purchase_amount + * @param array $allowed_installment_terms + * @param float $interest_rate + * @param float $min_allowed_amount * * @return array of an filtered available terms */ - public function get_valid_terms( $available_terms, $provider_detail, $purchase_amount ) { - $valid_terms = array(); + public function get_available_plans( $purchase_amount, $allowed_installment_terms, $interest_rate, $min_allowed_amount ) { + $plans = array(); - sort( $available_terms ); + sort( $allowed_installment_terms ); - foreach ( $available_terms as $term ) { - $monthly_amount = $this->calculate_monthly_payment_amount( - $purchase_amount, - $term, - $this->capabilities()->is_zero_interest() ? 0 : $provider_detail['interest_rate'] - ); + foreach ( $allowed_installment_terms as $term_length ) { + $monthly_amount = $this->calculate_monthly_payment_amount( $purchase_amount, $term_length, $interest_rate ); - if ( $monthly_amount < $provider_detail['min_allowed_amount'] ) { + if ( $monthly_amount < $min_allowed_amount ) { break; } - $valid_terms[] = array( - 'term' => $term, + $plans[] = array( + 'term_length' => $term_length, 'monthly_amount' => $monthly_amount ); } - return $valid_terms; + return $plans; } /** * @param float $purchase_amount - * @param int $term installment term. + * @param int $term_length A length of a given installment term. * @param float $interest_rate its value can be '0' if merchant absorbs an interest. * * @return float of a installment monthly payment (round up to 2 decimals). */ - public function calculate_monthly_payment_amount( $purchase_amount, $term, $interest_rate ) { - $interest = $purchase_amount * $term * $interest_rate; - return round( ( $purchase_amount + $interest ) / $term, 2 ); + public function calculate_monthly_payment_amount( $purchase_amount, $term_length, $interest_rate ) { + $interest = $purchase_amount * $term_length * $interest_rate; + return round( ( $purchase_amount + $interest ) / $term_length, 2 ); } } diff --git a/includes/class-omise-capabilities.php b/includes/class-omise-capabilities.php index 41e9eaee..2dd0937d 100644 --- a/includes/class-omise-capabilities.php +++ b/includes/class-omise-capabilities.php @@ -47,7 +47,7 @@ public function getInstallmentBackends( $currency = '', $amount = null ) { } /** - * @return bool True if merchant absorbs an interest or else, false. + * @return bool True if merchant absorbs the interest or else, false. */ public function is_zero_interest() { return $this->capabilities['zero_interest_installments']; diff --git a/templates/payment/form-installment.php b/templates/payment/form-installment.php index 76da4705..1a10a990 100644 --- a/templates/payment/form-installment.php +++ b/templates/payment/form-installment.php @@ -10,19 +10,19 @@ provider_name; ?>

- interest ); ?> + interest_rate ); ?> diff --git a/tests/unit/includes/backends/class-omise-backend-installment-test.php b/tests/unit/includes/backends/class-omise-backend-installment-test.php index f92719de..02b901a1 100644 --- a/tests/unit/includes/backends/class-omise-backend-installment-test.php +++ b/tests/unit/includes/backends/class-omise-backend-installment-test.php @@ -11,48 +11,40 @@ public static function setUpBeforeClass(): void { /** * @test */ - public function get_only_valid_terms_from_given_bbl_allowed_installment_terms() { + public function get_only_valid_plans_from_given_bbl_allowed_installment_terms() { $installment_backend = new Omise_Backend_Installment(); $purchase_amount = 3000.00; - $allowed_terms = array( 3, 4, 6, 8, 10 ); - $provider_detail = array( - 'bank_code' => 'bbl', - 'title' => __( 'Bangkok Bank', 'omise' ), - 'interest_rate' => 0.008, - 'min_allowed_amount' => 500.00, - ); + $allowed_terms = array( 3, 4, 6, 8, 10 ); + $interest_rate = 0.008; + $min_allowed_amount = 500.00; - $result = $installment_backend->get_valid_terms( $allowed_terms, $provider_detail, $purchase_amount ); + $result = $installment_backend->get_available_plans( $purchase_amount, $allowed_terms, $interest_rate, $min_allowed_amount ); $this->assertEquals( 3, count( $result ) ); $this->assertEquals( array( - array( 'term' => 3, 'monthly_amount' => 1024.00 ), - array( 'term' => 4, 'monthly_amount' => 774.00 ), - array( 'term' => 6, 'monthly_amount' => 524.00 ), + array( 'term_length' => 3, 'monthly_amount' => 1024.00 ), + array( 'term_length' => 4, 'monthly_amount' => 774.00 ), + array( 'term_length' => 6, 'monthly_amount' => 524.00 ), ), $result ); } /** * @test */ - public function get_only_valid_terms_from_given_bbl_unsorted_allowed_installment_terms() { + public function get_only_valid_plans_from_given_bbl_unsorted_allowed_installment_terms() { $installment_backend = new Omise_Backend_Installment(); $purchase_amount = 3000.00; - $available_terms = array( 3, 10, 4, 8, 6 ); - $provider_detail = array( - 'bank_code' => 'bbl', - 'title' => __( 'Bangkok Bank', 'omise' ), - 'interest_rate' => 0.008, - 'min_allowed_amount' => 500.00, - ); + $allowed_terms = array( 3, 10, 4, 8, 6 ); + $interest_rate = 0.008; + $min_allowed_amount = 500.00; - $result = $installment_backend->get_valid_terms( $available_terms, $provider_detail, $purchase_amount ); + $result = $installment_backend->get_available_plans( $purchase_amount, $allowed_terms, $interest_rate, $min_allowed_amount ); $this->assertEquals( 3, count( $result ) ); $this->assertEquals( array( - array( 'term' => 3, 'monthly_amount' => 1024.00 ), - array( 'term' => 4, 'monthly_amount' => 774.00 ), - array( 'term' => 6, 'monthly_amount' => 524.00 ), + array( 'term_length' => 3, 'monthly_amount' => 1024.00 ), + array( 'term_length' => 4, 'monthly_amount' => 774.00 ), + array( 'term_length' => 6, 'monthly_amount' => 524.00 ), ), $result ); } From 44da8e89d23362832ff0bc992a1ed37cc717a1a4 Mon Sep 17 00:00:00 2001 From: guzzilar Date: Thu, 6 Jun 2019 18:01:24 +0800 Subject: [PATCH 7/9] Code styling. --- includes/backends/class-omise-backend-installment.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/backends/class-omise-backend-installment.php b/includes/backends/class-omise-backend-installment.php index 40ca95f8..2502a8a5 100644 --- a/includes/backends/class-omise-backend-installment.php +++ b/includes/backends/class-omise-backend-installment.php @@ -70,10 +70,10 @@ public function get_available_providers( $currency, $purchase_amount ) { foreach ( $providers as &$provider ) { $provider_detail = self::$providers[ $provider->_id ]; - $provider->provider_code = str_replace( 'installment_', '', $provider->_id ); - $provider->provider_name = isset( $provider_detail ) ? $provider_detail['title'] : strtoupper( $provider->code ); - $provider->interest_rate = $this->capabilities()->is_zero_interest() ? 0 : ( $provider_detail['interest_rate'] * 100 ); - $provider->available_plans = $this->get_available_plans( + $provider->provider_code = str_replace( 'installment_', '', $provider->_id ); + $provider->provider_name = isset( $provider_detail ) ? $provider_detail['title'] : strtoupper( $provider->code ); + $provider->interest_rate = $this->capabilities()->is_zero_interest() ? 0 : ( $provider_detail['interest_rate'] * 100 ); + $provider->available_plans = $this->get_available_plans( $purchase_amount, $provider->allowed_installment_terms, $provider->interest_rate, From 59d81e0c86beee7ebefe9c6740bc675001e17907 Mon Sep 17 00:00:00 2001 From: guzzilar Date: Thu, 6 Jun 2019 19:31:25 +0800 Subject: [PATCH 8/9] Omise_Backend_Installment, improving its readability on an interest rate. --- .../backends/class-omise-backend-installment.php | 16 ++++++++-------- .../class-omise-backend-installment-test.php | 10 +++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/includes/backends/class-omise-backend-installment.php b/includes/backends/class-omise-backend-installment.php index 2502a8a5..8d582366 100644 --- a/includes/backends/class-omise-backend-installment.php +++ b/includes/backends/class-omise-backend-installment.php @@ -22,35 +22,35 @@ public function initiate() { 'installment_first_choice' => array( 'bank_code' => 'first_choice', 'title' => __( 'Krungsri First Choice', 'omise' ), - 'interest_rate' => 0.013, + 'interest_rate' => 1.3, 'min_allowed_amount' => 300.00, ), 'installment_bay' => array( 'bank_code' => 'bay', 'title' => __( 'Krungsri', 'omise' ), - 'interest_rate' => 0.008, + 'interest_rate' => 0.8, 'min_allowed_amount' => 300.00, ), 'installment_ktc' => array( 'bank_code' => 'ktc', 'title' => __( 'Krungthai Card (KTC)', 'omise' ), - 'interest_rate' => 0.008, + 'interest_rate' => 0.8, 'min_allowed_amount' => 300.00, ), 'installment_bbl' => array( 'bank_code' => 'bbl', 'title' => __( 'Bangkok Bank', 'omise' ), - 'interest_rate' => 0.008, + 'interest_rate' => 0.8, 'min_allowed_amount' => 500.00, ), 'installment_kbank' => array( 'bank_code' => 'kbank', 'title' => __( 'Kasikorn Bank', 'omise' ), - 'interest_rate' => 0.0065, + 'interest_rate' => 0.65, 'min_allowed_amount' => 500.00, ), ); @@ -72,7 +72,7 @@ public function get_available_providers( $currency, $purchase_amount ) { $provider->provider_code = str_replace( 'installment_', '', $provider->_id ); $provider->provider_name = isset( $provider_detail ) ? $provider_detail['title'] : strtoupper( $provider->code ); - $provider->interest_rate = $this->capabilities()->is_zero_interest() ? 0 : ( $provider_detail['interest_rate'] * 100 ); + $provider->interest_rate = $this->capabilities()->is_zero_interest() ? 0 : ( $provider_detail['interest_rate'] ); $provider->available_plans = $this->get_available_plans( $purchase_amount, $provider->allowed_installment_terms, @@ -116,12 +116,12 @@ public function get_available_plans( $purchase_amount, $allowed_installment_term /** * @param float $purchase_amount * @param int $term_length A length of a given installment term. - * @param float $interest_rate its value can be '0' if merchant absorbs an interest. + * @param float $interest_rate Its value can be '0' if merchant absorbs an interest. * * @return float of a installment monthly payment (round up to 2 decimals). */ public function calculate_monthly_payment_amount( $purchase_amount, $term_length, $interest_rate ) { - $interest = $purchase_amount * $term_length * $interest_rate; + $interest = $purchase_amount * $interest_rate * $term_length / 100; return round( ( $purchase_amount + $interest ) / $term_length, 2 ); } } diff --git a/tests/unit/includes/backends/class-omise-backend-installment-test.php b/tests/unit/includes/backends/class-omise-backend-installment-test.php index 02b901a1..aeebd82f 100644 --- a/tests/unit/includes/backends/class-omise-backend-installment-test.php +++ b/tests/unit/includes/backends/class-omise-backend-installment-test.php @@ -15,7 +15,7 @@ public function get_only_valid_plans_from_given_bbl_allowed_installment_terms() $installment_backend = new Omise_Backend_Installment(); $purchase_amount = 3000.00; $allowed_terms = array( 3, 4, 6, 8, 10 ); - $interest_rate = 0.008; + $interest_rate = 0.8; $min_allowed_amount = 500.00; $result = $installment_backend->get_available_plans( $purchase_amount, $allowed_terms, $interest_rate, $min_allowed_amount ); @@ -35,7 +35,7 @@ public function get_only_valid_plans_from_given_bbl_unsorted_allowed_installment $installment_backend = new Omise_Backend_Installment(); $purchase_amount = 3000.00; $allowed_terms = array( 3, 10, 4, 8, 6 ); - $interest_rate = 0.008; + $interest_rate = 0.8; $min_allowed_amount = 500.00; $result = $installment_backend->get_available_plans( $purchase_amount, $allowed_terms, $interest_rate, $min_allowed_amount ); @@ -55,7 +55,7 @@ public function correctly_calculating_monthly_payment_amount_as_buyer_absorbs_ca $installment_backend = new Omise_Backend_Installment(); $purchase_amount = 10000.00; $term = 10; - $interest_rate = 0.008; + $interest_rate = 0.8; $result = $installment_backend->calculate_monthly_payment_amount( $purchase_amount, $term, $interest_rate ); @@ -69,7 +69,7 @@ public function correctly_calculating_monthly_payment_amount_as_buyer_absorbs_ca $installment_backend = new Omise_Backend_Installment(); $purchase_amount = 17900.00; $term = 6; - $interest_rate = 0.0069; + $interest_rate = 0.69; $result = $installment_backend->calculate_monthly_payment_amount( $purchase_amount, $term, $interest_rate ); @@ -83,7 +83,7 @@ public function correctly_calculating_monthly_payment_amount_as_buyer_absorbs_ca $installment_backend = new Omise_Backend_Installment(); $purchase_amount = 5000.00; $term = 10; - $interest_rate = 0.008; + $interest_rate = 0.8; $result = $installment_backend->calculate_monthly_payment_amount( $purchase_amount, $term, $interest_rate ); From 0dfec4053e98caa82b6b5947fc20b7c5e06346ed Mon Sep 17 00:00:00 2001 From: guzzilar Date: Fri, 7 Jun 2019 12:36:56 +0800 Subject: [PATCH 9/9] templates/payment/form-installment.php, complete a missing translation. --- templates/payment/form-installment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/payment/form-installment.php b/templates/payment/form-installment.php index 1a10a990..df0046ca 100644 --- a/templates/payment/form-installment.php +++ b/templates/payment/form-installment.php @@ -40,7 +40,7 @@

- +