首先得在https://developer.apple.com/ 进行相关配置:
Certificates, Identifiers & Profiles——Identifiers
先配置:Merchant IDs,再配置:iOS App IDs,完成后下载并安装证书。
完成后在工程目录TARGETS——Capabilities中打开Apple Pay开关,
上图状态表示配置成功。
接下来就是代码集成了,也是比较简单的,就直接上代码了:
#import "ViewController.h"
#import <PassKit/PassKit.h>
@interface ViewController ()<PKPaymentAuthorizationViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *payView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1、判断设备是否支持Apple Pay
if (![PKPaymentAuthorizationViewController canMakePayments]) {
NSLog(@"当前设备不支持Apple Pay");
self.payView.hidden = YES;
} else if (![PKPaymentAuthorizationController canMakePaymentsUsingNetworks:@[PKPaymentNetworkChinaUnionPay,PKPaymentNetworkVisa]]) {
// 2、创建一个跳转按钮,进入添加银行卡界面
PKPaymentButton *btn = [PKPaymentButton buttonWithType:PKPaymentButtonTypeSetUp style:PKPaymentButtonStyleWhiteOutline];
[btn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
[self.payView addSubview:btn];
} else {
// 3、创建支付按钮
PKPaymentButton *btn = [PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleBlack];
[btn addTarget:self action:@selector(buy) forControlEvents:UIControlEventTouchUpInside];
[self.payView addSubview:btn];
}
}
- (void)jump {
PKPassLibrary *pl = [[PKPassLibrary alloc]init];
[pl openPaymentSetup];
}
- (void)buy {
// 授权成功
NSLog(@"授权成功,开始支付");
// 1、配置支付请求
PKPaymentRequest *requrst = [[PKPaymentRequest alloc]init];
// 1.1 配置支付请求
// 1.1.1 配置商户ID
requrst.merchantIdentifier = @"merchant.com.PJ.ApplePay.ApplePayDemo";
// 1.1.2 设置国家代码及货币码
requrst.countryCode = @"CN";
requrst.currencyCode = @"CNY";
// 1.1.3 设置支持的支付网络(方式)
requrst.supportedNetworks = @[PKPaymentNetworkChinaUnionPay,PKPaymentNetworkVisa];
// 1.1.4 设置商户处理方式 苹果要求3DS必须支持,可多选
requrst.merchantCapabilities = PKMerchantCapability3DS;
// 1.1.5 配置购买的商品列表
NSDecimalNumber *price = [[NSDecimalNumber alloc]initWithString:@"0.1"];
PKPaymentSummaryItem *item = [PKPaymentSummaryItem summaryItemWithLabel:@"iPhone 7" amount:price];
requrst.paymentSummaryItems = @[item];
// 1.2 配置请求附加信息
// 是否显示发票收货地址
requrst.requiredBillingAddressFields = PKAddressFieldAll;
// 是否显示商品收货地址
requrst.requiredShippingAddressFields = PKAddressFieldAll;
// 配置快递方式 可多个
NSDecimalNumber *price1 = [[NSDecimalNumber alloc]initWithString:@"0"];
PKShippingMethod *mothod = [PKShippingMethod summaryItemWithLabel:@"顺丰快递" amount:price1];
mothod.detail = @"24小时送达";
mothod.identifier = @"shunfeng";
requrst.shippingMethods = @[mothod];
// 配置快递类型
requrst.shippingType = PKShippingTypeStorePickup;
// 1.3 添加一些附加数据
// requrst.applicationData =
// 2、验证用户支付授权
PKPaymentAuthorizationViewController *payVC = [[PKPaymentAuthorizationViewController alloc]initWithPaymentRequest:requrst];
payVC.delegate = self;
[self presentViewController:payVC animated:YES completion:nil];
}
#pragma mark - PKPaymentAuthorizationViewControllerDelegate
// 授权成功
// 参数一 授权控制器
// 参数二 支付对象
// 参数三 系统给的回调代码块,通过这个代码块来确认支付是否成功了
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion {
BOOL isSucess = YES;
if (isSucess) {
completion(PKPaymentAuthorizationStatusSuccess);
} else {
completion(PKPaymentAuthorizationStatusSuccess);
}
}
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
NSLog(@"授权失败");
[self dismissViewControllerAnimated:YES completion:nil];
}
@end