PKPaymentAuthorizationViewController present as nil view controller

馋奶兔 提交于 2019-12-09 19:31:18

问题


I am trying to integrate Apple Pay in my demo app following this link & I am facing this issue issue. I have updated my iphone 6+ os to 8.1.1 version but still not able to present PKPaymentAuthorizationViewController properly & I am getting this error "Application tried to present a nil modal view controller on target".Please suggest something since I am stuck on this.Here is the code which I have written :-

PKPaymentRequest *request = [[PKPaymentRequest alloc] init];

request.currencyCode = @"USD";
request.countryCode = @"US";
// This is a test merchant id to demo the capability, this would work with Visa cards only.
request.merchantIdentifier = @"merchant.com.procharge";  // replace with YOUR_APPLE_MERCHANT_ID
request.applicationData = [@"" dataUsingEncoding:NSUTF8StringEncoding];
request.merchantCapabilities = PKMerchantCapability3DS;
request.supportedNetworks = @[PKPaymentNetworkMasterCard, PKPaymentNetworkVisa, PKPaymentNetworkAmex];
request.requiredBillingAddressFields = PKAddressFieldPostalAddress|PKAddressFieldPhone|PKAddressFieldEmail;
request.requiredShippingAddressFields = PKAddressFieldPostalAddress|PKAddressFieldPhone|PKAddressFieldEmail;

///Set amount here
NSString *amountText =  @"0.01"; // Get the payment amount
NSDecimalNumber *amountValue = [NSDecimalNumber decimalNumberWithString:amountText];

PKPaymentSummaryItem *item = [[PKPaymentSummaryItem alloc] init];
item.amount = amountValue;
//item.amount = [[NSDecimalNumber alloc] initWithInt:20];
item.label = @"Test Payment Total";

request.paymentSummaryItems = @[item];

PKPaymentAuthorizationViewController *vc = nil;

// need to setup correct entitlement to make the view to show
@try
{
    vc = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
}

@catch (NSException *e)
{
    NSLog(@"Exception %@", e);
}

if (vc != nil)
{
    vc.delegate = self;
    [self presentViewController:vc animated:YES completion:CompletionBlock];
}
else
{
    //The device cannot make payments. Please make sure Passbook has valid Credit Card added.
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"PassKit Payment Error"
                                                    message:NSLocalizedString(@"The device cannot make payment at this time. Please check Passbook has Valid Credit Card and Payment Request has Valid Currency & Apple MerchantID.", @"")
                                                   delegate:nil
                                          cancelButtonTitle:NSLocalizedString(@"OK", @"")
                                          otherButtonTitles:nil];
    [alert show];


}

Thanks & Regards


回答1:


Did you implement these methods to check before trying to present the viewcontroller?

// Determine whether this device can process payment requests.
// YES if the device is generally capable of making in-app payments.
// NO if the device cannot make in-app payments or if the user is restricted from authorizing payments.
+ (BOOL)canMakePayments;

// Determine whether this device can process payment requests using specific payment network brands.
// Your application should confirm that the user can make payments before attempting to authorize a payment.
// Your application may also want to alter its appearance or behavior when the user is not allowed
// to make payments.
// YES if the user can authorize payments on this device using one of the payment networks supported
// by the merchant.
// NO if the user cannot authorize payments on these networks or if the user is restricted from
// authorizing payments.
+ (BOOL)canMakePaymentsUsingNetworks:(NSArray *)supportedNetworks;


来源:https://stackoverflow.com/questions/26992970/pkpaymentauthorizationviewcontroller-present-as-nil-view-controller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!