iOS--->微信支付小结
说起支付,除了支付宝支付之外,微信支付也是我们三方支付中最重要的方式之一,承接上面总结的支付宝,接下来把微信支付也总结了一下
***那么首先还是由公司去创建并申请使用微信支付所需的信息
1.接下来就是微信支付的集成步骤了,参考着开发文档来,非常简单的 下载SDK,项目中导入所需的文件WxPay文件夹中,注意其中的.a文件容易丢失
2.根据文档对其中支持的非arc进行设置
3.设置微信支付的URL types
4.接下来就是代码内部的事情了,做支付我们知道首先需要在appdelegate中设置相应的属性
-(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//微信支付配置 自己申请注册的
[WXApi registerApp:@"wx0e65e1b96577f407" withDescription:@"demo 2.0"];
return YES;
}
//微信支付界面 onResp函数回调界面
//处理微信通过URL,启动App时传递的数据
-(BOOL)application:(UIApplication )application handleOpenURL:(NSURL )url{
return [WXApi handleOpenURL:url delegate:self];
}
说到支付,那么支付结果的回调是必不可少的,还是在appdelegate页面
//微信支付返回 回调
-(void)onResp:(BaseResp*)resp{
NSLog(@"tets");
NSInteger num=0;
if ([resp isKindOfClass:[PayResp class]]){
PayResp*response=(PayResp*)resp;
switch(response.errCode){
case WXSuccess:
{
num=1;
//服务器端查询支付通知或查询API返回的结果再提示成功
NSLog(@"支付成功");
// [self weixinzhifuhuidiao:1];
break;
}
case WXErrCodeUserCancel:
{
num=2;
NSLog(@"cewsgu");
// [self weixinzhifuhuidiao:2];
}
break;
default:
num=3;
// [self weixinzhifuhuidiao:3];
NSLog(@"支付失败,retcode=%d",resp.errCode);
break;
}
}
}
最后就不说闲话了,直接在支付页面上代码参考官方Demo即可(貌似官方有两种方式,这里仅使用其中一种)
#pragma mark --------- 微信支付 demo ---------
-(void)setWeixinSuccessString:(NSString *)weixinSuccessString{
_weixinSuccessString=weixinSuccessString;
[SVProgressHUD showWithStatus:@"正在提交预支付信息"];
GRWDDDRequest * request=[[GRWDDDRequest alloc]init];
[request weixinyuzhifuWithString:weixinyuazhifURL parems:@{@"Income":@(_totalprice),@"OrderNo":_dataDict[@"orderNo"]} success:^(id data) {
NSDictionary * dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if ([dict[@"Result"] floatValue] == 1) {
NSString * message = [NSString stringWithFormat:@"订单号:%@\r总支付价格:%0.2f元",_dataDict[@"orderNo"],_totalprice];
UIAlertView * alertView=[[UIAlertView alloc]initWithTitle:@"微信支付信息" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
alertView.delegate=self;
alertView.tag=2015;
[alertView show];
_weixinDict=dict[@"Data"];
}else{
UIAlertView * alertView=[[UIAlertView alloc]initWithTitle:@"支付失败" message:dict[@"Data"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
}
[SVProgressHUD dismiss];
} fail:^(NSError *error) {
[SVProgressHUD dismiss];
}];
}
// 请求回调微信支付
-(void)sendzhifu{
_dict =[NSDictionary dictionary];
_dict = [self qianming:_weixinDict];
PayReq *request = [[PayReq alloc] init];
request.partnerId = _dict[@"partnerid"];
request.prepayId= _dict[@"prepayid"];
request.package = _dict[@"package"];
request.nonceStr= _dict[@"noncestr"];
request.timeStamp= (UInt32)[_dict[@"timestamp"] integerValue];
request.sign= _dict[@"sign"];
[WXApi sendReq:request];
}
-(NSDictionary )qianming:(NSDictionary )ttdict{
payRequsestHandler * handler = [[payRequsestHandler alloc]init];
[handler init:@"wx0e65e1b96577f407" mch_id:@"1265805801"];
[handler setKey:@"CLSH1984AWST1982weixinapihd2015t"];
NSString *prePayid;
prePayid = ttdict[@"prepayid"];
if ( prePayid != nil) {
//获取到prepayid后进行第二次签名
NSString *package, *time_stamp, *nonce_str;
//设置支付参数
time_t now;
time(&now);
time_stamp = [NSString stringWithFormat:@"%ld", now];
nonce_str = [WXUtil md5:time_stamp];
//重新按提交格式组包,微信客户端暂只支持package=Sign=WXPay格式,须考虑升级后支持携带package具体参数的情况
//package = [NSString stringWithFormat:@"Sign=%@",package];
package = @"Sign=WXPay";
//第二次签名参数列表
NSMutableDictionary *signParams = [NSMutableDictionary dictionary];
[signParams setObject: ttdict[@"appId"] forKey:@"appid"];
[signParams setObject: nonce_str forKey:@"noncestr"];
[signParams setObject: package forKey:@"package"];
[signParams setObject: ttdict[@"partnerid"] forKey:@"partnerid"];
[signParams setObject: time_stamp forKey:@"timestamp"];
[signParams setObject: ttdict[@"prepayid"] forKey:@"prepayid"];
//[signParams setObject: @"MD5" forKey:@"signType"];
//生成签名
NSString *sign = [handler createMd5Sign:signParams];
//添加签名
[signParams setObject: sign forKey:@"sign"];
NSLog(@"*****%@ ******",sign);
//返回参数列表
return signParams;
}
else{
return nil;
}
return nil;
}
//微信支付返回 回调
-(void)onResp:(BaseResp*)resp{
NSLog(@"判断是否执行此路径 **** %@ **** %@",_orderNoLabel.text,_dataDict[@"orderNo"]);
self.ceshixiancheng=@"";
if ([resp isKindOfClass:[PayResp class]]){
PayResp*response=(PayResp*)resp;
switch(response.errCode){
case WXSuccess:
{
//服务器端查询支付通知或查询API返回的结果再提示成功
NSLog(@"支付成功");
break;
}
case WXErrCodeUserCancel:
{
}
break;
default:
NSLog(@"支付失败,retcode=%d",resp.errCode);
break;
}
}
}
//微信支付结果 二次确认
-(void)weixinzhifuhuidiao:(NSInteger)state{
NSUserDefaults * userDefaults=[NSUserDefaults standardUserDefaults];
NSDictionary * userDict=[userDefaults valueForKey:@"login"];
[SVProgressHUD showWithStatus:@"正在核对支付信息,请稍等……"];
GRWDDDRequest * request=[[GRWDDDRequest alloc]init];
[request weixinzhifujieguoWithString:weixinzhifujieguoURL parems:@{@"orderno":_dataDict[@"orderNo"],@"payno":_weixinDict[@"payNo"],@"state":@(state),@"menberId":userDict[@"Data"][@"Id"]} success:^(id data) {
NSDictionary * dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if ([dict[@"Result"] integerValue] == 1) {
[SVProgressHUD showSuccessWithStatus:dict[@"Data"]];
[self.navigationController popViewControllerAnimated:YES];
}else{
[SVProgressHUD showErrorWithStatus:dict[@"Data"]];
}
NSLog(@"%@",dict);
} fail:^(NSError *error) {
[SVProgressHUD dismiss];
}];
}
到这里微信支付也就结束了,其他的就是解决问题了,集成的过程中出现错误不可避免,解决即可
1>启动后,出现闪退现象:检查签名是否正确(需要二次签名),在URL Schemes中检查是否正确添加了微信appid,检查你是否注册了appid
***最普遍的错误就是:sign字段,必须要自己生成的