Apple Pay运行环境:iPhone6以上设备,操作系统最低iOS9.0以上,部分信息设置需要iOS9.2以上。目前还不支持企业证书添加。
环境搭建好后可以在模拟器上面运行,xcode7.2.1+iPhone6SP9.2系统下,系统会绑定几种虚拟的银行卡,和几个联系人,方便调试,支付也不会发生真实的付款,真的很方便。
准备工作
在接入Apple Pay之前,首先要申请MerchantID及对应证书。
工程设置
Capability中启用Apple Pay权限,并选择merchantID。
之后项目会多一个Applepay的配置文件ApplePayYasin.entitlements
Xcode7.0以上不需要再手动添加需要引用的库了,只需要导入头文件就可以了
1
2
3
|
#import <passkit passkit.h=""> //用户绑定的银行卡信息
#import <passkit pkpaymentauthorizationviewcontroller.h=""> //Apple pay的展示控件
#import <addressbook addressbook.h=""> //用户联系信息相关</addressbook></passkit></passkit>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
if (![PKPaymentAuthorizationViewController class]) {
//PKPaymentAuthorizationViewController需iOS8.0以上支持
NSLog(@ "操作系统不支持ApplePay,请升级至9.0以上版本,且iPhone6以上设备才支持" );
return ;
}
//检查当前设备是否可以支付
if (![PKPaymentAuthorizationViewController canMakePayments]) {
//支付需iOS9.0以上支持
NSLog(@ "设备不支持ApplePay,请升级至9.0以上版本,且iPhone6以上设备才支持" );
return ;
}
//检查用户是否可进行某种卡的支付,是否支持Amex、MasterCard、Visa与银联四种卡,根据自己项目的需要进行检测
NSArray *supportedNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard,PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay];
if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:supportedNetworks]) {
NSLog(@ "没有绑定支付卡" );
return ;
}
|
创建支付请求PKPaymentRequest
这里需要注意RMB的币种代码是CNY
1
2
3
4
5
6
7
|
//设置币种、国家码及merchant标识符等基本信息
PKPaymentRequest *payRequest = [[PKPaymentRequest alloc]init];
payRequest.countryCode = @ "CN" ; //国家代码
payRequest.currencyCode = @ "CNY" ; //RMB的币种代码
payRequest.merchantIdentifier = @ "merchant.ApplePayDemoYasin" ; //申请的merchantID
payRequest.supportedNetworks = supportedNetworks; //用户可进行支付的银行卡
payRequest.merchantCapabilities = PKMerchantCapability3DS|PKMerchantCapabilityEMV; //设置支持的交易处理协议,3DS必须支持,EMV为可选,目前国内的话还是使用两者吧
|
1
2
3
4
5
|
// payRequest.requiredBillingAddressFields = PKAddressFieldEmail;
//如果需要邮寄账单可以选择进行设置,默认PKAddressFieldNone(不邮寄账单)
//楼主感觉账单邮寄地址可以事先让用户选择是否需要,否则会增加客户的输入麻烦度,体验不好,
payRequest.requiredShippingAddressFields = PKAddressFieldPostalAddress|PKAddressFieldPhone|PKAddressFieldName;
//送货地址信息,这里设置需要地址和联系方式和姓名,如果需要进行设置,默认PKAddressFieldNone(没有送货地址)
|
送货信息页面展示
设置货物的配送方式,不需要不配置
1
2
3
4
5
6
7
8
9
10
|
//设置两种配送方式
PKShippingMethod *freeShipping = [PKShippingMethod summaryItemWithLabel:@ "包邮" amount:[NSDecimalNumber zero]];
freeShipping.identifier = @ "freeshipping" ;
freeShipping.detail = @ "6-8 天 送达" ;
PKShippingMethod *expressShipping = [PKShippingMethod summaryItemWithLabel:@ "极速送达" amount:[NSDecimalNumber decimalNumberWithString:@ "10.00" ]];
expressShipping.identifier = @ "expressshipping" ;
expressShipping.detail = @ "2-3 小时 送达" ;
payRequest.shippingMethods = @[freeShipping, expressShipping];
|
每条账单的设置
账单列表使用PKPaymentSummaryItem添加描述和价格,价格使用NSDecimalNumber。
PKPaymentSummaryItem初始化:
label为商品名字或者是描述,amount为商品价格,折扣为负数,type为该条账单为最终价格还是估算价格(比如出租车价格预估)
1
2
|
+ (instancetype)summaryItemWithLabel:(NSString *)label amount:(NSDecimalNumber *)amount;
+ (instancetype)summaryItemWithLabel:(NSString *)label amount:(NSDecimalNumber *)amount type:(PKPaymentSummaryItemType)type NS_AVAILABLE(NA, 9_0);
|
NSDecimalNumber初始化:
NSDecimalNumber可以使用数字初始化,也可以使用字符串。
使用方法请移步我写的NSDecimalNumber--十进制数
添加账单列表:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
NSDecimalNumber *subtotalAmount = [NSDecimalNumber decimalNumberWithMantissa:1275 exponent:-2 isNegative:NO]; //12.75
PKPaymentSummaryItem *subtotal = [PKPaymentSummaryItem summaryItemWithLabel:@ "商品价格" amount:subtotalAmount];
NSDecimalNumber *discountAmount = [NSDecimalNumber decimalNumberWithString:@ "-12.74" ]; //-12.74
PKPaymentSummaryItem *discount = [PKPaymentSummaryItem summaryItemWithLabel:@ "优惠折扣" amount:discountAmount];
NSDecimalNumber *methodsAmount = [NSDecimalNumber zero];
PKPaymentSummaryItem *methods = [PKPaymentSummaryItem summaryItemWithLabel:@ "包邮" amount:methodsAmount];
NSDecimalNumber *totalAmount = [NSDecimalNumber zero];
totalAmount = [totalAmount decimalNumberByAdding:subtotalAmount];
totalAmount = [totalAmount decimalNumberByAdding:discountAmount];
totalAmount = [totalAmount decimalNumberByAdding:methodsAmount];
PKPaymentSummaryItem *total = [PKPaymentSummaryItem summaryItemWithLabel:@ "Yasin" amount:totalAmount]; //最后这个是支付给谁。哈哈,快支付给我
summaryItems = [NSMutableArray arrayWithArray:@[subtotal, discount, methods, total]];
//summaryItems为账单列表,类型是 NSMutableArray,这里设置成成员变量,在后续的代理回调中可以进行支付金额的调整。
payRequest.paymentSummaryItems = summaryItems;
|
显示购物信息并进行支付
1
2
3
4
|
//ApplePay控件
PKPaymentAuthorizationViewController *view = [[PKPaymentAuthorizationViewController alloc]initWithPaymentRequest:payRequest];
view.delegate = self;
[self presentViewController:view animated:YES completion:nil];
|
PKPaymentAuthorizationViewControllerDelegate代理
PKPayment 支付成功信息
1
2
3
4
5
|
PKPaymentToken *payToken = payment.token;
//支付凭据,发给服务端进行验证支付是否真实有效
PKContact *billingContact = payment.billingContact; //账单信息
PKContact *shippingContact = payment.shippingContact; //送货信息
PKContact *shippingMethod = payment.shippingMethod; //送货方式
|
PKContact 联系人信息
1
2
3
4
5
|
NSPersonNameComponents *name = contact.name; //联系人姓名
CNPostalAddress *postalAddress = contact.postalAddress; //联系人地址
NSString *emailAddress = contact.emailAddress; //联系人邮箱
CNPhoneNumber *phoneNumber = contact.phoneNumber; //联系人手机
NSString *supplementarySubLocality = contact.supplementarySubLocality; //补充信息,地址详细描述,其他备注等等,iOS9.2及以上才有
|
送货地址回调
1
2
3
4
5
6
7
|
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didSelectShippingContact:(PKContact *)contact
completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<pkshippingmethod *> * _Nonnull, NSArray<pkpaymentsummaryitem *> * _Nonnull))completion{
//contact送货地址信息,PKContact类型
//送货信息选择回调,如果需要根据送货地址调整送货方式,比如普通地区包邮+极速配送,偏远地区只有付费普通配送,进行支付金额重新计算,可以实现该代理,返回给系统:shippingMethods配送方式,summaryItems账单列表,如果不支持该送货信息返回想要的PKPaymentAuthorizationStatus
completion(PKPaymentAuthorizationStatusSuccess, shippingMethods, summaryItems);
}</pkpaymentsummaryitem *></pkshippingmethod *>
|
送货方式回调
1
2
3
4
5
6
7
8
9
10
11
12
13
|
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didSelectShippingMethod:(PKShippingMethod *)shippingMethod
completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<pkpaymentsummaryitem *> * _Nonnull))completion{
//配送方式回调,如果需要根据不同的送货方式进行支付金额的调整,比如包邮和付费加速配送,可以实现该代理
PKShippingMethod *oldShippingMethod = [summaryItems objectAtIndex:2];
PKPaymentSummaryItem *total = [summaryItems lastObject];
total.amount = [total.amount decimalNumberBySubtracting:oldShippingMethod.amount];
total.amount = [total.amount decimalNumberByAdding:shippingMethod.amount];
[summaryItems replaceObjectAtIndex:2 withObject:shippingMethod];
[summaryItems replaceObjectAtIndex:3 withObject:total];
completion(PKPaymentAuthorizationStatusSuccess, summaryItems);
}</pkpaymentsummaryitem *>
|
支付卡选择回调
1
2
3
4
|
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectPaymentMethod:(PKPaymentMethod *)paymentMethod completion:(void (^)(NSArray<pkpaymentsummaryitem *> * _Nonnull))completion{
//支付银行卡回调,如果需要根据不同的银行调整付费金额,可以实现该代理
completion(summaryItems);
}</pkpaymentsummaryitem *>
|
送货地址回调,已弃用
1
2
3
|
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectShippingAddress:(ABRecordRef)address completion:(void (^)(PKPaymentAuthorizationStatus, NSArray<pkshippingmethod *> * _Nonnull, NSArray<pkpaymentsummaryitem *> * _Nonnull))completion{
//送货地址回调,已弃用
}</pkpaymentsummaryitem *></pkshippingmethod *>
|
付款成功苹果服务器返回信息回调,做服务器验证
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus status))completion {
PKPaymentToken *payToken = payment.token;
//支付凭据,发给服务端进行验证支付是否真实有效
PKContact *billingContact = payment.billingContact; //账单信息
PKContact *shippingContact = payment.shippingContact; //送货信息
PKContact *shippingMethod = payment.shippingMethod; //送货方式
//等待服务器返回结果后再进行系统block调用
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//模拟服务器通信
completion(PKPaymentAuthorizationStatusSuccess);
});
}
|
支付完成回调
1
2
3
|
-(void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller{
[controller dismissViewControllerAnimated:YES completion:nil];
}
#import "ViewController.h" #import <PassKit/PassKit.h> @interface ViewController ()<PKPaymentAuthorizationViewControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad];
//1、判断当前设备是否支持苹果支付 // 2、判断是否添加银行卡 if (![PKPaymentAuthorizationViewController class]) { //PKPaymentAuthorizationViewController需iOS8.0以上支持 NSLog(@"操作系统不支持ApplePay,请升级至9.0以上版本,且iPhone6以上设备才支持"); return; } else if (![PKPaymentAuthorizationViewController canMakePayments]) { NSLog(@"当前设备不支持applepay"); } //PKPaymentNetworkChinaUnionPay 银联卡 。PKPaymentNetworkVisa vsa 卡 //检查用户是否可进行某种卡的支付,是否支持Amex、MasterCard、Visa与银联四种卡,,根据自己项目的需要进行检测 else if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkChinaUnionPay,PKPaymentNetworkVisa]]) { // 如果没有添加银行卡,创建一个跳转按钮,跳转到添加银行卡的界面 PKPaymentButton *button=[PKPaymentButton buttonWithType:PKPaymentButtonTypeSetUp style:PKPaymentButtonStyleWhiteOutline]; button.center=self.view.center; [self.view addSubview:button]; [button addTarget:self action:@selector(jumpAddBank) forControlEvents:UIControlEventTouchUpInside]; } else { // 创建一个够买按钮,当用户点击按钮时,购买一个商品 PKPaymentButton *button=[PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleBlack]; button.center=self.view.center; [self.view addSubview:button]; [button addTarget:self action:@selector(clickBuy) forControlEvents:UIControlEventTouchUpInside]; } } #pragma mark - 点击去购买 -(void)clickBuy { // 1、创建一个支付请求 PKPaymentRequest *request=[[PKPaymentRequest alloc]init]; // 配置支付请求 //1.1 配置商家ID request.merchantIdentifier=@"merchant.com.cym.weiliaopay"; //1.2 配置国家代码,以及货币代码 request.countryCode=@"CN"; request.currencyCode=@"CNY"; // 1.3 支付的银行卡 request.supportedNetworks=@[PKPaymentNetworkChinaUnionPay,PKPaymentNetworkVisa]; // 1.4配置商户的处理方式//设置支持的交易处理协议,3DS必须支持,EMV为可选,目前国内的话还是使用两者吧 request.merchantCapabilities=PKMerchantCapability3DS|PKMerchantCapabilityEMV; // 1.5 配置购买的商品列表 NSDecimalNumber *price=[NSDecimalNumber decimalNumberWithString:@"0.01"]; PKPaymentSummaryItem *item=[PKPaymentSummaryItem summaryItemWithLabel:@"汽车保养" amount:price]; request.paymentSummaryItems=@[item]; //配置请求的附加项 //1、是否显示发票收货地址 。显示哪些选项 // request.requiredBillingAddressFields=PKAddressFieldAll; // // 2、是否显示快递地址,显示哪些选项 // request.requiredShippingAddressFields=PKAddressFieldAll; //2、 验证用户的支付授权 PKPaymentAuthorizationViewController *PayVC=[[PKPaymentAuthorizationViewController alloc]initWithPaymentRequest:request]; PayVC.delegate=self; [self presentViewController:PayVC animated:YES completion:nil]; } #pragma mark -代理方法 //如果当用户授权成功,就会调用这个方法 /* 参数一:授权控制器 参数二:支付对象 参数三:系统给定的一个回调代码块,我们需要执行这个代码块,来告诉系统当前的的支付状态是否成功 */ - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus status))completion { NSLog(@"token=%@",payment.token); // 一般在此外,拿到支付信息,发送给服务器处理,处理完毕之后,服务器返回一个状态,告诉客户端,是否支付成功,然后由客户端进行处理 BOOL isSuccess=YES;
if (isSuccess) { completion(PKPaymentAuthorizationStatusSuccess); } else { completion(PKPaymentAuthorizationStatusFailure); } }
#pragma mari 当用户授权成功,或者取消授权时调用 - (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller { [self dismissViewControllerAnimated:YES completion:nil]; }
#pragma mark - 添加银行卡界面 -(void)jumpAddBank { // 跳转到添加银行卡界面 PKPassLibrary *pl=[[PKPassLibrary alloc]init]; [pl openPaymentSetup]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
|
转载自原文链接, 如需删除请联系管理员。
原文链接:iOS 集成apple Pay 接入详细教程,转载请注明来源!