收集物理地址和电话号码
了解如何在移动应用中收集地址和电话号码。
To collect complete addresses for billing or shipping, use the Address Element.
还可以使用 Address Element 执行以下作:
- 收集客户电话号码
- 使用自动完成功能(在 iOS 中默认启用)
- 通过传入收货地址在 Payment Element 中预填充账单信息
Stripe 将收集的地址信息和支付方式合在一起来创建 PaymentIntent。

设置地址自动完成建议
iOS 系统会默认启用自动完成功能。
配置 Address Element
可以使用显示默认值、设置允许的国家/地区、自定义外观等详细信息来配置 Address Element。有关配置选项的完整列表,请参阅 AddressViewController.Configuration。
let addressConfiguration = AddressViewController.Configuration( additionalFields: .init(phone: .required), allowedCountries: ["US", "CA", "GB"], title: "Shipping Address" )
检索地址详情
通过满足 AddressViewControllerDelegate 并使用 addressViewControllerDidFinish 关闭视图控制器来检索地址详细信息。地址的值是有效的地址或空。
extension MyViewController: AddressViewControllerDelegate { func addressViewControllerDidFinish(_ addressViewController: AddressViewController, with address: AddressViewController.AddressDetails?) { addressViewController.dismiss(animated: true) self.addressDetails = address } }
显示 Address Element
使用以上步骤中的地址配置和委托创建 AddressViewController。您可以将其显示在导航控制器中,也可以将其推送到导航控制器上。
self.addressViewController = AddressViewController(configuration: addressConfiguration, delegate: self) let navigationController = UINavigationController(rootViewController: addressViewController) present(navigationController, animated: true)
可选在 Payment Element 中预填充收货地址
如果使用移动端 Payment Element,请将PaymentSheet.Configuration.shippingDetails 设置为地址元素收集的地址。当 shippingDetails 被填入时,用户的账单地址会被预填,他们会看到一个账单地址与收货地址相同的复选框。当确认 PaymentIntent 时,已填充 shippingDetails 的已确认 PaymentIntents 也会填充 shipping intent 属性。
var configuration = PaymentSheet.Configuration() // ... configuration.shippingDetails = { [weak self] in return self?.addressDetails }
可选自定义外观
您已向您的应用程序中添加了 Address Element,现在可以自定义它的外观,使其适合您的应用程序其他部分的设计了。您可以使用 AddressViewController.Configuration.appearance 通过 Appearance API 配置外观。
可选设置默认账单详情
要为支付表单中收集的账单详情设置默认值,请配置 defaultBillingDetails 属性。paymentSheet 会用您提供的值预先填充其字段。
var configuration = PaymentSheet.Configuration() configuration.defaultBillingDetails.address.country = "US" configuration.defaultBillingDetails.email = "foo@bar.com"
可选自定义账单地址信息的收集
用 billingDetailsCollectionConfiguration 来指定您希望如何在支付表单中收集账单详情。
可以收集客户的姓名、邮件地址、电话号码和地址。
如果您只想通过支付方式获得所需的账单详情,请将 billingDetailsCollectionConfiguration. 设置为 true。在这种情况下,PaymentSheet. 被设置为支付方式的 billing details。
如果您希望收集支付方式非必需的额外账单详情,请将 billingDetailsCollectionConfiguration. 设置为 false。在这种情况下,通过 PaymentSheet 收集的账单详情被设置为支付方式的账单详情。
var configuration = PaymentSheet.Configuration() configuration.defaultBillingDetails.email = "foo@bar.com" configuration.billingDetailsCollectionConfiguration.name = .always configuration.billingDetailsCollectionConfiguration.email = .never configuration.billingDetailsCollectionConfiguration.address = .full configuration.billingDetailsCollectionConfiguration.attachDefaultsToPaymentMethod = true
注意
请咨询律师,了解与收集信息有关的法律。仅在需要收集号码来完成交易时,才收集手机号码。