Apple pay作为一种全新的支付方式,一经登陆国内,便收到各大银行的激励追捧,相信原因大家都了解,所以作为iOS开发人员,个人感觉有必要了解下。
apple pay的使用基本需要下面几步:
获得Apple pay开发资格
在开发者中心注册一个Merchant ID
下载并安装Merchant ID对应的cer证书,如果安装证书出现问题可以参考http://www.jianshu.com/p/a5f0795cf781
在需要使用Apple pay的项目中添加Apple pay功能
1、打开项目当中的Apple pay的开关,选择对应的Merchant ID
2、导入PassKit框架#import <PassKit/PassKit.h>
3、直接上代码,简单实现
#import "ViewController.h"
#import <PassKit/PassKit.h>
@interface ViewController ()<PKPaymentAuthorizationViewControllerDelegate>
/**支付按钮*/
@property (weak, nonatomic) PKPaymentButton *payButton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//是否可以使用apple pay
if ([self isApplePayEnable])
{
//创建支付按钮
[self addPayButton];
}
}
#pragma mark - 是否添加了银行卡
- (BOOL)isApplePayEnable
{
//1、判断设备是否支持Apple pay
if (![PKPaymentAuthorizationViewController canMakePayments])
{
return NO;
}
//2、判断是否绑定了银行卡
if(![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkAmex,PKPaymentNetworkDiscover,PKPaymentNetworkMasterCard,PKPaymentNetworkPrivateLabel,PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay]])
{
//如果没有绑定银行卡可以提示用户绑定银行卡菜可以交易
return NO;
}
return YES;
}
#pragma mark - 添加支付按钮
- (void)addPayButton
{
//创建按钮
PKPaymentButton *payButton = [PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleBlack];
self.payButton = payButton;
CGFloat btnW = payButton.bounds.size.width;
CGFloat btnH = payButton.bounds.size.height;
CGFloat btnX = ([UIScreen mainScreen].bounds.size.width - btnW) * 0.5;
CGFloat btnY = 100;
payButton.frame = CGRectMake(btnX, btnY, btnW, btnH);
[self.view addSubview:payButton];
//添加点击事件,弹出支付授权控制器
[payButton addTarget:self action:@selector(presentPayViewControler) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 创建支付请求
- (PKPaymentRequest *)createPayRequest
{
PKPaymentRequest *pkRequest = [[PKPaymentRequest alloc] init];
//1、设置货币代码,中国就是CNY,度娘查
pkRequest.currencyCode = @"CNY";
//2、设置国家代码,同样找度娘
pkRequest.countryCode = @"CN";
//3、设置支持的绑定卡类型
pkRequest.supportedNetworks = @[PKPaymentNetworkMasterCard, PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay];
//4、设置商家id(Merchant ID)
pkRequest.merchantIdentifier = @"merchant.bjwlade";
//5、设置支付的处理标准,具体的,我也不是很清楚,都写上吧
pkRequest.merchantCapabilities = PKMerchantCapability3DS | PKMerchantCapabilityDebit | PKMerchantCapabilityEMV | PKMerchantCapabilityCredit;
//6、可选收货人送货信息,这一项可以不设置,如果设置了,就必须在支付控制器,手动添加相关信息
// pkRequest.requiredShippingAddressFields = PKAddressFieldPostalAddress | PKAddressFieldPhone | PKAddressFieldEmail | PKAddressFieldName;
//7、设置账单,或者说发票的投递地址,当然也可以不设置,因为用户可能不需要这个账单,如果设置了,同样就必须在支付控制器,手动添加相关信息
//这里设置只有PKAddressFieldPostalAddress有用
// pkRequest.requiredBillingAddressFields = PKAddressFieldEmail | PKAddressFieldPostalAddress;
//8、设置默认的收货人信息,默认的订单或者发票投递地址也这样设置,这里不做设置
//默认设置,如果设置了,在支付控制器页面是看不到这些信息显示的
PKContact *contact = [[PKContact alloc] init];
//8.1设置收货人姓名
NSPersonNameComponents *userName = [[NSPersonNameComponents alloc] init];
userName.givenName = @"晓飞";
userName.familyName = @"王";
contact.name = userName;
//8.2设置收货人的地址
CNMutablePostalAddress *userAddress = [[CNMutablePostalAddress alloc] init];
userAddress.street = @"回龙观东大街";
userAddress.city = @"北京";
userAddress.state = @"GA";
userAddress.postalCode = @"100000"; //邮编
contact.postalAddress = userAddress;
//8.3设置收件人信息
pkRequest.shippingContact = contact;
//9、设置货物的配送方式
pkRequest.shippingMethods = [self setUpPayWay];
//10、设置商品列表
pkRequest.paymentSummaryItems = [self goodList];
return pkRequest;
}
#pragma mark - 商品列表信息
//商品列表的最后一个前面参数必须是收款人,后面参数是收款总价
- (NSArray *)goodList
{
PKPaymentSummaryItem *good1 = [PKPaymentSummaryItem summaryItemWithLabel:@"外星人笔记本电脑" amount:[NSDecimalNumber decimalNumberWithString:@"15000"]];
PKPaymentSummaryItem *good2 = [PKPaymentSummaryItem summaryItemWithLabel:@"罗技鼠标垫" amount:[NSDecimalNumber decimalNumberWithString:@"40"]];
PKPaymentSummaryItem *good3 = [PKPaymentSummaryItem summaryItemWithLabel:@"西部数据移动硬盘SSD 1G" amount:[NSDecimalNumber decimalNumberWithString:@"2500"]];
PKPaymentSummaryItem *total = [PKPaymentSummaryItem summaryItemWithLabel:@"陈伟" amount:[NSDecimalNumber decimalNumberWithString:@"17540"]];
return @[good1, good2,good3, total];
}
#pragma mark - 配送方式
//PKShippingMethod的label就是标签描述,amount就是使用费用,如果使用费用直接写成0,就表示免费
- (NSArray *)setUpPayWay
{
PKShippingMethod * method1 = [PKShippingMethod summaryItemWithLabel:@"顺风快递" amount:[NSDecimalNumber decimalNumberWithString:@"0"]];
method1.identifier = @"顺丰";
method1.detail = @"24小时到货";
PKShippingMethod * method2 = [PKShippingMethod summaryItemWithLabel:@"圆通快递" amount:[NSDecimalNumber decimalNumberWithString:@"18.00"]];
method2.identifier = @"圆通";
method2.detail = @"货到付款";
return @[method1, method2];
}
#pragma mark - 支付授权控制器
- (void)presentPayViewControler
{
PKPaymentAuthorizationViewController *payViewController = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:[self createPayRequest]];
//因为这个控制器有可能创建失败
if (payViewController)
{
payViewController.delegate = self;
[self presentViewController:payViewController animated:YES completion:nil];
}
}
#pragma mark - PKPaymentAuthorizationViewControllerDelegate
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus status))completion
{
NSLog(@"支付成功");
}
//用户点击了右上角的取消就会调用此方法
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
{
NSLog(@"取消支付");
[controller dismissViewControllerAnimated:YES completion:nil];
}
@end
几个需要注意的地方
1、PKPaymentRequest在设置属性的时候以下属性是必须的,否则会导致PKPaymentAuthorizationViewController创建失败,除了这些必须的,其他属性设置根据实际情况选择
currencyCode//货币编码缩写
countryCode//国家编码缩写
supportedNetworks//支持的磁卡类型
merchantIdentifier//在开发者中心创建的商家ID
merchantCapabilities//支付标准
paymentSummaryItems//商品列表
2、货币代码缩写和国家代码缩写
货币代码参考:http://www.webmasterhome.cn/huilv/huobidaima.asp
国家代码参考:http://country-code.cl/
3、商品列表的最后一个前面参数必须是收款人,后面参数是收款总价,其中总价应该包括邮费
4、由于PKPaymentAuthorizationViewController可能创建失败,操作改控制器的时候应该先判空
5、由于PKPaymentAuthorizationViewController是一个控制器,所以不一定需要PKPaymentButton来操作,可以直接创建PKPaymentAuthorizationViewController,当普通控制器操作
6、Apple pay使用模拟器同样可以,本文所有实现都是在模拟器下
7、模拟器下在打开Apple pay的开关的时候,timeout,可以暂时忽略,真机暂时没有尝试
网友评论