-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from omise/installment-filter-terms
Installment, display instalment monthly amount & interest fee at the checkout page.
- Loading branch information
Showing
12 changed files
with
409 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
/** | ||
* Note: all calculations in this class are based only on Thailand VAT and fee | ||
* as currently Installment feature supported only for merchants | ||
* that have registered with Omise Thailand account. | ||
* | ||
* @since 3.4 | ||
* | ||
* @method public initiate | ||
* @method public get_available_providers | ||
* @method public get_available_plans | ||
* @method public calculate_monthly_payment_amount | ||
*/ | ||
class Omise_Backend_Installment extends Omise_Backend { | ||
/** | ||
* @var array of known installment providers. | ||
*/ | ||
protected static $providers = array(); | ||
|
||
public function initiate() { | ||
self::$providers = array( | ||
'installment_first_choice' => array( | ||
'bank_code' => 'first_choice', | ||
'title' => __( 'Krungsri First Choice', 'omise' ), | ||
'interest_rate' => 1.3, | ||
'min_allowed_amount' => 300.00, | ||
), | ||
|
||
'installment_bay' => array( | ||
'bank_code' => 'bay', | ||
'title' => __( 'Krungsri', 'omise' ), | ||
'interest_rate' => 0.8, | ||
'min_allowed_amount' => 300.00, | ||
), | ||
|
||
'installment_ktc' => array( | ||
'bank_code' => 'ktc', | ||
'title' => __( 'Krungthai Card (KTC)', 'omise' ), | ||
'interest_rate' => 0.8, | ||
'min_allowed_amount' => 300.00, | ||
), | ||
|
||
'installment_bbl' => array( | ||
'bank_code' => 'bbl', | ||
'title' => __( 'Bangkok Bank', 'omise' ), | ||
'interest_rate' => 0.8, | ||
'min_allowed_amount' => 500.00, | ||
), | ||
|
||
'installment_kbank' => array( | ||
'bank_code' => 'kbank', | ||
'title' => __( 'Kasikorn Bank', 'omise' ), | ||
'interest_rate' => 0.65, | ||
'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_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'] ); | ||
$provider->available_plans = $this->get_available_plans( | ||
$purchase_amount, | ||
$provider->allowed_installment_terms, | ||
$provider->interest_rate, | ||
$provider_detail['min_allowed_amount'] | ||
); | ||
} | ||
|
||
return $providers; | ||
} | ||
|
||
/** | ||
* @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_available_plans( $purchase_amount, $allowed_installment_terms, $interest_rate, $min_allowed_amount ) { | ||
$plans = array(); | ||
|
||
sort( $allowed_installment_terms ); | ||
|
||
foreach ( $allowed_installment_terms as $term_length ) { | ||
$monthly_amount = $this->calculate_monthly_payment_amount( $purchase_amount, $term_length, $interest_rate ); | ||
|
||
if ( $monthly_amount < $min_allowed_amount ) { | ||
break; | ||
} | ||
|
||
$plans[] = array( | ||
'term_length' => $term_length, | ||
'monthly_amount' => $monthly_amount | ||
); | ||
} | ||
|
||
return $plans; | ||
} | ||
|
||
/** | ||
* @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. | ||
* | ||
* @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 * $interest_rate * $term_length / 100; | ||
return round( ( $purchase_amount + $interest ) / $term_length, 2 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
/** | ||
* @since 3.4 | ||
*/ | ||
class Omise_Backend { | ||
public function __construct() { | ||
$this->initiate(); | ||
} | ||
|
||
/** | ||
* Class initiation. | ||
* | ||
* @return void | ||
*/ | ||
public function initiate() { | ||
return; | ||
} | ||
|
||
/** | ||
* @return Omise_Capabilities Instant. | ||
*/ | ||
public function capabilities() { | ||
return Omise_Capabilities::retrieve(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.1/phpunit.xsd" | ||
backupGlobals="true" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
stopOnError="false" | ||
stopOnFailure="false" | ||
stopOnIncomplete="false" | ||
stopOnSkipped="false" | ||
stopOnRisky="false" | ||
verbose="true"> | ||
<testsuites> | ||
<testsuite name="Omise WooCommerce Test Suite"> | ||
<directory suffix="-test.php">tests/unit</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
class Omise_Unit_Test { | ||
public static function include_class( $path ): void { | ||
require_once __DIR__ . '/../../includes/' . $path; | ||
} | ||
} | ||
|
||
/** | ||
* Mock WordPress __() function. | ||
* | ||
* @see wp-includes/l10n.php | ||
*/ | ||
function __( $text, $domain = 'default' ) { | ||
return $text; | ||
} |
Oops, something went wrong.