微信支付有很多坑的,官方提供的文档一点都不全面,而且demo也比较”羞涩”难懂。很多注意的细节都没有体现出来,走了很多弯路。于是,系统的开发流程如下。(完整的demo附在博客最后面)这里面包括微信支付开发和支付宝开发调用客户端的兼容处理。(两次签名放在移动端,博客中没提到没安装微信客户端的情况,也很简单[WXApi sendReq:request]调用这个方法的返回值为0即可判断。)
一、环境配置
1、首先是https协议访问,在plist设置白名单
四、demo
可供复制黏贴的代码(sign签名)
-(NSString )createMD5SingForPay:(NSString )appid_key partnerid:(NSString)partnerid_key prepayid:(NSString )prepayid_key package:(NSString )package_key noncestr:(NSString)noncestr_key timestamp:(UInt32)timestamp_key { NSMutableDictionary *signParams = [NSMutableDictionary dictionary]; [signParams setObject:appid_key forKey:@”appid”]; [signParams setObject:noncestr_key forKey:@”noncestr”]; [signParams setObject:package_key forKey:@”package”]; [signParams setObject:partnerid_key forKey:@”partnerid”]; [signParams setObject:prepayid_key forKey:@”prepayid”]; [signParams setObject:[NSString stringWithFormat:@”%u”,(unsigned int)timestamp_key] forKey:@”timestamp”];NSMutableString *contentString =[NSMutableString string]; NSArray *keys = [signParams allKeys]; //按字母顺序排序 NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {return [obj1 compare:obj2 options:NSNumericSearch]; }]; //拼接字符串 for (NSString *categoryId in sortedArray) { if ( ![[signParams objectForKey:categoryId] isEqualToString:@”“] && ![[signParams objectForKey:categoryId] isEqualToString:@”sign”] && ![[signParams objectForKey:categoryId] isEqualToString:@”key”] ) { [contentString appendFormat:@”%@=%@&”, categoryId, [signParams objectForKey:categoryId]];} } //添加商户密钥key字段 [contentString appendFormat:@”key=%@”, @”这里填写商户密钥”]; NSString *result = [self md5:contentString]; return result;}//创建发起支付时的sige签名-(NSString ) md5:(NSString )str { const char *cStr = [str UTF8String]; unsigned char result[16]= “0123456789abcdef”; CC_MD5(cStr, (CC_LONG)strlen(cStr), result); //这里的x是小写则产生的md5也是小写,x是大写则md5是大写,这里只能用大写,微信的大小写验证很逗 return [NSString stringWithFormat: @”%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X”, result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ]; }//MD5 加密
以上就是详解iOS微信支付开发案例代码的详细内容,更多请关注php中文网其它相关文章!