跳至正文

PaymentFlow

PaymentFlow 将收款与确认操作分离开来。presentPaymentFlow负责收集付款信息,并返回一个待处理的卡片信息。confirmPaymentFlow则负责在稍后的阶段确认付款操作,通常是在经过审核之后。

Image from Gyazo

使用 PaymentIntent 或 SetupIntent,并在服务器上创建对象。参见服务器集成。

平台支持

平台 PaymentFlow
iOS 原生 PaymentSheet.FlowController
Android 原生 PaymentSheet.FlowController
Web stripe-pwa-elements 银行卡模态窗口

Web 支持 paymentIntentClientSecret 或 setupIntentClientSecret,并可选传入 withZipCode。defaultBillingDetails、shippingDetails、billingDetailsCollectionConfiguration、enableApplePay、enableGooglePay、style 和 returnURL 等原生平台专用选项在 Web 上会被忽略。

1. createPaymentFlow

从后端获取可安全交给客户端的密钥,然后调用 createPaymentFlow。paymentIntentClientSecret 与 setupIntentClientSecret 必须且只能提供一个。customerId 和 customerEphemeralKeySecret 可一起省略;如果设置 customerId,就必须同时设置 customerEphemeralKeySecret。

import { firstValueFrom } from 'rxjs';
import { PaymentFlowEventsEnum, Stripe } from '@capacitor-community/stripe';

const { paymentIntent, ephemeralKey, customer } = await firstValueFrom(
  this.http.post<{
    paymentIntent: string;
    ephemeralKey: string;
    customer: string;
  }>(environment.api + 'intent', {}),
);

await Stripe.createPaymentFlow({
  paymentIntentClientSecret: paymentIntent,
  customerEphemeralKeySecret: ephemeralKey,
  customerId: customer,
  merchantDisplayName: 'rdlabo',
});

method createPaymentFlow(...)

创建 PaymentFlow 实例。应用需要先采集付款信息、稍后再确认时,请使用 PaymentFlow。

createPaymentFlow(options: CreatePaymentFlowOption) => Promise<void>

interface CreatePaymentFlowOption

属性 类型 说明 默认值 起始版本
paymentIntentClientSecret string 待确认的 PaymentIntent 的客户端密钥。paymentIntentClientSecret 与 setupIntentClientSecret 必须且只能提供一个。 3.0.2
setupIntentClientSecret string 用于保存付款方式的 SetupIntent 的客户端密钥。paymentIntentClientSecret 与 setupIntentClientSecret 必须且只能提供一个。 3.0.2
defaultBillingDetails DefaultBillingDetails 用于预填 PaymentSheet 的账单信息。仅适用于 iOS 和 Android。 https://docs.stripe.com/payments/mobile/collect-addresses?payment-ui=mobile&platform=ios#set-default-billing-details 7.2.0
shippingDetails AddressDetails 用于预填 PaymentSheet 的配送信息。仅适用于 Android;在 iOS 上请使用 Stripe 的地址组件。 https://docs.stripe.com/payments/mobile/collect-addresses?payment-ui=mobile&platform=android#prefill-addresses 7.2.0
billingDetailsCollectionConfiguration BillingDetailsCollectionConfiguration 控制 PaymentSheet 收集哪些账单信息。仅适用于 iOS 和 Android。 https://docs.stripe.com/payments/mobile/collect-addresses?payment-ui=mobile&platform=ios#customize-billing-details-collection 7.2.0
customerEphemeralKeySecret string 服务器为 Customer 返回的临时密钥。必须与 customerId 一起提供,不能只传其中一个。 3.0.0
customerId string 与 customerEphemeralKeySecret 关联的 Stripe Customer ID。 3.0.0
enableApplePay boolean 在原生 PaymentSheet 中启用 Apple Pay。仅适用于 iOS。 false 3.3.0
applePayMerchantId string 为应用配置的 Apple 商户标识符。当 enableApplePay 为 true 时必填,否则忽略。 3.3.0
enableGooglePay boolean 在原生 PaymentSheet 中启用 Google Pay。仅适用于 Android。 false 3.2.0
GooglePayIsTesting boolean 使用 Google Pay 测试环境。仅适用于 Android。 false 3.2.0
countryCode string Apple Pay 或 Google Pay 使用的两字母 ISO 3166-1 国家代码。两个钱包均未启用时忽略此值。 "US" 3.2.0
merchantDisplayName string 原生 PaymentSheet 上显示的商户名称。 "App Name" 3.0.0
returnURL string 通过重定向完成身份验证后返回应用所用的自定义 URL 方案。仅适用于 iOS。未配置时,Stripe 不会提供原本可用的重定向类付款方式。在应用的 URL 处理函数中将返回的 URL 转交给 handleURLCallback。 "" 3.0.0
paymentMethodLayout 'automatic' | 'horizontal' | 'vertical' 在 iOS 和 Android 上,PaymentSheet 展示付款方式时采用的布局。 "automatic" 7.2.2
allowsDelayedPaymentMethods boolean 在 iOS 和 Android 的 PaymentSheet 中显示美国银行账户(ACH)、SEPA 借记等延迟结算的付款方式。由于资金结算需要时间,PaymentSheet 默认隐藏这些方式。 false 8.3.0
style 'alwaysLight' | 'alwaysDark' 覆盖原生 PaymentSheet 的外观配置。仅适用于 iOS。 undefined 3.0.0
withZipCode boolean 在 Web 银行卡表单中显示邮政编码字段。仅适用于 Web。 true 3.6.0
currencyCode string Google Pay 使用的三字母 ISO 4217 货币代码。为 SetupIntent 启用 Google Pay 时必填。 "USD" 7.1.0

