カード支払いの事前設定
手動でのサーバー側の確定を使用するか、支払い方法を別途提示します。
警告
この場合、支払いの事前設定ガイドに従うことをお勧めします。このガイドは、手動でのサーバー側の確定を使用する必要がある場合、またはお使いのシステムで支払い方法を別に提示する必要がある場合にのみ使用してください。すでに Elements との連携が完了している場合は、Payment Element 移行ガイドをご覧ください。
Setup Intents API を使用すると、初回の支払いなしで顧客のカードを保存することができます。これは、今すぐ顧客をアカウント登録して支払いを設定し、将来顧客がオフラインの際に請求するときに役立ちます。
この組み込みを使用して、継続支払いを設定したり、最終金額が後で (顧客がサービスを受け取った後などに) 決定される 1 回限りの支払いを作成します。
Stripe を設定するサーバ側クライアント側![](https://b.stripecdn.com/docs-statics-srv/assets/fcc3a1c24df6fcffface6110ca4963de.svg)
サーバ側![](https://b.stripecdn.com/docs-statics-srv/assets/fcc3a1c24df6fcffface6110ca4963de.svg)
この組み込みには、Stripe API と通信するエンドポイントがサーバ上に必要です。Stripe の公式ライブラリを使用して、サーバから Stripe API にアクセスします。
クライアント側![](https://b.stripecdn.com/docs-statics-srv/assets/fcc3a1c24df6fcffface6110ca4963de.svg)
React Native SDK はオープンソースであり、詳細なドキュメントが提供されています。内部では、ネイティブの iOS および Android の SDK を使用します。Stripe の React Native SDK をインストールするには、プロジェクトのディレクトリーで (使用するパッケージマネージャーによって異なる) 次のいずれかのコマンドを実行します。
次に、その他の必要な依存関係をインストールします。
- iOS の場合は、ios ディレクトリーに移動して
pod install
を実行し、必要なネイティブ依存関係もインストールします。 - Android の場合は、依存関係をインストールする必要はありません。
Stripe の初期化![](https://b.stripecdn.com/docs-statics-srv/assets/fcc3a1c24df6fcffface6110ca4963de.svg)
React Native アプリで Stripe を初期化するには、決済画面を StripeProvider
コンポーネントでラップするか、initStripe
初期化メソッドを使用します。publishableKey
の API 公開可能キーのみが必要です。次の例は、StripeProvider
コンポーネントを使用して Stripe を初期化する方法を示しています。
import { StripeProvider } from '@stripe/stripe-react-native'; function App() { const [publishableKey, setPublishableKey] = useState(''); const fetchPublishableKey = async () => { const key = await fetchKey(); // fetch key from your server here setPublishableKey(key); }; useEffect(() => { fetchPublishableKey(); }, []); return ( <StripeProvider publishableKey={publishableKey} merchantIdentifier="merchant.identifier" // required for Apple Pay urlScheme="your-url-scheme" // required for 3D Secure and bank redirects > // Your app code here </StripeProvider> ); }
設定前に Customer を作成するサーバー側![](https://b.stripecdn.com/docs-statics-srv/assets/fcc3a1c24df6fcffface6110ca4963de.svg)
将来の支払いに備えてカードを設定するには、カードを Customer (顧客) に関連付ける必要があります。顧客がビジネスでアカウントを作成する際に、Customer オブジェクトを作成します。Customer オブジェクトを使用すると、支払い方法を再利用したり、複数の支払いを追跡したりできます。
作成に成功すると、Customer オブジェクトが返されます。オブジェクトで顧客の id
を調べて、その値を後で取得できるようにデータベースに保存できます。
これらの顧客は、ダッシュボードの顧客ページで見つけることができます。
SetupIntent を作成するサーバー側![](https://b.stripecdn.com/docs-statics-srv/assets/fcc3a1c24df6fcffface6110ca4963de.svg)
SetupIntent (支払い方法設定インテント) は、将来の支払いに備えて支払い方法を設定するという意図を示すオブジェクトです。SetupIntent オブジェクトには、アプリに渡すための一意のキーである、client secret が格納されます。
client secret を使用することで、customer
のような機密情報を保護すると同時に、設定の確認や支払い方法の詳細の更新などの一定のアクションをクライアント側で実行できるようになります。client secret を使用すると、クレジットカードネットワークを利用してカード詳細を検証および認証できます。client secret は機密情報なので、ログに記録したり、URL に埋め込んだり、当該の顧客以外に漏洩することがないようにしてください。
サーバー側![](https://b.stripecdn.com/docs-statics-srv/assets/fcc3a1c24df6fcffface6110ca4963de.svg)
サーバー側で SetupIntent を作成し、その client secret をアプリに返すエンドポイントを作成します。
If you only plan on using the card for future payments when your customer is present during the checkout flow, set the usage parameter to on_session to improve authorization rates.
カード詳細を収集するクライアント側![](https://b.stripecdn.com/docs-statics-srv/assets/fcc3a1c24df6fcffface6110ca4963de.svg)
カード番号、有効期限、セキュリティコード、郵便番号を収集する、SDK が提供する UI コンポーネント、CardField
を使用して、クライアント側でカード情報を安全に収集します。
CardField
は、リアルタイムで検証と形式の設定を行います。
CardField
コンポーネントを支払い画面に追加することで、顧客からカード詳細を安全に収集します。onCardChange
コールバックを使用して、カードのブランドや詳細情報の欠落の有無など、カードに関する機密性の低い情報を検査します。
import { CardField, useStripe } from '@stripe/stripe-react-native'; function PaymentScreen() { // ... return ( <View> <CardField postalCodeEnabled={true} placeholders={{ number: '4242 4242 4242 4242', }} cardStyle={{ backgroundColor: '#FFFFFF', textColor: '#000000', }} style={{ width: '100%', height: 50, marginVertical: 30, }} onCardChange={(cardDetails) => { console.log('cardDetails', cardDetails); }} onFocus={(focusedField) => { console.log('focusField', focusedField); }} /> </View> ); }
注意
特にヨーロッパでは、カードの再利用に関する規制があるため、カード詳細を保存して将来のオフセッションの支払いに使用する際は、カードを保存するための許可を取得します。カードをどのように使用するかを顧客に知らせるテキストを決済フローに含めてください。
設定を完了するには、顧客のカードと請求情報を confirmSetupIntent
に渡します。このメソッドには、useConfirmSetupIntent
フックまたは useStripe
フックを使用してアクセスできます。
function PaymentScreen() { // ... const { confirmSetupIntent, loading } = useConfirmSetupIntent(); // ... const handlePayPress = async () => { // Gather the customer's billing information (for example, email) const billingDetails: BillingDetails = { email: 'jenny.rosen@example.com', }; // Create a setup intent on the backend const clientSecret = await createSetupIntentOnBackend(); const { setupIntent, error } = await confirmSetupIntent(clientSecret, { paymentMethodType: 'Card', paymentMethodData: { billingDetails, } }); if (error) { //Handle the error } // ... }; return ( <View> // ... <Button onPress={handlePayPress} title="Save" loading={loading} /> </View> ); }
一部の支払い方法では、支払いを完了するために追加の認証手順が必要です。SDK が、支払いの確定と認証フローを管理します。これには、認証に必要な追加の画面の表示が含まれる場合があります。認証プロセスをテストするには、テストカード 4000 0025 0000 3155
を、セキュリティコード、郵便番号、および有効期限とともに使用します。
SetupIntent
が成功すると、(setupIntent.
で) 生成された PaymentMethod ID が、指定された Customer
に保存されます。
保存されたカードに後で請求するサーバー側![](https://b.stripecdn.com/docs-statics-srv/assets/fcc3a1c24df6fcffface6110ca4963de.svg)
顧客にオフセッションで請求する準備ができたら、Customer ID と PaymentMethod ID を使用して、PaymentIntent を作成します。請求するカードを見つけるには、Customer に関連付けられた PaymentMethod のリストを作成します。
Customer ID と PaymentMethod ID を取得したら、支払いの金額と通貨を使用して PaymentIntent を作成します。下記のパラメータを設定し、オフセッションの支払いを行います。
- off_session を
true
に設定して、支払いの実行時に顧客が決済フローに存在しないことを示します。これにより、認証が必要な場合は PaymentIntent からエラーが返されます。 - PaymentIntent の confirm プロパティの値を
true
に設定します。これにより、PaymentIntent が作成されると直ちに確定されます。 - payment_method を PaymentMethod の ID に設定し、customer を Customer の ID に設定します。
リカバリフローを開始する![](https://b.stripecdn.com/docs-statics-srv/assets/fcc3a1c24df6fcffface6110ca4963de.svg)
PaymentIntent に他のステータスがある場合、支払いは成功せず、リクエストは失敗します。支払いを完了するために (メール、テキスト、プッシュ通知などで) アプリケーションに戻るように顧客に通知します。支払いが最初に失敗した理由を示し、顧客が再試行できるようにするリカバリフローをアプリで作成することをお勧めします。
リカバリフローで client secret を使用して PaymentIntent を取得します。PaymentIntent の lastPaymentError
を確認し、支払いの試行が失敗した理由を調べます。カードエラーの場合、支払いエラーの最後のメッセージをユーザーに表示できます。それ以外の場合は、汎用の失敗メッセ―ジを表示します。
function PaymentScreen() { // ... const {retrievePaymentIntent} = useStripe(); // ... const handleRecoveryFlow = async () => { const {paymentIntent, error} = await retrievePaymentIntent(clientSecret); if (error) { Alert.alert(`Error: ${error.code}`, error.message); } else if (paymentIntent) { // Default to a generic error message const failureReason = 'Payment failed, try again.'; if (paymentIntent.lastPaymentError.type === 'Card') { failureReason = paymentIntent.lastPaymentError.message; } } }; return ( <View> // ... <Button onPress={handleRecoveryFlow} title="Recovery flow" loading={loading} /> </View> ); }
顧客に再試行してもらう![](https://b.stripecdn.com/docs-statics-srv/assets/fcc3a1c24df6fcffface6110ca4963de.svg)
保存されたカードを更新または削除し、支払いを再試行するオプションをリカバリフローで顧客に提供します。最初の支払いを受け付けるのと同じ手順に従いますが、1 つ異なる点があります。元の失敗した PaymentIntent を確定するために新しい支払いインテントを作成するのではなく、その client secret を再利用します。
認証が必要なために支払いが失敗した場合は、新しい支払い方法を作成するのではなく、既存の PaymentMethod を使用して再試行してください。
function PaymentScreen() { // ... const {retrievePaymentIntent} = useStripe(); // ... const handleRecoveryFlow = async () => { const {paymentIntent, error} = await retrievePaymentIntent(clientSecret); if (error) { Alert.alert(`Error: ${error.code}`, error.message); } else if (paymentIntent) { // Default to a generic error message let failureReason = 'Payment failed, try again.'; if (paymentIntent.lastPaymentError.type === 'Card') { failureReason = paymentIntent.lastPaymentError.message; } // If the last payment error is authentication_required, let the customer // complete the payment without asking them to reenter their details. if (paymentIntent.lastPaymentError?.code === 'authentication_required') { // Let the customer complete the payment with the existing PaymentMethod const {error} = await confirmPayment(paymentIntent.clientSecret, { paymentMethodType: 'Card', paymentMethodData: { billingDetails, paymentMethodId: paymentIntent.lastPaymentError?.paymentMethod.id, }, }); if (error) { // handle error } } else { // Collect a new PaymentMethod from the customer } } }; return ( <View> // ... <Button onPress={handleRecoveryFlow} title="Recovery flow" loading={loading} /> </View> ); }
組み込みをテストする![](https://b.stripecdn.com/docs-statics-srv/assets/fcc3a1c24df6fcffface6110ca4963de.svg)
ここまでで、以下を実行する組み込みが完成しています。
- SetupIntent を使用して、顧客に請求することなくカード詳細を収集・保存する
- オフセッションでカードに請求し、支払い拒否と認証リクエストを処理するリカバリーフローが備わっている
この組み込みの稼働準備ができていることを確認するために使用できるいくつかのテストカードがあります。任意のセキュリティコード、郵便番号、および今後の有効期限を指定して使用します。
番号 | 説明 |
---|---|
成功し、支払いがすぐに処理されます。 | |
初めての購入には認証が必要ですが、カードに setup_ の設定があれば、以降の支払い (オフセッションの支払いを含む) に成功します。 | |
初めての購入には認証が必要で、以降の支払い (オフセッションの支払いを含む) には失敗し、支払い拒否コード authentication_ が返されます。 | |
初めての購入には認証が必要で、以降の支払い (オフセッションの支払いを含む) には失敗し、支払い拒否コード insufficient_ が返されます。 | |
(初めての購入を含め) 常に失敗し、支払い拒否コード insufficient_ が返されます。 |
テストカードの全一覧をご覧ください。