调至内容部分
创建账户
或
登录
Stripe 文档徽标
/
询问人工智能
创建账户
登录
开始
付款
销售收入
平台和交易市场
资金管理
开发人员资源
概览
关于 Stripe 支付
升级您的集成
支付分析
线上付款
概览查找您的用例Managed Payments
使用 Payment Link
构建结账页面
构建高级集成
构建应用内集成
支付方式
添加支付方式
    概览
    支付方式集成选项
    在管理平台中管理默认支付方式
    支付方式类型
    银行卡
    使用 Stripe 余额支付
    加密货币
    银行借记
    银行重定向
    银行转账
    贷记转账(来源)
    先买后付
    实时付款
    付款凭单
    钱包
      支付宝
      Amazon Pay
      Apple Pay
        最佳实践
        Cartes Bancaires with Apple Pay
        Apple Pay 经常性交易
        Apple Pay 商家令牌
        Apple Pay 责任转移、争议和退款
      Cash App Pay
      Google Pay
      GrabPay
      Link
      MB WAY
      MobilePay
      PayPal
      PayPay
      Revolut Pay
      Satispay
      Secure Remote Commerce
      Vipps
      微信支付
    按国家启用本地支付方式
    自定义支付方式
管理支付方式
用 Link 更快结账
支付接口
Payment Links
结账
Web Elements
应用内 Element
支付场景
处理多种货币
自定义支付流程
灵活收单
编排
线下支付
Terminal
超越支付功能
成立公司
加密货币
Financial Connections
Climate
首页付款Add payment methodsWallets

Apple Pay

允许客户在他们的 iPhone、iPad 或 Apple Watch 上用 Apple Pay 安全地付款。

支持的设备

参考 Apple 的兼容性文档,了解哪些设备支持 Apple Pay。

Apple Pay 与大多数 Stripe 产品和功能兼容。Stripe 用户可以在 iOS 9 及更高版本的 iOS 应用程序以及 iOS 10 或 macOS Sierra 及以上版本的 Safari 网页上接受 Apple Pay。处理 Apple Pay 付款时没有额外费用,定价与其他银行卡交易一样。

Apple Pay 可由在受支持国家的参与银行那里开户的持卡人使用。有关更多信息,请参考 Apple 的参与银行文档。

  • 客户所在地

    印度以外的全球

  • 出示货币

    参见受支持的出示货币

  • 支付确认

    客户发起

  • 支付方式类型

    钱包

  • 经常性付款

    是(墨西哥除外)

  • 提现时间

    适用标准提现时间

  • Connect 支持

    是

  • 争议支持

    是

  • 手动捕获支持

    是

  • 退款/部分退款

    是 / 是

付款流程

以下是您的结账页面的 Apple Pay 付款流程演示:

Apple Pay 付款流程动画显示 Stripe 结账页面、Apple Pay 按钮和测试时的确认对话框。

使用 Stripe 和 Apple Pay 与应用内购买

对于实物商品、服务和某些其他东西的销售,您的应用程序可以接受 Apple Pay 或其他任何 Stripe 支持的支付方式。这些付款是通过 Stripe 处理的,只需支付 Stripe 的手续费。但是,数字产品、内容和某些其他东西的销售必须使用 Apple 的应用内购买。这些付款由 Apple 公司处理,会收取交易费。

有关哪些类型的销售必须使用应用内购买的更多信息,请参见 Apple App Store 审查指南。

接受 Apple Pay

Stripe 提供了多种添加 Apple Pay 的方法。有关集成的详细信息,请选择您最喜欢的方法:

通过 Stripe iOS SDK,您可以接受 Apple Pay 及传统的信用卡支付方式。开始之前,您需要注册 Apple Developer Program。接下来,执行这些步骤:

  1. 设置 Stripe
  2. 注册 Apple Merchant ID
  3. 创建新的 Apple Pay 证书
  4. 用 Xcode 集成
  5. 检查是否支持 Apple Pay
  6. 创建付款请求
  7. 出示支付表单
  8. 向 Stripe 提交付款

设置 Stripe
服务器端
客户端

首先,您需要有 Stripe 账户。立即注册。

服务器端

该集成要求您的服务器上的端点与 Stripe API 通讯。请用官方库从您的服务器访问 Stripe API:

Command Line
Ruby
# Available as a gem sudo gem install stripe
Gemfile
Ruby
# If you use bundler, you can add this line to your Gemfile gem 'stripe'

