0
votes

How to call paymentForm.requestCardNonce() method asynchronous

I am integrating square payment gateway in AngularJs, Now I have situation where I have to call paymentForm.requestCardNonce() and this method calls web service in background. Now I have to wait for background call than I can proceed with furthered process. So how can i handle callback from paymentForm.requestCardNonce() method which is provided by Square Payment Gateway https://docs.connect.squareup.com/payments/sqpaymentform/setup

Initialising code sample

var paymentForm = new SqPaymentForm({
    applicationId: "sandbox-xxx-xxxxxxxxxxxx",
    inputClass: 'sq-input',
    callbacks: {
        cardNonceResponseReceived: function (errors, nonce, cardData, billingContact, shippingContact) {
        // I am receiving response of web service Here
        }
    }
}

How can I get success response in my custom function by calling method in it ?(Note - its working and calling a method also but i have to do next steps based on response from web service)

addCard = function () {
   paymentForm.requestCardNonce();
   // Till this function executes I need to wait here. how can I write promise here ?
}

Refrence LInks https://docs.connect.squareup.com/payments/sqpaymentform/how-it-works

https://github.com/square/connect-api-examples/tree/master/templates/web-ui/payment-form/basic

1
Do you mind explaining what you’re trying to do? requestCardNonce is asynchronous, and when it completes it will call cardNonceResponseReceived, so if you’re wanting to call a function after it’s finished, you should place it in cardNonceResponseReceived. - sjosey
hi @sjosey, As you explained I have implemented and it works! I have added next execution code to another function and called in cardNonceResponseReceived. Thanks - Ajarudin Gunga
Anyone know, Is possible with real-time inventory sync with square and Magento 1.x? - Gem

1 Answers

1
votes

For posterity, pulling from the comments.

requestCardNonce is asynchronous, and when it completes it will call cardNonceResponseReceived, so if you’re wanting to call a function after it’s finished, you should place it in cardNonceResponseReceived.