In my Intents extension, I'm using the PKPaymentAuthorizationController
to allow the user to make a payment. I created a wrapper around it which looks like this:
class ApplePayModalController: NSObject {
public typealias AuthorizationHandler = (ApplePayModalController, PKPayment, (PKPaymentAuthorizationResult) -> Void) -> Void
private var onAuthorize: AuthorizationHandler?
func present(paymentRequest: PKPaymentRequest, onAuthorize: @escaping AuthorizationHandler) {
let controller = PKPaymentAuthorizationController(paymentRequest: paymentRequest)
self.onAuthorize = onAuthorize
controller.delegate = self
controller.present()
}
}
extension ApplePayModalController: PKPaymentAuthorizationControllerDelegate {
func paymentAuthorizationControllerDidFinish(_ controller: PKPaymentAuthorizationController) {
controller.dismiss()
}
func paymentAuthorizationController(_ controller: PKPaymentAuthorizationController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
guard let onAuthorize = self.onAuthorize else {
return completion(.init(status: .failure, errors: nil))
}
onAuthorize(self, payment, completion)
}
}
And this is where I'm getting the PKPaymentRequest that I'm using to present it:
public func createPKRequest(order: Order) -> PKPaymentRequest {
let paymentRequest = Stripe.paymentRequest(withMerchantIdentifier: PaymentRepository.merchantIdentifier, country: "US", currency: "USD")
paymentRequest.paymentSummaryItems = [
PKPaymentSummaryItem(label: "HSCO", amount: 1.00),
PKPaymentSummaryItem(label: "Phil", amount: NSDecimalNumber(value: Double(order.price) / 100))
]
paymentRequest.shippingType = .storePickup
paymentRequest.requiredBillingContactFields = [PKContactField.postalAddress]
return paymentRequest
}
However, I'm having the following issues when I present the controller:
Pressing the 'cancel' button causes the entire controller to stop working. I can't change the payment method, it doesn't dismiss, and the 'pay' button stops responding as well. However, I can still tap outside of the controller to dismiss it
The delegate methods aren't firing––at all. I tried adding more delegate methods and those weren't firing either; because of this, I can't handle payment authorization.
The behavior of the controller when I try to pay with different cards is not consistent. Using an Apple Amex test card, attempting to pay will fail immediately; using an Apple test Visa, it gets stuck on 'processing' for 15-20 seconds before displaying an alert saying "Apple Pay Not Completed" (Same as this post)
This is all running on an iPhone X, iOS 12.1.4, using Apple Pay in sandbox mode.