diff --git a/Helper/OmiseHelper.php b/Helper/OmiseHelper.php index da1e2958..77e750ff 100644 --- a/Helper/OmiseHelper.php +++ b/Helper/OmiseHelper.php @@ -151,7 +151,18 @@ class OmiseHelper extends AbstractHelper Installment::UOB_ID => Installment::CODE, Installment::MBB_ID => Installment::CODE, - // offsite installment banking payment + // offsite wlb installment banking payment + Installment::WLB_BAY_ID => Installment::CODE, + Installment::WLB_BBL_ID => Installment::CODE, + Installment::WLB_UOB_ID => Installment::CODE, + Installment::WLB_FIRST_CHOICE_ID => Installment::CODE, + Installment::WLB_KBANK_ID => Installment::CODE, + Installment::WLB_KTC_ID => Installment::CODE, + Installment::WLB_SCB_ID => Installment::CODE, + Installment::WLB_TTB_ID => Installment::CODE, + Installment::WLB_UOB_ID => Installment::CODE, + + // offsite mobile banking payment Mobilebanking::BAY_ID => Mobilebanking::CODE, Mobilebanking::BBL_ID => Mobilebanking::CODE, Mobilebanking::KBANK_ID => Mobilebanking::CODE, diff --git a/Model/Api/Capabilities.php b/Model/Api/Capabilities.php index cdcfed16..3239c7c0 100644 --- a/Model/Api/Capabilities.php +++ b/Model/Api/Capabilities.php @@ -99,6 +99,6 @@ public function getTokenizationMethods() */ public function getInstallmentMinLimit() { - return $this->capabilities['limits']['installment_amount']['min']; + return $this->capabilities ? $this->capabilities['limits']['installment_amount']['min'] : 0; } } diff --git a/Model/Config/Installment.php b/Model/Config/Installment.php index 9fc0f494..7082bff1 100644 --- a/Model/Config/Installment.php +++ b/Model/Config/Installment.php @@ -32,4 +32,23 @@ class Installment extends Config const UOB_ID = "installment_uob"; const MBB_ID = "installment_mbb"; + + /** + * WLB Installments + */ + const WLB_BAY_ID = "installment_wlb_bay"; + + const WLB_BBL_ID = "installment_wlb_bbl"; + + const WLB_FIRST_CHOICE_ID = "installment_wlb_first_choice"; + + const WLB_KBANK_ID = "installment_wlb_kbank"; + + const WLB_KTC_ID = "installment_wlb_ktc"; + + const WLB_SCB_ID = "installment_wlb_scb"; + + const WLB_TTB_ID = "installment_wlb_ttb"; + + const WLB_UOB_ID = "installment_wlb_uob"; } diff --git a/Observer/InstallmentDataAssignObserver.php b/Observer/InstallmentDataAssignObserver.php index 09c20fa0..b66beb7b 100644 --- a/Observer/InstallmentDataAssignObserver.php +++ b/Observer/InstallmentDataAssignObserver.php @@ -1,10 +1,6 @@ configMock = m::mock(Config::class); + $this->configMock->shouldReceive('canInitialize')->andReturn(true); + $this->omiseCapabilitiesMock = m::mock('alias:OmiseCapabilities'); + } + + protected function tearDown(): void + { + m::close(); + } + + /** + * @covers Omise\Payment\Model\Api\Capabilities + */ + public function testGetInstallmentMinLimit() + { + $data = [ + 'limits' => [ + 'installment_amount' => [ + 'min' => 3000 + ] + ] + ]; + $this->omiseCapabilitiesMock->shouldReceive('retrieve')->andReturn($data); + $capabilities = new Capabilities($this->configMock); + $result = $capabilities->getInstallmentMinLimit(); + + $this->assertEquals($data['limits']['installment_amount']['min'], $result); + } + + /** + * @covers Omise\Payment\Model\Api\Capabilities + */ + public function testGetInstallmentMinLimitReturnsZeroIfCapabilitiesIsNotSet() + { + $this->omiseCapabilitiesMock->shouldReceive('retrieve')->andReturn(null); + $capabilities = new Capabilities($this->configMock); + $result = $capabilities->getInstallmentMinLimit(); + + $this->assertEquals(0, $result); + } +} 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 2a0a6b77..cb292f1d 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 @@ -24,72 +24,25 @@ define( selectPaymentMethodAction ) { 'use strict'; - const CAPTION = $.mage.__('Choose number of monthly payments'); 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 - }, - ] + "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) { return Math.round(parseFloat(dollarAmount) * 100); @@ -121,16 +74,6 @@ define( initObservable: function () { this._super() .observe([ - 'omiseOffsite', - 'installmentTermsFC', - 'installmentTermsKTC', - 'installmentTermsKBank', - 'installmentTermsBBL', - 'installmentTermsBAY', - 'installmentTermsSCB', - 'installmentTermsUOB', - 'installmentTermsMBB', - 'installmentTermsTTB', 'omiseInstallmentError', 'omiseInstallmentToken', 'omiseInstallmentSource', @@ -139,7 +82,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; }, @@ -165,23 +108,14 @@ define( }, applyOmiseJsToElement: function (self, element) { - const iframeHeightMatching = { - '40px': 258, - '44px': 270, - '48px': 282, - '52px': 295, - } - const localeMatching = { en_US: 'en', ja_JP: 'ja', th_TH: 'th' } - const { theme, locale, formDesign } = window.checkoutConfig.payment.omise_cc - const { font, input, checkbox } = formDesign - let iframeElementHeight = iframeHeightMatching[input.height] - element.style.height = 500 + 'px'; + const { locale } = window.checkoutConfig.payment.omise_cc + element.style.height = '500px'; OmiseCard.configure({ publicKey: self.getPublicKey(), @@ -294,89 +228,6 @@ define( return $.mage.__('Minimum order value is %amount').replace('%amount', this.getFormattedAmount(this.getInstallmentMinLimit())); }, - /** - * Get formatted message about installment caption - * - * @return {string} - */ - getCaptionText: function () { - return CAPTION - }, - - /** - * 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 * @@ -386,70 +237,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 * @@ -465,41 +252,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 * @@ -514,16 +266,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))) }, /** diff --git a/view/frontend/web/template/payment/offsite-installment-form.html b/view/frontend/web/template/payment/offsite-installment-form.html index e16d0d7c..5d4db769 100644 --- a/view/frontend/web/template/payment/offsite-installment-form.html +++ b/view/frontend/web/template/payment/offsite-installment-form.html @@ -1,13 +1,13 @@
+ 'id': getCode() + }, + value: getCode(), + checked: isChecked, + click: selectPaymentMethod, + visible: isRadioButtonVisible(), + enable: isActive() && !orderValueTooLow()" /> @@ -49,50 +49,6 @@ - + name="payment[omise_source]" + data-bind="attr: { + 'id': getCode() + 'InstallmentSource', + 'data-container': getCode() + '-cc-source', + }, + value: omiseInstallmentSource" + />