iOS集成微信支付

说明:微信支付是以跳转到微信客户端的形式实现的。
下面以微信官方demo提供的appid为例进行集成。

第一步:设置URL type

iOS集成微信支付_第1张图片
6A9D814A-ECE5-4680-8B69-347F793AC0CA.png

URL Schemes 填写申请得到的appid

第二步:导入API

iOS集成微信支付_第2张图片
41B153C1-B884-4F6C-BF5B-E8C8CDD75FEA.png

第三步:导入链接库

iOS集成微信支付_第3张图片
49353FBC-9147-4299-909D-9DD0E5D8AA04.png

后缀名为.dylib的文件可以根据以下操作导入:

iOS集成微信支付_第4张图片
FB9AA09D-1D9E-4274-A98F-99D7C3493E58.png

点击Add Other...后,按快捷键command+shift+G出现如下画面,按照图中的地址搜索即可:

iOS集成微信支付_第5张图片
E2337D2C-C96C-4BC8-8FD9-5F185F2A2757.png

第四步:设置白名单

89BB60F9-91A8-45D1-AEDB-49A52FFA6004.png

第五步:向微信注册

#import "WXApi.h"
遵守WXApiDelegate代理

注册:

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 注册微信
    [WXApi registerApp:@"appKey" withDescription:@"demo 2.0"];
    
    return YES;
}

 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    
    return [WXApi handleOpenURL:url delegate:self];
}

第六步:回调处理

- (void)onResp:(BaseResp *)resp {
    NSString *strMsg = [NSString stringWithFormat:@"errcode:%d",resp.errCode];
    NSString *strTitle;
    
    if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
        strTitle = [NSString stringWithFormat:@"发送媒体消息结果"];
    }
    
    if ([resp isKindOfClass:[PayResp class]]) {
        // 支付返回结果,实际支付结果需要去微信服务器端查询
        strTitle = [NSString stringWithFormat:@"支付结果"];
        switch (resp.errCode) {
            case WXSuccess:
                strMsg = @"支付结果:成功!";
                break;
                
            default:
                strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d,retstr = %@",resp.errCode,resp.errStr];
                break;
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}

第七步:发起支付

// 判断是否安装微信客户端
    if ([WXApi isWXAppInstalled] == NO) {
        [self showErrorToast:@"尚未安装微信客户端"];
        return;
    }
    
    NSString *urlString   = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios";
    //解析服务端返回json数据
    NSError *error;
    //加载一个NSURL对象
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    //将请求的url数据放到NSData对象中
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if ( response != nil) {
        NSMutableDictionary *dict = NULL;
        //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
        dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
        
        NSLog(@"url:%@",urlString);
        if(dict != nil){
            NSMutableString *retcode = [dict objectForKey:@"retcode"];
            if (retcode.intValue == 0){
                NSMutableString *stamp  = [dict objectForKey:@"timestamp"];
                
                //调起微信支付
                PayReq* req             = [[PayReq alloc] init];
                req.partnerId           = [dict objectForKey:@"partnerid"];
                req.prepayId            = [dict objectForKey:@"prepayid"];
                req.nonceStr            = [dict objectForKey:@"noncestr"];
                req.timeStamp           = stamp.intValue;
                req.package             = [dict objectForKey:@"package"];
                req.sign                = [dict objectForKey:@"sign"];
                [WXApi sendReq:req];
                //日志输出
                NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
            }else{
                NSLog(@"retmsg:%@",[dict objectForKey:@"retmsg"]);
            }
        }else{
            NSLog(@"服务器返回错误,未获取到json对象");
        }
    }else{
        NSLog(@"服务器返回错误");
    }

第八步:展示支付结果

请求后台接口,返回支付结果并展示。

注意:一定要从我们的后台查询支付结果,不能直接展示微信客户端返回的结果!

你可能感兴趣的:(iOS集成微信支付)