概念:
Apple Pay,一種移動(dòng)支付方式辅肾,由蘋果公司在2014年發(fā)布的一種基于NFC(近場(chǎng)通訊)的手機(jī)支付功能陪毡。
支付方式:
通過(guò)Touch ID/Passcode驗(yàn)證方式米母,用戶可以使用事先已經(jīng)儲(chǔ)存在6,6p或者更高級(jí)的設(shè)備上的銀行卡支付方式毡琉,其實(shí)本身相當(dāng)于一個(gè)錢包铁瞒。
使用前提:設(shè)備支持(6以上),系統(tǒng)支持(9.2以上)桅滋,在wallet應(yīng)用當(dāng)中已經(jīng)輸入了銀行卡信息
配置支付環(huán)境:1.使用xcode創(chuàng)建一個(gè)工程慧耍,并設(shè)置好對(duì)應(yīng)的bundleid
?????????????????????? 2.注冊(cè)并配置一個(gè)商業(yè)標(biāo)示符(a.登陸開(kāi)發(fā)者中心,b.進(jìn)入證書(shū)配置欄目,c.添加一個(gè)App ID蜂绎,d.配置Merchant ID, e.為Merchant ID配置證書(shū)栅表,并下載證書(shū)安裝到鑰匙串,f.檢查安裝到鑰匙串里面的證書(shū)是否有效师枣,g.綁定Merchant ID到App ID)
?????????????????????? 3.配置xcode項(xiàng)目怪瓶,開(kāi)啟apple pay功能(a.判斷當(dāng)前設(shè)備是否可以支付,b.判斷wallet有沒(méi)有添加該支付網(wǎng)絡(luò)的儲(chǔ)蓄卡或信用卡践美,c.創(chuàng)建一個(gè)支付請(qǐng)求洗贰,并配置各項(xiàng)信息,d.給支付授權(quán)陨倡,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.判斷當(dāng)前設(shè)備是否支持apple pay
if(![PKPaymentAuthorizationViewControllercanMakePayments]){
NSLog(@"當(dāng)前設(shè)備不支持apple pay");
//2.判斷wallet是否添加了銀行卡
}elseif([PKPaymentAuthorizationViewControllercanMakePaymentsUsingNetworks:@[PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay]])
{
//3.創(chuàng)建一個(gè)跳轉(zhuǎn)按鈕敛滋,當(dāng)用戶點(diǎn)擊按鈕時(shí),跳轉(zhuǎn)到添加銀行卡的界面
PKPaymentButton*button=[PKPaymentButtonbuttonWithType:PKPaymentButtonTypeSetUpstyle:PKPaymentButtonStyleWhiteOutline];
[button addTarget:self action:@selector(jump)forControlEvents:UIControlEventTouchUpInside];
[self.payView addSubview:button];
}else
{
//創(chuàng)建購(gòu)買按鈕兴革,當(dāng)用戶點(diǎn)擊按鈕時(shí)绎晃,點(diǎn)擊購(gòu)買
//3.創(chuàng)建一個(gè)跳轉(zhuǎn)按鈕,當(dāng)用戶點(diǎn)擊按鈕時(shí)杂曲,跳轉(zhuǎn)到添加銀行卡的界面
PKPaymentButton*button=[PKPaymentButtonbuttonWithType:PKPaymentButtonTypeSetUpstyle:PKPaymentButtonStyleWhiteOutline];
[button addTarget:self action:@selector(buy)forControlEvents:UIControlEventTouchUpInside];
[self.payView addSubview:button];
}
}
-(void)jump
{
//跳轉(zhuǎn)到銀行卡界面
PKPassLibrary*pl=[[PKPassLibraryalloc]init];
[pl openPaymentSetup];
}
//購(gòu)買
-(void)buy
{
NSLog(@"購(gòu)買商品庶艾,開(kāi)始支付");
//1.創(chuàng)建一個(gè)支付請(qǐng)求
PKPaymentRequest*request=[[PKPaymentRequestalloc]init];
//1.1配置支付請(qǐng)求
//1.1.1配置商家ID
request.merchantIdentifier=@"XXXXXX";
//1.1.2配置貨幣代碼,國(guó)家代碼
request.countryCode=@"CN";
request.currencyCode=@"CNY";
//1.1.3配置請(qǐng)求的支付網(wǎng)絡(luò)
request.supportedNetworks=@[PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay];
//1.1.4配置商戶的處理方式
request.merchantCapabilities=PKMerchantCapability3DS;
//1.1.5配置購(gòu)買的商品列表
NSDecimalNumber*price=[NSDecimalNumberdecimalNumberWithString:@"10.0"];
PKPaymentSummaryItem*item1=[PKPaymentSummaryItemsummaryItemWithLabel:@"蘋果6s"amount:price];
request.paymentSummaryItems=@[item1];
//1.2配置請(qǐng)求的附加項(xiàng)
//1.2.1 是否顯示發(fā)票的地址,顯示哪些選項(xiàng)
request.requiredBillingAddressFields=PKAddressFieldAll;
//1.2.2 是否顯示快遞地址擎勘,顯示哪些選項(xiàng)
request.requiredShippingAddressFields=PKAddressFieldAll;
//1.2.3 配置快遞方式
NSDecimalNumber*price1=[NSDecimalNumberdecimalNumberWithString:@"11.0"];
PKShippingMethod*method=[PKShippingMethodsummaryItemWithLabel:@"順豐快遞"amount:price1];
method.identifier=@"shunfeng";
method.detail=@"24小時(shí)內(nèi)送到";
request.shippingMethods=@[method];
//1.2.3.2配置快遞的類型
request.shippingType=PKShippingTypeStorePickup;
//1.3 添加一些附加數(shù)據(jù)
request.applicationData=[@"buyID = 1234"dataUsingEncoding:NSUTF8StringEncoding];
//2.驗(yàn)證用戶的支付授權(quán)
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{
//一般在此處咱揍,拿到支付信息,發(fā)送給服務(wù)器處理棚饵,處理完畢之后煤裙,服務(wù)器會(huì)返回一個(gè)狀態(tài),告訴客戶端噪漾,是否支付成功硼砰,然后由客戶端進(jìn)行處理
BOOL isSuccess=YES;
if(isSuccess){
completion(PKPaymentAuthorizationStatusSuccess);
}else
{
completion(PKPaymentAuthorizationStatusFailure);
}
}
-(void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController*)controller
{
NSLog(@"授權(quán)結(jié)束");
}
-(void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
?