Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you show an UIAlertView above the Apple Pay PKPaymentAuthorizationViewController?

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.

like image 814
CTL Avatar asked Jun 02 '15 21:06

CTL


3 Answers

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

like image 125
klinger Avatar answered Oct 23 '22 19:10

klinger


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.

like image 45
pesch Avatar answered Oct 23 '22 21:10

pesch


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];
}
like image 37
Nikhlesh Bagdiya Avatar answered Oct 23 '22 21:10

Nikhlesh Bagdiya