iOS 微信客户端调用统一下单接口签名错误解决

由于后台没有时间,所以只能前端自己调用统一下单接口获取预支付订单然后进行支付。

签名检验工具地址https://api.mch.weixin.qq.com/pay/unifiedorder

随机字符串方法:

-(NSString *)ret32bitString{

char data[32];

for (int x=0;x<32;data[x++] = (char)('A' + (arc4random_uniform(26))));

return [[NSString alloc] initWithBytes:data length:32 encoding:NSUTF8StringEncoding];

}

md5加密算法:

- (NSString *) md5:(NSString *) input {

const char *cStr = [input UTF8String];

unsigned char digest[CC_MD5_DIGEST_LENGTH];

CC_MD5( cStr, (CC_LONG)strlen(cStr), digest ); // This is the md5 call 需要导入头文件#import

NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)

[output appendFormat:@"%02x", digest[i]];

return  [output uppercaseString]; //转化为大写

}

通过我的踩坑处了文档中的几条注意事项还有几个别的原因:


iOS 微信客户端调用统一下单接口签名错误解决_第1张图片

1.一定要确认key是商户平台的api密钥。

2.一定要注意总金额total_fee是分不是元我被坑惨了在这!!!(跟支付宝一块做的所以在金额这没在意,哎都是泪!)



总之一句话:文档一定要认真查看别放过任何细节,文档!文档!文档!重要的事情说三遍!


客户端调用微信支付问题

可能被忽略的问题就是需要把参数再次签名

需要签名的参数是这几个:

iOS 微信客户端调用统一下单接口签名错误解决_第2张图片

参数签名的代码:

PayReq *request = [[PayReq alloc] init];

request.partnerId = MCH_ID;

request.prepayId = dicModel.prepay_id;

request.package = @"Sign=WXPay";

request.nonceStr = dicModel.nonce_str;

NSTimeInterval a = [[NSDate date] timeIntervalSince1970];

NSString *timeString = [NSString stringWithFormat:@"%.0f", a];

request.timeStamp = [timeString intValue];

NSDictionary *dicsign = @{@"appid":WECHATKEY,

@"partnerid":request.partnerId,

@"prepayid":request.prepayId,

字符串升序排序代码:

#pragma mark 字符串排序生序排列

- (NSString *)dicZhuanString:(NSDictionary *)dicInfo {

// NOTE: 排序,得出最终请求字串

NSArray* sortedKeyArray = [[dicInfo allKeys] sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {

return [obj1 compare:obj2];

}];

NSMutableArray *tmpArray = [NSMutableArray new];

for (NSString* key in sortedKeyArray) {

NSString* orderItem = [self orderItemWithKeyPay:key andValue:[dicInfo objectForKey:key] encoded:NO];

if (orderItem.length > 0) {

[tmpArray addObject:orderItem];

}

}

return [tmpArray componentsJoinedByString:@"&"];

}

- (NSString*)orderItemWithKeyPay:(NSString*)key andValue:(NSString*)value encoded:(BOOL)bEncoded

{

if (key.length > 0 && value.length > 0) {

if (bEncoded) {

value = [self encodeValues:value];

}

return [NSString stringWithFormat:@"%@=%@", key, value];

}

return nil;

}

- (NSString*)encodeValues:(NSString*)value

{

NSString* encodedValue = value;

if (value.length > 0) {

encodedValue = (__bridge_transfer  NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)value, NULL, (__bridge CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 );

}

return encodedValue;

}

你可能感兴趣的:(iOS 微信客户端调用统一下单接口签名错误解决)