From 0ff4e82ec522f4258dbadc7d9a11c342972bd552 Mon Sep 17 00:00:00 2001 From: Aashish Gurung <101558497+aashishgurung@users.noreply.github.com> Date: Fri, 8 Nov 2024 17:56:20 +0700 Subject: [PATCH 1/4] Resolve issue of payment failing on installment when total amount is updated after selecting payment (#485) * Resolve issue of failing installment amount when cart is updated changing total amount. * Resolve installment payment issue in WooCommerce blocks. --- .../javascripts/omise-payment-form-handler.js | 2 +- .../js/build/omise_installment.asset.php | 2 +- .../assets/js/build/omise_installment.js | 2 +- .../blocks/assets/js/omise-installment.js | 28 +++++++++++++++++-- includes/class-omise-capabilities.php | 2 +- templates/payment/form-installment.php | 1 + 6 files changed, 31 insertions(+), 6 deletions(-) diff --git a/assets/javascripts/omise-payment-form-handler.js b/assets/javascripts/omise-payment-form-handler.js index 317537a2..db5a3970 100755 --- a/assets/javascripts/omise-payment-form-handler.js +++ b/assets/javascripts/omise-payment-form-handler.js @@ -257,7 +257,7 @@ showOmiseInstallmentForm({ element: omiseInstallmentElement, publicKey: omise_installment_params.key, - amount: omise_installment_params.amount, + amount: OMISE_UPDATED_CART_AMOUNT, locale: LOCALE, onSuccess: handleCreateOrder, onError: (error) => { diff --git a/includes/blocks/assets/js/build/omise_installment.asset.php b/includes/blocks/assets/js/build/omise_installment.asset.php index 521aa0a0..5d88bbbf 100644 --- a/includes/blocks/assets/js/build/omise_installment.asset.php +++ b/includes/blocks/assets/js/build/omise_installment.asset.php @@ -1 +1 @@ - array('react', 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n'), 'version' => 'c95a8877d63c5c3bb2fda93273b0a81a'); \ No newline at end of file + array('react', 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n'), 'version' => 'e2436da6ade36f0dc6835eb771337040'); \ No newline at end of file diff --git a/includes/blocks/assets/js/build/omise_installment.js b/includes/blocks/assets/js/build/omise_installment.js index 1c956852..84f607e0 100644 --- a/includes/blocks/assets/js/build/omise_installment.js +++ b/includes/blocks/assets/js/build/omise_installment.js @@ -1 +1 @@ -(()=>{"use strict";const e=window.React,t=window.wp.element,n=window.wp.i18n,s=window.wp.htmlEntities,a=window.wc.wcBlocksRegistry,l=(0,window.wc.wcSettings.getSetting)("omise_installment_data",{}),o=(0,s.decodeEntities)(l.title)||"No title set",r=a=>{const{eventRegistration:o,emitResponse:r}=a,{onPaymentSetup:c,onCheckoutValidation:i,onCheckoutFail:m}=o,u=(0,s.decodeEntities)(l.description||""),{installments_enabled:d,total_amount:w,public_key:p}=l.data,E=(0,n.__)("Purchase Amount is lower than the monthly minimum payment amount.","omise"),y=(0,t.useRef)(null),f=(0,t.useRef)(null),h=(0,t.useRef)(null),R=()=>{if(d){let e=l.locale.toLowerCase();e=["en","th","ja"].includes(e)?e:"en",showOmiseInstallmentForm({element:y.current,publicKey:p,amount:w,locale:e,onSuccess:e=>{f.current=e},onError:e=>{h.current=e}})}};return(0,t.useEffect)((()=>{R()}),[d]),(0,t.useEffect)((()=>i((()=>(OmiseCard.requestCardToken(),!0)))),[i]),(0,t.useEffect)((()=>m((()=>(f.current=null,R(),!0)))),[m]),(0,t.useEffect)((()=>{const e=c((async()=>await new Promise(((e,t)=>{const n=setInterval((()=>{if(f.current){clearInterval(n);try{const t={type:r.responseTypes.SUCCESS,meta:{paymentMethodData:{omise_source:f.current.source,omise_token:f.current.token}}};e(t)}catch(e){clearInterval(n);const s={type:r.responseTypes.ERROR,message:e.message};t(s)}}}),1e3)}))));return()=>e()}),[c]),(0,e.createElement)(e.Fragment,null,u&&(0,e.createElement)("p",null,u),d?(0,e.createElement)("div",{ref:y,id:"omise-installment",style:{width:"100%",maxWidth:"400px"}}):(0,e.createElement)("p",null,E))};(0,a.registerPaymentMethod)({name:l.name||"",label:(0,e.createElement)((t=>{const{PaymentMethodLabel:n}=t.components;return(0,e.createElement)(n,{text:o})}),null),content:(0,e.createElement)(r,null),edit:(0,e.createElement)(r,null),canMakePayment:()=>l.is_active,ariaLabel:o,supports:{features:l.supports}})})(); \ No newline at end of file +(()=>{"use strict";const e=window.React,t=window.wp.element,n=window.wp.i18n,a=window.wp.htmlEntities,s=window.wc.wcBlocksRegistry,r=(0,window.wc.wcSettings.getSetting)("omise_installment_data",{}),l=(0,a.decodeEntities)(r.title)||"No title set",{select:o,subscribe:c}=window.wp.data,i=window.wc.wcBlocksData.CART_STORE_KEY,u=s=>{const{eventRegistration:l,emitResponse:u}=s,{onPaymentSetup:m,onCheckoutValidation:w,onCheckoutFail:d}=l,p=(0,a.decodeEntities)(r.description||""),{installments_enabled:E,total_amount:f,public_key:y}=r.data,h=(0,n.__)("Purchase Amount is lower than the monthly minimum payment amount.","omise"),_=(0,t.useRef)(null),R=(0,t.useRef)(null),g=(0,t.useRef)(null),C=(0,t.useRef)(null),k=()=>{if(E){const e=o(i).getCartData();C.current=e.totals.total_price;let t=r.locale.toLowerCase();t=["en","th","ja"].includes(t)?t:"en",_.current.innerHTML="",showOmiseInstallmentForm({element:_.current,publicKey:y,amount:C.current,locale:t,onSuccess:e=>{R.current=e},onError:e=>{g.current=e}})}},b=()=>{const e=o(i).getCartData();C.current=e.totals.total_price,k()};return(0,t.useEffect)((()=>c(b,i)),[i]),(0,t.useEffect)((()=>{k()}),[E]),(0,t.useEffect)((()=>w((()=>(OmiseCard.requestCardToken(),!0)))),[w]),(0,t.useEffect)((()=>d((()=>(R.current=null,k(),!0)))),[d]),(0,t.useEffect)((()=>{const e=m((async()=>await new Promise(((e,t)=>{const n=setInterval((()=>{if(R.current){clearInterval(n);try{const t={type:u.responseTypes.SUCCESS,meta:{paymentMethodData:{omise_source:R.current.source,omise_token:R.current.token}}};e(t)}catch(e){clearInterval(n);const a={type:u.responseTypes.ERROR,message:e.message};t(a)}}}),1e3)}))));return()=>e()}),[m]),(0,e.createElement)(e.Fragment,null,p&&(0,e.createElement)("p",null,p),E?(0,e.createElement)("div",{ref:_,id:"omise-installment",style:{width:"100%",maxWidth:"400px"}}):(0,e.createElement)("p",null,h))};(0,s.registerPaymentMethod)({name:r.name||"",label:(0,e.createElement)((t=>{const{PaymentMethodLabel:n}=t.components;return(0,e.createElement)(n,{text:l})}),null),content:(0,e.createElement)(u,null),edit:(0,e.createElement)(u,null),canMakePayment:()=>r.is_active,ariaLabel:l,supports:{features:r.supports}})})(); \ No newline at end of file diff --git a/includes/blocks/assets/js/omise-installment.js b/includes/blocks/assets/js/omise-installment.js index 36309701..e0a0daed 100644 --- a/includes/blocks/assets/js/omise-installment.js +++ b/includes/blocks/assets/js/omise-installment.js @@ -1,4 +1,4 @@ -import {useEffect, useRef} from '@wordpress/element'; +import {useEffect, useRef, useState} from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { decodeEntities } from '@wordpress/html-entities'; import { registerPaymentMethod } from '@woocommerce/blocks-registry'; @@ -11,6 +11,9 @@ const Label = ( props ) => { return } +const {select, subscribe} = window.wp.data; +const cartStoreKey = window.wc.wcBlocksData.CART_STORE_KEY; + const InstallmentPaymentMethod = (props) => { const {eventRegistration, emitResponse} = props; const {onPaymentSetup, onCheckoutValidation, onCheckoutFail} = eventRegistration; @@ -20,17 +23,26 @@ const InstallmentPaymentMethod = (props) => { const el = useRef(null); const wlbInstallmentRef = useRef(null); const cardFormErrors = useRef(null); + const totalAmount = useRef(null); const loadInstallmentForm = () => { if (installments_enabled) { + // Getting the new total price that might be updated when shipping method + // was updated while other payment method was selected + const cart = select( cartStoreKey ).getCartData(); + totalAmount.current = cart.totals.total_price + let locale = settings.locale.toLowerCase(); let supportedLocales = ['en', 'th', 'ja']; locale = supportedLocales.includes(locale) ? locale : 'en'; + // removing previous iframe if there is any + el.current.innerHTML = ""; + showOmiseInstallmentForm({ element: el.current, publicKey: public_key, - amount: total_amount, + amount: totalAmount.current, locale, onSuccess: (payload) => { wlbInstallmentRef.current = payload; @@ -42,6 +54,18 @@ const InstallmentPaymentMethod = (props) => { } } + // Update total amount on cart update. We need this to send the update amount to the source API + const onCartChange = () => { + const cart = select( cartStoreKey ).getCartData(); + totalAmount.current = cart.totals.total_price; + loadInstallmentForm() + } + + useEffect(() => { + const unsubscribe = subscribe( onCartChange, cartStoreKey ); + return unsubscribe; + }, [cartStoreKey]) + useEffect(() => { loadInstallmentForm(); }, [installments_enabled]) diff --git a/includes/class-omise-capabilities.php b/includes/class-omise-capabilities.php index 7a73b086..1dcda951 100644 --- a/includes/class-omise-capabilities.php +++ b/includes/class-omise-capabilities.php @@ -98,7 +98,7 @@ public static function isFromCheckoutPage() return false; } - $endpoints = ['checkout', 'batch']; + $endpoints = ['checkout', 'batch', 'cart/select-shipping-rate']; foreach($endpoints as $endpoint) { if (trim($wp->request) !== '') { diff --git a/templates/payment/form-installment.php b/templates/payment/form-installment.php index 4c91c551..fb014769 100644 --- a/templates/payment/form-installment.php +++ b/templates/payment/form-installment.php @@ -3,6 +3,7 @@ if ( ! empty( $viewData['installments_enabled'] ) ) : ?>
From e60ca9861c46dea96eff92d232496cbef87c9d7c Mon Sep 17 00:00:00 2001 From: Aashish Gurung <101558497+aashishgurung@users.noreply.github.com> Date: Mon, 11 Nov 2024 12:09:05 +0700 Subject: [PATCH 2/4] Fix Duitnow bank list issue (#486) * Fix duitnow bank list issue * Include test for get_bank_list() --- .../js/build/omise_duitnow_obw.asset.php | 2 +- .../assets/js/build/omise_duitnow_obw.js | 2 +- .../blocks/assets/js/omise-duitnow-obw.js | 2 +- .../gateways/omise-block-duitnow-obw.php | 6 +- .../class-omise-payment-duitnow-obw.php | 85 +++++++++++++++++-- .../blocks/gateways/traits/mock-gateways.php | 7 ++ .../class-omise-payment-duitnow-obw-test.php | 85 ++++++++++++++++++- 7 files changed, 169 insertions(+), 20 deletions(-) diff --git a/includes/blocks/assets/js/build/omise_duitnow_obw.asset.php b/includes/blocks/assets/js/build/omise_duitnow_obw.asset.php index 28f44fb9..aa42cd09 100644 --- a/includes/blocks/assets/js/build/omise_duitnow_obw.asset.php +++ b/includes/blocks/assets/js/build/omise_duitnow_obw.asset.php @@ -1 +1 @@ - array('react', 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n'), 'version' => '9895ac689bce188c4a74717ce7c149c5'); \ No newline at end of file + array('react', 'wc-blocks-registry', 'wc-settings', 'wp-element', 'wp-html-entities', 'wp-i18n'), 'version' => 'b1a7ad893b10317f131d580cebf99812'); \ No newline at end of file diff --git a/includes/blocks/assets/js/build/omise_duitnow_obw.js b/includes/blocks/assets/js/build/omise_duitnow_obw.js index 02028bd0..de7d53fc 100644 --- a/includes/blocks/assets/js/build/omise_duitnow_obw.js +++ b/includes/blocks/assets/js/build/omise_duitnow_obw.js @@ -1 +1 @@ -(()=>{"use strict";const e=window.React,t=window.wp.element,n=(window.wp.i18n,window.wp.htmlEntities),a=window.wc.wcBlocksRegistry,s=(0,window.wc.wcSettings.getSetting)("omise_duitnow_obw_data",{}),l=(0,n.decodeEntities)(s.title)||"No title set",o=a=>{const{eventRegistration:l,emitResponse:o}=a,{onPaymentSetup:c}=l,r=(0,n.decodeEntities)(s.description||""),{banks:i}=s.data,[m,p]=(0,t.useState)(null);return(0,t.useEffect)((()=>{const e=c((async()=>{if(!m)return{type:o.responseTypes.ERROR,message:"Select a bank"};try{return{type:o.responseTypes.SUCCESS,meta:{paymentMethodData:{bank:m}}}}catch(e){return{type:o.responseTypes.ERROR,message:e.message}}}));return()=>e()}),[o.responseTypes.ERROR,o.responseTypes.SUCCESS,c,m]),(0,e.createElement)(e.Fragment,null,r&&(0,e.createElement)("p",null,r),(0,e.createElement)("fieldset",{id:"omise-form-installment"},(0,e.createElement)("div",{className:"fpx-select-bank"},(0,e.createElement)("label",{htmlFor:"fpx-select-bank"},"Select Bank"),(0,e.createElement)("select",{className:"fpx-bank-logo default",id:"duitnow-obw-select-bank",name:"source[bank]",defaultValue:"",onChange:e=>{p(e.target.value)}},(0,e.createElement)("option",{value:"",disabled:!0},"-- Select your option --"),i.map((t=>(0,e.createElement)("option",{key:t.code,className:t.code,value:t.code},t.name)))))))};(0,a.registerPaymentMethod)({name:s.name||"",label:(0,e.createElement)((t=>{const{PaymentMethodLabel:n}=t.components;return(0,e.createElement)(n,{text:l})}),null),content:(0,e.createElement)(o,null),edit:(0,e.createElement)(o,null),canMakePayment:()=>s.is_active,ariaLabel:l,supports:{features:s.supports}})})(); \ No newline at end of file +(()=>{"use strict";const e=window.React,t=window.wp.element,n=(window.wp.i18n,window.wp.htmlEntities),a=window.wc.wcBlocksRegistry,s=(0,window.wc.wcSettings.getSetting)("omise_duitnow_obw_data",{}),l=(0,n.decodeEntities)(s.title)||"No title set",o=a=>{const{eventRegistration:l,emitResponse:o}=a,{onPaymentSetup:c}=l,r=(0,n.decodeEntities)(s.description||""),{banks:i}=s.data,[m,p]=(0,t.useState)(null);return(0,t.useEffect)((()=>{const e=c((async()=>{if(!m)return{type:o.responseTypes.ERROR,message:"Select a bank"};try{return{type:o.responseTypes.SUCCESS,meta:{paymentMethodData:{bank:m}}}}catch(e){return{type:o.responseTypes.ERROR,message:e.message}}}));return()=>e()}),[o.responseTypes.ERROR,o.responseTypes.SUCCESS,c,m]),(0,e.createElement)(e.Fragment,null,r&&(0,e.createElement)("p",null,r),(0,e.createElement)("fieldset",{id:"omise-form-installment"},(0,e.createElement)("div",{className:"fpx-select-bank"},(0,e.createElement)("label",{htmlFor:"fpx-select-bank"},"Select Bank"),(0,e.createElement)("select",{className:"fpx-bank-logo default",id:"duitnow-obw-select-bank",name:"source[bank]",defaultValue:"",onChange:e=>{p(e.target.value)}},(0,e.createElement)("option",{value:"",disabled:!0},"-- Select your option --"),Object.values(i).map((t=>(0,e.createElement)("option",{key:t.code,className:t.code,value:t.code},t.name)))))))};(0,a.registerPaymentMethod)({name:s.name||"",label:(0,e.createElement)((t=>{const{PaymentMethodLabel:n}=t.components;return(0,e.createElement)(n,{text:l})}),null),content:(0,e.createElement)(o,null),edit:(0,e.createElement)(o,null),canMakePayment:()=>s.is_active,ariaLabel:l,supports:{features:s.supports}})})(); \ No newline at end of file diff --git a/includes/blocks/assets/js/omise-duitnow-obw.js b/includes/blocks/assets/js/omise-duitnow-obw.js index 810ad9ae..f3bbad51 100644 --- a/includes/blocks/assets/js/omise-duitnow-obw.js +++ b/includes/blocks/assets/js/omise-duitnow-obw.js @@ -63,7 +63,7 @@ const DuitNowOBWPaymentMethod = (props) => { > { - banks.map((bank) => ( + Object.values(banks).map((bank) => (