Skip to content

Commit

Permalink
Updated WLB installment to be fully compatible with WooCommerce block.
Browse files Browse the repository at this point in the history
  • Loading branch information
aashishgurung committed Oct 4, 2024
1 parent c20f13f commit d191d8f
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 573 deletions.
21 changes: 0 additions & 21 deletions assets/javascripts/omise-payment-form-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,31 +309,10 @@
}
}

function initializeInstallmentForm() {
const omiseInstallmentElement = document.getElementById('omise-installment');
if (omiseInstallmentElement && $('#payment_method_omise_installment').is(':checked')){
showOmiseInstallmentForm({
element: omiseInstallmentElement,
publicKey: omise_installment_params.key,
amount: omise_installment_params.amount,
locale: LOCALE,
onSuccess: handleCreateOrder,
onError: (error) => {
showError(error)
$form.unblock()
}
})
} else {
OmiseCard.destroy();
}
}

function setupOmiseForm() {
var selectedPaymentMethod = $('input[name="payment_method"]:checked').val();
if (selectedPaymentMethod === 'omise') {
initializeSecureCardForm();
} else if (selectedPaymentMethod === 'omise_installment') {
initializeInstallmentForm();
} else {
OmiseCard.destroy();
}
Expand Down
170 changes: 2 additions & 168 deletions includes/backends/class-omise-backend-installment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,98 +6,10 @@
*
* @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.16,
'min_allowed_amount' => 300.00,
),

'installment_bay' => array(
'bank_code' => 'bay',
'title' => __('Krungsri', 'omise'),
'interest_rate' => 0.74,
'min_allowed_amount' => 500.00,
),

'installment_ktc' => array(
'bank_code' => 'ktc',
'title' => __('Krungthai Card (KTC)', 'omise'),
'interest_rate' => 0.74,
'min_allowed_amount' => 300.00,
),

'installment_bbl' => array(
'bank_code' => 'bbl',
'title' => __('Bangkok Bank', 'omise'),
'interest_rate' => 0.74,
'min_allowed_amount' => 500.00,
),

'installment_kbank' => array(
'bank_code' => 'kbank',
'title' => __('Kasikorn Bank', 'omise'),
'interest_rate' => 0.65,
'min_allowed_amount' => 300.00,
),

'installment_scb' => array(
'bank_code' => 'scb',
'title' => __('Siam Commercial Bank', 'omise'),
'interest_rate' => 0.74,
'min_allowed_amount' => 500.00,
),

'installment_ttb' => array(
'bank_code' => 'ttb',
'title' => __('TMBThanachart Bank', 'omise'),
'interest_rate' => 0.8,
'min_allowed_amount' => 500.00,
),

'installment_uob' => array(
'bank_code' => 'uob',
'title' => __('United Overseas Bank', 'omise'),
'interest_rate' => 0.64,
'min_allowed_amount' => 500.00,
),

'installment_mbb' => array(
'bank_code' => 'mbb',
'title' => __('Maybank', 'omise'),
'interest_rate' => 0,
'min_allowed_amount' => 500.00,
'zero_interest_installments' => true,
'terms_min_allowed_amount' => [
6 => 500.00,
12 => 1000.00,
18 => 1500.00,
24 => 2000.00
]
),
);
}

public function get_provider($id)
{
return self::$providers[$id];
}

