diff --git a/view/frontend/web/js/view/payment/method-renderer/omise-offsite-installment-method.js b/view/frontend/web/js/view/payment/method-renderer/omise-offsite-installment-method.js index f8360695..ad48bd09 100644 --- a/view/frontend/web/js/view/payment/method-renderer/omise-offsite-installment-method.js +++ b/view/frontend/web/js/view/payment/method-renderer/omise-offsite-installment-method.js @@ -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) { @@ -177,16 +73,6 @@ define( initObservable: function () { this._super() .observe([ - 'omiseOffsite', - 'installmentTermsFC', - 'installmentTermsKTC', - 'installmentTermsKBank', - 'installmentTermsBBL', - 'installmentTermsBAY', - 'installmentTermsSCB', - 'installmentTermsUOB', - 'installmentTermsMBB', - 'installmentTermsTTB', 'omiseInstallmentError', 'omiseInstallmentToken', 'omiseInstallmentSource', @@ -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; }, @@ -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(), @@ -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 * @@ -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 * @@ -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 * @@ -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))) }, /**