调至内容部分
创建账户或登录
Stripe 文档徽标
/
询问人工智能
创建账户登录
开始
付款
销售收入
平台和交易市场
资金管理
开发人员资源
API 和 SDK帮助

开始在 iOS SDK 中使用 Sources已废弃

学习如何在您的 iOS 应用中使用 Sources。

警告

We deprecated the Sources API and plan to remove support for local payment methods. If you currently handle any local payment methods using the Sources API, you must migrate them to the Payment Methods API.

While we don’t plan to remove support for card payments, we recommend replacing any use of the Sources API with the PaymentMethods API, which provides access to our latest features and payment method types.

This guide assumes you’ve already installed and configured the Stripe iOS SDK and are familiar with Sources.

在 iOS SDK 中用 Source 创建付款涉及到多个步骤:

  1. 创建 STPSource 对象,用它表示客户的支付方式。
  2. 检查是否需要客户进一步操作。

如果不需要进一步操作:

  • 确认 Source 可供使用。
  • 用 Source 在您的后端创建收款请求。

如果需要进一步操作:

  • 向客户显示授权收款所需的任何确认信息。
  • 在您的后端,侦听 Stripe webhooks,用 Source 创建收款。
  • 在您的应用程序中,根据来源的状态向客户显示相应的确认信息。

创建 STPSource 对象

SDK 参考

请查看 iOS SDK 参考,其中介绍了此处所述类别的每个方法和属性。

收集了客户的付款详情后,可以用 STPAPIClient 类别来创建来源。首先,用您收集的支付信息构建一个 STPSourceParams 对象。然后,将此对象传递到 STPAPIClient 的 createSourceWithParams: 方法。

创建 STPSourceParams 对象时,使用我们提供的某个辅助构造函数 (Helper Constructors),其中指定了每种支付方式所需的信息。

检查是否需要客户进一步操作。

要确定是否需要客户采取进一步操作,请查看最新创建的 STPSource 对象中的 flow 属性。如果 flow 是 STPSourceFlowNone,则不需要进一步操作。例如,如果您创建了一个银行卡付款来源,则它的状态会立即设置为 STPSourceStatusChargeable。不需要客户额外操作,因此可告诉后端立即用此来源创建收款。

Swift
Objective C
No results
let cardParams = STPCardParams() cardParams.name = "Jenny Rosen" cardParams.number = "4242424242424242" cardParams.expMonth = 12 cardParams.expYear = 18 cardParams.cvc = "424" let sourceParams = STPSourceParams.cardParams(withCard: cardParams) STPAPIClient.shared.createSource(with: sourceParams) { (source, error) in if let s = source, s.flow == .none && s.status == .chargeable { self.createBackendChargeWithSourceID(s.stripeID) } }

如果来源的流程不是 STPSourceFlowNone,则您的客户需要完成一项操作,然后此来源才能被用于收款请求中。

流程描述
STPSourceFlowRedirect必须将客户重定向到支付方式的网站或应用程序来确认收款。查看下方的更多信息。
STPSourceFlowReceiver客户必须将资金推入 Source 对象中提供的账户信息。请参阅您正在使用的特定支付方式文档,了解更多信息。
STPSourceFlowVerification客户必须通过提供您发布到 Stripe API 的验证码来验证其对账户的所有权。请参阅您正在使用的特定支付方式文档,了解更多信息。

如果某个来源要求客户进一步操作,则您的 iOS 应用_不_应告诉您的后端来创建收款请求。而是,后端应侦听 source.chargeable webhook 事件,对来源扣款。这可以确保即使客户在完成了要求的动作但未返回您的应用程序的情况下,也能对来源扣款。有关用 Webhook 支持不同支付方式的更多信息,请参阅最佳实践。

重定向客户,授权支付来源

对于需要重定向客户来授权付款的来源,在创建来源时,需要指定一个返回页面。这样,客户验证完付款才能重定向回您的应用程序。对于此返回页面,既可以是使用 自定义页面内跳转协议 (URL Scheme),也可以使用您的应用程序支持的通用链接。有关在您的应用程序中注册和处理 URL 的更多信息,请参考 Apple 文档:

  • 部署自定义页面内跳转协议 (URL Scheme)
  • 支持通用链接

要处理向 Source 对象的 redirect.url 参数中的 URL 的客户重定向操作,建议使用 STPRedirectContext,可用它在 SFSafariViewController 中打开 URL(如果有),否则可以使用手机 Safari。要使用 STPRedirectContext,您需要先设置您的应用程序的代理,将 URL 转到 Stripe SDK。

Swift
Objective C
No results
// This method handles opening native URLs (for example, "your-app://stripe-redirect") func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { let stripeHandled = StripeAPI.handleURLCallback(with: url) if (stripeHandled) { return true } else { // This was not a stripe url – do whatever url handling your app // normally does, if any. } return false } // This method handles opening universal link URLs (for example, "https://example.com/stripe_ios_callback") 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 { // This was not a stripe url – do whatever url handling your app // normally does, if any. } } } return false }

客户返回到您的应用程序后,调用 STPRedirectContext 的完成块。这时,用户可能完成也可能未完成验证过程。您可以用您自己服务器上的 Webhook 来接收来源的可扣款状态变化的通知。有关使用 Source 时如何构建确认界面的更多信息,请参阅最佳实践。

如需更多帮助,请查看用多种不同支付方式演示创建收款的示例应用程序。

另请参阅

  • Using PaymentIntents on iOS
  • 迁移到 Payment Methods
此页面的内容有帮助吗?
是否
  • 需要帮助?联系支持。
  • 查看我们的更改日志。
  • 有问题?联系销售。
  • LLM? Read llms.txt.
  • Powered by Markdoc