客户端

Stripe iOS SDK 是开源的,有完整的文档,并且与支持 iOS 13 或更高版本操作系统的应用程序兼容。

要安装 SDK,按这些步骤进行:

  1. 在 Xcode 中,选择文件 > **添加工具包依赖…**并输入 https://github.com/stripe/stripe-ios-spm 作为仓库 URL。
  2. 从我们的发布页面选择最新的版本号。
  3. 将 StripeApplePay 产品添加到您的目标应用程序。

注意

有关最新 SDK 发布及过往版本的详细信息,请查看 GitHub 上的发布页面。要想在新版本发布时接收通知,请查看仓库的发布。

在应用程序启动时使用您的 Stripe 公钥 配置 SDK。这样可使您的应用程序向 Stripe API 发出请求。

AppDelegate.swift
Swift
import UIKit import StripeApplePay @main class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { StripeAPI.defaultPublishableKey =
"pk_test_TYooMQauvdEDq54NiTphI7jx"
// do any other necessary launch configuration return true } }

注意

测试时使用您的测试密钥,发布应用时使用真实模式密钥。

注册 Apple Merchant ID

可通过在 Apple 开发人员网站注册新的标识符来获取 Apple 商家 ID。

在表单中填写描述和标识符。描述内容仅供您自己记录之用,之后可随时更改。Stripe 建议用您的应用的名称作为标识符(例如,merchant.com.{{YOUR_APP_NAME}})。

创建新的 Apple Pay 证书

为您的应用创建证书,以加密支付数据。

转到管理平台中的 iOS 证书设置,点击添加新应用程序,然后按照说明进行操作。

下载证书签名请求 (CSR) 文件,来从 Apple 获取安全证书,以便使用 Apple Pay。

必须用一个 CSR 文件发布具体的一个证书。如果您切换您的 Apple Merchant ID,则必须前往管理平台的 iOS 证书设置中获取一个新的 CSR 和证书。

集成 Xcode

将 Apple Pay 功能添加到您的应用程序。在 Xcode 中,打开您的项目设置,点击签名和功能选项卡,然后添加 Apple Pay 功能。此时,系统可能会提示您登入您的开发人员账户。选择您之前创建的商家 ID,您的应用程序就可以接受 Apple Pay 了。

在 Xcode 中启用 Apple Pay 功能

检查是否支持 Apple Pay

注意

如果您使用着 PaymentSheet,则那个类别会为您处理剩下的。

在您的应用程序中显示 Apple Pay 此支付选项之前,请确定用户的设备支持 Apple Pay,并且他们的钱包中添加了银行卡:

CheckoutViewController.swift
Swift
import StripeApplePay import PassKit class CheckoutViewController: UIViewController, ApplePayContextDelegate { let applePayButton: PKPaymentButton = PKPaymentButton(paymentButtonType: .plain, paymentButtonStyle: .black) override func viewDidLoad() { super.viewDidLoad() // Only offer Apple Pay if the customer can pay with it applePayButton.isHidden = !StripeAPI.deviceSupportsApplePay() applePayButton.addTarget(self, action: #selector(handleApplePayButtonTapped), for: .touchUpInside) } // ...continued in next step }

创建付款请求

当用户点击 Apple Pay 按钮时,调用 StripeAPI paymentRequestWithMerchantIdentifier:country:currency:,创建一个 PKPaymentRequest。

然后,配置 PKPaymentRequest 以显示贵商家的名称和总金额。您还可以收集账单详情或配送信息等信息。

关于如何自定义付款请求的完整指南,请参见 Apple 文档。

CheckoutViewController.swift
Swift
func handleApplePayButtonTapped() { let merchantIdentifier = "merchant.com.your_app_name" let paymentRequest = StripeAPI.paymentRequest(withMerchantIdentifier: merchantIdentifier, country: "US", currency: "USD") // Configure the line items on the payment request paymentRequest.paymentSummaryItems = [ // The final line should represent your company; // it'll be prepended with the word "Pay" (that is, "Pay iHats, Inc $50") PKPaymentSummaryItem(label: "iHats, Inc", amount: 50.00), ] // ...continued in next step }

出示支付表单

用 PKPaymentRequest 创建一个 STPApplePayContext 实例,并使用它来呈现 Apple Pay 表单。

CheckoutViewController.swift
Swift
func handleApplePayButtonTapped() { // ...continued from previous step // Initialize an STPApplePayContext instance if let applePayContext = STPApplePayContext(paymentRequest: paymentRequest, delegate: self) { // Present Apple Pay payment sheet applePayContext.presentApplePay(on: self) } else { // There is a problem with your Apple Pay configuration } }

Apple 要求用户操作触发 Apple Pay 模态窗口(例如,点击按钮或与表单交互)。确保您的代码遵循以下规则:

  • 通过用户激活事件直接调用支付表单。
  • 将支付表单的代码添加到用户操作事件处理器的顶部或顶部附近,并且需位于任何异步或长时间运行的代码之前。
  • 在用户操作后,设置一个合理的时间限制来调用 confirmPayment。

向 Stripe 提交付款

服务器端

生成一个端点,创建一个具有 amount 和 currency 的 PaymentIntent。始终在服务器端决定扣款金额,这是一个可信的环境,客户端不行。这样可防止客户自己选择价格。

Command Line
curl
curl https://api.stripe.com/v1/payment_intents \ -u
sk_test_BQokikJOvBiI2HlWgH4olfQ2
:
\ -d "amount"=1099 \ -d "currency"="usd"

客户端

部署 applePayContext(_:didCreatePaymentMethod:completion:),用来自上述端点的 PaymentIntent 客户端机密调用完成块。

在您调用完成块 (completion block) 后,STPApplePayContext 完成付款,取消 Apple Pay 表单,然后用付款状态调用 applePayContext(_:didCompleteWithStatus:error:)。部署这种方法,向客户出示收据。

CheckoutViewController.swift
Swift
extension CheckoutViewController { func applePayContext(_ context: STPApplePayContext, didCreatePaymentMethod paymentMethod: StripeAPI.PaymentMethod, paymentInformation: PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock) { let clientSecret = ... // Retrieve the PaymentIntent client secret from your backend (see Server-side step above) // Call the completion block with the client secret or an error completion(clientSecret, error) } func applePayContext(_ context: STPApplePayContext, didCompleteWith status: STPApplePayContext.PaymentStatus, error: Error?) { switch status { case .success: // Payment succeeded, show a receipt view break case .error: // Payment failed, show the error break case .userCancellation: // User canceled the payment break @unknown default: fatalError() } } }

最后,处理付款后事件,完成若干操作,例如,向客户发送订单确认邮件、在数据库中记录订单,或启动配送流程。

故障排除

如果您在尝试创建令牌时看到 Stripe API 出现错误,则很可能是您的 Apple Pay Certificate 有问题。如本页所述,您需要生成一个新证书并上传到 Stripe。请确保您使用的是从您的管理平台获得的 CSR,而不是您自己生成的那个。Xcode 经常错误地缓存旧的证书,所以除了生成新证书外,Stripe 建议创建一个新的 Apple Merchant ID。

如果您收到错误:

您尚未将您的 Apple 商家账户添加到 Stripe

您的应用程序好像正在发送用以前的(非 Stripe) CSR/Certificate 加密的数据。务必撤销您的 Apple Merchant ID 下任何不是由 Stripe 生成的证书。如果这样不能解决问题,请删除您的 Apple 账户中的商家 ID,然后重新创建它。然后,根据之前使用的相同(Stripe 提供的) CSR 创建一个新证书。您不需要将此新的证书上传到 Stripe。完成后,在您的应用程序中打开并关闭 Apple Pay Credentials,以确保正确刷新。

App Clips

StripeApplePay 模态是一个简版 Stripe SDK,为在 App Clip 中使用进行了优化。按照以上步骤将 StripeApplePay 模态添加到您的 App Clip 的目标中。

注意

仅 Swift 中支持 StripeApplePay 模态。Objective-C 用户必须从 Stripe 模态导入 STPApplePayContext。

从 STPApplePayContext 迁移

如果您当前使用的是 STPApplePayContext 并且想切换到简版 Apple Pay SDK,则按照这些步骤进行:

  1. 在您的 App Clip 目标的依赖中,将 Stripe 模态替换为 StripeApplePay 模态。
  2. 在您的代码中,将 import Stripe 替换为 import StripeApplePay。
  3. 将您使用的 STPApplePayContextDelegate 替换为新的 ApplePayContextDelegate 协议。
  4. 更改您的 applePayContext(_:didCreatePaymentMethod:completion:) 执行程序,以接受 StripeAPI.PaymentMethod。
  5. 更改您的 applePayContext(_:didCompleteWith:error:) 部署,以接受 STPApplePayContext.PaymentStatus。
升级前
升级后
import Stripe class CheckoutViewController: UIViewController, STPApplePayContextDelegate { func applePayContext(_ context: STPApplePayContext, didCreatePaymentMethod paymentMethod: STPPaymentMethod, paymentInformation: PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock) { // ... } func applePayContext(_ context: STPApplePayContext, didCompleteWith status: STPPaymentStatus, error: Error?) { // ... } }
import StripeApplePay class CheckoutViewController: UIViewController, ApplePayContextDelegate { func applePayContext(_ context: STPApplePayContext, didCreatePaymentMethod paymentMethod: StripeAPI.PaymentMethod, paymentInformation: PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock) { // ... } func applePayContext(_ context: STPApplePayContext, didCompleteWith status: STPApplePayContext.PaymentStatus, error: Error?) { // ... } }

经常性付款

在 iOS 16 或更高版本中,可通过在 PKPaymentRequest 上设置 recurringPaymentRequest 或 automaticReloadPaymentRequest 属性来采用商家令牌。

Recurring payments can used saved payment methods for off-session transactions only.

CheckoutViewController.swift
Swift
extension CheckoutViewController { func handleApplePayButtonTapped() { let request = StripeAPI.paymentRequest(withMerchantIdentifier: merchantIdentifier, country: "US", currency: "USD") let billing = PKRecurringPaymentSummaryItem(label: "My Subscription", amount: NSDecimalNumber(string: "59.99")) billing.startDate = Date() billing.endDate = Date().addingTimeInterval(60 * 60 * 24 * 365) billing.intervalUnit = .month request.recurringPaymentRequest = PKRecurringPaymentRequest(paymentDescription: "Recurring", regularBilling: billing, managementURL: URL(string: "https://my-backend.example.com/customer-portal")!) request.recurringPaymentRequest?.billingAgreement = "You'll be billed $59.99 every month for the next 12 months. To cancel at any time, go to Account and click 'Cancel Membership.'" request.paymentSummaryItems = [billing] } }

要了解有关如何通过 Apple Pay 使用经常性付款的更多信息,请参阅 Apple 的 PassKit 文档。

订单跟踪

要在 iOS 16 或以上版本中采用订单跟踪,在您的 ApplePayContextDelegate 中部署 applePayContext(context:willCompleteWithResult:handler:) 函数。Stripe 在支付完成后调用您的部署环境,但是在 iOS 关闭 Apple Pay 表单之前。

在您的部署环境中:

  1. 从您的服务器获取已完成订单的详情。
  2. 将这些详情添加到提供的 PKPaymentAuthorizationResult。
  3. 调用主队列上提供的完成处理程序。

要了解有关订单跟踪的更多信息,请参阅 Apple 钱包订单文档。

CheckoutViewController.swift
Swift
extension CheckoutViewController { func applePayContext(_ context: STPApplePayContext, willCompleteWithResult authorizationResult: PKPaymentAuthorizationResult, handler: @escaping (PKPaymentAuthorizationResult) -> Void) { // Fetch the order details from your service MyAPIClient.shared.fetchOrderDetails(orderID: myOrderID) { myOrderDetails authorizationResult.orderDetails = PKPaymentOrderDetails( orderTypeIdentifier: myOrderDetails.orderTypeIdentifier, // "com.myapp.order" orderIdentifier: myOrderDetails.orderIdentifier, // "ABC123-AAAA-1111" webServiceURL: myOrderDetails.webServiceURL, // "https://my-backend.example.com/apple-order-tracking-backend" authenticationToken: myOrderDetails.authenticationToken) // "abc123" // Call the handler block on the main queue with your modified PKPaymentAuthorizationResult handler(authorizationResult) } } }

测试 Apple Pay

要测试 Apple Pay,必须使用真实的信用卡卡号和测试 API 密钥。Stripe 会识别到您处于测试模式,并返回一个成功的测试卡令牌供您使用,因此您可以在不收款的情况下使用真实卡来测试付款。

您无法将Stripe测试卡或 Apple Pay 测试卡保存到 Apple Pay 钱包来测试 Apple Pay。

另见

  • iOS 集成
  • 网页版 Apple Pay
  • Apple Pay 最佳实践
此页面的内容有帮助吗?
是否
需要帮助?联系支持。
加入我们的早期使用计划。
查看我们的更改日志。
有问题?联系销售。
LLM? Read llms.txt.
Powered by Markdoc