iOS 微信支付

本文介绍了在iOS应用中集成微信支付的流程,包括在微信开放平台注册应用、设置AppDelegate、处理回调以及与服务器交互生成签名和发送支付请求。支付结果通过回调函数onResp进行处理,最终以服务器返回的结果为准确认支付状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在很多应用都用了微信支付,具体的步骤微信开放平台上应该都有,我这里只记录一下大体的流程和注意事项。

首先要去微信开放平台注册这个应用,取得应用的appid。

然后在appdelegate里面,    [WXApi registerApp:APP_ID withDescription:YOUR_DESCREPTION];

接着去target→ info里面,添加url types,用于调回本应用时的标识。


然后在appdelegate里面,用于返回本应用时,区别到底是从哪个应用返回的,

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    NSLog(@"url2 = %@",url);
    if ([url.host isEqualToString:@"pay"]) {//微信
            return  [WXApi handleOpenURL:url delegate:[[WebShopPayPageViewController alloc] init]];
    }else if([url.host isEqualToString:@"safepay"]){//支付宝
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result11 = %@",resultDic);
            
        }];
        return YES;
    }
    return YES;
    
}




这样准备工作就完成了。

根据微信的官方描述,签名在服务器端的生成是最安全的,我们需要的就是把后台需要的参数传过去,然后后台吧我们需要传给微信的参数,包括签名,prepayid等传回来。代码如下:

            PayReq *req = [[PayReq alloc] init];
            req.partnerId = [NSString stringWithFormat:@"%@",infoDict[@"data"][@"partnerID"]];//申请的partnerID
            req.openID = [NSString stringWithFormat:@"%@",infoDict[@"data"][@"appid"]];//申请的appid
            req.prepayId = [NSString stringWithFormat:@"%@",infoDict[@"data"][@"prepayid"]];//返回的prepayid
            req.package = @"Sign=WXPay";//固定值
            req.nonceStr = [NSString stringWithFormat:@"%@",infoDict[@"data"][@"noncestr"]];
            req.timeStamp = [infoDict[@"data"][@"timestamp"] intValue];
            //这个签名是重新生成的,只有这6个参数参与签名
            req.sign = [NSString stringWithFormat:@"%@",infoDict[@"data"][@"sign"]];//签名,后台生成并返回
            [WXApi sendReq:req];//发送请求,调用微信客户端

然后支付的结果,通过回调获得:

-(void)onResp:(BaseResp*)resp{
    NSLog(@"resp =   =  %@,%d",resp.errStr,resp.errCode);
    if ([resp isKindOfClass:[PayResp class]]){
        PayResp*response=(PayResp*)resp;
        NSLog(@"res.er = %@",response.errStr);
        switch(response.errCode){
            case WXSuccess:
                //服务器端查询支付通知或查询API返回的结果再提示成功
                {
                    //页面被销毁了,

                    [self needServiceResult];//下面有说明
                NSLog(@"订单ID = %@",SHAREDAPP.orderID);
              //  NSLog(@"支付成功");
                break;
            }
                
            default:
            {
                NSLog(@"支付失败,retcode=%d",resp.errCode);
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alert show];
            }
        
                break;
        }
    }
}

//微信反复抢到,支付结果要以我们后台的返回结果为主

- (void)needServiceResult
{
    NSLog(@"self.ta = %ld",(long)self.tagOfAmount);
    HttpRequest *request = [[HttpRequest alloc] init];
        [request httpRequestWith:@{@"resultType":@"json",@"token":SHAREDAPP.userToken,@"tradeno":SHAREDAPP.orderID} withURL:[NSString stringWithFormat:@"%@%@",MAIN_URL,BACK_PAY]];
    }
    request.httpRequestBlock = ^(NSDictionary *infoDict){
        NSLog(@"retMsg = %@",infoDict);
        if([infoDict[@"retCode"] integerValue] == 1){//如果服务器返回成功
                self.hadPayed = YES;
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                [self.navigationController popToRootViewControllerAnimated:YES];
                return ;
        }else{
            sleep(1);//每隔1秒掉一次
            if (self.tagOfAmount <=30) {//这个地方我用了递归,如果服务器30秒内没返回正确的结果,默认支付失败
                [self needServiceResult];

            }else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];//次情况为服务器没返回订单支付成功的信息,需要和客服联系。
                return ;
            }
            self.tagOfAmount ++;//计数,记录递归的次数,由于一秒调用一次,所以几次就是几秒
        }
        
    };

}


到这里,微信支付就算完成了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值