-1

I'm trying to implement GMO payment gateway to woocommerce. Here goes the flow: 1. get credit card info from user [checkout page] 2. run a javascript api function call and it will responce by executing another js function defined by me 3. from the new executed js function, I can get the token and some other info 4. Now I need that token in the function of process_payment [woocommerce]

Here is the JS code:

<script type="text/javascript">
        function execPurchase(response) {
            if (response.resultCode != "000") {
                window.alert("購入処理中にエラーが発生しました");
            } else {

                document.getElementById("token").value = response.tokenObject.token;
                document.getElementById("purchaseForm").submit();
            }
        }
        function doPurchase() {
            var cardno, expire, securitycode, holdername;
            var cardno = document.getElementById("CardNumber").value.replace(/\s/g, '');
            var expire = document.getElementById("expireYear").value + document.getElementById("expireMonth").value;
            var securitycode = document.getElementById("securityCode").value;
            var holdername = document.getElementById("holderName").value;
            var tokennumber = document.getElementById("tokenNumber").value;

            Multipayment.init(shop_id);
            Multipayment.getToken({
                cardno : cardno,
                expire : expire,
                securitycode : securitycode,
                holdername : holdername,
                tokennumber : tokennumber
            }, execPurchase);
        }
    </script>
Nazmul Hasan
  • 67
  • 1
  • 7
  • Why not just use AJAX? Send a value back from the Server, test for it, execute on the Client what's already on the Client. – StackSlave Jan 10 '20 at 06:50

1 Answers1

0

There are several ways to achieve your goal :

1) There are various woocommerce hooks for checkout page , which you can use for applying your js .

Here is the visual guide for checkout page hooks https://businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/

2) If you are not comfortable with hooks and are using a custom theme or can customize in the theme , so you can override woocommerce checkout page and use easily your js function with full customizable ability as you wish.

Here is how the link to override woocommerce templates from theme : https://docs.woocommerce.com/document/template-structure/

you can run js in php by escaping like :

?>
<script>
function execPurchase(response) {
// your js here
}
</script>
<?php

or by using echo

echo '<script type="text/javascript">',
     'execPurchase();',
     '</script>'
;

I Hope this will help you. Good Luck, Thanks

Community
  • 1
  • 1