Adyen Components for iOS
Adyen Components for iOS 允许您通过提供您创建结账体验所需的基本组件来接收应用内支付。
安装
可以通过 CocoaPods、Carthage 或 Swift Package Manager 获取 Adyen Components for iOS。
CocoaPods
- 在您的
Podfile
中添加pod 'Adyen'
。 - 运行
pod install
。
您可以根据需求和个人集成类型安装所有模块或添加个别模块。要支持微信支付,必须显式添加Adyen/WeChatPay
模块。要使用针对SwiftUI的特定助手,必须显式添加Adyen/SwiftUI
模块。
pod 'Adyen' // Add DropIn with all modules except WeChat Pay and SwiftUI.
// Add individual modules
pod 'Adyen/Card' // Card components.
pod 'Adyen/Encryption' // Encryption module.
pod 'Adyen/Components' // All other payment components except WeChat Pay.
pod 'Adyen/Actions' // Action Components.
pod 'Adyen/WeChatPay' // WeChat Pay Component.
pod 'Adyen/SwiftUI' // SwiftUI apps specific module.
Adyen/AdyenWeChatPay
和AdyenWeChatPayInternal
模块不支持模拟器,只能在真实设备上进行测试。
Carthage
- 将
github "adyen/adyen-ios"
添加到您的Cartfile
中。 - 运行
carthage update
。 - 按照Carthage Readme中的说明链接框架到您的目标中。
您可以选择添加所有模块或添加个别模块到您的集成中。但请确保包含每个模块的依赖模块。
AdyenDropIn
:DropIn组件。AdyenCard
:卡片组件。AdyenComponents
:除了微信支付之外的所有支付组件。AdyenActions
:动作组件。AdyenEncryption
:加密。AdyenWeChatPay
:微信支付组件。AdyenWeChatPayInternal
:微信支付组件。AdyenSwiftUI
:针对SwiftUI应用特定模块。
AdyenWeChatPay
和AdyenWeChatPayInternal
模块不支持任何模拟器,只能在真实设备上进行测试。
Swift Package Manager
- 按照Apple的Adding Package Dependencies to Your App指南了解如何添加Swift Package依赖。
- 使用
https://github.com/Adyen/adyen-ios
作为存储库URL。 - 指定版本至少为
3.8.0
。
您可以选择添加所有模块或添加个别模块到您的集成中。要支持微信支付,必须显式添加AdyenWeChatPay
模块。要使用针对SwiftUI的特定助手,必须显式添加AdyenSwiftUI
模块。
AdyenDropIn
:除了AdyenWeChatPay
之外的所有模块。AdyenCard
:卡片组件。AdyenComponents
:除了微信支付之外的所有支付组件。AdyenActions
:动作组件。AdyenEncryption
:加密。AdyenWeChatPay
:微信支付组件。AdyenSwiftUI
:针对SwiftUI应用特定模块。
Adyen
时使用Xcode 12.0+。
AdyenWeChatPay
和AdyenWeChatPayInternal
模块不支持任何模拟器,只能在真实设备上进行测试。
Drop-in
Drop-in 负责展示可用的支付方式和随后的客户支付详情输入。它使用 /paymentMethods
的响应进行初始化,并提供了一切必要的功能,以便执行 /payments
和 /payments/details
的 API 调用。
使用方法
展示 Drop-in
Drop-in 需要使用 /paymentMethods
端点的响应进行初始化。为了将响应传递给 Drop-in,请解码响应至 PaymentMethods
结构。
let paymentMethods = try JSONDecoder().decode(PaymentMethods.self, from: response)
所有组件都需要一个 APIContext
。一个 APIContext
实例封装了你的客户端密钥和环境。请在此阅读更多关于客户端密钥及其获取方法的信息:这里。使用 Environment.test 作为环境。当你准备接受实时支付时,请将值更改为我们的实时环境之一。
let apiContext = APIContext(clientKey: clientKey, environment: Environment.test)
let configuration = DropInComponent.Configuration(apiContext: apiContext)
某些支付方式需要额外的配置。例如 ApplePayComponent
。可以在 DropInComponent.Configuration
实例中设置这些特定于支付方式的配置参数。
let summaryItems = [
PKPaymentSummaryItem(label: "Item A", amount: 75, type: .final),
PKPaymentSummaryItem(label: "Item B", amount: 25, type: .final),
PKPaymentSummaryItem(label: "Total", amount: 100, type: .final)
]
let configuration = DropInComponent.Configuration(apiContext: apiContext)
configuration.payment = payment
configuration.applePay = .init(summaryItems: summaryItems,
merchantIdentifier: "merchant.com.adyen.MY_MERCHANT_ID")
对于诸如 Doku 变体之类的代金券支付方式,为了让 DokuComponent
启用购物者保存代金券,需要请求访问购物者的照片,因此需要在应用程序的 Info.plist 中适当的 key 下添加适当的文本 NSPhotoLibraryAddUsageDescription
。
在序列化支付方式并创建配置后,Drop-in 即可初始化。分配一个 delegate
并使用 viewController
属性将 Drop-in 展示在屏幕上。
let dropInComponent = DropInComponent(paymentMethods: paymentMethods, configuration: configuration)
dropInComponent.delegate = self
present(dropInComponent.viewController, animated: true)
实现DropInComponentDelegate
为了处理Drop-in的结果,应实现以下DropInComponentDelegate
方法
func didSubmit(_ data: PaymentComponentData, for paymentMethod: PaymentMethod, from component: DropInComponent)
当客户已选择支付方式并输入付款详细信息时,将调用此方法。付款详细信息可以从data.paymentMethod
中读取,并将其提交到/payments
。
func didProvide(_ data: ActionComponentData, from component: DropInComponent)
当在第一次调用/payments
后,Drop-in提供额外的详细信息时,将调用此方法。例如,在3D Secure 2身份验证流程或任何重定向流程期间发生这种情况。可以从data.details
中获取额外详细信息,并将其提交到/payments/details
。
func didFail(with error: Error, from component: DropInComponent)
当使用Drop-in期间发生错误时,将调用此方法。关闭Drop-in的视图控制器并显示错误消息。
func didComplete(from component: DropInComponent)
当操作组件完成,并且不需要应用程序执行任何其他步骤时(例如在优惠券支付方式的情况下),将调用此方法。应用程序只需关闭DropInComponent
。
func didCancel(component: PaymentComponent, from dropInComponent: DropInComponent)
在用户关闭由Drop-in管理的支付组件时,会调用此可选方法。
func didOpenExternalApplication(_ component: DropInComponent)
在发生转发到外部应用程序之后,将调用此可选方法。
处理操作
当/payments
或/payments/details
返回非最终结果和action
时,可以使用以下技术之一。
使用Drop-in
在Drop-in集成的情况下,必须在当前的DropInComponent
实例上使用内置的动作处理器。
let action = try JSONDecoder().decode(Action.self, from: actionData)
dropInComponent.handle(action)
使用组件
如果需要使用单个组件,而不是嵌入组件,请创建并持久化一个AdyenActionComponent
实例
lazy var actionComponent: AdyenActionComponent = {
let handler = AdyenActionComponent(apiContext: apiContext)
handler.delegate = self
handler.presentationDelegate = self
return handler
}()
然后使用它来处理操作
let action = try JSONDecoder().decode(Action.self, from: actionData)
actionComponent.handle(action)
接收重定向
如果客户被重定向到外部URL或应用,请确保让RedirectComponent
知道当用户返回您的应用时。您可以通过在您的UIApplicationDelegate
中实现以下操作来做到这一点
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
RedirectComponent.applicationDidOpen(from: url)
return true
}
组件
为了对结账流程有更多的灵活性,您可以使用我们的组件来分别展示每种支付方式。我们可以找到组件的详细实现信息在我们的组件API参考中。
可用的组件
- 卡片组件
- 3D Secure 2 组件
- Apple Pay 组件
- BCMC 组件
- iDEAL 组件
- SEPA 直接借记组件
- MOLPay 组件
- Dotpay 组件
- EPS 组件
- Entercash 组件
- 开放银行组件
- 微信支付组件
- QIWI 钱包组件
- 重定向组件
- MB Way 组件
- BLIK 组件
- Doku 组件
- Boleto 组件
自定义
Drop-in 和组件都提供了一些自定义选项,以允许您匹配您应用程序的外观。例如,要将 Drop-in 中的部分标题和表单字段标题更改为红色,并将提交按钮的背景更改为黑色,前景色为白色
var style = DropInComponent.Style()
style.listComponent.sectionHeader.title.color = .red
style.formComponent.textField.title.color = .red
style.formComponent.mainButtonItem.button.backgroundColor = .black
style.formComponent.mainButtonItem.button.title.color = .white
let dropInComponent = DropInComponent(paymentMethods: paymentMethods,
configuration: configuration,
style: style)
或创建一个带有白色文字的黑色卡组件
var style = FormComponentStyle()
style.backgroundColor = .black
style.header.title.color = .white
style.textField.title.color = .white
style.textField.text.color = .white
style.switch.title.color = .white
let component = CardComponent(paymentMethod: paymentMethod,
apiContext: apiContext,
style: style)
完整的自定义选项列表可以在API参考中找到。
要求
- iOS 11.0+
- Xcode 11.0+
- Swift 5.1
参见
支持
如果您有功能请求,或发现了一个错误或技术问题,请创建一个GitHub问题。对于其他问题,请联系我们的支持团队。
貢獻
我們強烈鼓勵您加入到這個存儲庫的貢獻中,讓所有人都能受益於
- 新功能與功能
- 已解決的錯誤修復與問題
- 任何一般的改善
閱讀我們的 貢獻指南 了解如何 操作。
許可證
此存儲庫是开源的,並在MIT許可證下發布。更多信息,請見LICENSE文件。