第一部分 准备阶段
1)硬件要求:
iPhone6及更新的设备,还有iPad air2和iPad mini3之后的平板,以及apple watch。具体型号(截止2016年2月):iPhone 6,iPhone 6 Plus,iPhone 6s, iPhone 6s Plus;iPad Air 2,iPad mini 3,iPad mini 4,iPad Pro;还有Apple Watch。
2)系统要求:
iPhone或iPad至少要升到iOS 9.2版,手表至少watch OS 2.1。
3)配置支付环境
1.进入苹果开发者中心,注册App ID。
App ID必须是explicit类型,否则无法使用ApplePay服务。
2.勾选Apple Pay并点击continue,然后点击“submit”提交。
3.在App IDs中找到刚刚注册成功的App ID,点击edit,对Apple Pay进行配置。
4.创建MerchantID。
5.配置Apple Pay服务,勾选Merchant ID。
6.App ID的apple pay选项变成enable表示配置成功。
7.创建Xcode项目,填入刚注册的BundleID。
8.在项目的target->Capabilities中,打开Apple Pay,并勾选Merchant ID。
第二部分 代码实现
项目地址:https://github.com/ZakariyyaSv/ApplePayDemo
一、判断支付环境
1.导入Passkit框架,加载view。
1
| #import <PassKit/PassKit.h>
|
2.判断当前设备是否支持Apple Pay以及wallet中是否有符合所支持网络的银行卡。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| if ([PKPaymentAuthorizationViewController canMakePayments]) { if ([PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkChinaUnionPay]]) { PKPaymentButton *buyBtn = [PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleBlack]; [buyBtn addTarget:self action:@selector(buyOnClick) forControlEvents:UIControlEventTouchUpInside]; [self.payView addSubview:buyBtn]; } else { PKPaymentButton *setupBtn = [PKPaymentButton buttonWithType:PKPaymentButtonTypeSetUp style:PKPaymentButtonStyleWhiteOutline]; [setupBtn addTarget:self action:@selector(jumpToWallet) forControlEvents:UIControlEventTouchUpInside]; [self.payView addSubview:setupBtn]; } } else { NSLog(@"The device cannot support ApplePay!"); }
|
运行效果如下图所示:
3.若wallet中没有绑定所支持网络的银行卡,则点击按钮跳转到wallet中去绑定银行卡。
1 2 3 4
| - (void)jumpToWallet{ PKPassLibrary *walletLibrary = [[PKPassLibrary alloc] init]; [walletLibrary openPaymentSetup]; }
|
4.若该设备支持Apple Pay并且有绑定所支持网络的银行卡则发起支付请求。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| - (void)buyOnClick{ NSLog(@"start to pay!"); PKPaymentRequest *payRequest = [[PKPaymentRequest alloc] init]; payRequest.countryCode = @"CN"; payRequest.currencyCode = @"CNY"; payRequest.merchantIdentifier = @"merchant.com.zakariyya.fabrunMerchant"; payRequest.merchantCapabilities = PKMerchantCapability3DS; payRequest.supportedNetworks = @[PKPaymentNetworkChinaUnionPay]; PKPaymentSummaryItem *cameraItem = [PKPaymentSummaryItem summaryItemWithLabel:@"单反相机" amount:[NSDecimalNumber decimalNumberWithString:@"5999.0"] type:PKPaymentSummaryItemTypeFinal]; PKPaymentSummaryItem *totalItem = [PKPaymentSummaryItem summaryItemWithLabel:@"佳能" amount:[NSDecimalNumber decimalNumberWithString:@"5999.0"] type:PKPaymentSummaryItemTypeFinal]; payRequest.paymentSummaryItems = @[cameraItem,totalItem]; PKPaymentAuthorizationViewController *authViewController = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:payRequest]; authViewController.delegate = self; [self presentViewController:authViewController animated:YES completion:nil]; }
|
支付请求配置包括国家代码、货币代码、商家ID、商家的处理方式、商家支持的网络、订单的详情,这几个要素缺一不可。
其中,merchantCapabilities是一个枚举值:
1 2 3 4 5 6
| typedef NS_OPTIONS(NSUInteger, PKMerchantCapability) { PKMerchantCapability3DS = 1UL << 0, PKMerchantCapabilityEMV = 1UL << 1, PKMerchantCapabilityCredit NS_ENUM_AVAILABLE_IOS(9_0) = 1UL << 2, PKMerchantCapabilityDebit NS_ENUM_AVAILABLE_IOS(9_0) = 1UL << 3 } NS_ENUM_AVAILABLE(NA, 8_0);
|
这些枚举值当中PKMerchantCapability3DS是必选项,其他的则是可选项。
supportedNetworks是一个包含常量字符串的数组:
1 2 3 4 5 6 7
| extern NSString * const PKPaymentNetworkAmex NS_AVAILABLE(NA, 8_0); extern NSString * const PKPaymentNetworkChinaUnionPay NS_AVAILABLE(NA, 9_2); extern NSString * const PKPaymentNetworkDiscover NS_AVAILABLE(NA, 9_0); extern NSString * const PKPaymentNetworkInterac NS_AVAILABLE(NA, 9_2); extern NSString * const PKPaymentNetworkMasterCard NS_AVAILABLE(NA, 8_0); extern NSString * const PKPaymentNetworkPrivateLabel NS_AVAILABLE(NA, 9_0); extern NSString * const PKPaymentNetworkVisa NS_AVAILABLE(NA, 8_0);
|
国内一般我们用到的会是中国银联,即PKPaymentNetworkChinaUnionPay,注意:iOS 9.2以后才开始支持银联支付。
5.监听用户授权状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #pragma mark - PKPaymentAuthorizationViewControllerDelegate - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion{ BOOL isSuccess = YES; if (isSuccess) { completion(PKPaymentAuthorizationStatusSuccess); } else { completion(PKPaymentAuthorizationStatusFailure); } } - (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller{ NSLog(@"Authorization did finished!"); [self dismissViewControllerAnimated:YES completion:nil]; }
|
附: