Skip to content

Commit

Permalink
Refactored /omise-offsite-installment-method.js
Browse files Browse the repository at this point in the history
  • Loading branch information
aashishgurung committed Sep 13, 2024
1 parent 2d062ea commit 2ab0dce
Showing 1 changed file with 21 additions and 315 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,126 +25,22 @@ define(
) {
'use strict';
const providers = [
{
id: "installment_ktc",
title: $.mage.__('Krungthai Card'),
code: 'ktc',
logo: 'ktc',
active: true
},
{
id: "installment_first_choice",
title: $.mage.__('First Choice'),
code: 'first_choice',
logo: 'fc',
active: true
},
{
id: "installment_kbank",
title: $.mage.__('Kasikorn Bank'),
code: 'kbank',
logo: 'kbank',
active: true
},
{
id: "installment_bbl",
title: $.mage.__('Bangkok Bank'),
code: 'bbl',
logo: 'bbl',
active: true
},
{
id: "installment_bay",
title: $.mage.__('Krungsri'),
code: 'bay',
logo: 'bay',
active: true
},
{
id: "installment_scb",
title: $.mage.__('Siam Commercial Bank'),
code: 'scb',
logo: 'scb',
active: true
},
{
id: "installment_uob",
title: $.mage.__('United Overseas Bank'),
code: 'uob',
logo: 'uob',
active: true
},
{
id: "installment_mbb",
title: $.mage.__('MayBank'),
code: 'mbb',
logo: 'mbb',
active: true
},
{
id: "installment_ttb",
title: $.mage.__('TMBThanachart Bank'),
code: 'ttb',
logo: 'ttb',
active: true
},

{
id: "installment_wlb_ktc",
title: $.mage.__('Krungthai Card'),
code: 'ktc',
logo: 'ktc',
active: true
},
{
id: "installment_wlb_first_choice",
title: $.mage.__('First Choice'),
code: 'first_choice',
logo: 'fc',
active: true
},
{
id: "installment_wlb_kbank",
title: $.mage.__('Kasikorn Bank'),
code: 'kbank',
logo: 'kbank',
active: true
},
{
id: "installment_wlb_bbl",
title: $.mage.__('Bangkok Bank'),
code: 'bbl',
logo: 'bbl',
active: true
},
{
id: "installment_wlb_bay",
title: $.mage.__('Krungsri'),
code: 'bay',
logo: 'bay',
active: true
},
{
id: "installment_wlb_scb",
title: $.mage.__('Siam Commercial Bank'),
code: 'scb',
logo: 'scb',
active: true
},
{
id: "installment_wlb_uob",
title: $.mage.__('United Overseas Bank'),
code: 'uob',
logo: 'uob',
active: true
},
{
id: "installment_wlb_ttb",
title: $.mage.__('TMBThanachart Bank'),
code: 'ttb',
logo: 'ttb',
active: true
},
"installment_ktc",
"installment_first_choice",
"installment_kbank",
"installment_bbl",
"installment_bay",
"installment_scb",
"installment_uob",
"installment_mbb",
"installment_ttb","installment_wlb_ktc",
"installment_wlb_first_choice",
"installment_wlb_kbank",
"installment_wlb_bbl",
"installment_wlb_bay",
"installment_wlb_scb",
"installment_wlb_uob",
"installment_wlb_ttb",
];

function convertToCents(dollarAmount) {
Expand Down Expand Up @@ -177,16 +73,6 @@ define(
initObservable: function () {
this._super()
.observe([
'omiseOffsite',
'installmentTermsFC',
'installmentTermsKTC',
'installmentTermsKBank',
'installmentTermsBBL',
'installmentTermsBAY',
'installmentTermsSCB',
'installmentTermsUOB',
'installmentTermsMBB',
'installmentTermsTTB',
'omiseInstallmentError',
'omiseInstallmentToken',
'omiseInstallmentSource',
Expand All @@ -195,7 +81,7 @@ define(
this.capabilities = checkoutConfig.omise_payment_list[this.code];

// filter provider for checkout page
this.providers = this.get_available_providers()
this.providers = this.getAvailableProviders()
this.openOmiseJs();
return this;
},
Expand Down Expand Up @@ -228,7 +114,7 @@ define(
}

const { locale } = window.checkoutConfig.payment.omise_cc
element.style.height = 500 + 'px';
element.style.height = '500px';

OmiseCard.configure({
publicKey: self.getPublicKey(),
Expand Down Expand Up @@ -341,80 +227,6 @@ define(
return $.mage.__('Minimum order value is %amount').replace('%amount', this.getFormattedAmount(this.getInstallmentMinLimit()));
},

/**
* Get Installment minimum
* this function respects info from: https://www.omise.co/installment-payment
*
* NOTE: in the future this function should return data from capabilities object.
*
* @param {string} id - Bank ID
* @return {integer}
*/
getInstallmentMinimum: function (id) {
return {
'kbank': 300,
'bbl': 500,
'bay': 500,
'first_choice': 300,
'ktc': 300,
'scb': 500,
'uob': 500,
'mbb': 83.33,
'ttb': 500,
}[id];
},

/**
* Get Installment monthly interest rate
*
* NOTE: in the future this function should return data from capabilities object.
*
* @param {string} id - Bank id
* @return {float}
*/
getInstallmentInterestRate: function (id) {
return {
'kbank': 0.0065,
'bbl': 0.0074,
'bay': 0.0074,
'first_choice': 0.0116,
'ktc': 0.0074,
'scb': 0.0074,
'uob': 0.0064,
'mbb': 0,
'ttb': 0.008,
}[id];
},

/**
* Get zero interest setting
*
* @return {boolean}
*/
isZeroInterest: function () {
return window.checkoutConfig.is_zero_interest;
},

/**
* Calculates single installment amount
*
* @param {string} id - Bank ID
* @param {integer} terms - number of monthly installments
* @return {integer}
*/
calculateSingleInstallmentAmount: function (id, terms) {
const total = this.getTotal();

if (this.isZeroInterest()) {
//merchant pays interest
return (total / terms).toFixed(2)
}

const rate = this.getInstallmentInterestRate(id);
const interest = rate * terms * total;
return + (((total + interest) / terms).toFixed(2));
},

/**
* Get total amount of an order
*
Expand All @@ -424,70 +236,6 @@ define(
return + window.checkoutConfig.quoteData.grand_total;
},

/**
* Get installment terms
*
* @return {string|null}
*/
getTerms: function () {
return (
this.installmentTermsBBL() ||
this.installmentTermsKBank() ||
this.installmentTermsFC() ||
this.installmentTermsKTC() ||
this.installmentTermsBAY() ||
this.installmentTermsSCB() ||
this.installmentTermsUOB() ||
this.installmentTermsMBB() ||
this.installmentTermsTTB()
);
},

/**
* Get installment term ko.observable by name
*
* @return {string|null}
*/
getObservableTerm: function (name) {
switch (name) {
case 'installment_uob':
return this.observe().installmentTermsUOB
case 'installment_scb':
return this.observe().installmentTermsSCB
case 'installment_bbl':
return this.observe().installmentTermsBBL
case 'installment_kbank':
return this.observe().installmentTermsKBank
case 'installment_first_choice':
return this.observe().installmentTermsFC
case 'installment_ktc':
return this.observe().installmentTermsKTC
case 'installment_bay':
return this.observe().installmentTermsBAY
case 'installment_mbb':
return this.observe().installmentTermsMBB
case 'installment_ttb':
return this.observe().installmentTermsTTB
default:
return null
}
},

/**
* Reset selected terms
*/
resetTerms: function () {
this.installmentTermsBBL(null);
this.installmentTermsKBank(null);
this.installmentTermsFC(null);
this.installmentTermsKTC(null);
this.installmentTermsBAY(null);
this.installmentTermsSCB(null);
this.installmentTermsUOB(null);
this.installmentTermsMBB(null);
this.installmentTermsTTB(null);
},

/**
* Get a checkout form data
*
Expand All @@ -503,41 +251,6 @@ define(
};
},

/**
* Returns Installment Terms
* @param {string} id - Bank id
* @return {array}
*/
getInstallmentTerms: function (id) {
const installmentBackends = this.capabilities;
const templateLabel = $.mage.__('%terms months (%amount / month)');

for (const key in installmentBackends) {
if (installmentBackends[key]._id !== 'installment_' + id) {
continue;
}

let dispTerms = [];
const terms = installmentBackends[key].allowed_installment_terms;
const minSingleInstallment = this.getInstallmentMinimum(id);

for (let i = 0; i < terms.length; i++) {
const amount = this.calculateSingleInstallmentAmount(id, terms[i]);

if (amount >= minSingleInstallment) {
dispTerms.push({
label: templateLabel.replace('%terms', terms[i]).replace('%amount', this.getFormattedAmount(amount)),
key: terms[i]
});
}
}

return ko.observableArray(
dispTerms
);
}
},

/**
* Check if order value meets minimum requirement
*
Expand All @@ -552,16 +265,9 @@ define(
*
* @return {Array}
*/
get_available_providers: function () {
let _providers = Object.values(this.capabilities);

return ko.observableArray(providers.filter((a1) => _providers.find(a2 => {
if (a1.id === a2._id) {
a1.obs = this.getInstallmentTerms(a2._id)
return true
}
}
)))
getAvailableProviders: function () {
const paymentMethods = this.capabilities.map(item => item._id)
return ko.observableArray(providers.filter(item => paymentMethods.includes(item)))
},

/**
Expand Down

0 comments on commit 2ab0dce

Please sign in to comment.