Cash App Pay 支払い
システムに Cash App Pay のサポートを追加します。
Cash App Pay は、すべての Cash App の顧客が 1 回限りの使用およびビジネスへの継続支払いに使用できる支払い方法です。Cash App Pay は、顧客の保存された残高または関連付けられたデビットカードを使用して、支払いを実行します。顧客は、以下の 2 通りの方法のいずれかで支払いを確定できます。
- モバイルデバイスからの決済時に、サイトで顧客は Cash App モバイルアプリケーションにリダイレクトされ、そこで認証を行います。支払いはリダイレクト時に認証されます。Cash App モバイルアプリケーションでは購入を完了するために、それ以上のアクションを実行する必要はありません。その後、顧客はサイトにリダイレクトされて戻ります。
- デスクトップ版ウェブアプリケーションからの決済時に、顧客はモバイルデバイスで QR コードをスキャンして、取引を認証します。
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
の支払い方法のタイプのリストにcashapp
を追加します。ダッシュボードで Cash App Pay が有効になっていることを確認してください。
返される 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: 'usd', }), }); 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> ); }
Cash App Pay での決済を確定するクライアント側
顧客が CashApp での支払いを試みてタップすると、confirmPayment を呼び出して支払いを完了します。これにより WebView が表示され、顧客はそこから CashApp で支払いを完了できます。完了後、プロミスは、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: 'CashApp', }); 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> ); }
導入をテストする
テスト API キーを使用して Cash App Pay の導入をテストするには、リダイレクトページを表示します。リダイレクトページで支払いを認証することにより、支払い成功のケースをテストできます。PaymentIntent (支払いインテント) は requires_
から succeeded
に変化します。
ユーザーが認証に失敗するケースをテストするには、テスト API キーを使用してリダイレクトページを表示します。リダイレクトページで Fail test payment (テスト支払い失敗) をクリックします。PaymentIntent (支払いインテント) が、requires_
から requires_
に変化します。
テスト環境で PaymentIntents を手動でキャプチャする場合、キャプチャされていない PaymentIntent は、認証が成功してから 60 分後に自動的に期限切れになります。
本番環境では、PaymentIntent (支払いインテント) を確定すると、Cash App にリダイレクトされます。ベストプラクティスとして、顧客にリリースする前に、実際の Cash App アカウントを使用して本番環境でテストすることをお勧めします。本番環境では、Cash App 内で支払いを承認または拒否するオプションはありません。顧客が Cash App にリダイレクトされると、支払いは自動的に承認されます。
失敗した支払い
Cash App Pay uses multiple data points to decide when to decline a transaction (for example, their AI model detected high consumer fraud risk for the transaction, or the consumer has revoked your permission to charge them in Cash App).
このケースでは、PaymentMethod (支払い方法) は切り離され、PaymentIntent (支払いインテント) オブジェクトのステータスは自動的に requires_
に移行します。
支払いが拒否された場合を除き、Cash App Pay の PaymentIntent (支払いインテント) が requires_
ステータスである場合、顧客は Cash App にリダイレクトされてから 10 分以内に支払いを完了する必要があります。10 分経過してもアクションが行われない場合、PaymentMethod (支払い方法) の関連付けが解除され、PaymentIntent オブジェクトのステータスは自動的に requires_
に移行します。
これが発生すると、Payment Element はエラーメッセージを表示し、顧客に別の支払い方法で再試行するように求めます。
エラーコード
次の表は、一般的なエラーコードと推奨アクションの詳細を示しています。
エラーコード | 推奨される対応 |
---|---|
payment_ | 適切な通貨を入力します。Cash App Pay は usd にのみ対応しています。 |
missing_ | 必須パラメーターの詳細については、エラーメッセージをご確認ください。 |
payment_ | このコードは PaymentIntent の last_payment_error.code フィールドに表示されることがあります。エラーメッセージで失敗の詳細な理由と推奨されるエラー処理を確認します。 |
payment_ | Cash App Pay で PaymentIntent を確定する際は、return_ を指定します。 |