Swish 決済招待のみ
スウェーデンで広く普及している支払い方法である Swish を受け付ける方法をご紹介します。
Swish は、スウェーデンで使用されている 1 回限りの使用の支払い方法です。顧客は Swish モバイルアプリとスウェーデンの BankID モバイルアプリを使用して、支払いの認証と承認を行うことができます。
支払いが成功したか失敗したかに関する即時通知を受け取ります。
必要な通知
Swish の規則に準拠するには、顧客が Swish 支払いを承認する前に、決済フローの画面に以下のテキストを表示する必要があります。
- 英語の場合: “Stripe Technology Europe Limited (“Stripe”) has acquired the claim for payment. Therefore your payment will be made to Stripe.”
- スウェーデン語の場合: “Stripe Technology Europe Limited (“Stripe”) har övertagit fordran på betalning. Din betalning görs därför till Stripe.”
- 他の言語の場合、上記の翻訳文。
Checkout や Payment Element、Stripe がホストする決済フォームや UI コンポーネントから Swish を導入する場合、Stripe はこの通知を表示します。
Stripe を設定するサーバー側クライアント側
まず、Stripe アカウントが必要です。今すぐご登録ください。
サーバー側
この接続方法では、Stripe API と通信するエンドポイントがサーバー上に必要です。サーバーから Stripe API にアクセスするには、Stripe の公式ライブラリを使用します。
クライアント側
React Native SDK はオープンソースであり、詳細なドキュメントが提供されています。内部では、ネイティブの iOS および Android の SDK を使用します。Stripe の React Native SDK をインストールするには、プロジェクトのディレクトリーで (使用するパッケージマネージャーによって異なる) 次のいずれかのコマンドを実行します。
次に、その他の必要な依存関係をインストールします。
- iOS の場合は、ios ディレクトリーに移動して
pod install
を実行し、必要なネイティブ依存関係もインストールします。 - Android の場合は、依存関係をインストールする必要はありません。
注
We recommend following the official TypeScript guide to add TypeScript support.
Stripe の初期化
React Native アプリで Stripe を初期化するには、決済画面を StripeProvider
コンポーネントでラップするか、initStripe
初期化メソッドを使用します。publishableKey
の API 公開可能キーのみが必要です。次の例は、StripeProvider
コンポーネントを使用して Stripe を初期化する方法を示しています。
import { useState, useEffect } from 'react'; 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> ); }
PaymentIntent を作成するサーバー側クライアント側
サーバー側
PaymentIntent は、顧客から支払いを回収する意図を表すオブジェクトで、決済プロセスのライフサイクルの各段階を追跡します。
サーバーで PaymentIntent
を作成して確定するには、以下の手順に従います。
返される PaymentIntent には client secret が含まれ、これは PaymentIntent の確定に使用されます。次のステップで使用できるように、クライアントに client secret を送り返します。
クライアント側
クライアント側で、サーバーの PaymentIntent をリクエストし、その client secret を保存します。
function PaymentScreen() { const fetchPaymentIntentClientSecret = async () => { const response = await fetch(`${API_URL}/create-payment-intent`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ currency: 'sek', }), }); const {clientSecret} = await response.json(); return clientSecret; }; const handlePayPress = async () => { // See below }; return ( <View> <Button onPress={handlePayPress} title="Pay" /> </View> ); }
client secret は、Stripe API リクエストを認証する API キーとは異なります。これは支払いを完了できるため、慎重に扱う必要があります。記録したり、URL に埋め込んだり、当該の顧客以外に漏洩することがないようにしてください。
戻り先 URL を設定する (iOS のみ)クライアント側
When a customer exits your app (for example to authenticate in Safari or their banking app), provide a way for them to automatically return to your app. Many payment method types require a return URL. If you don’t provide one, we can’t present payment methods that require a return URL to your users, even if you’ve enabled them.
To provide a return URL:
- Register a custom URL. Universal links aren’t supported.
- Configure your custom URL.
- Set up your root component to forward the URL to the Stripe SDK as shown below.
注
Expo を使用している場合は、app.
ファイルでスキームを設定します。
import { useEffect, useCallback } from 'react'; import { Linking } from 'react-native'; import { useStripe } from '@stripe/stripe-react-native'; export default function MyApp() { const { handleURLCallback } = useStripe(); const handleDeepLink = useCallback( async (url: string | null) => { if (url) { const stripeHandled = await handleURLCallback(url); if (stripeHandled) { // This was a Stripe URL - you can return or add extra handling here as you see fit } else { // This was NOT a Stripe URL – handle as you normally would } } }, [handleURLCallback] ); useEffect(() => { const getUrlAsync = async () => { const initialUrl = await Linking.getInitialURL(); handleDeepLink(initialUrl); }; getUrlAsync(); const deepLinkListener = Linking.addEventListener( 'url', (event: { url: string }) => { handleDeepLink(event.url); } ); return () => deepLinkListener.remove(); }, [handleDeepLink]); return ( <View> <AwesomeAppComponent /> </View> ); }
Swish 決済を確定するクライアント側
顧客が Swish での支払いを試みてタップすると、confirmPayment を呼び出して支払いを完了します。これにより WebView が表示され、顧客はそこから Swish で支払いを完了できます。完了後、プロミスは、paymentIntent
フィールド、または支払いでエラーが発生した場合には error
フィールドのどちらかを格納したオブジェクトで解決されます。
import {useConfirmPayment} from '@stripe/stripe-react-native'; function PaymentScreen() { const {confirmPayment, loading} = useConfirmPayment(); const fetchPaymentIntentClientSecret = async () => { // See above }; const handlePayPress = async () => { // Fetch the client secret from the backend. const clientSecret = await fetchPaymentIntentClientSecret(); const {error, paymentIntent} = await confirmPayment(clientSecret, { paymentMethodType: 'Swish', }); if (error) { console.log('Payment confirmation error: ', error); } else if (paymentIntent) { console.log('Successfully confirmed payment: ', paymentIntent); } }; return ( <View> <Button onPress={handlePayPress} title="Pay" disabled={loading} /> </View> ); }
失敗した支払い
Swish は複数のデータポイントを使用して、取引を拒否する状況を判断します (たとえば、顧客の銀行口座に十分な残高がない場合や、顧客がアプリでキャンセルをクリックした場合など)。
このケースでは、PaymentMethod は切り離され、PaymentIntent オブジェクトのステータスは自動的に requires_
に移行します。
支払いが拒否された場合を除き、Swish の PaymentIntent (支払いインテント) が requires_
ステータスである場合、顧客は 3 分以内に支払いを完了する必要があります。3 分経過してもアクションが行われない場合、PaymentMethod (支払い方法) の関連付けが解除され、PaymentIntent オブジェクトのステータスは自動的に requires_
に移行します。