4
votes

I am getting the PK Payment auth view controller instance returned as nil. What is wrong with this code?

if([PKPaymentAuthorizationViewController canMakePayments])
{
    if ([PKPaymentAuthorizationViewController    canMakePaymentsUsingNetworks:@[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa]]) 
    {
        PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
        request.currencyCode = @"USD";
        request.countryCode = @"US";
        request.merchantCapabilities = 0;
        request.requiredBillingAddressFields=PKAddressFieldAll;
        request.merchantIdentifier = @"merchant.com.domain.mine";
        PKPaymentSummaryItem *item = [[PKPaymentSummaryItem alloc] init];
        item.label=@"Merchant";
        item.amount=[NSDecimalNumber decimalNumberWithString:@"10"];
        request.paymentSummaryItems=@[item];
        PKPaymentAuthorizationViewController *viewController =  [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
        viewController.delegate = self;
        [self presentViewController:viewController animated:YES   completion:nil];
    }
}
3

3 Answers

8
votes

Before accessing the PKPaymentAuthorizationViewController, you should configure Apple Pay properly on your iPhone device. If you have not configured Apple Pay on your device you'll get nil value for PKPaymentAuthorizationViewController. You can even find an exception on the console stating "This device cannot make payment."

To configure Apple Pay on your device follow the below steps:

  • Go to Settings.
  • Select Passbook and Apple Pay option (if this option is not visible in settings, go to General -> Language & Region, change your region to US or UK, after this you'll be able to see the Passbook & Apple Pay option in Settings)
  • Open Passbook application from your home screen and configure a valid credit/debit card (US/UK based card only).
  • After verifying the added card, run your application you'll get a valid PKPaymentAuthorizationViewController instance.

Hope this will help.

1
votes

I had a similar issue. It looks like you included it, but for anyone else struggling with this, my problem was not initially supplying merchantCapabilities to the request.

Swift:

request.merchantCapabilities = PKMerchantCapability.capability3DS

https://developer.apple.com/documentation/passkit/pkmerchantcapability?language=objc

1
votes

If you are instantiating the supporting networks with raw value, make sure they are done so with the proper capitalization.

// Summarized for posting purposes
let networks = ["AmEx", "Visa", "MasterCard", "Discover"].reduce(into: [PKPaymentNetwork]()) { $0.append(PKPaymentNetwork($1)) }

if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: networks, capabilities: .capability3DS) {
    // Hooray
}