一、集成微信支付必要SDK和前期的设置工作(参考官方文档)
微信支付官网文档
不得不吐槽,这个官方文档难道是实习生写的?发出来不review一下?
二、调起支付(关键性代码)其实这些参数也可以后台计算后返回给我们这样andriod和iOS端都不用做
PayReq *request = [[PayReq alloc] init];
request.partnerId =partnerId; //商户号
request.prepayId=prepayId;//订单id
request.package = @"Sign=WXPay";
NSArray *arr = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"];
// 获取随机数,通过随机0-25数字取数组中的字母,拼接成一个26位的随机字符串
NSMutableString *strnon = [[NSMutableString alloc]init];
strnon = [[NSMutableString alloc]init];
for (int i=0; i<26; i++) {
int i = arc4random() % 26 ;
NSString *strnon1 = [arr objectAtIndex:i];
[strnon appendString:strnon1];
}
request.nonceStr= strnon;
//获取系统当前的时间间隔
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSString *timeString = [NSString stringWithFormat:@"%0.f", a];
//10位整型
request.timeStamp= [timeString intValue];
// 拼接参数
NSString *str1 = [NSString stringWithFormat:@"appid=%@&noncestr=%@&package=%@&partnerid=%@&prepayid=%@×tamp=%@",appid ,strnon, @"Sign=WXPay",partnerId,prepayId,timeString];
// 拼接商户密钥key
NSString *str2 = [NSString stringWithFormat:@"%@&key=%@",str1,key];
NSString *sign1 = [self md5:str2];
// 小写变大写
NSString *sign2 = [sign1 uppercaseString];
// 赋值生成的参数MD5签名
request.sign =sign2;
//调用微信支付请求
[WXApi sendReq:request];
用到的MD5函数
//MD5 32位小写加密
- (NSString *)md5:(NSString *)str {
const char *cStr = [str UTF8String];
unsigned char result[16];
CC_MD5( cStr, (int)strlen(cStr), result );
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]];
}
三、微信支付回调(注册通知,在接收到支付状态后发送通知)
/**
- 支持9.0以上
*/
- (BOOL)application:(UIApplication *)app openURL:(NSURL )url options:(NSDictionary
, id> *)options
{
[WXApi handleOpenURL:url delegate:self];
return YES;
}
/**
- 只支持9.0以下
*/
- (BOOL)application:(UIApplication )application handleOpenURL:(NSURL )url
{
return [WXApi handleOpenURL:url delegate:self];
}
/
- 只支持9.0以下
*/
-
(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
[WXApi handleOpenURL:url delegate:self];
return YES;
}
/**
- 微信回调
/
-(void)onResp:(BaseResp)resp{
if ([resp isKindOfClass:[PayResp class]]){
PayResp *response=(PayResp *)resp;
switch(response.errCode){
case WXSuccess:
//服务器端查询支付通知或查询API返回的结果再提示成功
NSLog(@"支付成功");
[[NSNotificationCenter defaultCenter] postNotificationName:WxPaySucceedNoti object:nil];
break;
case WXErrCodeUserCancel:
NSLog(@"支付取消");
[[NSNotificationCenter defaultCenter] postNotificationName:WxPayCancelNoti object:nil];
break;
default:
NSLog(@"支付失败,retcode=%d",resp.errCode);
[[NSNotificationCenter defaultCenter] postNotificationName:WxPayFailedNoti object:nil];
break;
}
}
}
四、遇到的问题
- 出现白色按钮,没有跳到支付页面
-> 应该是前面签名参数不对, request.sign 与 微信服务器算出的不一致