Integrate Stripe’s prebuilt payment UI into the checkout of your Android app with the PaymentSheet class.
Stripe を設定する まず、Stripe アカウントが必要です。今すぐ登録 してください。
サーバー側 この接続方法では、Stripe API と通信するエンドポイントがサーバー上に必要です。サーバーから Stripe API にアクセスするには、Stripe の公式ライブラリーを使用します。
クライアント側 Stripe Android SDK はオープンソースであり、詳細なドキュメントが提供されています 。
To install the SDK, add stripe-android
to the dependencies
block of your app/build.gradle file:
plugins {
id ( "com.android.application" )
}
android { .. . }
dependencies {
implementation ( "com.stripe:stripe-android:21.4.1" )
}
支払い方法を有効にする
支払い方法の設定 を表示して、サポートする支払い方法を有効にします。PaymentIntent を作成するには、少なくとも 1 つは支払い方法を有効にする必要があります。
By default, Stripe enables cards and other prevalent payment methods that can help you reach more customers, but we recommend turning on additional payment methods that are relevant for your business and customers. See Payment method support for product and payment method support, and our pricing page for fees.
この接続方法では、以下の 3 つの Stripe API オブジェクトを使用します。
PaymentIntent (支払いインテント) : Stripe はこれを使用して、顧客から支払いを回収する意図を示し、プロセス全体を通して支払いの試行と支払い状態の変化を追跡します。
(オプション) Customer (顧客) : 今後の支払いに備えて決済手段を設定するには、決済手段をCustomer に関連付ける必要があります。Customer オブジェクトは、顧客がビジネスでアカウントを作成するときに作成します。顧客がゲストとして支払いを行う場合は、支払いの前に Customer オブジェクトを作成し、後でこのオブジェクトを顧客のアカウントを表す内部表現に関連付けることができます。
(オプション) Customer Ephemeral Key (顧客の一時キー): Customer オブジェクトの情報は機密情報であるため、アプリから直接取得することはできません。Ephemeral Key により、SDK に Customer への一時的なアクセス権が付与されます。
注 Customer にカードを保存したことがなく、リピート顧客に保存されたカードの再利用を許可しない場合は、実装で Customer オブジェクトおよび Customer Ephemeral Key オブジェクトを省略できます。
セキュリティ上の理由により、アプリでこれらのオブジェクトを作成することはできません。代わりに、サーバー側で以下を行うエンドポイントを追加します。
Customer を取得するか、新規作成する。 Customer の一時キーを作成する。 amount 、currency 、customer 、 を指定して PaymentIntent を作成します。オプションで、automatic_ payment_ methods
パラメーターを含めることもできます。Stripe は、最新バージョンの API ではこの機能をデフォルトで有効にしています。PaymentIntent の client secret 、一時キーの secret
、顧客の id 、および貴社の公開可能キー をアプリに返します。 決済プロセス中に顧客に表示される支払い方法は、PaymentIntent にも含まれています。Stripe にダッシュボードの設定から支払い方法を取得するよう指定することも、手動でリストに表示することもできます。選択したオプションにかかわらず、顧客に表示される支払い方法は、PaymentIntent で渡す通貨によって絞り込まれることにご注意ください。たとえば、PaymentIntent で eur
を渡し、ダッシュボードで OXXO が有効になっている場合、OXXO は eur
による決済に対応していないため、顧客に表示されません。
構築済みのシステムで、支払い方法を提供するためにコードベースのオプションが必要になる場合を除き、自動化されたオプションを使用することをお勧めします。これは、Stripe が通貨、支払い方法の制約、その他のパラメーターを評価して、対応可能な支払い方法を決定するためです。自動化されたオプションでは、購入完了率の向上につながり、使用通貨と顧客の所在地に最適な支払い方法が優先的に表示されます。
注 このエンドポイントの実行可能な実装内容を Glitch でテストします。
支払い方法はダッシュボード で管理できます。Stripe は取引額、通貨、決済フローなどの要素に基づいて、適切な支払い方法が返されるように処理します。PaymentIntent は、ダッシュボードで設定された支払い方法を使用して作成されます。ダッシュボードを使用しない場合や、支払い方法を手動で指定する場合は、payment_ method_ types
属性を使用して支払い方法を一覧表示することができます。
curl https://api.stripe.com/v1/customers \
-u sk_test_4eC39HqLyjWDarjtT1zdp7dc
: \
-X "POST"
curl https://api.stripe.com/v1/ephemeral_keys \
-u sk_test_4eC39HqLyjWDarjtT1zdp7dc
: \
-H "Stripe-Version: 2025-01-27.acacia" \
-X "POST" \
-d "customer" = "{{CUSTOMER_ID}}" \
curl https://api.stripe.com/v1/payment_intents \
-u sk_test_4eC39HqLyjWDarjtT1zdp7dc
: \
-X "POST" \
-d "customer" = "{{CUSTOMER_ID}}" \
-d "amount" = 1099 \
-d "currency" = "eur" \
-d "automatic_payment_methods[enabled]" = true \
支払いの詳細を収集する 決済ページでは、Mobile Payment Element を表示する前に以下を実行する必要があります。
決済アクティビティーの onCreate
内で、結果を処理するメソッドを渡して、PaymentSheet
インスタンスを初期化 します。
import androidx . compose . runtime . Composable
import com . stripe . android . paymentsheet . PaymentSheetResult
import com . stripe . android . paymentsheet . rememberPaymentSheet
@Composable
fun App ( ) {
val paymentSheet = rememberPaymentSheet ( :: onPaymentSheetResult )
}
private fun onPaymentSheetResult ( paymentSheetResult : PaymentSheetResult ) {
}
次に、前のステップで作成したエンドポイントから、PaymentIntent の client secret、一時キーの secret、Customer ID、公開可能キーを取得します。公開可能キーは PaymentConfiguration
を使用して設定し、その他は保存して、PaymentSheet を表示するときに使用します。
import androidx . compose . runtime . Composable
import androidx . compose . runtime . LaunchedEffect
import androidx . compose . runtime . getValue
import androidx . compose . runtime . mutableStateOf
import androidx . compose . runtime . remember
import androidx . compose . runtime . setValue
import androidx . compose . ui . platform . LocalContext
import com . github . kittinunf . fuel . httpPost
import com . github . kittinunf . fuel . json . responseJson
import com . stripe . android . PaymentConfiguration
import com . stripe . android . paymentsheet . PaymentSheet
import com . stripe . android . paymentsheet . PaymentSheetResult
import com . stripe . android . paymentsheet . rememberPaymentSheet
import com . github . kittinunf . result . Result
@Composable
fun App ( ) {
val paymentSheet = rememberPaymentSheet ( :: onPaymentSheetResult )
val context = LocalContext . current
var customerConfig by remember { mutableStateOf < PaymentSheet . CustomerConfiguration ? > ( null ) }
var paymentIntentClientSecret by remember { mutableStateOf < String ? > ( null ) }
LaunchedEffect ( context ) {
"Your backend endpoint/payment-sheet" . httpPost ( ) . responseJson { _ , _ , result ->
if ( result is Result . Success ) {
val responseJson = result . get ( ) . obj ( )
paymentIntentClientSecret = responseJson . getString ( "paymentIntent" )
customerConfig = PaymentSheet . CustomerConfiguration (
id = responseJson . getString ( "customer" ) ,
ephemeralKeySecret = responseJson . getString ( "ephemeralKey" )
)
val publishableKey = responseJson . getString ( "publishableKey" )
PaymentConfiguration . init ( context , publishableKey )
}
}
}
}
private fun onPaymentSheetResult ( paymentSheetResult : PaymentSheetResult ) {
}
When the customer taps your checkout button, call presentWithPaymentIntent to present the payment sheet. After the customer completes the payment, the sheet dismisses and the PaymentSheetResultCallback is called with a PaymentSheetResult .
import androidx . compose . material . Button
import androidx . compose . material . Text
import androidx . compose . runtime . Composable
import androidx . compose . runtime . LaunchedEffect
import androidx . compose . runtime . getValue
import androidx . compose . runtime . mutableStateOf
import androidx . compose . runtime . remember
import androidx . compose . runtime . setValue
import androidx . compose . ui . platform . LocalContext
import com . github . kittinunf . fuel . httpPost
import com . github . kittinunf . fuel . json . responseJson
import com . stripe . android . PaymentConfiguration
import com . stripe . android . paymentsheet . PaymentSheet
import com . stripe . android . paymentsheet . PaymentSheetResult
import com . stripe . android . paymentsheet . rememberPaymentSheet
import com . github . kittinunf . result . Result
@OptIn ( ExperimentalCustomerSessionApi :: class )
@Composable
fun App ( ) {
val paymentSheet = rememberPaymentSheet ( :: onPaymentSheetResult )
val context = LocalContext . current
var customerConfig by remember { mutableStateOf < PaymentSheet . CustomerConfiguration ? > ( null ) }
var paymentIntentClientSecret by remember { mutableStateOf < String ? > ( null ) }
LaunchedEffect ( context ) {
"Your backend endpoint/payment-sheet" . httpPost ( ) . responseJson { _ , _ , result ->
if ( result is Result . Success ) {
val responseJson = result . get ( ) . obj ( )
paymentIntentClientSecret = responseJson . getString ( "paymentIntent" )
customerConfig = PaymentSheet . CustomerConfiguration (
id = responseJson . getString ( "customer" ) ,
ephemeralKeySecret = responseJson . getString ( "ephemeralKey" )
)
val publishableKey = responseJson . getString ( "publishableKey" )
PaymentConfiguration . init ( context , publishableKey )
}
}
}
Button (
onClick = {
val currentConfig = customerConfig
val currentClientSecret = paymentIntentClientSecret
if ( currentConfig != null && currentClientSecret != null ) {
presentPaymentSheet ( paymentSheet , currentConfig , currentClientSecret )
}
}
) {
Text ( "Checkout" )
}
}
private fun presentPaymentSheet (
paymentSheet : PaymentSheet ,
customerConfig : PaymentSheet . CustomerConfiguration ,
paymentIntentClientSecret : String
) {
paymentSheet . presentWithPaymentIntent (
paymentIntentClientSecret ,
PaymentSheet . Configuration (
merchantDisplayName = "My merchant name" ,
customer = customerConfig ,
allowsDelayedPaymentMethods = true
)
)
}
private fun onPaymentSheetResult ( paymentSheetResult : PaymentSheetResult ) {
when ( paymentSheetResult ) {
is PaymentSheetResult . Canceled -> {
print ( "Canceled" )
}
is PaymentSheetResult . Failed -> {
print ( "Error: ${ paymentSheetResult . error } " )
}
is PaymentSheetResult . Completed -> {
print ( "Completed" )
}
}
}
allowsDelayedPaymentMethods
を true に設定すると、アメリカの銀行口座などの 遅延通知型 の支払い方法を使用できます。これらの支払い方法では、PaymentSheet
が完了した時点では最終的な支払いステータスが判明せず、後になって成功または失敗が確定します。このようなタイプの支払い方法に対応する場合は、注文が確定済みであることを顧客に通知し、支払いが成功した場合にのみ注文のフルフィルメント (商品の発送など) を実行するようにします。
支払い後のイベントを処理する 支払いが完了すると、Stripe は payment_intent.succeeded イベントを送信します。ダッシュボードの Webhook ツール を使用するか Webhook のガイド に従ってこれらのイベントを受信し、顧客への注文確認メールの送信、データベースでの売上の記録、配送ワークフローの開始などのアクションを実行します。
クライアントからのコールバックを待つのではなく、これらのイベントをリッスンします。クライアントでは、コールバックが実行される前に顧客がブラウザーのウィンドウを閉じたり、アプリを終了する場合、また悪意を持つクライアントがレスポンスを不正操作する場合もあります。非同期型のイベントをリッスンするよう組み込みを設定すると、単一の組み込みで複数の異なるタイプの支払い方法 を受け付けることができます。
Payment Element を使用して支払いを回収する場合は、payment_ intent. succeeded
イベントのほかにこれらのイベントを処理することをお勧めします。
イベント 説明 アクション payment_intent.succeeded 顧客が正常に支払いを完了したときに送信されます。 顧客に注文の確定を送信し、顧客の注文のフルフィルメント を実行します。 payment_intent.processing 顧客が正常に支払いを開始したが、支払いがまだ完了していない場合に送信されます。このイベントは、多くの場合、顧客が口座引き落としを開始するときに送信されます。その後、payment_ intent. succeeded
イベント、また、失敗の場合は payment_ intent. payment_ failed
イベントが送信されます。 顧客に注文確認メールを送信し、支払いが保留中であることを示します。デジタル商品では、支払いの完了を待たずに注文のフルフィルメントを行うことが必要になる場合があります。 payment_intent.payment_failed 顧客が支払いを試みたが、支払いに失敗する場合に送信されます。 支払いが processing
から payment_ failed
に変わった場合は、顧客に再度支払いを試すように促します。
組み込みをテストする
カード番号 シナリオ テスト方法 4242 4242 4242 4242 カード支払いは成功し、認証は必要とされません。 クレジットカード番号と、任意の有効期限、セキュリティコード、郵便番号を使用してクレジットカードフォームに入力します。 4000 0025 0000 3155 カード支払いには認証 が必要です。 クレジットカード番号と、任意の有効期限、セキュリティコード、郵便番号を使用してクレジットカードフォームに入力します。 4000 0000 0000 9995 カードは、insufficient_ funds
などの拒否コードで拒否されます。 クレジットカード番号と、任意の有効期限、セキュリティコード、郵便番号を使用してクレジットカードフォームに入力します。 6205 5000 0000 0000 004 UnionPay カードは、13 ~ 19 桁の可変長です。 クレジットカード番号と、任意の有効期限、セキュリティコード、郵便番号を使用してクレジットカードフォームに入力します。
実装内容をテストするためのその他の情報については、テスト をご覧ください。