Using the standard UIAlertView code below will show the alert underneath the Apple Pay PKPaymentAuthorizationViewController sheet.
[[[UIAlertView alloc] initWithTitle:@"Payment Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil] show];
How can I show it above the payment authorization sheet? Or is there a different way of presenting error messages for Apple Pay? I want to give specific error messages when the user enters an invalid shipping address.
You can't show UI elements on top of any Remote View Controllers
because it could compromise the security of the system. This includes PKPaymentAuthorizationViewController
.
Read more about Remote View Controllers here
There's a new callback in iOS 11 for
public func paymentAuthorizationController(_ controller: PKPaymentAuthorizationController, didAuthorizePayment payment: PKPayment,
handler completion: (PKPaymentAuthorizationResult) -> Void)
As you can see, the handler changes from
completion: (PKPaymentAuthorizationStatus) -> Void)
to
handler completion: (PKPaymentAuthorizationResult) -> Void)
Starting with iOS 11 we will get a status
with an array of NSErrors
on the completion handler.
Have a look at this year's session for more details.
You can't show UIAlertView
on the PKPaymentAuthorizationViewController
because of the security of the system.
The entire UI of PKPaymentAuthorizationViewController
is presented via a Remote View Controller. This means that outside the PKPaymentRequest you give it, it’s impossible to otherwise style or modify the contents of this view.
And for handle Apple Pay error you have to use PKPaymentAuthorizationViewControllerDelegate
delegate method to show payment is successfully complete or there are any error.
For show PKPaymentAuthorizationViewController
,
present payment view controller as:
PKPaymentAuthorizationViewController *paymentVC = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
paymentVC.delegate = self;
[self presentViewController:paymentVC animated:true completion:nil];
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus status))completion {
//=========================================
//=========================================
// Call your api here for charge payment and according to that api result show complition as follow
//========================================
//========================================
// Use your payment processor's SDK to finish charging your customer.
// When this is done, call:
completion(PKPaymentAuthorizationStatusSuccess);
// When this is Payment not completed, call:
// completion(PKPaymentAuthorizationStatusFailure);
// When this is Supplied billing address is insufficient or otherwise invalid, call:
// completion(PKPaymentAuthorizationStatusInvalidBillingPostalAddress);
// When this is Supplied postal address is insufficient or otherwise invalid, call:
// completion(PKPaymentAuthorizationStatusInvalidShippingPostalAddress);
// When this is Supplied contact information is insufficient or otherwise invalid, call:
// completion(PKPaymentAuthorizationStatusInvalidShippingContact);
}
// Sent to the delegate when payment authorization is finished. This may occur when
// the user cancels the request, or after the PKPaymentAuthorizationStatus parameter of the
// paymentAuthorizationViewController:didAuthorizePayment:completion: has been shown to the user.
//
// The delegate is responsible for dismissing the view controller in this method.
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
[self dismissViewControllerAnimated:true completion:nil];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With