Alipay 是一种单次使用的支付方式,客户需要验证他们的付款。客户在您的网站或应用程序中被跳转到 Alipay 页面,授权付款,然后返回您的网站或应用,然后在这里收到付款成功或失败的即时通知。
首先,您需要有 Stripe 账户。立即注册。
服务器端
该集成要求您的服务器上的端点与 Stripe API 通讯。请用我们的官方库从您的服务器访问 Stripe API:
客户端
Stripe iOS SDK 是开源的,有完整的文档,并且与支持 iOS 13 或更高版本操作系统的应用程序兼容。
要安装 SDK,按这些步骤进行:
- 在 Xcode 中,选择文件 > **添加工具包依赖…**并输入
https://github.com/stripe/stripe-ios-spm
作为仓库 URL。 - 从我们的发布页面选择最新的版本号。
- 将 StripePaymentsUI 产品添加到您的目标应用程序。
注意
有关最新 SDK 发布及过往版本的详细信息,请查看 GitHub 上的发布页面。要想在新版本发布时接收通知,请查看仓库的发布。
在应用程序启动时使用您的 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
}
}
PaymentIntent 是一个用来表示您从客户收款意图的对象,并可用于跟踪付款流程的生命周期。在您的服务器上创建一个 PaymentIntent
,并指定要收取的金额和支持的币种。如果您有现成的 Payment Intents 集成,则将 alipay
添加到 payment method types 列表。
curl https://api.stripe.com/v1/payment_intents \
-u "sk_test_BQokikJOvBiI2HlWgH4olfQ2
:" \
-d "payment_method_types[]"=alipay \
-d amount=1099 \
-d currency=hkd
进入您的应用程序的设置中的“信息”选项卡,为您的应用注册一个自定义 URL Scheme(网页地址协议)。
支付宝应用打开此 URL,客户完成付款后,返回到您的应用。将 URL 转发给您的 UISceneDelegate
或 UIApplicationDelegate
中 Stripe SDK。
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
return
}
let stripeHandled = StripeAPI.handleURLCallback(with: url)
if (!stripeHandled) {
}
}
当客户点击支付宝进行支付时,用 STPPaymentHandler confirmPayment 确认 PaymentIntent。这样将启动支付宝应用,如果没有安装支付宝的话,会显示一个网络视图。
import UIKit
import StripePayments
class CheckoutViewController: UIViewController {
func pay() {
let clientSecret = ...
let paymentIntentParams = STPPaymentIntentParams(clientSecret: clientSecret)
paymentIntentParams.paymentMethodParams = STPPaymentMethodParams(alipay: STPPaymentMethodAlipayParams(), billingDetails: nil, metadata: nil)
paymentIntentParams.paymentMethodOptions = STPConfirmPaymentMethodOptions()
paymentIntentParams.paymentMethodOptions?.alipayOptions = STPConfirmAlipayOptions()
paymentIntentParams.returnURL = "{{URL SCHEME}}://safepay/"
STPPaymentHandler.shared().confirmPayment(paymentIntentParams, with: self) { (status, intent, error) in
switch status {
case .canceled:
case .failed:
case .succeeded:
@unknown default:
fatalError()
}
}
}
}
extension CheckoutViewController: STPAuthenticationContext {
func authenticationPresentingViewController() -> UIViewController {
return self
}
}
支付宝打开以 safepay/
开头的返回页面。例如,如果您的自定义网页地址协议是 myapp
,则回调页面必须是 myapp://safepay/
。
支持的货币
Your account must have a bank account for one of the following currencies. You can create Alipay payments in the currencies that map to your country. The default local currency for Alipay is cny
and customers also see their purchase amount in cny
.
货币 | 国家/地区 |
---|
cny | 任何国家 |
aud | 澳大利亚 |
cad | 加拿大 |
eur | 奥地利、比利时、保加利亚、塞浦路斯、捷克共和国、丹麦、爱沙尼亚、芬兰、法国、德国、希腊、爱尔兰、意大利、拉脱维亚、立陶宛、卢森堡、马耳他、荷兰、挪威、葡萄牙、罗马尼亚、斯洛伐克、斯洛文尼亚、西班牙、瑞典、瑞士 |
gbp | 英国 |
hkd | 香港 |
jpy | 日本 |
myr | 马来西亚 |
nzd | 新西兰 |
sgd | 新加坡 |
usd | 美国 |
如果您拥有以另一种货币开设的银行账户,并希望以该货币创建支付宝支付,可以联系支持服务。我们会根据具体情况为其他货币提供支持服务。