iOS开发中微信支付的集成流程与注意事项

来源:互联网 发布:matlab 输出数组 编辑:程序博客网 时间:2024/05/16 18:03

文章原文:http://www.cnblogs.com/shaoting/p/5162706.html

1.导入SDK:

上官网下载SDK,然后拷入工程即可

2.导入相应依赖库,选择"target"->"Link Binary With Libraries".

3.在AppDelegate.m中注册微信,如下所示:

1
2
3
4
5
6
7
8
9
10
11
#import "AppDelegate.h"
#import "WXApi.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
//测试时.真机测试
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [WXApi registerApp:@"wxb4ba3c02aa476ea1" withDescription:@"微信接口测试"];
    return YES;
}

 4.在点击controller中添加如下代码.

在点击按钮方法中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//============================================================
    // V3&V4支付流程实现
    // 注意:参数配置请查看服务器端Demo
    // 更新时间:2015年11月20日
    //============================================================
   //微信测试接口,集成时换位自己的后台接口.
    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"];
        NSLog(@"%@",retcode);
        NSLog(@"%@",stamp);
    //NSMutableDictionary *signDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:dict[@""], nil]
     
    //调起微信支付
    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"];// @"47B7A45B8DB4E7BB88B8F9F4E268CB5D"; //[dict objectForKey:@"sign"];
    BOOL result = [WXApi sendReq:req];
 
        NSLog(@"-=-=-=-=- %d", result);
    //日志输出
    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 );
    return @"";
    }else{
    return [dict objectForKey:@"retmsg"];
    }
    }else{
    return @"服务器返回错误,未获取到json对象";
    }
    }else{
    return @"服务器返回错误";
    }

 5.注册协议WXApiDelegate,需实现onResp方法.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//微信回调
-(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 = @"支付结果:成功!";
                NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
                break;
                 
            default:
                strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
                NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
                break;
        }
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nilnil];
    [alert show];
}

另:因为iOS9的原因,需要配置下项目,如:

https:

白名单:

更多白名单http://www.cnblogs.com/shaoting/p/5148323.html

URL types:

 

 

下面是朋友做的demo实现,本人亲测可用,需真机测试:

http://download.csdn.net/detail/shaoting19910730/9419497 


0 0