Search code examples
javascriptbraintree

Call function inside onPaymentMethodReceived(obj) method of Braintree


I am trying to call the function in onPaymentMethodReceived:function (obj) method but I am getting an error the obj contains the payments nonce.

My JS code is

 this.braintree.setup(this.clientToken, "dropin", {
   container: "dropin-container",
   onPaymentMethodReceived:function (obj) {
     console.log("nonce  "+obj.nonce);
     this.ckeckoutClick(obj.nonce)  //I want call this function which is post request to server//
   }
 })

my HTML is

<form id="checkout">
  <div id="dropin-container"></div>
  <input type="submit" value="Place Order">
</form>

Please tell me what I am doing wrong

Thank you


Solution

  • If you want to use this inside the function use () => instead of function ()

       this.braintree.setup(this.clientToken, "dropin", {
            container: "dropin-container",
            onPaymentMethodReceived: (obj) => {
                console.log("nonce  "+obj.nonce);
                this.ckeckoutClick(obj.nonce)  //I want call this function which is post request to server//
             }
        })
    

    otherwise this.checkoutClick() will call checkoutClick on braintree or from wherever the callback is called.