作品鏈接:
http://www.lxweimin.com/users/1e0f5e6f73f6/top_articles
1.導(dǎo)入第三方框架和設(shè)置支付界面
#import <PassKit/PassKit.h>
@interface ViewController ()<PKPaymentAuthorizationViewControllerDelegate>
/**支付view */
@property (weak, nonatomic) IBOutlet UIView *payView;
@end
2.設(shè)置支付框架
- (void)viewDidLoad {
[super viewDidLoad];
// 1.判斷當(dāng)前設(shè)備是否是支付蘋果支付
if (![PKPaymentAuthorizationViewController canMakePayments]) {
NSLog(@"當(dāng)前設(shè)備不支持APPlepay");
self.payView.hidden = YES;
// 判斷是否添加了銀行卡 PKPaymentNetworkChinaUnionPay 銀聯(lián)卡,在iOS9.2才支持
} else if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay]]){
// 創(chuàng)建一個(gè)跳轉(zhuǎn)按鈕,當(dāng)用戶點(diǎn)擊按鈕時(shí),跳轉(zhuǎn)到添加銀行卡界面
PKPaymentButton *button = [PKPaymentButton buttonWithType:PKPaymentButtonTypeSetUp style:PKPaymentButtonStyleWhiteOutline];
[button addTarget:self action:@selector(addCard) forControlEvents:UIControlEventTouchUpInside];
[self.payView addSubview:button];
} else {
// 創(chuàng)建一個(gè)購(gòu)買按鈕,當(dāng)用戶點(diǎn)擊按鈕時(shí),購(gòu)買一個(gè)商品
PKPaymentButton *button = [PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleBlack];
[button addTarget:self action:@selector(buy) forControlEvents:UIControlEventTouchUpInside];
}
}
3.添加銀聯(lián)卡設(shè)置
// 跳轉(zhuǎn)到添加銀行卡界面 添加銀行卡需在真機(jī),模擬器不行
- (void)addCard
{
PKPassLibrary *pl = [[PKPassLibrary alloc] init];
[pl openPaymentSetup];
}
4.購(gòu)買商品
// 購(gòu)買商品
- (void)buy
{
NSLog(@"購(gòu)買商品");
// 1.創(chuàng)建一個(gè)請(qǐng)求支付
PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
// 2.配置支付請(qǐng)求
// 2.1配置商家ID
request.merchantIdentifier = @"商家ID";
// 2.2配置貨幣代碼,以及國(guó)家代碼
request.countryCode = @"CN";
request.currencyCode = @"CNY";
// 2.3配置請(qǐng)求支持的網(wǎng)絡(luò)
request.supportedNetworks = @[PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay];
// 2.4配置商戶的處理方式
request.merchantCapabilities = PKMerchantCapability3DS;
// 2.5配置購(gòu)買的商品列表
NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:@"100.00"];
PKPaymentSummaryItem *item1 = [PKPaymentSummaryItem summaryItemWithLabel:@"保溫杯" amount:price];
NSDecimalNumber *prices = [NSDecimalNumber decimalNumberWithString:@"100.00"];
PKPaymentSummaryItem *item2 = [PKPaymentSummaryItem summaryItemWithLabel:@"商家" amount:prices];
// 注意:支付列表最后一個(gè),代表匯總
request.paymentSummaryItems = @[item1, item2];
// 3.配置請(qǐng)求的附加項(xiàng)
// 3.1是否顯示發(fā)票收貨地址,顯示哪些選項(xiàng)
request.requiredBillingAddressFields = PKAddressFieldAll;
// 3.2是否顯示快遞地址,顯示哪些選項(xiàng)
request.requiredShippingAddressFields = PKAddressFieldAll;
// 4.配置快遞方式
// 4.1快遞費(fèi)用
NSDecimalNumber *p1 = [NSDecimalNumber decimalNumberWithString:@"15.00"];
PKShippingMethod *method = [PKShippingMethod summaryItemWithLabel:@"順風(fēng)快遞" amount:p1];
method.detail = @"送貨上門";
method.identifier = @"shunfeng";
request.shippingMethods = @[method];
// 4.2配置快遞的類型
request.shippingType = PKShippingTypeStorePickup;
// 4.3添加一些附加數(shù)據(jù)
request.applicationData = [@"buyID=123456789" dataUsingEncoding:NSUTF8StringEncoding];
// 5.驗(yàn)證用戶的支付授權(quán)
PKPaymentAuthorizationViewController *avc = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
avc.delegate = self;// 注意:需遵守PKPaymentAuthorizationViewControllerDelegate方法
[self presentViewController:avc animated:YES completion:nil];
}
5.代理方法
#pragma mark - PKPaymentAuthorizationViewControllerDelegate方法
/**
* 如果用戶授權(quán)成功,就會(huì)調(diào)用這個(gè)方法
*
* @param controller 授權(quán)控制器
* @param payment 支付對(duì)象
* @param completion 系統(tǒng)給定的一個(gè)回調(diào)代碼塊,來告訴系統(tǒng)當(dāng)前的支付狀態(tài)是否是成功
*/
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus status))completion
{
// 一般在此處,拿到支付信息,發(fā)送給服務(wù)器,處理完畢之后,服務(wù)器返回一個(gè)狀態(tài),告訴客戶端是否支持成功,然后客戶端進(jìn)行處理
BOOL isSucess = YES;
if (isSucess) {
completion(PKPaymentAuthorizationStatusSuccess);
} else {
completion(PKPaymentAuthorizationStatusFailure);
}
}
// 當(dāng)用戶授權(quán)結(jié)束,或者取消授權(quán)時(shí)調(diào)用
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
{
NSLog(@"授權(quán)結(jié)束");
[self dismissViewControllerAnimated:controller completion:nil];
}