博客> 微信支付的实现及问题
微信支付的实现及问题
2017-09-22 20:42 评论:0 阅读:247 飞翔信念
微信支付的实现及问题

昨天在项目中集成微信支付,调起支付的时候总是显示recode=-2,今天无意中发现是签名的问题,因为昨天在网上看到要自己生成签名,其实应该直接用服务器返回的签名。

1.首先要导入头文件和sdk,这不用说了,wechatAurhsdk.h,wxapi.h,wxapimanager.h,wxapimanager.m,wxapiobject.h,libwechatsdk.a 然后在配置在配置url type 填写微信的appid 然后在info.plist中配置白名单添加wechat和wexin,具体不说了

2出现错误搜一下基本就是wxapimanager支持arc,或者是导入framework不全等问题下面说代码

3appdelegate.h中代码

#import "WXApi.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

4appdelegate.m中的代码实现因为有sharesdk,有一部分是sharesdk中的注册代码包含微信

#import "WXApi.h"
#import "WXApiManager.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//微信支付注册
    [WXApi registerApp:@"wxc5aff9ad4c3c3b4c" withDescription:@"demo 2.0"];
case SSDKPlatformTypeWechat:
[ShareSDKConnector connectWeChat:[WXApi class] delegate:self];
 break;
case SSDKPlatformTypeWechat:
[appInfo SSDKSetupWeChatByAppId:@"APPID"
appSecret:@"APPSECRET"];

break;
[self.window makeKeyAndVisible];

    return YES;
}
- (BOOL)application:(UIApplication *)application
      handleOpenURL:(NSURL *)url
{
    return  [WXApi handleOpenURL:url delegate:self];

}
-(BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary<NSString> *)options
{
    return  [WXApi handleOpenURL:url delegate:self];
}

- (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 = @"支付结果:成功!";
                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:nil, nil];
    [alert show];
}

5调用地方实现调起支付调起支付就是下面这段话,weixindic是请求的服务器返回的数据里面有appid,签名等东西,签名一定要用服务器返回的,刚开始我自己生成了签名和服务器不一样,一直导致retcode=-2错误码,这样基本就可以成功支付了

//调起微信支付
    PayReq* req             = [[PayReq alloc] init];
    req.partnerId           = [weiXinDic objectForKey:@"partnerid"];
    req.prepayId            = [weiXinDic objectForKey:@"prepayid"];
    req.nonceStr            = [weiXinDic objectForKey:@"noncestr"];
    req.timeStamp           = [[weiXinDic objectForKey:@"timestamp"] intValue];
    req.package             = [weiXinDic objectForKey:@"package"];
    req.sign                = [weiXinDic objectForKey:@"sign"];

    [WXApi sendReq:req];
    //日志输出
    NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[weiXinDic objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,[weiXinDic objectForKey:@"sign"] );
收藏
0
sina weixin mail 回到顶部