Stripe JS Angular,无法在 paymentRequest.on("paymentMethod") 内调用方法

Stripe JS Angular, cannot call method within paymentRequest.on("paymentMethod")

我想在我的 angular 应用程序中使用条纹处理付款。为此,我创建了这个方法:

 paymentRequest.on('paymentmethod', function (ev) {
    console.log(ev.paymentMethod.id);
    const clienSecret = dataset.secret
    stripe.confirmCardPayment(
      clienSecret,
      {payment_method: ev.paymentMethod.id},
      {handleActions: false}
    ).then(function (confirmResult) {
      if (confirmResult.error) {
        // Report to the browser that the payment failed, prompting it to
        // re-show the payment interface, or show an error message and close
        // the payment interface.
        alert('Transaktion fehlgeschlagen');
        ev.complete('fail');
      } else {
        // Report to the browser that the confirmation was successful, prompting
        // it to close the browser payment method collection interface.
        ev.complete('success');

        // Check if the PaymentIntent requires any actions and if so let Stripe.js
        // handle the flow. If using an API version older than "2019-02-11" instead
        // instead check for: `paymentIntent.status === "requires_source_action"`.
        if (confirmResult.paymentIntent.status === 'requires_action') {
          // Let Stripe.js handle the rest of the payment flow.
          stripe.confirmCardPayment(clienSecret).then(function (result) {
            if (result.error) {
              alert('Bezahlung fehlgeschlagen')
              // The payment failed -- ask your customer for a new payment method.
            } else {
              // The payment has succeeded.
              this.completePaymentInOrder();
              console.log('payment succeeeded');
            }
          });
        } else {
          this.completePaymentInOrder();
          console.log('payment succeeded');
        }
      }
    });
  });

不幸的是我无法调用方法 completePaymentInOrder();

我收到无法读取未定义的 属性“completePaymentInOrder”的错误。

这就是我创建方法的方式:

  completePaymentInOrder() {
const url = `https://myurl/delivery/payment/approved?orderId=${this.orderid}`;
this.http.get<any>(url, {
  headers: {token: this.apiKey},
}).subscribe(async dataset => {
  // TODO perform Deeplink
}, err => {
  alert('Status konnte nicht aktualisiert werden, bitte Support kontaktieren.');
});

}

"this" 范围将在 promise 内更改。所以你必须在顶部声明 "this" 并且你可以调用函数

let newThis = this; // assigning this scope to new vaiable

paymentRequest.on('paymentmethod', function (ev) {
console.log(ev.paymentMethod.id);
const clienSecret = dataset.secret
stripe.confirmCardPayment(
  clienSecret,
  {payment_method: ev.paymentMethod.id},
  {handleActions: false}
).then(function (confirmResult) {
  if (confirmResult.error) {
    // Report to the browser that the payment failed, prompting it to
    // re-show the payment interface, or show an error message and close
    // the payment interface.
    alert('Transaktion fehlgeschlagen');
    ev.complete('fail');
  } else {
    // Report to the browser that the confirmation was successful, prompting
    // it to close the browser payment method collection interface.
    ev.complete('success');

    // Check if the PaymentIntent requires any actions and if so let Stripe.js
    // handle the flow. If using an API version older than "2019-02-11" instead
    // instead check for: `paymentIntent.status === "requires_source_action"`.
    if (confirmResult.paymentIntent.status === 'requires_action') {
      // Let Stripe.js handle the rest of the payment flow.
      stripe.confirmCardPayment(clienSecret).then(function (result) {
        if (result.error) {
          alert('Bezahlung fehlgeschlagen')
          // The payment failed -- ask your customer for a new payment method.
        } else {
          // The payment has succeeded.
          newThis.completePaymentInOrder();
          console.log('payment succeeeded');
        }
      });
    } else {
      this.completePaymentInOrder();
      console.log('payment succeeded');
    }
  }
 });
});