微信支付
在入口类里微信支付注册
[WXApi registerApp:WeiXin_AppId withDescription:@"demo2.0"];
在入口类方法里添加url
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if ([[url scheme] isEqualToString:WeiXin_AppId])
{
return [WXApi handleOpenURL:url delegate:self];
}
}
支付回调
-(void)onResp:(BaseResp *)resp
{
if([resp isKindOfClass:[PayResp class]]){
switch (resp.errCode) {
case WXSuccess:
[self.narvigationController pushViewController:[[YDPayMoneySuccessViewController alloc]init] animated:YES];
break;
default:
[SVProgressHUD showView:self.window image:nil status:@"支付失败"];
break;
}
}
}
发送微信支付请求
if ([WXApi isWXAppInstalled]) {
PayReq *weiXinReq = [[PayReq alloc]init];
weiXinReq.openID = WeiXin_AppId;
weiXinReq.partnerId = WeiXin_mchId;
weiXinReq.prepayId = self.balanceRespondEntity.backInfo.prepayId;
weiXinReq.package = @"Sign=WXPay";
weiXinReq.timeStamp = [self setTimeStamp];
weiXinReq.nonceStr = [WXUtil md5:[NSString stringWithFormat:@"%d",weiXinReq.timeStamp]];
//获取签名的方法,要注意
NSMutableDictionary *signParams = [NSMutableDictionary dictionary];
[signParams setObject: WeiXin_AppId forKey:@"appid"];
[signParams setObject: weiXinReq.nonceStr forKey:@"noncestr"];
[signParams setObject: weiXinReq.package forKey:@"package"];
[signParams setObject: weiXinReq.partnerId forKey:@"partnerid"];
[signParams setObject: weiXinReq.prepayId forKey:@"prepayid"];
[signParams setObject:[NSString stringWithFormat:@"%d",weiXinReq.timeStamp] forKey:@"timestamp"];
payRequsestHandler *payHandler = [[payRequsestHandler alloc]init];
//如果出现闪退加上这句话
[payHandler setKey:WeiXin_pkey];
NSString *sign = [payHandler createMd5Sign:signParams];
weiXinReq.sign = sign;
[WXApi sendReq:weiXinReq];
}
else
{
[SVProgressHUD showView:self.view image:nil status:@"未安装微信"];
}
网友评论