一,配置支付证书
1,创建 AppID 时记得勾选 Apple Pay service
2,创建完成后可以看到如下图,为 Configurable状态,点击编辑
3,再次点击编辑
4,选择 MerchantIDs
5,如果没有MerchantIDs进行创建
6,创建 Merchant 的步骤, 需要上传本机的 csr 文件,完成后下载证书到本地,添加到 KeyChains.
二,代码开发
开发结果示意图
代码如下:
//
// ViewController.m
// ApplePay
//
// Created by HMC on 16/10/9.
// Copyright © 2016年 SKing. All rights reserved.
//
#import "ViewController.h"
#import <PassKit/PassKit.h>
@interface ViewController ()<PKPaymentAuthorizationViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UIView *bgview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//检测设备是否支持 ApplePay
if (![PKPaymentAuthorizationViewController canMakePayments]) {
NSLog(@"设备不支持 Apple Pay");
}//检测 wallet 是否添加银行卡
else if(![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkChinaUnionPay]]) {
NSLog(@"wallet 中未添加银行卡");
PKPaymentButton * payButton = [PKPaymentButton buttonWithType:PKPaymentButtonTypeSetUp style:PKPaymentButtonStyleWhite ];
[payButton addTarget:self action:@selector(setWallet) forControlEvents:UIControlEventTouchUpInside];
//payButton.center = self.bgview.center;
[self.bgview addSubview:payButton];
}//正常支付
else {
PKPaymentButton * payButton = [PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleWhiteOutline ];
[payButton addTarget:self action:@selector(payWallet) forControlEvents:UIControlEventTouchUpInside];
//payButton.center = self.bgview.center;
[self.bgview addSubview:payButton];
}
}
/**
* 设置 wallet 的银行卡
*/
-(void)setWallet
{
PKPassLibrary * setLib = [PKPassLibrary new];
//跳转设置界面
[setLib openPaymentSetup];
}
/**
* 支付
*/
-(void)payWallet
{
NSLog(@"开始支付订单");
PKPaymentRequest * request = [PKPaymentRequest new];
//商家 id
request.merchantIdentifier = @"merchant.hmc.com";
//国家代码
request.countryCode = @"CN";
//国家货币代码
request.currencyCode = @"CNY";
//支付网络
request.supportedNetworks = @[PKPaymentNetworkChinaUnionPay];
//支付类型
request.merchantCapabilities = PKMerchantCapability3DS;
NSDecimalNumber * countNum = [[NSDecimalNumber alloc] initWithString:@"0.11"];
PKPaymentSummaryItem * item = [PKPaymentSummaryItem summaryItemWithLabel:@"中国美术学院" amount:countNum];
request.paymentSummaryItems = @[item];
PKPaymentAuthorizationViewController * authorViewController = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
authorViewController.delegate = self;
[self presentViewController:authorViewController animated:YES completion:nil];
}
/**
* 授权成功会调用
*
* @param controller 控制器
* @param payment 支付对象
* @param completion 完成代码块
*/
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion
{
// 支付的过程:
// 1,使用商家公钥加密付款信息和客户信息
// 2,发送客户的支付信息到商家的系统
// 3,商家系统使用商家的私钥解密,生成支付状态返回客户端
// 返回payment 的支付信息,获取服务器的支付状态
BOOL paymenyOfStatus = YES;
if (paymenyOfStatus) {
completion(PKPaymentAuthorizationStatusSuccess);
}else{
completion(PKPaymentAuthorizationStatusFailure);
}
}
-(void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
{
NSLog(@"支付成功");
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
代码地址: 点这里