使用离子电容器尝试通过 apple pay 进行条纹时出现意外错误

unexpected error when trying stripe with apple pay using ionic capacitor

我正在使用 https://github.com/capacitor-community/stripe 将 ionic-angular 应用与 Apple Pay 集成。

有效载荷和代码如下所示

const Stripe = Plugins.Stripe as StripePlugin;
Stripe.setPublishableKey({ key: 'xxxx' }); //test key
const clientSecret: string = 'xxxx'; //test secret

pay(){
 await Stripe.confirmPaymentIntent({
      clientSecret,
      applePayOptions: {
        // options here
        merchantId: 'merchant.xxx.xxx',
        country: 'US',
        currency: 'USD',
        items: [
          {
            label: 'my desc',
            amount: 1, // amount in dollars
          }
        ]
      },
    })
}

这很好地显示了 apple 付款单,但是当我双击确认付款时出现错误,我可以在 xcode 控制台中看到如下所示

ERROR MESSAGE: {"message":"payment failed: There was an unexpected error -- try again in a few seconds","errorMessage":"There was an unexpected error -- try again in a few seconds"}

我已经在设备上尝试过,也使用了实时密钥,但没有成功

Stripe 只允许整数作为金额 (1 = 1cent),因此 0.5$ 为 50,1$ 为 100。

const Stripe = Plugins.Stripe as StripePlugin;
Stripe.setPublishableKey({ key: 'xxxx' }); //test key
const clientSecret: string = 'xxxx'; //test secret

pay(){
 await Stripe.confirmPaymentIntent({
      clientSecret,
      applePayOptions: {
        // options here
        merchantId: 'merchant.xxx.xxx',
        country: 'US',
        currency: 'USD',
        items: [
          {
            label: 'my desc',
            amount: 50, // amount in cents
          }
        ]
      },
    })
}

下面的代码对我有用。 请注意,它是 1 美元,但 stripe 使用 100 美元 1 美元。

try {
  const paymentIntent = await this.stripeService.createPaymentIntentOnServer(100);

  const result = await StripePlg.confirmPaymentIntent(
    { clientSecret: paymentIntent.client_secret,
      applePayOptions: {
      merchantId: 'merchant.app.myapp',
      items: [{ label: 'Bread sticks', amount: 1, },],
      currency: 'NZD',
      country: 'NZ', 
    }, 
  });

  // then call finalizeApplePayTransaction with the result to dismiss the UI modal
  StripePlg.finalizeApplePayTransaction({ success: true });
} catch (err) {
  console.log(err);
  StripePlg.finalizeApplePayTransaction({ success: false })
}

// Server
// createPaymentIntentOnServer - this returns the client_secret
const paymentIntent = await stripe.paymentIntents.create({
  amount: paymentAmount, // 100
  currency: 'nzd',
  payment_method_types: ['card'],
  customer: customer.id,  // I use a Stripe Customer here which I create first
  expand: ['invoice'] 
});