ios调用微信付款接口,返回签名错误
解决方案
40
微信签名
// 签名
– (NSString *)genSign:(NSDictionary *)signParams
{
// 排序
NSArray *keys = [signParams allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2 options:NSNumericSearch];
}];
// 签名
– (NSString *)genSign:(NSDictionary *)signParams
{
// 排序
NSArray *keys = [signParams allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2 options:NSNumericSearch];
}];
// 生成
NSMutableString *sign = [NSMutableString string];
for (NSString *key in sortedKeys) {
[sign appendString:key];
[sign appendString:@”=”];
[sign appendString:[signParams objectForKey:key]];
[sign appendString:@”&”];
}
NSString *signString = [[sign copy] substringWithRange:NSMakeRange(0, sign.length – 1)];
NSString *result = [CommonUtil sha1:signString];
NSLog(@”– Gen sign: %@”, result);
return result;
}