官方SDk下载地址 https://open.unionpay.com/ajweb/index
测试Demo : http://pan.baidu.com/s/1kUKMaqz
1.导入官方SDK。直接拖入工程即可
2.添加系统包:Target –> build Phases –> Link Binary With Libraries 路径下添加4个包: CFNetwork.framework 、 SystemConfiguration.framework 、Libz.tbd 、 libPaymentControl.a (拖入工程的时候自动导入了).效果图
3.在工程的info.plist设置中。添加一个URl Types回调协议。只需要将工程名复制一遍到URL Schemes
4.配置info.plist文件 将这两个字段粘贴到info.plist的source Code 模式下 字段如下:
LSApplicationQueriesSchemes
uppaysdk
uppaywallet
uppayx1
uppayx2
uppayx3
NSAppTransportSecurity NSAllowsArbitraryLoads
《1》第一个Array 表示协议白名单。 《2》 第二个字典表示ios 9 https 接入网络协议字段
6.添加 -ObjC宏 :选择工程targets——》build settings ->Linking->other linker flags
7.将涉及到引用UPPaymentControl.h的源文件的后缀名都改为.mm
在APPDelegate.m文件中复制下面代码:
------------------------APPDelegate.m--------------------------------------------
- (BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
[[UPPaymentControl defaultControl] handlePaymentResult:url completeBlock:^(NSString *code, NSDictionary *data) {
//结果code为成功时,先校验签名,校验成功后做后续处理
if([code isEqualToString:@"success"]) {
//判断签名数据是否存在
if(data == nil){
//如果没有签名数据,建议商户app后台查询交易结果
return;
}
//数据从NSDictionary转换为NSString
NSData *signData = [NSJSONSerialization dataWithJSONObject:data
options:0
error:nil];
NSString *sign = [[NSString alloc] initWithData:signData encoding:NSUTF8StringEncoding];
//验签证书同后台验签证书
//此处的verify,商户需送去商户后台做验签
if([self verify:sign]) {
NSLog(@"支付成功");
//支付成功且验签成功,展示支付成功提示
}
else {
NSLog(@"验签失败");
//验签失败,交易结果数据被篡改,商户app后台查询交易结果
}
}
else if([code isEqualToString:@"fail"]) {
//交易失败
}
else if([code isEqualToString:@"cancel"]) {
//交易取消
}
}];
return YES;
}
-(BOOL) verify:(NSString *) resultStr {
//验签证书同后台验签证书
//此处的verify,商户需送去商户后台做验签
return NO;
}
------------------------APPDelegate.mm--------------------------------------------
------------------------ViewController.mm--------------------------------------------
#import "UPPaymentControl.h"
#define kURL_TN_Normal @"http://101.231.204.84:8091/sim/getacptn"
@interface ViewController (){
NSMutableData* _responseData;
}
@end
- (IBAction)Pay:(UIButton *)sender {
// 检查是否安装银联App的接口 - (BOOL)isPaymentAppInstalled
[self startNetWithURL:[NSURL URLWithString:kURL_TN_Normal]];
}
- (void)startNetWithURL:(NSURL *)url
{
NSURLRequest * urlRequest=[NSURLRequest requestWithURL:url];
NSURLConnection* urlConn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
[urlConn start];
}
#pragma mark - connection
//接收到与 银联支付 的对接信息
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
{
NSHTTPURLResponse *rsp = (NSHTTPURLResponse*)response;
NSInteger code = [rsp statusCode];
if (code != 200)
{
NSLog(@"网络错误");
[connection cancel];
}
else
{
_responseData = [[NSMutableData alloc] init];
}
}
//拼接报文
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_responseData appendData:data];
}
//连接已经完成导入
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *tn = [[NSMutableString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding];
if (tn != nil && tn.length > 0)
{
NSLog(@"tn=%@",tn);
//tn :交易单号
//scheme :支付完成之后返回的协议: 一般我们填APP名称
//mode 01 :测试环境 00:生产环境
//
[[UPPaymentControl defaultControl] startPay:tn fromScheme:这写你的APP工程名 mode:@"01" viewController:self];
}
}
//连接失败
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"网络错误");
}
------------------------ViewController.mm--------------------------------------------
最后,感谢樊总的大力支持。