SecureTrading iOS SDK
一个原生的 Swift 接口,允许通过 SecureTrading 进行令牌化和支付授权。
目录
=================
特性
- 拦截 ApplePay 授权以处理 SecureTrading 交易。
版本
1.0.0:支持 Apple Pay 授权。 1.0.1:授权安全更新。
需求
- iOS 10+
- XCode 11
- Cocoapods
- Swift 4+ / Objective-C
假设
- 只有符合条件的客户才能使用 Apple Pay 完成交易。
- 例如,客户必须使用支持的苹果设备,并且位于苹果支付运营的国家/地区。请参考苹果的文档获取完整的列表要求。
- 您的收购银行必须支持带有安全交易功能的苹果支付(请联系我们的支持团队获取更多信息)。
- 您需要一个 苹果支付商家ID。
- 我们建议为您的测试和在线网站分别注册ID。
- 我们建议将“API用户名”和“API密钥”存储在您的私有后端,并在应用启动事件中请求数据。
集成
签名证书
您需要联系我们的支持团队以在安全交易账户上启用苹果支付。在以下过程中,您将被要求提供以下信息
-
您的网站引用
-
您的苹果支付商家ID
准备支付处理证书
支持团队将针对每个商家ID返回两个证书(CSR格式),您需要按照以下步骤针对每个商家ID启用账户上的苹果支付。
-
登录至苹果开发者中心:https://developer.apple.com/account/
-
从导航栏中点击“证书、标识符和配置文件”
-
导航至“证书”并点击添加按钮。
-
选择“苹果支付处理证书”选项并按“继续”
-
选择为该目的创建的商家ID并按“继续”
-
按位于“苹果支付处理证书”部分下的“创建证书”按钮。
-
下一屏幕将询问以下问题
“将该商家ID关联的支付是否将在中国独家处理?”。选择“不”,然后单击“继续” -
按“选择文件”按钮,并选择之前由SecureTrading提供的“支付处理” .csr文件。
-
苹果将提示您下载一个新的苹果支付证书。
下载此证书并保管好——您以后需要用到!不要更改此CER文件名
准备商家身份证书
- 登录苹果开发者中心:https://developer.apple.com/account/
- 从导航栏中点击“证书、标识符和配置文件”
-
导航至“标识”并单击为此次用途创建的商家标识所对应的行。
-
按下位于“Apple Pay 商家身份证书”部分下的“创建证书”按钮。
-
按下“选择文件”按钮,并选择SecureTrading之前提供的“商家身份” .csr文件。
-
苹果将提示您下载新的Apple Pay商家身份证书。
下载此证书并保管好——您以后需要用到!不要更改此CER文件名
使用证书联系支持
-
按照以上步骤操作后,您现在应该拥有两个Apple Pay证书(两个CER文件)。
-
将这两个文件压缩成一个.zip文件,将其附加到电子邮件中并发送给我们的支持团队([email protected])。
-
一旦支持收到证书并已通过电子邮件确认,您就可以进行下一步了。
您必须确保在给支持发送证书时发送的是.zip文件。
安装到移动开发环境
先决条件和假设
要在iOS上使用Apple Pay SDK进行构建,必须满足以下条件
-
您的应用的部署目标为iOS 10.0或更高版本
-
您已将Secure Trading SDK添加到Cocoapods移动开发环境
此外,本指南做出以下假设
- 您通常熟悉iOS应用程序开发。如果您是iOS开发的新手,我们建议您在Swift.org阅读基本知识。
您正在使用Xcode 11或更高版本。
CocoaPods集成
默认情况下,库必须使用CocoaPods进行集成。如果您是cocoapods新手,我们建议您阅读cocoapods.org网站上的入门指南。
要使用CocoaPods进行安装,请 将以下内容添加到Podfile中
use_frameworks!
pod 'SecureTradingSDK'
配置项目目标以支持ApplePay功能
集成ApplePay需要您的应用程序支持“ApplePay”功能,要启用它,请按照以下步骤操作
-
点击您的项目文件。
-
在项目详细信息中选择您的目标。
-
在“签名和功能”选项卡下按“+功能”按钮。
-
选择“ApplePay”选项。
-
项目功能列表中会出现一个新的“ApplePay”行,按添加按钮(“+”)
-
系统将提示您输入您的商家ID,填写与您之前在SecureTrading中配置的标识符匹配的文本框
在您的App Delegate中初始化SecureTrading库
按照以下格式更新AppDelegate文件中的SITE_REFERENCE
、API_USERNAME
和API_KEY
字符串
import SecureTrading
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
STSecureTrading.initialize(sitereference: "SITE_REFERENCE",
apiUsername: "API_USERNAME",
apiKey: "API_KEY",
sandboxModeEnabled: false)
return true
}
}
处理支付授权
1.导入库和PassKit框架
import SecureTrading // Import ST Library
import PassKit // Import PassKit framework to access Apple Pay related classes
2.实现“PKPaymentAuthorizationViewControllerDelegate”来拦截支付授权结果
class ViewController: UIViewController, PKPaymentAuthorizationViewControllerDelegate
3.设置当前案例接受的支付网络
STSecureTrading.shared.acceptedApplePayNetworks = [.amex,.visa,.JCB,.masterCard,.discover]
4.控制支付按钮可见性:该库提供了一种方法来检查用户是否设置了任何接受卡的Apple Wallet。
STSecureTrading.shared.isApplePayAvailable()
5.设置支付请求:初始化Payment Request对象,实例化授权视图控制器,设置授权代理,并呈现支付授权视图控制器。
let itemSummary = PKPaymentSummaryItem(label: "Product Name", amount: 10.0) // Set item description.
paymentRequest = PKPaymentRequest() // Initialize payment request.
paymentRequest.countryCode = "GB" // Set ISO country code.
paymentRequest.currencyCode = "GBP" // Set three-letter ISO 4217 currency code.
paymentRequest.paymentSummaryItems = [itemSummary] // Assign created items to request object.
paymentRequest.supportedNetworks = [.amex,.visa,.JCB,.masterCard,.discover] // Assign supported networks to request object.
paymentRequest.merchantIdentifier = "merchant.com.mobilize.applepay.demo" // Set merchant identifier created for this purpose.
paymentRequest.merchantCapabilities = [.capability3DS] // Enable 3DS capability by adding it to the request.
// Initialize a PKPaymentAuthorizationViewController object using the payment request we created.
guard let applePayVC = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest) else {
print("Error found creating payment request") // Manage error.
return
}
applePayVC.delegate = self // Set delegate to current class.
present(applePayVC, animated: true, completion: nil) // Present the Payment Authorization View Controller we just initialized.
6.拦截授权完成事件:当过程完成时,关闭返回的控制器。
// Required implementation to dismiss payment authorization view controller.
func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
controller.dismiss(animated: true, completion: nil) // Dismiss payment controller.
}
7.实现授权回调:将返回的支付对象传递给Secure Trading库,由SecureTrading的API进行处理。
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
// Call the following library method to process payment with SecureTrading.
STSecureTrading.shared.processApplePayPayment(payment: payment, paymentRequest: paymentRequest, totalAmount: 1000) { (transactionResult, error) in
if error != nil {
// Required: Call completion block if error occurred.
completion(PKPaymentAuthorizationResult(status: .failure, errors: [])) // Manage completion error.
} else {
// Required: Call completion block if payment was processed successfuly by Secure Trading.
completion(PKPaymentAuthorizationResult(status: .success, errors: [])) // Manage completion success.
}
}
}
完整实现
import UIKit
import SecureTrading // Import ST Library
import PassKit // Import PassKit framework to access Apple Pay related classes
// Required: Implement PKPaymentAuthorizationViewControllerDelegate to intercept Payment authorizations
class ViewController: UIViewController, PKPaymentAuthorizationViewControllerDelegate {
@IBOutlet var paymentButton: UIButton! // Start payment button
var paymentRequest = PKPaymentRequest()
override func viewDidLoad() {
super.viewDidLoad()
// Set accespted payment networks to SecureTrading Library.
STSecureTrading.shared.acceptedApplePayNetworks = [.amex,.visa,.JCB,.masterCard,.discover]
// Payment buttons can be enabled based on apple pay availability.
paymentButton.isEnabled = STSecureTrading.shared.isApplePayAvailable()
}
@IBAction func paymentButtonPressed(_ sender: Any) {
let itemSummary = PKPaymentSummaryItem(label: "Product Name", amount: 10.0) // Set item description.
paymentRequest = PKPaymentRequest() // Initialize payment request.
paymentRequest.countryCode = "GB" // Set ISO country code.
paymentRequest.currencyCode = "GBP" // Set three-letter ISO 4217 currency code.
paymentRequest.paymentSummaryItems = [itemSummary] // Assign created items to request object.
paymentRequest.supportedNetworks = STSecureTrading.shared.acceptedApplePayNetworks
// Assign supported networks to request object.
paymentRequest.merchantIdentifier = "merchant.com.company.app.applepay" // Set merchant identifier created for this purpose.
paymentRequest.merchantCapabilities = [.capability3DS] // Enable 3DS capability by adding it to the request.
// Initialize a PKPaymentAuthorizationViewController object using the payment request we created.
guard let applePayVC = PKPaymentAuthorizationViewController(paymentRequest: paymentRequest) else {
print("Error found creating payment request") // Manage error.
return
}
applePayVC.delegate = self // Set delegate to current class.
present(applePayVC, animated: true, completion: nil) // Present the Payment Authorization View Controller we just initialized.
}
//MARK: PKPaymentAuthorizationViewControllerDelegate methods
// Required implementation to dismiss payment authorization view controller.
func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
controller.dismiss(animated: true, completion: nil) // Dismiss payment controller.
}
// Required implementation to manage payment authorization result.
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
// Call the following library method to process payment with SecureTrading.
STSecureTrading.shared.processApplePayPayment(payment: payment, paymentRequest: paymentRequest, totalAmount: 1000) { (transactionResult, error) in
if error != nil {
// Required: Call completion block if error occurred.
completion(PKPaymentAuthorizationResult(status: .failure, errors: [])) // Manage completion error.
} else {
// Required: Call completion block if payment was processed successfuly by Secure Trading.
completion(PKPaymentAuthorizationResult(status: .success, errors: [])) // Manage completion success.
}
}
}
}
MyST
如同常规卡支付一样,使用Apple Pay处理的所有AUTH交易记录都可以在MyST中查看。在MyST中查看Apple Pay交易时,“钱包来源”将显示为“APPLEPAY”。点击此处查看我们的MyST文档。
测试
您的测试站点引用将连接到Apple Pay测试沙盒。因此,要在您的测试站点引用上处理支付,您需要将测试卡详细信息添加到您的受支持的设备上的Apple Wallet。
您将需要使用Apple提供的测试卡详细信息。请参阅以下链接:https://developer.apple.com/support/apple-pay-sandbox/
请注意,我们在测试文档中提供的测试卡详细信息不能用于处理Apple Pay测试交易。
如果您已在您的站点引用上启用了用户定义的规则(UDR),您需要确保您了解这些规则可能会如何影响您的Apple Pay交易。
如果您需要任何帮助,请联系我们的客户支持团队。