您的位置:首页 > 移动开发 > 微信开发

微信支付具体步骤和一些常见的坑

2016-04-14 11:50 459 查看
1.先去下载sdk   然后集成sdk  链接:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1

2.添加链接库: SystemConfiguration.framework,libz.dylib,libsqlite3.0.dylib,libc++.dylib。
3.在info里面添加配置信息

4.添加白名单

5.在AppDelegate里面 -
(BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions 方法中注册appid 

7.在AppDelegate里面重写两个方法

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{

    

    return [WXApi
handleOpenURL:url
delegate:self];    

}

- (BOOL)application:(UIApplication *)application openURL:(nonnull
NSURL *)url options:(nonnull
NSDictionary<NSString *,id> *)options

{

    return [WXApi
handleOpenURL:url
delegate:self];

}

8.向服务器请求数据拿到返回的数据

9.生成签名  生成签名的规则官方文档都有给出 链接:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3   要注意的是key=value&key1=value1;
key值小写,然后MD5加密,再把所有的字母转换成大写(注意生成的签名是32位的字符串)

 NSString *sign1 = [[NSString
stringWithFormat:@"appid=%@&noncestr=%@&package=%@&partnerid=%@&prepayid=%@×tamp=%@&key=%@",_appid,_noncestr,_package,_partnerid,_prepayid,_timesTamp,_key]
MD5];

    _sign = [sign1
uppercaseString];

    NSLog(@"sign = %@",_sign);

10.调起微信

if ([WXApi
isWXAppInstalled]) {

        PayReq *request = [[PayReq
alloc] init];

        request.partnerId =_partnerid;

        request.prepayId=
_prepayid;

        request.package =
_package;

        request.nonceStr=
_noncestr;

        request.timeStamp =
_timesTamp.intValue;

        request.sign=
_sign;

        [WXApi
sendReq:request];

        NSLog(@"partnerId=%@---prepayId=%@----package=%@----nonceStr=%@----timeStamp=%@----sign=%@",_partnerid,_prepayid,_package,_noncestr,_timesTamp,_sign);

        NSLog(@"进去没有");

        

    }else{

        NSLog(@"没有安装微信");

    }

8.回调

-(void) onResp:(BaseResp*)resp

{

    NSString *strMsg = [NSString
stringWithFormat:@"errcode:%d", resp.errCode];

    

    if([resp
isKindOfClass:[PayResp
class]]){

        NSNumber *numb = [NSNumber
numberWithInt:resp.errCode];

        

        NSNumberFormatter *numberFormatter = [[NSNumberFormatter
alloc] init];

        

        NSString *respCode = [numberFormatter
stringFromNumber:numb];//numb.description;

        

        NSDictionary *dic =
@{@"errCode":respCode};;

        

        [[NSNotificationCenter
defaultCenter] postNotificationName:@"weiXinPayCellback"
object:self
userInfo:dic];

        

        

        //支付返回结果

        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;

        }

        

    }

}

-(void)
onResp:(BaseResp*)resp  方法需要在AppDelegate里面调用,所以我要在支付界面使用这个结果就要发送一个通知;

具体的步骤就这么多了,很多小的细节大家要注意

调起的时候回出现只有一个确定按钮的情况像这种最主要的原因就是参数错了,可以看时间戳的类型,核对一下其他的参数(自己踩过的坑);

支付成功之后没有回调是因为-(void)
onResp:(BaseResp*)resp写的位置不对,需要写在AppDelegate里面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sdk 支付 微信