这几天一直没细搞,后台那边说在弄着我这边就没管,但是好几天了还没有成功,自己也翻阅了下官方文档和其他项目。发现大部分的签名都是后台搞得,所以我就记录下防止以后再出现类似情况。总结了以下几点。
1.首先确认后台预支付的订单是否和微信支付对象PayReq中的属性值是否一一对应
PayReq*request = [[PayReqalloc]init];
// 商家id,在注册的时候给的
request.partnerId= [objDictobjectForKey:@"mch_id"]?:@"";
// 预支付订单这个是后台跟微信服务器交互后,微信服务器传给你们服务器的,你们服务器再传给你
request.prepayId= [objDictobjectForKey:@"prepay_id"]?:@"";
// 根据财付通文档填写的数据和签名//这个比较特殊,是固定的,只能是即req.package = Sign=WXPay
request.package= [objDictobjectForKey:@"package"]?:@"";
// 随机编码,为了防止重复的,在后台生成
request.nonceStr= [objDictobjectForKey:@"nonce_str"]?:@"";
// 这个是时间戳,也是在后台生成的,为了验证支付的
inttimestam = [[objDictobjectForKey:@"timestamp"]intValue];
request.timeStamp= (uint32_t)timestam;
//后台的签名
request.sign= [objDictobjectForKey:@"sign"]?:@"";
//自己的签名
request.sign = [self createMD5SingForPayWithAppID:[objDict objectForKey:@"appid"]?:@"" partnerid:request.partnerId prepayid:request.prepayId package:request.package noncestr:request.nonceStr timestamp:request.timeStamp];
[WXApisendReq:requestcompletion:^(BOOLsuccess) {
FBLog(@"微信支付结果::::%@",success==YES?@"1":@"2");
}];
2.确认sign 的值,微信中的支付对象有一个sing 属性,该属性为签名。该属性的值比较特殊,需要后台做好二次签名后才可以直接使用,如果后台没有做二次签名需要我们自己做的话需要用一下方法对,sign 值进行处理
-(NSString*)createMD5SingForPayWithAppID:(NSString*)appid_keypartnerid:(NSString*)partnerid_keyprepayid:(NSString*)prepayid_keypackage:(NSString*)package_keynoncestr:(NSString*)noncestr_keytimestamp:(UInt32)timestamp_key{
NSMutableDictionary *signParams = [NSMutableDictionary dictionary];
[signParamssetObject:appid_keyforKey:@"appid"];//微信appid 例如wxfb132134e5342
[signParamssetObject:noncestr_keyforKey:@"noncestr"];//随机字符串
[signParamssetObject:package_keyforKey:@"package"];//扩展字段 参数为 Sign=WXPay
[signParamssetObject:partnerid_keyforKey:@"partnerid"];//商户账号
[signParamssetObject:prepayid_keyforKey:@"prepayid"];//此处为统一下单接口返回的预支付订单号
[signParamssetObject:[NSStringstringWithFormat:@"%u",timestamp_key]forKey:@"timestamp"];//时间戳
NSMutableString*contentString =[NSMutableStringstring];
NSArray*keys = [signParamsallKeys];
//按字母顺序排序
NSArray*sortedArray = [keyssortedArrayUsingComparator:^NSComparisonResult(idobj1,idobj2) {
return [obj1 compare:obj2 options:NSNumericSearch];
}];
//拼接字符串
for(NSString*categoryIdinsortedArray) {
if(![[signParamsobjectForKey:categoryId]isEqualToString:@""]
&& ![[signParamsobjectForKey:categoryId]isEqualToString:@"sign"]
&& ![[signParamsobjectForKey:categoryId]isEqualToString:@"key"]){
[contentStringappendFormat:@"%@=%@&", categoryId, [signParamsobjectForKey:categoryId]];
}
}
//添加商户密钥key字段 API 密钥
[contentStringappendFormat:@"key=%@", @"自己项目的商户秘钥"];
NSString*result = [selfmd5String:contentString];//md5加密
returnresult;
}
3.MD5加密
- (NSString*)md5String:(NSMutableString*)contentString{
if(self==nil|| [contentStringlength] ==0)returnnil;
unsigned char digest[CC_MD5_DIGEST_LENGTH], i;
CC_MD5([contentString UTF8String], (int)[contentString lengthOfBytesUsingEncoding:NSUTF8StringEncoding], digest);
NSMutableString *ms = [NSMutableString string];
for(i=0;i<CC_MD5_DIGEST_LENGTH;i++){
[msappendFormat:@"%02x", (int)(digest[i])];
}
return[mscopy];
}
网友评论