/**
* @param string $currency
* @param float $purchase_amount
Expand All @@ -112,87 +24,9 @@ public function get_available_providers($currency, $purchase_amount)
return null;
}

$supportedProviderList = [];

// Note: As installment payment at the moment only supports THB and MYR currency, the
// $purchase_amount is multiplied with 100 to convert the amount into subunit (satang and sen).
$providers = $capabilities->getInstallmentBackends($currency, ($purchase_amount * 100));

foreach ($providers as &$provider) {
if (isset(self::$providers[$provider->_id])) {
$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 = $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
);
if (count($provider->available_plans) > 0) {
$supportedProviderList[] = $provider;
}
}
}

usort($supportedProviderList, function ($a, $b) {
return strcmp($a->provider_name, $b->provider_name);
});

return $supportedProviderList;
}

/**
* @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, $provider_detail)
{
$plans = array();

$min_allowed_amount = $provider_detail['min_allowed_amount'];
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 (isset($provider_detail['terms_min_allowed_amount'])) {
$terms_min_allowed_amount = $provider_detail['terms_min_allowed_amount'];
$min_allowed_amount = round($terms_min_allowed_amount[$term_length] / $term_length, 2);
}

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);
$installments = $capabilities->getInstallmentBackends($currency, ($purchase_amount * 100));
return count($installments) > 0;
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n'), 'version' => 'd2a1602f618f3b67fe3f74ab703c2398');
<?php return array('dependencies' => array('react', 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n'), 'version' => 'd2aee42914cfc4b82c03f61eae373fe9');
2 changes: 1 addition & 1 deletion includes/blocks/assets/js/build/omise_installment.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

128 changes: 55 additions & 73 deletions includes/blocks/assets/js/omise-installment.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,96 +13,78 @@ const Label = ( props ) => {

const InstallmentPaymentMethod = (props) => {
const {eventRegistration, emitResponse} = props;
const {onPaymentSetup} = eventRegistration;
const {onPaymentSetup, onCheckoutValidation} = eventRegistration;
const description = decodeEntities( settings.description || '' )
const { installment_backends, is_zero_interest } = settings.data;
const { installments_enabled, total_amount, public_key } = settings.data;
const noPaymentMethods = __( 'Purchase Amount is lower than the monthly minimum payment amount.', 'omise' );
const installmentRef = useRef(null);
const termRef = useRef(null);
const el = useRef(null);
const wlbInstallmentRef = useRef(null);
const cardFormErrors = useRef(null);

const onInstallmentSelected = (e) => {
installmentRef.current = e.target.value
termRef.current = null
}
useEffect(() => {
if (installments_enabled) {
let locale = settings.locale.toLowerCase();
let supportedLocales = ['en', 'th', 'ja'];
locale = supportedLocales.includes(locale) ? locale : 'en';

const onTermsSelected = (e) => {
termRef.current = e.target.value
}
showOmiseInstallmentForm({
element: el.current,
publicKey: public_key,
amount: total_amount,
locale,
onSuccess: (payload) => {
wlbInstallmentRef.current = payload;
},
onError: (error) => {
cardFormErrors.current = error;
},
});
}
}, [installments_enabled])

useEffect( () => {
const unsubscribe = onCheckoutValidation( () => {
OmiseCard.requestCardToken()
return true;
} );
return unsubscribe;
}, [ onCheckoutValidation ] );

useEffect(() => {
const unsubscribe = onPaymentSetup(async () => {
if (!installmentRef.current || !termRef.current) {
return {type: emitResponse.responseTypes.ERROR, message: 'Select a bank and term'}
}
try {
return {
type: emitResponse.responseTypes.SUCCESS,
meta: {
paymentMethodData: {
"source": installmentRef.current,
[`${installmentRef.current}_installment_terms`]: termRef.current,
return await new Promise(( resolve, reject ) => {
const intervalId = setInterval( () => {
if (wlbInstallmentRef.current) {
clearInterval(intervalId);
try {
const response = {
type: emitResponse.responseTypes.SUCCESS,
meta: {
paymentMethodData: {
"source": wlbInstallmentRef.current.source,
"token": wlbInstallmentRef.current.token,
}
}
};
resolve(response)
} catch (error) {
clearInterval(intervalId);
const response = {type: emitResponse.responseTypes.ERROR, message: error.message}
reject(response)
}
}
};
} catch (error) {
return {type: emitResponse.responseTypes.ERROR, message: error.message}
}
}, 1000 );
});
});
return () => unsubscribe();
}, [ onPaymentSetup ]);

return (<>
{description && <p>{description}</p>}
{
installment_backends.length == 0
!installments_enabled
? <p>{noPaymentMethods}</p>
: (
<fieldset id="omise-form-installment">
<ul className="omise-banks-list">
{
installment_backends.map((backend, i) => (
<li key={backend['_id'] + i} className="item">
<input id={backend['_id']} type="radio" name="source[type]" value={backend['_id']} onChange={onInstallmentSelected} />
<label htmlFor={backend['_id']}>
<div className={`bank-logo ${backend['provider_code']}`}></div>
<div className="bank-label">
<span className="title">{backend['provider_name']}</span><br/>
<select
id={`${backend['_id']}_installment_terms`}
name={`${backend['_id']}_installment_terms`}
className="installment-term-select-box"
onChange={onTermsSelected}
>
<option>Select term</option>
{
backend['available_plans'].map((installment_plan, i) => (
<option
key={`${installment_plan['term_length']}_${installment_plan['monthly_amount']}_${i}`}
value={installment_plan['term_length']}
>
{__(`${installment_plan['term_length']} months`, 'omise')}
<>&nbsp;</>
({__(`${installment_plan['monthly_amount']} / months`, 'omise')})
</option>
))
}
</select>
{
is_zero_interest && <>
<br />
<span className="omise-installment-interest-rate">
{__( `( interest ${backend.interest_rate} )`, 'omise' )}
</span>
</>
}
</div>
</label>
</li>
))
}
</ul>
</fieldset>
)
: <div ref={el} id="omise-installment" style={{ width:"100%", maxWidth: "400px" }}></div>
}
</>)
}
Expand Down
1 change: 1 addition & 0 deletions includes/blocks/gateways/abstract-omise-block-payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function get_payment_method_data() {
'supports' => array_filter($this->gateway->supports, [$this->gateway, 'supports']),
'data' => $this->additional_data,
'is_active' => $this->is_active(),
'locale' => get_locale(),
];
}

Expand Down
Loading

0 comments on commit d191d8f

Please sign in to comment.