我没有实际的设备来测试Apple Pay实现,因此,我使用模拟器(iPhone 6),当我点击Apple Pay按钮时,我能够看到我设置的所有信息(PaymentRequest)它手动。
但是,当我点击Pay with Passcode
按钮时,我收到tokenizedApplePayPayment = nil
并显示付款未完成。
-(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];
}