3.2 代码实现
// 用户绑定的银行卡信息#import // Apple pay的展示控件#import // 用户联系信息相关#import
if(![PKPaymentAuthorizationViewController canMakePayments]) { NSLog(@"不能支付"); return;}
- 3) 判断"Wallet有没有添加该支付网络的储蓄卡/信用卡"
if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay]]) { NSLog(@"Wallet没有添加该支付网络的储蓄卡/信用卡"); // 创建一个设置按钮 // PKPaymentButton *button = [PKPaymentButton buttonWithType:PKPaymentButtonTypeSetUp style:PKPaymentButtonStyleWhiteOutline]; // [button addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside]; // button.center = self.view.center; // [self.view addSubview:button]; return;}
// 1. 创建一个支付请求PKPaymentRequest *request = [[PKPaymentRequest alloc] init];// 2. 参数配置// 2.1 商店标识request.merchantIdentifier = @"merchant.520it.com";// 2.2 货币代码request.currencyCode = @"CNY";// 2.3 国家编码request.countryCode = @"CN";// 2.4 支持的支付网络(PKPaymentNetworkChinaUnionPay iOS9.2开始支持)request.supportedNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay];// 2.5 支付请求包含一个支付摘要项目的列表NSDecimalNumber *price1 = [NSDecimalNumber decimalNumberWithString:@"2"];PKPaymentSummaryItem *item1 = [PKPaymentSummaryItem summaryItemWithLabel:@"手榴弹" amount:price1];NSDecimalNumber *price2 = [NSDecimalNumber decimalNumberWithString:@"6"];PKPaymentSummaryItem *item2 = [PKPaymentSummaryItem summaryItemWithLabel:@"炸弹" amount:price2 type:PKPaymentSummaryItemTypePending];NSDecimalNumber *totalAmount = [NSDecimalNumber zero];totalAmount = [totalAmount decimalNumberByAdding:price1];totalAmount = [totalAmount decimalNumberByAdding:price2];PKPaymentSummaryItem *total = [PKPaymentSummaryItem summaryItemWithLabel:@"小码哥财务中心" amount:totalAmount type:PKPaymentSummaryItemTypePending];// 注意: 数组最后一个是总价格request.paymentSummaryItems = @[item1, item2, total];// 2.6 运输方式NSDecimalNumber *shippingPrice = [NSDecimalNumber decimalNumberWithString:@"18.0"];PKShippingMethod *method = [PKShippingMethod summaryItemWithLabel:@"顺丰快递" amount:shippingPrice];method.detail = @"24小时送到!";method.identifier = @"shunfeng";request.shippingMethods = @[method];request.shippingType = PKShippingTypeServicePickup;// 2.7 通过指定merchantCapabilities属性来指定你支持的支付处理标准,3DS支付方式是必须支持的,EMV方式是可选的,request.merchantCapabilities = PKMerchantCapability3DS | PKMerchantCapabilityEMV | PKMerchantCapabilityCredit | PKMerchantCapabilityDebit;// 2.8 需要的配送信息和账单信息request.requiredBillingAddressFields = PKAddressFieldAll;request.requiredShippingAddressFields = PKAddressFieldAll;// 2.9 存储额外信息// 使用applicationData属性来存储一些在你的应用中关于这次支付请求的唯一标识信息,比如一个购物车的标识符。在用户授权支付之后,这个属性的哈希值会出现在这次支付的token中。request.applicationData = [@"购物车ID: 123456" dataUsingEncoding:NSUTF8StringEncoding];
// 3. 开始支付PKPaymentAuthorizationViewController *paymentPane = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];if (paymentPane == nil) { NSLog(@"授权控制器创建失败"); return;}paymentPane.delegate = self;[self presentViewController:paymentPane animated:YES completion:nil];
/*** 当授权成功之后会调用这个代理方法*/- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus status))completion { // PKPayment *temp = payment; NSLog(@"验证授权---%@", payment.token); NSLog(@"验证通过后, 需要开发者继续完成交易"); // 它需要你连接服务器并上传支付令牌和 其他信息,以完成整个支付流程。 BOOL isSuccess = YES; if (isSuccess) { completion(PKPaymentAuthorizationStatusSuccess); } else { completion(PKPaymentAuthorizationStatusFailure); }}
/*** 当授权成功之后或者取消授权之后会调用这个代理方法*/- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller { NSLog(@"取消或者交易完成"); [self dismissViewControllerAnimated:YES completion:nil];}
- 8) 支付授权的流程:
- 框架发送支付请求给安全模块,只有安全模块可以访问存储在设备上的标记化的卡信息。
- 安全模块把特定的卡和商家等支付数据加密,以保证只有苹果可以读取,然后发送给框架。框架会将这些数据发送给苹果。
- 苹果服务器再次加密这些支付数据,以保证只有商家可以读取。然后服务器对它进行签名,生成支付token,然后发送给设备。
- 框架调用相应的代理方法并传入这个token,然后你的代理方法传送token给你的服务器。