ApplePay集成教程

£神魔★判官ぃ 2021-09-11 03:50 341阅读 0赞

Apple Pay运行环境:iPhone6以上设备,操作系统最低iOS9.0以上,部分信息设置需要iOS9.2以上。目前还不支持企业证书添加。

环境搭建好后可以在模拟器上面运行,xcode7.2.1+iPhone6SP9.2系统下,系统会绑定几种虚拟的银行卡,和几个联系人,方便调试,支付也不会发生真实的付款,真的很方便。

准备工作

在接入Apple Pay之前,首先要申请MerchantID及对应证书。

申请MerchantID

Center

申请对应证书

Center 1

工程设置

Capability中启用Apple Pay权限,并选择merchantID。

Center 2

之后项目会多一个Applepay的配置文件ApplePayDemo.entitlements

如果Steps出现那三项就证明配置正确

Center 3

[objc] view plain copy

在CODE上查看代码片 派生到我的代码片

  1. #import “ViewController.h”
  2. #import
  3. @interface ViewController ()
  4. {
  5. NSMutableArray *summaryItems;
  6. NSMutableArray *shippingMethods;
  7. }
  8. @end
  9. @implementation ViewController
  10. - (void)viewDidLoad {
  11. [super viewDidLoad];
  12. UIButton *btn=[[UIButton alloc]init];
  13. btn.backgroundColor=[UIColor colorWithRed:0.196 green:0.371 blue:0.248 alpha:1.000];
  14. [btn setTitle:@”开始支付” forState:UIControlStateNormal];
  15. [btn addTarget:self action:@selector(buyNow) forControlEvents:UIControlEventTouchUpInside];
  16. btn.frame=CGRectMake(100, 100, 100, 50);
  17. [self.view addSubview:btn];
  18. }
  19. - (void)buyNow {
  20. if (![PKPaymentAuthorizationViewController class]) {
  21. //PKPaymentAuthorizationViewController需iOS8.0以上支持
  22. NSLog(@”操作系统不支持ApplePay,请升级至9.0以上版本,且iPhone6以上设备才支持”);
  23. return;
  24. }
  25. //检查当前设备是否可以支付
  26. if (![PKPaymentAuthorizationViewController canMakePayments]) {
  27. //支付需iOS9.0以上支持
  28. NSLog(@”设备不支持ApplePay,请升级至9.0以上版本,且iPhone6以上设备才支持”);
  29. return;
  30. }
  31. //检查用户是否可进行某种卡的支付,是否支持Amex、MasterCard、Visa与银联四种卡,根据自己项目的需要进行检测
  32. NSArray *supportedNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard,PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay];
  33. if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:supportedNetworks]) {
  34. NSLog(@”没有绑定支付卡”);
  35. return;
  36. }
  37. NSLog(@”可以支付,开始建立支付请求”);
  38. //设置币种、国家码及merchant标识符等基本信息
  39. PKPaymentRequest *payRequest = [[PKPaymentRequest alloc]init];
  40. payRequest.countryCode = @”CN”; //国家代码
  41. payRequest.currencyCode = @”CNY”; //RMB的币种代码
  42. payRequest.merchantIdentifier = @”merchant.com.kuaichengwuliu”; //申请的merchantID
  43. payRequest.supportedNetworks = supportedNetworks; //用户可进行支付的银行卡
  44. payRequest.merchantCapabilities = PKMerchantCapability3DS|PKMerchantCapabilityEMV; //设置支持的交易处理协议,3DS必须支持,EMV为可选,目前国内的话还是使用两者吧
  45. //如果需要邮寄账单可以选择进行设置,默认PKAddressFieldNone(不邮寄账单)
  46. // payRequest.requiredBillingAddressFields = PKAddressFieldEmail;
  47. //楼主感觉账单邮寄地址可以事先让用户选择是否需要,否则会增加客户的输入麻烦度,体验不好,
  48. //送货地址信息,这里设置需要地址和联系方式和姓名,如果需要进行设置,默认PKAddressFieldNone(没有送货地址)
  49. // payRequest.requiredShippingAddressFields = PKAddressFieldPostalAddress|PKAddressFieldPhone|PKAddressFieldName;
  50. //设置两种配送方式 用户可以手动选择
  51. PKShippingMethod *freeShipping = [PKShippingMethod summaryItemWithLabel:@”包邮” amount:[NSDecimalNumber zero]];
  52. freeShipping.identifier = @”freeshipping”;
  53. freeShipping.detail = @”6-8 天 送达”;
  54. PKShippingMethod *expressShipping = [PKShippingMethod summaryItemWithLabel:@”极速送达” amount:[NSDecimalNumber decimalNumberWithString:@”10.00”]];
  55. expressShipping.identifier = @”expressshipping”;
  56. expressShipping.detail = @”2-3 小时 送达”;
  57. shippingMethods = [NSMutableArray arrayWithArray:@[freeShipping, expressShipping]];
  58. //shippingMethods为配送方式列表,类型是 NSMutableArray,这里设置成成员变量,在后续的代理回调中可以进行配送方式的调整。
  59. payRequest.shippingMethods = shippingMethods;
  60. //配置价格 优惠价格
  61. NSDecimalNumber *subtotalAmount = [NSDecimalNumber decimalNumberWithMantissa:1275 exponent:-2 isNegative:NO]; //12.75
  62. PKPaymentSummaryItem *subtotal = [PKPaymentSummaryItem summaryItemWithLabel:@”商品价格” amount:subtotalAmount];
  63. NSDecimalNumber *discountAmount = [NSDecimalNumber decimalNumberWithString:@”-12.74”]; //-12.74
  64. PKPaymentSummaryItem *discount = [PKPaymentSummaryItem summaryItemWithLabel:@”优惠折扣” amount:discountAmount];
  65. NSDecimalNumber *methodsAmount = [NSDecimalNumber zero];
  66. PKPaymentSummaryItem *methods = [PKPaymentSummaryItem summaryItemWithLabel:@”包邮” amount:methodsAmount];
  67. NSDecimalNumber *totalAmount = [NSDecimalNumber zero];
  68. totalAmount = [totalAmount decimalNumberByAdding:subtotalAmount];
  69. totalAmount = [totalAmount decimalNumberByAdding:discountAmount];
  70. totalAmount = [totalAmount decimalNumberByAdding:methodsAmount];
  71. PKPaymentSummaryItem *total = [PKPaymentSummaryItem summaryItemWithLabel:@”ls” amount:totalAmount]; //最后这个是支付给谁。哈哈,快支付给我
  72. summaryItems = [NSMutableArray arrayWithArray:@[subtotal, discount, methods, total]];
  73. //summaryItems为账单列表,类型是 NSMutableArray,这里设置成成员变量,在后续的代理回调中可以进行支付金额的调整。
  74. payRequest.paymentSummaryItems = summaryItems;
  75. //ApplePay控件
  76. PKPaymentAuthorizationViewController *view = [[PKPaymentAuthorizationViewController alloc]initWithPaymentRequest:payRequest];
  77. view.delegate = self;
  78. [self presentViewController:view animated:YES completion:nil];
  79. }
  80. #pragma mark - PKPaymentAuthorizationViewControllerDelegate
  81. - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
  82. didSelectShippingContact:(PKContact *)contact
  83. completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<PKShippingMethod *> * _Nonnull, NSArray<PKPaymentSummaryItem *> * _Nonnull))completion{
  84. //contact送货地址信息,PKContact类型
  85. NSPersonNameComponents *name = contact.name; //联系人姓名
  86. CNPostalAddress *postalAddress = contact.postalAddress; //联系人地址
  87. NSString *emailAddress = contact.emailAddress; //联系人邮箱
  88. CNPhoneNumber *phoneNumber = contact.phoneNumber; //联系人手机
  89. NSString *supplementarySubLocality = contact.supplementarySubLocality; //补充信息,iOS9.2及以上才有
  90. //送货信息选择回调,如果需要根据送货地址调整送货方式,比如普通地区包邮+极速配送,偏远地区只有付费普通配送,进行支付金额重新计算,可以实现该代理,返回给系统:shippingMethods配送方式,summaryItems账单列表,如果不支持该送货信息返回想要的PKPaymentAuthorizationStatus
  91. completion(PKPaymentAuthorizationStatusSuccess, shippingMethods, summaryItems);
  92. }
  93. - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
  94. didSelectShippingMethod:(PKShippingMethod *)shippingMethod
  95. completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<PKPaymentSummaryItem *> * _Nonnull))completion{
  96. //配送方式回调,如果需要根据不同的送货方式进行支付金额的调整,比如包邮和付费加速配送,可以实现该代理
  97. PKShippingMethod *oldShippingMethod = [summaryItems objectAtIndex:2];
  98. PKPaymentSummaryItem *total = [summaryItems lastObject];
  99. total.amount = [total.amount decimalNumberBySubtracting:oldShippingMethod.amount];
  100. total.amount = [total.amount decimalNumberByAdding:shippingMethod.amount];
  101. [summaryItems replaceObjectAtIndex:2 withObject:shippingMethod];
  102. [summaryItems replaceObjectAtIndex:3 withObject:total];
  103. completion(PKPaymentAuthorizationStatusSuccess, summaryItems);
  104. }
  105. -(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectPaymentMethod:(PKPaymentMethod *)paymentMethod completion:(void (^)(NSArray<PKPaymentSummaryItem *> * _Nonnull))completion{
  106. //支付银行卡回调,如果需要根据不同的银行调整付费金额,可以实现该代理
  107. completion(summaryItems);
  108. }
  109. -(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectShippingAddress:(ABRecordRef)address completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<PKShippingMethod *> * _Nonnull, NSArray<PKPaymentSummaryItem *> * _Nonnull))completion{
  110. //送货地址回调,已弃用
  111. }
  112. - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
  113. didAuthorizePayment:(PKPayment *)payment
  114. completion:(void (^)(PKPaymentAuthorizationStatus status))completion {
  115. PKPaymentToken *payToken = payment.token;
  116. //支付凭据,发给服务端进行验证支付是否真实有效
  117. PKContact *billingContact = payment.billingContact; //账单信息
  118. PKContact *shippingContact = payment.shippingContact; //送货信息
  119. PKContact *shippingMethod = payment.shippingMethod; //送货方式
  120. // 这里需要将Token和地址信息发送到自己的服务器上,进行订单处理,处理之后,根据自己的服务器返回的结果调用completion()代码块,根据传进去的参数界面的显示结果会不同
  121. //等待服务器返回结果后再进行系统block调用
  122. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(33 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  123. //模拟服务器通信
  124. completion(PKPaymentAuthorizationStatusFailure);
  125. });
  126. }
  127. - (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller{
  128. [controller dismissViewControllerAnimated:YES completion:nil];
  129. }
  130. @end

demo下载地址 https://github.com/lsmakethebest/LSApplePayDemo

感谢作者原文地址:http://blog.csdn.net/itiapp\_home/article/details/52448012

发表评论

表情:
评论列表 (有 0 条评论,341人围观)

还没有评论,来说两句吧...

相关阅读