埋め込み可能な決済フォームである Mobile Payment Element を使用して、Amazon や他の支払い方法を、最小限の作業で既存の Stripe システムに追加できます。
Alma は 1 回限りの決済手段であり、顧客は 2 回、3 回、4 回の分割払いを選択できます。顧客は、ウェブサイトまたはアプリからリダイレクトされ、Alma で支払いを承認すると、ウェブサイトまたはアプリに戻されます。お客様は支払いが成功したか失敗したかに関する即時通知を受け取ります。
まず、Stripe アカウントが必要です。今すぐ登録してください。
サーバ側
この組み込みには、サーバ上に Stripe API と通信するエンドポイントが必要です。サーバから Stripe API にアクセスするには、Stripe の公式ライブラリを使用します。
クライアント側
Stripe iOS SDK はオープンソースです。詳細なドキュメントが提供されており、iOS 13 以降をサポートするアプリと互換性があります。
SDK をインストールするには、以下のステップに従います。
- In Xcode, select File > Add Package Dependencies… and enter
https://github.com/stripe/stripe-ios-spm
as the repository URL. - リリースページから最新のバージョン番号を選択します。
- StripePaymentsUI 製品をアプリのターゲットに追加します。
注
For details on the latest SDK release and past versions, see the Releases page on GitHub. To receive notifications when a new release is published, watch releases for the repository.
アプリの起動時に Stripe 公開可能キーを使用して SDK を設定します。これにより、アプリが Stripe API にリクエストを送信できるようになります。
import UIKit
import StripePaymentsUI
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
StripeAPI.defaultPublishableKey = "pk_test_TYooMQauvdEDq54NiTphI7jx"
return true
}
}
注
Use your test keys while you test and develop, and your live mode keys when you publish your app.
サーバー側
PaymentIntent (支払いインテント) は、顧客から支払いを回収する意図を表すオブジェクトで、決済プロセスのライフサイクルの各段階を追跡します。
注意
クライアント側ではなく、信頼できる環境のサーバー側で常に支払い金額を指定してください。これにより、顧客が金額を恣意的に選択できないようにします。
支払い方法はダッシュボードで管理できます。Stripe は、取引金額、通貨、決済フローなどの要因に基づいて、適切な支払い方法が返されるように処理します。
価格と通貨を指定して、サーバーで PaymentIntent を作成します。Payment Intent を作成する前に、支払い方法の設定ページで Alma を有効にしてください。
curl https://api.stripe.com/v1/payment_intents \
-u "sk_test_BQokikJOvBiI2HlWgH4olfQ2
:" \
-d amount=1099 \
-d currency=usd \
-d "automatic_payment_methods[enabled]"=true
クライアント側
クライアント側でサーバーの PaymentIntent をリクエストし、その client secret を保存します。
class CheckoutViewController: UIViewController {
var paymentIntentClientSecret: String?
override func viewDidLoad() {
startCheckout()
}
func startCheckout() {
}
}
顧客が支払いのために Alma をタップしたら、PaymentIntent
を確定して支払いを完了します。PaymentIntent
client secret を使用して STPPaymentIntentParams
オブジェクトを構成します。
client secret は、Stripe API リクエストを認証する API キーとは異なります。このデバイスは支払いを完了できるため、慎重に扱う必要があります。記録したり、URL に埋め込んだり、当該の顧客以外に漏洩することがないようにしてください。
戻り先 URL を設定する
iOS SDK では、Alma による決済を完了するための WebView がアプリに表示されます。認証が終了すると、顧客が閉じなくても、自動的に WebView が閉じられるようにすることができます。この動作を有効にするには、カスタム URL スキームまたはユニバーサルリンクを設定して、URL を SDK に転送するようにアプリのデリゲートを設定します。
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
let stripeHandled = StripeAPI.handleURLCallback(with: url)
if (stripeHandled) {
return true
} else {
}
return false
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
if let url = userActivity.webpageURL {
let stripeHandled = StripeAPI.handleURLCallback(with: url)
if (stripeHandled) {
return true
} else {
}
}
}
return false
}
PaymentIntent を確定する際に、その URL を return_url
として渡します。WebView での認証が終わると、Stripe はユーザーを return_url
にリダイレクトします。
Alma の支払いを確定する
STPPaymentHandler.confirmPayment
を呼び出して支払いを完了します。これにより WebView が表示され、顧客はそこから Alma で支払いを完了できます。完了すると、支払い結果とともに、完了ブロックが呼び出されます。
let paymentIntentParams = STPPaymentIntentParams(clientSecret: paymentIntentClientSecret)
let alma = STPPaymentMethodAlmaParams()
let paymentMethodParams = STPPaymentMethodParams(alma: alma, billingDetails: nil, metadata: nil)
paymentIntentParams.paymentMethodParams = paymentMethodParams
paymentIntentParams.returnURL = "payments-example://stripe-redirect"
STPPaymentHandler.shared().confirmPayment(paymentIntentParams,
with: self)
{ (handlerStatus, paymentIntent, error) in
switch handlerStatus {
case .succeeded:
case .canceled:
case .failed:
@unknown default:
fatalError()
}
}
サポートされている通貨
お客様の国に対応する通貨で Alma での支払いを作成できます。Alma のデフォルトの現地通貨は eur
で、顧客は購入金額を eur
でも確認できます。
通貨 | 国 |
---|
eur | France, Italy, Spain, Netherlands, Belgium, Luxembourg |