作品链接:
http://www.jianshu.com/users/1e0f5e6f73f6/top_articles
1.导入第三方框架和设置支付界面
#import <PassKit/PassKit.h>
@interface ViewController ()<PKPaymentAuthorizationViewControllerDelegate>
/**支付view */
@property (weak, nonatomic) IBOutlet UIView *payView;
@end
2.设置支付框架
- (void)viewDidLoad {
[super viewDidLoad];
// 1.判断当前设备是否是支付苹果支付
if (![PKPaymentAuthorizationViewController canMakePayments]) {
NSLog(@"当前设备不支持APPlepay");
self.payView.hidden = YES;
// 判断是否添加了银行卡 PKPaymentNetworkChinaUnionPay 银联卡,在iOS9.2才支持
} else if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay]]){
// 创建一个跳转按钮,当用户点击按钮时,跳转到添加银行卡界面
PKPaymentButton *button = [PKPaymentButton buttonWithType:PKPaymentButtonTypeSetUp style:PKPaymentButtonStyleWhiteOutline];
[button addTarget:self action:@selector(addCard) forControlEvents:UIControlEventTouchUpInside];
[self.payView addSubview:button];
} else {
// 创建一个购买按钮,当用户点击按钮时,购买一个商品
PKPaymentButton *button = [PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleBlack];
[button addTarget:self action:@selector(buy) forControlEvents:UIControlEventTouchUpInside];
}
}
3.添加银联卡设置
// 跳转到添加银行卡界面 添加银行卡需在真机,模拟器不行
- (void)addCard
{
PKPassLibrary *pl = [[PKPassLibrary alloc] init];
[pl openPaymentSetup];
}
4.购买商品
// 购买商品
- (void)buy
{
NSLog(@"购买商品");
// 1.创建一个请求支付
PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
// 2.配置支付请求
// 2.1配置商家ID
request.merchantIdentifier = @"商家ID";
// 2.2配置货币代码,以及国家代码
request.countryCode = @"CN";
request.currencyCode = @"CNY";
// 2.3配置请求支持的网络
request.supportedNetworks = @[PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay];
// 2.4配置商户的处理方式
request.merchantCapabilities = PKMerchantCapability3DS;
// 2.5配置购买的商品列表
NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:@"100.00"];
PKPaymentSummaryItem *item1 = [PKPaymentSummaryItem summaryItemWithLabel:@"保温杯" amount:price];
NSDecimalNumber *prices = [NSDecimalNumber decimalNumberWithString:@"100.00"];
PKPaymentSummaryItem *item2 = [PKPaymentSummaryItem summaryItemWithLabel:@"商家" amount:prices];
// 注意:支付列表最后一个,代表汇总
request.paymentSummaryItems = @[item1, item2];
// 3.配置请求的附加项
// 3.1是否显示发票收货地址,显示哪些选项
request.requiredBillingAddressFields = PKAddressFieldAll;
// 3.2是否显示快递地址,显示哪些选项
request.requiredShippingAddressFields = PKAddressFieldAll;
// 4.配置快递方式
// 4.1快递费用
NSDecimalNumber *p1 = [NSDecimalNumber decimalNumberWithString:@"15.00"];
PKShippingMethod *method = [PKShippingMethod summaryItemWithLabel:@"顺风快递" amount:p1];
method.detail = @"送货上门";
method.identifier = @"shunfeng";
request.shippingMethods = @[method];
// 4.2配置快递的类型
request.shippingType = PKShippingTypeStorePickup;
// 4.3添加一些附加数据
request.applicationData = [@"buyID=123456789" dataUsingEncoding:NSUTF8StringEncoding];
// 5.验证用户的支付授权
PKPaymentAuthorizationViewController *avc = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
avc.delegate = self;// 注意:需遵守PKPaymentAuthorizationViewControllerDelegate方法
[self presentViewController:avc animated:YES completion:nil];
}
5.代理方法
#pragma mark - PKPaymentAuthorizationViewControllerDelegate方法
/**
* 如果用户授权成功,就会调用这个方法
*
* @param controller 授权控制器
* @param payment 支付对象
* @param completion 系统给定的一个回调代码块,来告诉系统当前的支付状态是否是成功
*/
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus status))completion
{
// 一般在此处,拿到支付信息,发送给服务器,处理完毕之后,服务器返回一个状态,告诉客户端是否支持成功,然后客户端进行处理
BOOL isSucess = YES;
if (isSucess) {
completion(PKPaymentAuthorizationStatusSuccess);
} else {
completion(PKPaymentAuthorizationStatusFailure);
}
}
// 当用户授权结束,或者取消授权时调用
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
{
NSLog(@"授权结束");
[self dismissViewControllerAnimated:controller completion:nil];
}