Here is Apple's guidelines on implementing Apple Pay.
Here is the relevant section:
Using PKPaymentAuthorizationViewController methods
If canMakePayments
returns NO
, the device does not support Apple Pay. Do not display the Apple Pay button. Instead, fall back to another method of payment.
If canMakePayments
returns YES
but canMakePaymentsUsingNetworks
: returns NO
, the device supports Apple Pay, but the user has not added a card for any of the requested networks. You can, optionally, display a payment setup button, prompting the user to set up his or her card. As soon as the user taps this button, initiate the process of setting up a new card (for example, by calling the openPaymentSetup method).
To create an Apple Pay–branded button for initiating payment request on iOS 8.3 or later, use the PKPaymentButton class.
From the PKPaymentButton docs:
Provides a button that is used either to trigger payments through
Apple Pay or to prompt the user to set up a card.
You can initialize it with a type setUp
.
When the user taps this button, call 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()
}
}