欢迎使用 Braintree 的 iOS 集成式 SDK!
Braintree iOS 集成式 SDK 允许的部署目标为 iOS 12.0 或更高版本。 它需要 Xcode 15.0+ 和 Swift 5.9+。
我们建议使用 Swift Package Manager 或 CocoaPods 将 Braintree 集成式 SDK 与您的项目集成。
此功能仅在 v9 中可用。 v9.Y.0 需要 Xcode 15.0+。
要将 BraintreeDropIn
包添加到您的 Xcode 项目,请选择 File > Swift Packages > Add Package Dependency,然后输入 https://github.com/braintree/braintree-ios-drop-in
作为存储库 URL。 选中 BraintreeDropIn
的复选框。
如果您查看您的应用程序目标,您将看到 BraintreeDropIn
库已自动链接为框架到您的应用程序(请参阅 General > Frameworks, Libraries, and Embedded Content)。
注意:对于使用带有二进制依赖项的 Swift Package 的应用程序扩展,存在一个 已知的 SPM 问题。 该问题发生在将构建上传到 App Store Connect 时。 您可以在 Swift Forums 上阅读该报告并关注 此解决方法。
添加到您的 Podfile
pod 'BraintreeDropIn'
然后运行 pod install
。
请参阅我们的 Podspec
以获取更多信息。
注意: 如果您在 Xcode 12 中使用 Braintree 集成式 iOS SDK 的 8.x.x 版本,您可能会看到警告 The iOS Simulator deployment target is set to 8.0, but the range of supported deployment target versions is 9.0 to 14.0.99
。 这不会阻止您的应用编译。 这是一个 CocoaPods 问题,有一个已知的解决方法。
将以下 import 语句添加到您使用 BraintreeDropIn 的任何类中。
import BraintreeDropIn
呈现 BTDropInController
以收集客户的付款信息,并接收要发送到您服务器的 nonce
。 如果您在创建客户端令牌时指定了 customer_id
,则会显示已保存的付款方式。
func showDropIn(clientTokenOrTokenizationKey: String) {
let request = BTDropInRequest()
let dropIn = BTDropInController(authorization: clientTokenOrTokenizationKey, request: request)
{ (controller, result, error) in
if (error != nil) {
print("ERROR")
} else if (result?.isCanceled == true) {
print("CANCELED")
} else if let result = result {
// Use the BTDropInResult properties to update your UI
let selectedPaymentMethodType = result.paymentMethodType
let selectedPaymentMethod = result.paymentMethod
let selectedPaymentMethodIcon = result.paymentIcon
let selectedPaymentMethodDescription = result.paymentDescription
}
controller.dismiss(animated: true, completion: nil)
}
self.present(dropIn!, animated: true, completion: nil)
}
默认情况下,Apple Pay 在集成式界面中启用。 只要在控制面板中启用 Apple Pay 并且设备支持,集成式界面就会将 Apple Pay 显示为一种付款方式。 要选择退出,请在您的 BTDropInRequest
上设置 applePayDisabled = true
。
重要提示: 如果您的客户选择 Apple Pay 作为其首选付款方式,则 result.paymentMethodType == .applePay
并且 result.paymentMethod
将为 nil
。 选择 Apple Pay 不会显示 Apple Pay 表单或创建 nonce。 在您收到 BTDropInResult
后,您需要
PKPaymentButton
。PKPaymentAuthorizationViewController
。PKPayment
标记化并将生成的 nonce 发送到您的服务器以创建交易。使用 BTApplePayClient
来标记客户的 Apple Pay 信息 - 查看我们的官方文档以获取更多信息。
集成式界面支持 3D Secure 验证。 您必须在控制面板中启用 3D Secure。 创建一个 BTThreeDSecureRequest
,尽可能多地设置其中的字段; 设置的字段越多,用户就越不可能遇到挑战。 在 BTDropInRequest
上设置 BTThreeDSecureRequest
。
let request = BTDropInRequest()
let threeDSecureRequest = BTThreeDSecureRequest()
threeDSecureRequest.threeDSecureRequestDelegate = self
threeDSecureRequest.amount = 1.00
threeDSecureRequest.email = "test@example.com"
threeDSecureRequest.requested = .version2
let address = BTThreeDSecurePostalAddress()
address.givenName = "Jill"
address.surname = "Doe"
address.phoneNumber = "5551234567"
address.streetAddress = "555 Smith St"
address.extendedAddress = "#2"
address.locality = "Chicago"
address.region = "IL"
address.postalCode = "12345"
address.countryCodeAlpha2 = "US"
threeDSecureRequest.billingAddress = address
// Optional additional information.
// For best results, provide as many of these elements as possible.
let additionalInformation = BTThreeDSecureAdditionalInformation()
additionalInformation.shippingAddress = address
threeDSecureRequest.additionalInformation = additionalInformation
request.threeDSecureRequest = threeDSecureRequest
默认情况下,如果您使用使用客户 ID 生成的客户端令牌初始化集成式界面,则集成式界面会将付款方式添加到 Braintree Vault 中该客户。 您可以选择允许删除该客户的付款方式,方法是启用 vaultManager
。
let request = BTDropInRequest()
request.vaultManager = true
如果您的用户已经有一种现有的付款方式,您可能不需要显示集成式界面。 您可以使用 BTDropInResult.mostRecentPaymentMethod
检查他们是否具有现有的付款方式。 请注意,您必须使用使用 customer_id
创建的客户端令牌。 BTDropInResult
可以轻松获取付款方式的描述和图标。
BTDropInResult.mostRecentPaymentMethod(forClientToken: authorization) { result, error in
guard let result = result, error == nil else {
// either an error occurred or the customer doesn't have any vaulted payment methods
return
}
if result.paymentOptionType == .applePay {
// Apple Pay is the most recently selected option
// Note that result.paymentMethod will be nil in this case; display Apple Pay button and tokenize using `BTApplePayClient`
}
// Update your UI
let type = result.paymentMethodType
let icon = result.paymentIcon
let description = result.paymentDescription
// Use the payment method to transact
let paymentMethod = result.paymentMethod
}
集成式界面目前已针对 22 种语言进行了本地化。
集成式界面是完全可定制的,但我们也提供 Light
、Dark
和 Dynamic
配色方案。 动态配色方案将根据设备是否处于浅色或深色模式在浅色和深色之间切换。 Dynamic
方案仅在 iOS 13 或更高版本中可用。 默认情况下,集成式界面将使用 Light
配色方案。
let uiCustomization = BTDropInUICustomization(colorScheme: .dark)
let dropInRequest = BTDropInRequest()
dropInRequest.uiCustomization = uiCustomization
使用 BTDropInUICustomization
来自定义集成式界面的外观。
let uiCustomization = BTDropInUICustomization(colorScheme: .dynamic)
uiCustomization.fontFamily = "Helvetica"
uiCustomization.boldFontFamily = "Helvetica Bold"
let dropInRequest = BTDropInRequest()
dropInRequest.uiCustomization = uiCustomization
默认情况下,系统和自定义字体都启用动态类型。 我们强烈建议您保持此功能开启,但如果您希望禁用它,您可以通过在 BTDropInUICustomization
上将 disableDynamicType
设置为 true
来实现。
let uiCustomization = BTDropInUICustomization(colorScheme: .light)
uiCustomization.disableDynamicType = true
let dropInRequest = BTDropInRequest()
dropInRequest.uiCustomization = uiCustomization
集成式界面元素支持旁白。
目前,集成式界面不正式支持 SwiftUI。
首先阅读 “Hello, Client!” 获取有关基本设置和用法的说明。
另请参阅我们的 参考文档。
此 SDK 遵守我们的客户端 SDK 弃用政策。 有关 SDK 潜在状态的更多信息,请查看我们的 开发者文档。
主要版本号 | 状态 | 已发布 | 已弃用 | 不支持 |
---|---|---|---|---|
9.x.x | 活跃 | 2021 年 4 月 | 待定 | 待定 |
8.x.x | 已弃用 | 2020 年 2 月 | 2022 年 4 月 | 待定 |
7.x.x | 不支持 | 2018 年 12 月 | 2021 年 4 月 | 2022 年 4 月 |
不支持 6 及以下版本。
此项目中包含一个演示应用程序。 运行演示应用程序需要 Xcode 15.0+。 打开 BraintreeDropIn.xcworkspace 并运行 Demo 方案以查看它。
演示应用程序使用 Swift Package Manager 来获取依赖项。 当您打开工作区时,Xcode 将自动下载依赖项。 删除派生数据将删除依赖项。 转到 File -> Swift Packages -> Resolve Package Versions 重新下载它们。
Braintree iOS 集成式 SDK 正在积极开发中,我们欢迎您的反馈!
以下是联系我们的几种方式
Braintree iOS 集成式 SDK 是开源的,并根据 MIT 许可证提供。 有关更多信息,请参阅 LICENSE 文件。