19

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.

2
  • how are you presenting the alert? I do not know too much about applepay, but since it is a view controller, are you presenting the alert on the applepayviewcontroller's view? Jul 18, 2017 at 15:00
  • Not sure about your deployment target. I think instead of showing a UIAlertView, presenting a UIAlertController might be a better idea. As long as you have the reference to the UIViewController that you want to show alert on top of, this should not be a problem. Jul 24, 2017 at 5:29

4 Answers 4

16
+100

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

0
10

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];
  • The customer approves the purchase using Touch ID (or, if that fails 3 times, by entering their passcode).
  • The thumbprint icon turns into a spinner, with the label “Processing”
  • Your delegate receives the paymentAuthorizationViewController(_:didAuthorizePayment:completion:) callback
  • Your application communicates asynchronously with your payment processor and website backend to actually make a charge with those payment details. Once this complete, you invoke the completion handler that you’re given as a parameter with either PKPaymentAuthorizationStatus.success or PKPaymentAuthorizationStatus.failure depending on the result.
  • The PKPaymentAuthorizationViewController spinner animates into a success or failure icon. If successful, a notification will arrive from PassBook indicating a charge on the customer’s credit card.
  • Your delegate receives the paymentAuthorizationViewControllerDidFinish(_:) callback. It is then responsible for calling dismiss(animated:completion:) to dismiss the payment screen.

Error Screen

Error Screen

- (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];
}
8

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.

0

An action sheet is a specific style of alert that appears in response to a control or action, and presents a set of two or more choices related to the current context. Use an action sheet to let people initiate tasks, or to request confirmation before performing a potentially destructive operation. On smaller screens, an action sheet slides up from the bottom of the screen. On larger screens, an action sheet appears all at once as a popover.

Provide a Cancel button if it adds clarity. A Cancel button instills confidence when the user is abandoning a task. Cancel buttons should always be included in action sheets at the bottom of the screen.

Make destructive choices prominent. Use red for buttons that perform destructive or dangerous actions, and display these buttons at the top of an action sheet.

Avoid enabling scrolling in an action sheet. If an action sheet has too many options, people must scroll to see all of the choices. Scrolling requires extra time to make a choice and is hard to do without inadvertently tapping a button.

For developer guidance, see the UIAlertControllerStyleActionSheet constant in UIAlertController.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.