在 iOS 上,为 PayPal、3D Secure 以及其他需要重定向的支付方式配置 returnURL 和 handleURLCallback。没有返回 URL 时,即使满足其他条件,Stripe 也不会提供这些支付方式。请参阅 iOS 上需要重定向的支付方式。

从 v8.3.0 起,在创建选项中设置 allowsDelayedPaymentMethods: true 即可在 iOS 和 Android 上启用符合条件的 ACH、SEPA Debit 等延迟结算方式。默认值为 false;此选项对 Web 无效。请在 Stripe 中启用相关方式并相应配置 Intent。即使结果为 Completed,也可能仍在处理付款;收到付款成功的 webhook 后才可履约。参见 Stripe 延迟结算方式指南。

2. presentPaymentFlow

仅在 createPaymentFlow 成功后调用 presentPaymentFlow。返回的 cardNumber 是掩码值,Intent 此时尚未确认。

const presentResult = await Stripe.presentPaymentFlow();
console.log(presentResult); // { cardNumber: "●●●● ●●●● ●●●● ****" }

method presentPaymentFlow()

展示由 createPaymentFlow() 创建的 PaymentFlow,返回的 Promise 在完成时提供所选卡号的后四位。

presentPaymentFlow() => Promise<{ cardNumber: string; }>

如果顾客取消,Promise 会被拒绝,或触发 Canceled 事件。必须先收到 Created 或 presentPaymentFlow 的成功结果,才能调用 confirmPaymentFlow。

3. confirmPaymentFlow

const confirmResult = await Stripe.confirmPaymentFlow();
if (confirmResult.paymentResult === PaymentFlowEventsEnum.Completed) {
  // 这里只更新界面。履行订单之前,请在服务器上通过 webhook 确认付款成功。
}

method confirmPaymentFlow()

确认 presentPaymentFlow() 采集的付款信息。

confirmPaymentFlow() => Promise<{ paymentResult: PaymentFlowResultInterface; }>

type alias PaymentFlowResultInterface

PaymentFlowEventsEnum.Completed | PaymentFlowEventsEnum.Canceled | PaymentFlowEventsEnum.Failed

将 Canceled 视为取消,Failed 视为错误。两者都不能单独作为履约依据。

4. addListener

在应用启动时注册一次结果监听器。Android Activity 重建后,优先通过事件(包括 Created)获取结果,而非依赖 Promise。详见事件监听器。

await Promise.all([
  Stripe.addListener(PaymentFlowEventsEnum.Created, (info) => {
    console.log(info.cardNumber);
  }),
  Stripe.addListener(PaymentFlowEventsEnum.Completed, () => {
    console.log('PaymentFlowEventsEnum.Completed');
  }),
  Stripe.addListener(PaymentFlowEventsEnum.Canceled, () => {
    console.log('PaymentFlowEventsEnum.Canceled');
  }),
  Stripe.addListener(PaymentFlowEventsEnum.Failed, (error) => {
    console.log('PaymentFlowEventsEnum.Failed', error);
  }),
]);

enum PaymentFlowEventsEnum

成员 值
Loaded 'paymentFlowLoaded'
FailedToLoad 'paymentFlowFailedToLoad'
Opened 'paymentFlowOpened'
Created 'paymentFlowCreated'
Completed 'paymentFlowCompleted'
Canceled 'paymentFlowCanceled'
Failed 'paymentFlowFailed'

参考

payment-flow.ts
import { PaymentFlowEventsEnum, Stripe } from '@capacitor-community/stripe';

(async () => {
  await Stripe.addListener(PaymentFlowEventsEnum.Completed, () => {
    console.log('PaymentFlowEventsEnum.Completed');
  });

  // Replace `/your-intent-endpoint` with your backend from Server Integration.
  const response = await fetch('/your-intent-endpoint', {
    method: 'POST',
  });
  if (!response.ok) {
    throw new Error(`Intent request failed: ${response.status}`);
  }
  const { paymentIntent, ephemeralKey, customer } = (await response.json()) as {
    paymentIntent: string;
    ephemeralKey: string;
    customer: string;
  };

  // Prepare PaymentFlow with CreatePaymentFlowOption.
  await Stripe.createPaymentFlow({
    paymentIntentClientSecret: paymentIntent,
    // setupIntentClientSecret: setupIntent,
    customerEphemeralKeySecret: ephemeralKey,
    customerId: customer,
  });

  // Collect payment details. The Intent is not confirmed yet.
  const presentResult = await Stripe.presentPaymentFlow();
  console.log(presentResult); // { cardNumber: "●●●● ●●●● ●●●● ****" }

  // Confirm PaymentFlow. Completed.
  const confirmResult = await Stripe.confirmPaymentFlow();
  if (confirmResult.paymentResult === PaymentFlowEventsEnum.Completed) {
    // Update UI only. Fulfill orders from a verified server webhook.
  }
})();