司小文的专栏

总会有人走的很慢,幸运的是我们从不停息~

微信支付的使用(微信支付SDK)

微信的思路与支付宝的思路类似,一样是把各项参数从你们公司的APP中获取到,然后实例化一个微信封装好的对象,给这个对象的属性赋值,从而把对象通过调用微信的方法发起请求,获得支付结果。(微信官方demo中需要我们了解的方法请仔细观看,可在demo中搜索jumpToBizPay这个方法)

如下:

#pragma mark 微信支付
-(void)weChatPay:(NSDictionary*)dic{
    
    NSLog(@"%@",dic);
    
    //创建支付签名对象
    payRequsestHandler *req = [payRequsestHandler alloc] ;
    //初始化支付签名对象--商户号
    [req init: APP_ID mch_id:dic[@"mchid"]];//[arr[0] toString]
    //设置密钥--
    [req setKey:dic[@"partnerid"]];//[arr[1] toString]
    
    ///需要客户端自己签名
    //获取到实际调起微信支付的参数后,在app端调起支付
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[req getPrepayWithPramas:dic]];
    
    if(dict ==nil){
        //错误提示
        NSString *debug = [req getDebugifo];
        return
        
        NSLog(@"%@\n\n",debug);
    }else{
        NSLog(@"%@\n\n",[req getDebugifo]);
        //[self alert:@"确认" msg:@"下单成功,点击OK后调起支付!"];
        
        NSMutableString *stamp  = [dict objectForKey:@"timestamp"];
        NSLog(@"%@",dict);
        //调起微信支付
        PayReq* req             = [[PayReq alloc] init];
        req.openID              = [NSString stringWithFormat:@"%@",[dict objectForKey:@"appid"]];
        req.partnerId           = [NSString stringWithFormat:@"%@",[dict objectForKey:@"partnerid"]];
        req.prepayId            = [NSString stringWithFormat:@"%@",[dict objectForKey:@"prepayid"]];
        req.nonceStr            = [NSString stringWithFormat:@"%@",[dict objectForKey:@"noncestr"]];
        req.timeStamp           = stamp.intValue;
        req.package             = [NSString stringWithFormat:@"%@",[dict objectForKey:@"package"]];
        req.sign                = [NSString stringWithFormat:@"%@",[dict objectForKey:@"sign"]];
        
        NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",req.openID,req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
        
        [WXApi sendReq:req];
    }
}


如果用以上的方法,为demo中的方法,但是微信与支付宝都因为安全问题,建议我们把签名和加密放在后台来操作,所以我们经常会由后台来签名加密,然后把需要的参数传递回来直接赋值。

如:

#pragma mark 微信支付
-(void)weChatPay:(NSDictionary*)dict{
    NSMutableString *stamp  = [dictobjectForKey:@"timestamp"];
    NSLog(@"%@",dict);
    //调起微信支付
    PayReq* req             = [[PayReqalloc] init];
    req.openID              = [NSString stringWithFormat:@"%@",[dict objectForKey:@"appid"]];
    req.partnerId           = [NSString stringWithFormat:@"%@",[dict objectForKey:@"partnerid"]];
    req.prepayId            = [NSString stringWithFormat:@"%@",[dict objectForKey:@"prepayid"]];
    req.nonceStr            = [NSString stringWithFormat:@"%@",[dict objectForKey:@"noncestr"]];
    req.timeStamp           = stamp.intValue;
    req.package             = [NSString stringWithFormat:@"%@",[dict objectForKey:@"package"]];
    req.sign                = [NSString stringWithFormat:@"%@",[dict objectForKey:@"sign"]];
    
    NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",req.openID,req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
    
    [WXApisendReq:req];
}

上面方法中的dic是由服务器传过来的数据,服务器已经签名成功后给的数据。

微信支付就是这些了,如果有疏漏的地方欢迎留言呦~

感谢你的到来,学以致用更感谢。



阅读更多
版权声明:本文为博主原创文章,转载时请声明:http://blog.csdn.net/siwen1990 转载 https://blog.csdn.net/siwen1990/article/details/49966383
个人分类: iOS-功能
想对作者说点什么? 我来说一句

微信支付 Java SDK

2017年08月31日 13KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