I do not have an actual device to test Apple Pay implementation, therefore, I am using emulator (iPhone 6), when I click Apple Pay button, and I could able to see all the information (PaymentRequest) that I have set it up manually.
However, when I click Pay with Passcode
button, I am getting tokenizedApplePayPayment = nil
and it shows payment not completed.
-(void) viewWillAppear: (BOOL) animated {
if ([PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkVisa, PKPaymentNetworkMasterCard, PKPaymentNetworkAmex]]) {
UIButton *button = [self applePayButton];
[self.view addSubview:button];
}
}
- (UIButton *)applePayButton {
UIButton *button;
if ([PKPaymentButton class]) { // Available in iOS 8.3+
button = [PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleBlack];
}
[button addTarget:self action:@selector(tappedApplePay) forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (IBAction)tappedApplePay {
PKPaymentRequest *paymentRequest = [self paymentRequest];
PKPaymentAuthorizationViewController *vc = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:paymentRequest];
vc.delegate = self;
[self presentViewController:vc animated:YES completion:nil];
}
- (PKPaymentRequest *)paymentRequest {
PKPaymentRequest *paymentRequest = [[PKPaymentRequest alloc] init];
paymentRequest.merchantIdentifier = @"merchant.com.xxxx.xxxx";
paymentRequest.supportedNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkVisa, PKPaymentNetworkMasterCard];
paymentRequest.merchantCapabilities = PKMerchantCapability3DS;
paymentRequest.countryCode = @"US"; // e.g. US
paymentRequest.currencyCode = @"USD"; // e.g. USD
paymentRequest.paymentSummaryItems =
@[
[PKPaymentSummaryItem summaryItemWithLabel:@"Beef Kabob" amount:[NSDecimalNumber decimalNumberWithString:@"10"]],
// Add add'l payment summary items...
[PKPaymentSummaryItem summaryItemWithLabel:@"XXX" amount:[NSDecimalNumber decimalNumberWithString:@"110"]]
];
return paymentRequest;
}
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus))completion {
// Example: Tokenize the Apple Pay payment
BTApplePayClient *applePayClient = [[BTApplePayClient alloc]
initWithAPIClient:self.braintreeClient];
[applePayClient tokenizeApplePayPayment:payment
completion:^(BTApplePayCardNonce *tokenizedApplePayPayment,
NSError *error) {
if (tokenizedApplePayPayment) {
completion(PKPaymentAuthorizationStatusSuccess);
} else {
// Tokenization failed. Check `error` for the cause of the failure.
// Indicate failure via the completion callback:
completion(PKPaymentAuthorizationStatusFailure);
}
}];
}
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
[self dismissViewControllerAnimated:YES completion:nil];
}
You can not test Apple Pay with simulator. You have to use real device because for simulator Apple is sending token value as nil.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.