概念:
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
?