PaymentSheet
PaymentSheet は支払い情報の入力と Intent の確定を一度の表示で行います。カードを保留状態にして後から確定する必要がある場合は PaymentFlowを使用してください。
すぐに課金するには PaymentIntent、支払い方法を後で使うため保存するには SetupIntent を使用します。これらはサーバーで作成します。サーバー連携を参照してください。
プラットフォーム対応
| プラットフォーム | PaymentSheet |
|---|---|
| iOS | ネイティブ Stripe PaymentSheet |
| Android | ネイティブ Stripe PaymentSheet |
| Web | stripe-pwa-elements のカードモーダル |
Web はネイティブ PaymentSheet を表示しません。Web の createPaymentSheet は paymentIntentClientSecret と任意の withZipCode を使用し、現在 SetupIntent には対応していません。defaultBillingDetails、shippingDetails、enableApplePay、enableGooglePay、style、returnURL などのネイティブ専用オプションは無視されます。
1. createPaymentSheet
バックエンドからクライアントへ安全に渡せるシークレットを取得し、createPaymentSheet を呼びます。プラグインは Stripe のシークレット API を呼びません。HttpClient、fetch などを利用してください。
iOS と Android では paymentIntentClientSecret と setupIntentClientSecret のどちらか一方を、Web では paymentIntentClientSecret を渡します。customerId と customerEphemeralKeySecret は任意ですが、customerId を設定する場合は両方が必要です。Customer を持たない PaymentIntent も有効です。
const { paymentIntent, ephemeralKey, customer } = await firstValueFrom(
this.http.post(environment.api + 'intent', {}),
);
await Stripe.createPaymentSheet({
paymentIntentClientSecret: paymentIntent,
customerId: customer,
customerEphemeralKeySecret: ephemeralKey,
merchantDisplayName: 'rdlabo',
});
method createPaymentSheet(...)
Creates and configures a PaymentSheet instance. Wait for this Promise or
the Loaded event before calling presentPaymentSheet().
createPaymentSheet(options: CreatePaymentSheetOption) => Promise<void>
interface CreatePaymentSheetOption
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
paymentIntentClientSecret |
string |
Client secret of the PaymentIntent to confirm. Provide exactly one of paymentIntentClientSecret or setupIntentClientSecret. |
3.0.0 | |
setupIntentClientSecret |
string |
Client secret of the SetupIntent used to save a payment method. Provide exactly one of paymentIntentClientSecret or setupIntentClientSecret. |
3.0.0 | |
defaultBillingDetails |
DefaultBillingDetails |
Billing details used to prefill PaymentSheet. iOS and Android only. https://docs.stripe.com/payments/mobile/collect-addresses?payment-ui=mobile&platform=ios#set-default-billing-details | 7.2.0 | |
shippingDetails |
AddressDetails |
Shipping details used to prefill PaymentSheet. Android only; on iOS use Stripe's address element instead. https://docs.stripe.com/payments/mobile/collect-addresses?payment-ui=mobile&platform=android#prefill-addresses | 7.2.0 | |
billingDetailsCollectionConfiguration |
BillingDetailsCollectionConfiguration |
Controls which billing details PaymentSheet collects. iOS and Android only. https://docs.stripe.com/payments/mobile/collect-addresses?payment-ui=mobile&platform=ios#customize-billing-details-collection | 7.2.0 | |
customerEphemeralKeySecret |
string |
Customer ephemeral-key secret returned by your server. Use together with customerId; do not provide only one of the pair. |
3.0.0 | |
customerId |
string |
Stripe Customer ID associated with customerEphemeralKeySecret. |
3.0.0 | |
enableApplePay |
boolean |
Enables Apple Pay in native PaymentSheet. iOS only. | false | 3.3.0 |
applePayMerchantId |
string |
Apple merchant identifier configured for the app. Required when enableApplePay is true and ignored otherwise. |
3.3.0 | |
enableGooglePay |
boolean |
Enables Google Pay in native PaymentSheet. Android only. | false | 3.2.0 |
GooglePayIsTesting |
boolean |
Uses the Google Pay test environment. Android only. | false | 3.2.0 |
countryCode |
string |
Two-letter ISO 3166-1 country code used by Apple Pay or Google Pay. Ignored when neither wallet is enabled. | "US" | 3.2.0 |
merchantDisplayName |
string |
Merchant name displayed in native PaymentSheet. | "App Name" | 3.0.0 |
returnURL |
string |
Custom URL scheme used to return to the app after redirect-based authentication. iOS only. | "" | 3.0.0 |
paymentMethodLayout |
'automatic' | 'horizontal' | 'vertical' |
Layout used to display payment methods in PaymentSheet on iOS and Android. | "automatic" | 7.2.2 |
style |
'alwaysLight' | 'alwaysDark' |
Appearance override for native PaymentSheet. iOS only. | undefined | 3.0.0 |
withZipCode |
boolean |
Shows the ZIP-code field in the web card form. Web only. | true | 3.6.0 |
currencyCode |
string |
Three-letter ISO 4217 currency code used by Google Pay. Required when Google Pay is enabled for a SetupIntent. | "USD" | 7.1.0 |
ネイティブでは style、enableApplePay と applePayMerchantId、enableGooglePay、iOS 3D Secure 用の returnURL、請求先収集設定などを任意で指定できます。withZipCode は Web 専用です。SetupIntent で enableGooglePay を有効にする場合は currencyCode が必要です。
2. presentPaymentSheet
createPaymentSheet が成功した後だけ呼び出します。
const result = await Stripe.presentPaymentSheet();
if (result.paymentResult === PaymentSheetEventsEnum.Completed) {
// UIだけを更新します。注文確定前にWebhookでIntentを確認してください。
}
Canceled は利用者がシートを閉じた状態、Failed はエラーです。どちらの結果だけでも注文を確定できません。
method presentPaymentSheet()
Presents the PaymentSheet created by createPaymentSheet() and resolves
with its completed, canceled, or failed result.
presentPaymentSheet() => Promise<{ paymentResult: PaymentSheetResultInterface; }>
type alias PaymentSheetResultInterface
PaymentSheetEventsEnum.Completed | PaymentSheetEventsEnum.Canceled | PaymentSheetEventsEnum.Failed
3. addListener
結果リスナーはシートを表示する前に、アプリケーション起動時に一度だけ登録します。Android Activity の再生成後は Promise よりイベントを優先してください。イベントリスナーを参照してください。
await Promise.all([
Stripe.addListener(PaymentSheetEventsEnum.Completed, () => console.log('Completed')),
Stripe.addListener(PaymentSheetEventsEnum.Canceled, () => console.log('Canceled')),
Stripe.addListener(PaymentSheetEventsEnum.Failed, (error) => console.log(error)),
]);
enum PaymentSheetEventsEnum
| Member | Value |
|---|---|
Loaded |
'paymentSheetLoaded' |
FailedToLoad |
'paymentSheetFailedToLoad' |
Completed |
'paymentSheetCompleted' |
Canceled |
'paymentSheetCanceled' |
Failed |
'paymentSheetFailed' |
参考資料
import { firstValueFrom } from 'rxjs';
import { PaymentSheetEventsEnum, Stripe } from '@capacitor-community/stripe';
(async () => {
Stripe.addListener(PaymentSheetEventsEnum.Completed, () => {
console.log('PaymentSheetEventsEnum.Completed');
});
// Connect to your backend endpoint, and get every key.
const { paymentIntent, ephemeralKey, customer } = await firstValueFrom(this.http.post<{
paymentIntent: string;
ephemeralKey: string;
customer: string;
}>(environment.api + 'intent', {}));
// prepare PaymentSheet with CreatePaymentSheetOption.
await Stripe.createPaymentSheet({
paymentIntentClientSecret: paymentIntent,
customerId: customer,
customerEphemeralKeySecret: ephemeralKey,
});
// present PaymentSheet and get result.
const result = await Stripe.presentPaymentSheet();
if (result.paymentResult === PaymentSheetEventsEnum.Completed) {
// Happy path
}
})();
