qq_34589430
qq_34589430
采纳率0%
2017-08-05 10:31 阅读 2.7k

微信支付一直给我报支付验证签名失败,怎么解决????

#pragma mark - 微信支付
/*
colorbeans 用户购买彩豆数
*/

  • (NSString )appWxpay :(NSString *)colorbeanstype :(NSString *)colorbeans :(NSString *)callback{ AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; NSString *jsda =[[NSString alloc]init]; NSRange loran = [appdelegate.login rangeOfString:@"YES"]; NSArray array; if(loran.length > 0){ if(colorbeans.length > 0){ NSString *deviceIP =[self getIPAddress]; array= [NSArray arrayWithObjects:@"xyjJspname",@"Colorbean",@"xyjid",@"GMColorbeanWithWxpay",@"userid",appdelegate.userid,@"colorbeans",colorbeans,@"colorbeanstype",colorbeanstype,@"userip",deviceIP,nil]; Server *postSv = [[Server alloc]init]; NSArray *backDate = [postSv postServerY:array]; if([backDate[0] isEqualToString:@"true"]){ NSDictionary *ncD = backDate[1]; appdelegate.partnerid =[ncD objectForKey:@"partnerid"]; appdelegate.prepayid =[ncD objectForKey:@"prepayid"]; appdelegate.timestamp =[self getNowTimeTimestamp]; appdelegate.noncestr =[self nonceStr]; appdelegate.package =@"Sign=WXPay"; appdelegate.out_trade_no =[ncD objectForKey:@"out_trade_no"]; appdelegate.type =[ncD objectForKey:@"type"]; appdelegate.sign =[self signCreat]; PayReq *request = [[PayReq alloc] init]; request.openID = appdelegate.appid; request.partnerId = appdelegate.partnerid ; request.prepayId = appdelegate.prepayid; request.package = appdelegate.package; request.nonceStr = appdelegate.noncestr; request.timeStamp = [appdelegate.timestamp intValue]; request.sign = appdelegate.sign; [WXApi sendReq:request]; } return jsda; } #pragma mark - Sign签名的生成 -(NSString *)signCreat{ AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; NSMutableDictionary *stringB =[NSMutableDictionary dictionary]; [stringB setObject:appdelegate.appid forKey:@"appid"]; [stringB setObject:appdelegate.noncestr forKey:@"noncestr"]; [stringB setObject:appdelegate.package forKey:@"package"]; [stringB setObject:appdelegate.partnerid forKey:@"partnerid"]; [stringB setObject:appdelegate.prepayid forKey:@"prepayId"]; [stringB setObject:[NSString stringWithFormat:@"%@",appdelegate.timestamp] forKey:@"timestamp"]; NSArray *keys =[stringB allKeys]; NSArray *sortedkeys =[keys sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { return [obj1 compare:obj2 options:NSNumericSearch]; }]; NSMutableString *sign1 =[NSMutableString string]; for (NSString * key in sortedkeys) { [sign1 appendString:key]; [sign1 appendString:@"="]; [sign1 appendString:[stringB objectForKey:key]]; [sign1 appendString:@"&"]; } NSString * api= [NSString stringWithFormat:@"key=%@",appdelegate.api]; NSString * stringA =[NSString stringWithFormat:@"%@%@",sign1,api]; NSString *result = [StringMD sha1:stringA]; NSString * signmd=[StringMD stringToMD5:result]; NSString *sign = [[signmd uppercaseString]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; return sign; } #pragma mark - 获取随机字符串
  • (NSString )nonceStr{ NSString *string = [[NSString alloc]init]; for (int i = 0; i < 32; i++) { int number = arc4random() % 36; if (number < 10) { int figure = arc4random() % 10; NSString *tempString = [NSString stringWithFormat:@"%d", figure]; string = [string stringByAppendingString:tempString]; }else { int figure = (arc4random() % 26) + 97; char character = figure; NSString *tempString = [NSString stringWithFormat:@"%c", character]; string = [string stringByAppendingString:tempString]; } } // 把字符串全部转化为大写 NSString *str = [[string uppercaseString]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; return string ; } #pragma mark - 获取当前时间戳 (以毫秒为单位) -(NSString *)getNowTimeTimestamp{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss Z"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 //设置时区,这个对于时间的处理有时很重要 NSTimeZone timeZone = [NSTimeZone timeZoneWithName:@""]; [formatter setTimeZone:timeZone]; NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式 NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]]; return [timeSp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