这是苹果的
guidelines on implementing Apple Pay.
以下是相关章节:
使用
PKPaymentAuthorizationViewController
方法
如果
canMakePayments
回报
NO
,该设备不支持Apple Pay。不要显示Apple Pay按钮。相反,回到另一种支付方式。
如果
可以付款
回报
YES
但是
canMakePaymentsUsingNetworks
:返回
不
,该设备支持Apple Pay,但用户尚未为任何请求的网络添加卡。您可以选择显示一个支付设置按钮,提示用户设置其卡。用户点击此按钮后,立即启动设置新卡的过程(例如,通过调用openPaymentSetup方法)。
要创建一个Apple Pay品牌按钮,用于在iOS8.3或更高版本上启动付款请求,请使用
PKPaymentButton
上课。
从PKPaymentButton文档:
提供用于触发付款的按钮
苹果支付或提示用户设置一张卡。
你可以用一个类型初始化它
setUp
.
当用户点击此按钮时,调用
openPaymentSetup
.
override func viewDidLoad() {
super.viewDidLoad()
var applePayButton: PKPaymentButton?
if !PKPaymentAuthorizationViewController.canMakePayments() {
// Apple Pay not supported
return
}
if !PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: [.masterCard]) {
// Apple Pay supported and payment not setup
applePayButton = PKPaymentButton.init(paymentButtonType: .setUp, paymentButtonStyle: .black)
applePayButton?.addTarget(self, action: #selector(self.setupPressed(_:)), for: .touchUpInside)
} else {
// Apple Pay supported and payment setup
applePayButton = PKPaymentButton.init(paymentButtonType: .buy, paymentButtonStyle: .black)
applePayButton?.addTarget(self, action: #selector(self.payPressed(_:)), for: .touchUpInside)
}
applePayButton?.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(applePayButton!)
applePayButton?.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
applePayButton?.widthAnchor.constraint(equalToConstant: 200).isActive = true
applePayButton?.heightAnchor.constraint(equalToConstant: 60).isActive = true
applePayButton?.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -20).isActive = true
}
@objc func payPressed(_ sender: PKPaymentButton){
// Start payment
}
@objc func setupPressed(_ sender: PKPaymentButton){
let passLibrary = PKPassLibrary()
passLibrary.openPaymentSetup()
}
}