IOS 银联支付

官方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--------------------------------------------

最后,感谢樊总的大力支持。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值