今儿的地铁里已经摆满了Apple Pay的广告.So.我也来尝试下Apple Pay的集成.
新建一个项目.暂时起名为JLLApplePayTest.
ApplePay 设计到了对Merchant ID 的使用.经过个人测试,用了两种方法.
第一是直接在Capabilities 里首先将ApplePay 选为ON.此时的Merchant ID是为空.点击+号,会出现选择开发者账号界面.选择自己的开发者账号后.会出现如下界面.
创建完成后点OK就可以在Merchant ID里进行选择.但是我并不推荐这么做.虽然也创建了Merchant ID.但是并未增改其相关属性.可行度待验证.但是如果集成demo的话.是可以成立的.
下面介绍比较正规的Merchant ID申请方法.
在开发者中心里.找到Merchant ID.
点击添加按照顺序进行创建.
此时.重点来了,点击需要运用的Merchant ID.会出现以下情况
点击Edit 进行编辑
of course. 这里选择 YES .
接下来会出现一个选择CSR文件的界面.从钥匙请求CSR文件之类不再多提.
最后生成一个CSR证书.点击运行.
OK--- 证书告一段落.
接下来进入Xcode.
继续在Capabilities 里Apple Pay里进行操作.选择刚刚创建的Merchant ID.
此时看我们的项目,会多一个这样的文件.
OK.开始代码.
由于只是个小demo.所以我直接在ViewController里进行书写
首先,导入库.
#import <PassKit/PassKit.h>由于要实现协议方法.我们要遵守它的协议
@interface ViewController ()<PKPaymentAuthorizationViewControllerDelegate>
然后开始我们的编码.
- (void)ApplePayJudge{ //检查当前设备 if ([PKPaymentAuthorizationViewController canMakePayments]){ NSLog(@"可以支付"); PKPaymentRequest * request = [[PKPaymentRequest alloc]init]; PKPaymentSummaryItem * goodsOne = [PKPaymentSummaryItem summaryItemWithLabel:@"无尽" amount:[NSDecimalNumber decimalNumberWithString:@"3000"]]; PKPaymentSummaryItem * goodsTwo = [PKPaymentSummaryItem summaryItemWithLabel:@"三项" amount:[NSDecimalNumber decimalNumberWithString:@"3000"]]; PKPaymentSummaryItem * goodsThree = [PKPaymentSummaryItem summaryItemWithLabel:@"电刀" amount:[NSDecimalNumber decimalNumberWithString:@"2800"]]; PKPaymentSummaryItem * goodsFour = [PKPaymentSummaryItem summaryItemWithLabel:@"总计" amount:[NSDecimalNumber decimalNumberWithString:@"8800"]]; request.paymentSummaryItems = @[goodsOne,goodsTwo,goodsThree,goodsFour]; request.countryCode = @"CN"; request.currencyCode = @"CNY"; request.supportedNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa]; request.merchantIdentifier = @"merchant.com.JLL"; request.merchantCapabilities = PKMerchantCapabilityEMV; PKPaymentAuthorizationViewController *paymentPane = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request]; paymentPane.delegate = self; [self presentViewController:paymentPane animated:TRUE completion:nil]; }else{ NSLog(@"不支持支付"); } }
request.countryCode = @"CN"; request.currencyCode = @"CNY";
即为中国以及人民币的编码.相信大家会用的比较多.
此时在viewdidload 里实现这个方法.就能看到界面了.
此时.再实现协议的两个方法,来完成支付判断以及界面移除即可.
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controlle didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus status))completion{ NSLog(@"Payment was authorized: %@", payment); BOOL asyncSuccessful = FALSE; if(asyncSuccessful) { completion(PKPaymentAuthorizationStatusSuccess); NSLog(@"支付成功"); } else { completion(PKPaymentAuthorizationStatusFailure); NSLog(@"支付失败"); } } - (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller { NSLog(@"支付完成"); //支付完成后隐藏窗口 [controller dismissViewControllerAnimated:TRUE completion:nil]; }