0

I have a window event in Angular 6. On the token event I need to call my Angular method, how do I do this?

openCheckout(order) {

  var handler = (<any>window).StripeCheckout.configure({
    key: 'pk_test_',
    locale: 'auto',
    token: function (token: any) {
      console.log(token.id);
      console.log(token.email);
      // --- makePayment ERRORS AS UNDEFINED
      this.makePayment(token.id, token.email);
      location.reload();
    }
  });

  handler.open({
    name: 'Name',
    description: 'Desc',
    currency: 'gbp',
    amount: order.transactionAmount,
    image: 'https://stripe.com/img/documentation/checkout/marketplace.png'
  });
}

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Doolali
  • 956
  • 9
  • 13
  • 2
    Possible duplicate of [How to access the correct \`this\` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – ConnorsFan Oct 19 '18 at 00:18
  • 1
    Define the callback as an arrow function: `token: (token: any) => { ... }`. – ConnorsFan Oct 19 '18 at 00:19

1 Answers1

0

Thanks Connor, that did the trick!

 var handler = (<any>window).StripeCheckout.configure({
  key: 'pk_test_',
  locale: 'auto',
  token: (token: any) => {
    console.log(token.id);
    console.log(token.email);
    this.makePayment(token.id, token.email);
  }
});
Doolali
  • 956
  • 9
  • 13