Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apple Pay detect Wallet has no credit cards

I am trying to implement Apple Pay for my app. I have the PKPaymentAuthorizationViewController trying to load up the Apple Pay View. This view controller was being returned as Nil by the constructor if I didn't have any cards already in my wallet. So, I decided to guide the user though the process where they enter their card information. I was able to achieve this using

PKPassLibrary* lib = [[PKPassLibrary alloc] init];
[lib openPaymentSetup];

Here is the part where I have the initialization of the PKPaymentAuthorizationViewController. This returns a valid object on Simulator showing the view. But on a real device without a configured credit card returns nil and runs into an runtime exception. Here is the initialization code:

if ([PKPaymentAuthorizationViewController canMakePayments]) {
// init arr
[arr addObject:total];
request.paymentSummaryItems = arr;
PKPaymentAuthorizationViewController *paymentPane = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
paymentPane.delegate = self;
[self presentViewController:paymentPane animated:TRUE completion:nil];
}

Here the array is a valid NSArray of PKPaymentSummaryItem which is why is successfully works on simulator.

I need to call the above method of openPaymentSetup, everytime I see a user without the credit card in their wallet. Is there a way to detect that?

Currently I am using

if ( [PKPassLibrary isPassLibraryAvailable] ) {
    PKPassLibrary* lib = [[PKPassLibrary alloc] init];
    if  ([lib passesOfType:PKPassTypePayment].count == 0 ) {
        [lib openPaymentSetup];
   }
}

But this will not work since I am looking at the count of passes in wallet. Which may be like boarding pass for airline, or eventbrite pass, etc.

Looked at : PKPaymentAuthorizationViewController present as nil view controller

Apple pay PKPaymentauthorizationViewController always returning nil when loaded with Payment request

https://developer.apple.com/library/ios/documentation/PassKit/Reference/PKPaymentAuthorizationViewController_Ref/

like image 813
Rushabh Avatar asked Nov 20 '15 21:11

Rushabh


1 Answers

I did as suggested by @maddy, and it actually worked. Its unfortunate that apple has very limited documentation about it. Thanks Maddy.

Here is my code

-(BOOL) openAddCardForPaymentUIIfNeeded
{
    if ( [PKPassLibrary isPassLibraryAvailable] )
    {
        if ( ![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:[NSArray arrayWithObjects: PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa, nil]] )
        {
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Add a Credit Card to Wallet" message:@"Would you like to add a credit card to your wallet now?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
            [alert show];
            return true;

        }
    }
    return false;
}

Now I am directing the user to go to add a card wizard in the wallet app. Is there any way I can get the user back to the App after he/she has finished adding the card in the Wallet?

Thanks!

like image 94
Rushabh Avatar answered Sep 28 '22 00:09

Rushabh