首先导入框架:#import <PassKit/PassKit.h>
然后是代理:PKPaymentAuthorizationViewControllerDelegate
这个代理方法是为了和后台数据做判断完成支付的对接:
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion{
1.//把支付信息发送给服务器进行处理
2.这个方法是返回苹果服务器:
completion(PKPaymentAuthorizationStatusSuccess);
然后执行另一个代理方法:来处理UI
}
3.处理UI
-(void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller{
[controller dismissViewControllerAnimated:YES completion:nil];
NSLog(@"wancheng");
}
核心代码块:
在这之前必须判断手机是不是支持Applepay
[PKPaymentAuthorizationViewController canMakePayments]:
然后:
-(void)payStyleWithApplePay{
if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkChinaUnionPay,PKPaymentNetworkVisa]]) {
NSLog(@"不支持visa 和 银联");
//设置银行卡的代码:
[[[PKPassLibrary alloc]init] openPaymentSetup];
return;
}
//创建请求
PKPaymentRequest *re = [[PKPaymentRequest alloc]init];
re.merchantIdentifier = @"merchant.com.ezparking.ios.yoparking";
re.countryCode = @"CN";
re.currencyCode = @"CNY";//货币类型:人民币
re.supportedNetworks = @[PKPaymentNetworkChinaUnionPay,PKPaymentNetworkVisa];
re.merchantCapabilities = PKMerchantCapability3DS;
PKPaymentSummaryItem *widget = [PKPaymentSummaryItem summaryItemWithLabel:@"喜泊客" amount:[NSDecimalNumber decimalNumberWithString:@"0.1"]];
re.paymentSummaryItems = @[widget];//商品
//显示支付界面
PKPaymentAuthorizationViewController *vc = [[PKPaymentAuthorizationViewController alloc]initWithPaymentRequest:re];
vc.delegate = self;
[self presentViewController:vc animated:NO completion:nil];
}
最终结果: