概念:
Apple Pay,一种移动支付方式,由苹果公司在2014年发布的一种基于NFC(近场通讯)的手机支付功能。
支付方式:
通过Touch ID/Passcode验证方式,用户可以使用事先已经储存在6,6p或者更高级的设备上的银行卡支付方式,其实本身相当于一个钱包。
使用前提:设备支持(6以上),系统支持(9.2以上),在wallet应用当中已经输入了银行卡信息
配置支付环境:1.使用xcode创建一个工程,并设置好对应的bundleid
2.注册并配置一个商业标示符(a.登陆开发者中心,b.进入证书配置栏目,c.添加一个App ID,d.配置Merchant ID, e.为Merchant ID配置证书,并下载证书安装到钥匙串,f.检查安装到钥匙串里面的证书是否有效,g.绑定Merchant ID到App ID)
3.配置xcode项目,开启apple pay功能(a.判断当前设备是否可以支付,b.判断wallet有没有添加该支付网络的储蓄卡或信用卡,c.创建一个支付请求,并配置各项信息,d.给支付授权,e.处理支付凭证)
//
// ViewController.m
// Apple Pay
//
// Created by 马悦 on 16/12/11.
// Copyright © 2016年 mayue. All rights reserved.
//
#import "ViewController.h"
#import
@interfaceViewController()
@property(nonatomic,strong)UIView*payView;
@end
@implementationViewController
-(void)viewDidLoad{
[super viewDidLoad];
//1.判断当前设备是否支持apple pay
if(![PKPaymentAuthorizationViewControllercanMakePayments]){
NSLog(@"当前设备不支持apple pay");
//2.判断wallet是否添加了银行卡
}elseif([PKPaymentAuthorizationViewControllercanMakePaymentsUsingNetworks:@[PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay]])
{
//3.创建一个跳转按钮,当用户点击按钮时,跳转到添加银行卡的界面
PKPaymentButton*button=[PKPaymentButtonbuttonWithType:PKPaymentButtonTypeSetUpstyle:PKPaymentButtonStyleWhiteOutline];
[button addTarget:self action:@selector(jump)forControlEvents:UIControlEventTouchUpInside];
[self.payView addSubview:button];
}else
{
//创建购买按钮,当用户点击按钮时,点击购买
//3.创建一个跳转按钮,当用户点击按钮时,跳转到添加银行卡的界面
PKPaymentButton*button=[PKPaymentButtonbuttonWithType:PKPaymentButtonTypeSetUpstyle:PKPaymentButtonStyleWhiteOutline];
[button addTarget:self action:@selector(buy)forControlEvents:UIControlEventTouchUpInside];
[self.payView addSubview:button];
}
}
-(void)jump
{
//跳转到银行卡界面
PKPassLibrary*pl=[[PKPassLibraryalloc]init];
[pl openPaymentSetup];
}
//购买
-(void)buy
{
NSLog(@"购买商品,开始支付");
//1.创建一个支付请求
PKPaymentRequest*request=[[PKPaymentRequestalloc]init];
//1.1配置支付请求
//1.1.1配置商家ID
request.merchantIdentifier=@"XXXXXX";
//1.1.2配置货币代码,国家代码
request.countryCode=@"CN";
request.currencyCode=@"CNY";
//1.1.3配置请求的支付网络
request.supportedNetworks=@[PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay];
//1.1.4配置商户的处理方式
request.merchantCapabilities=PKMerchantCapability3DS;
//1.1.5配置购买的商品列表
NSDecimalNumber*price=[NSDecimalNumberdecimalNumberWithString:@"10.0"];
PKPaymentSummaryItem*item1=[PKPaymentSummaryItemsummaryItemWithLabel:@"苹果6s"amount:price];
request.paymentSummaryItems=@[item1];
//1.2配置请求的附加项
//1.2.1 是否显示发票的地址,显示哪些选项
request.requiredBillingAddressFields=PKAddressFieldAll;
//1.2.2 是否显示快递地址,显示哪些选项
request.requiredShippingAddressFields=PKAddressFieldAll;
//1.2.3 配置快递方式
NSDecimalNumber*price1=[NSDecimalNumberdecimalNumberWithString:@"11.0"];
PKShippingMethod*method=[PKShippingMethodsummaryItemWithLabel:@"顺丰快递"amount:price1];
method.identifier=@"shunfeng";
method.detail=@"24小时内送到";
request.shippingMethods=@[method];
//1.2.3.2配置快递的类型
request.shippingType=PKShippingTypeStorePickup;
//1.3 添加一些附加数据
request.applicationData=[@"buyID = 1234"dataUsingEncoding:NSUTF8StringEncoding];
//2.验证用户的支付授权
PKPaymentAuthorizationViewController*avc=[[PKPaymentAuthorizationViewControlleralloc]initWithPaymentRequest:request];
avc.delegate=self;
[self presentViewController:avc animated:YES completion:nil];
}
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController*)controller
didAuthorizePayment:(PKPayment*)payment
completion:(void(^)(PKPaymentAuthorizationStatusstatus))completion{
//一般在此处,拿到支付信息,发送给服务器处理,处理完毕之后,服务器会返回一个状态,告诉客户端,是否支付成功,然后由客户端进行处理
BOOL isSuccess=YES;
if(isSuccess){
completion(PKPaymentAuthorizationStatusSuccess);
}else
{
completion(PKPaymentAuthorizationStatusFailure);
}
}
-(void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController*)controller
{
NSLog(@"授权结束");
}
-(void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end