ios -- 微信 支付

本文详细介绍了如何在iOS应用中集成微信支付功能,包括下载并配置SDK、注册APPID、实现支付逻辑等步骤,并提供了关键代码示例。

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



1、去微信开发平台 下载 sdk :https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_5   

2、将SDK文件中包含的 libWeChatSDK.a,WXApi.h,WXApiObject.h 三个文件添加到你所建的工程中


3、导入对应的库文件:

SystemConfiguration.framework,libz.dylib,libsqlite3.0.dylib,libc++.dylib。

4、appdelegate里面:

@interface AppDelegate : UIResponder <WXApiDelegate>
{

enum WXScene _scene;

}

//微信支付
#import "WXApi.h"
#import "WXApiObject.h"


注册APPID

商户APP工程中引入微信lib库和头文件,调用API前,需要先向微信注册您的APPID,代码如下:

[WXApi registerApp:@"wxd930ea5d5a258f4f" withDescription:@"demo 2.0"];


  #pragma mark - //向微信注册
    
    [WXApi registerApp:APP_ID withDescription:@"demo 2.0"];


#pragma mark -     微信支付 -      ------

-(void) changeScene:(NSInteger )scene
{
    _scene = (enum WXScene)scene;
}

-(void) onResp:(BaseResp*)resp
{
    NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
    NSString *strTitle;
    
    if([resp isKindOfClass:[SendMessageToWXResp class]])
    {
        strTitle = [NSString stringWithFormat:@"发送媒体消息结果"];
    }
    if([resp isKindOfClass:[PayResp class]]){
        //支付返回结果,实际支付结果需要去微信服务器端查询
        strTitle = [NSString stringWithFormat:@"支付结果"];
        
        switch (resp.errCode) {
            case WXSuccess:
                strMsg = @"支付结果:成功!";
                NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
                break;
                
            default:
            strMsg = [NSString stringWithFormat:@"支付结果:失败!"];
           // strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
                NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
                break;
        }
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    
}


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return  [WXApi handleOpenURL:url delegate:self];
}


5、

调起支付接口:

 

  if (![WXApi isWXAppInstalled]) {
            [PTHudView showOnlyText:@"请先安装微信!" dismiss:YES];
            return;
        }
        
        
        NSMutableDictionary * params = [NSMutableDictionary dictionary];
        params[@"SIMILAR_ID"] = @"15c0992d0db44bdd9170e02a98aaf9c5";
        [HWHttpTool post:[NSString stringWithFormat:@"http://12xxxxxxxapp/pay/wxOrder"] params:params success:^(id json) {
       
            NSString * stamp = [json objectForKey:@"timestamp"];
            //调起微信支付
            PayReq* req             = [[PayReq alloc] init];
            req.openID              = [json objectForKey:@"appid"];
            req.partnerId           = [json objectForKey:@"partnerid"];
            req.prepayId            = [json objectForKey:@"prepayid"];
            req.nonceStr            = [json objectForKey:@"noncestr"];
            req.timeStamp           = stamp.intValue;
            req.package             = [json objectForKey:@"package"];
            req.sign                = [json objectForKey:@"sign"];
            
            [WXApi sendReq:req];
            
        } failure:^(NSError *error) {
            
        }];


        if(self.weixindic ==nil || ![[self.weixindicobjectForKey:@"status"]isEqualToString:@"success"]){

            //错误提示

            //NSString *debug = [req getDebugifo];

            NSString *debug =[self.weixindicobjectForKey:@"msg"];

           // [self alert:@"提示信息" msg:debug];

            

            NSLog(@" debug=== %@\n\n",debug);

        }else{

            //NSLog(@"%@\n\n",[req getDebugifo]);

            //[self alert:@"确认" msg:@"下单成功,点击OK后调起支付!"];

            

            NSMutableString *stamp  = [self.weixindicobjectForKey:@"timestamp"];

            

            //调起微信支付

            PayReq* req             = [[PayReqalloc]init];

            req.openID              = [self.weixindicobjectForKey:@"appid"];

            req.partnerId           = [self.weixindicobjectForKey:@"partnerid"];

            req.prepayId            = [self.weixindicobjectForKey:@"prepayid"];

            req.nonceStr            = [self.weixindicobjectForKey:@"noncestr"];

            req.timeStamp           = stamp.intValue;

            req.package             = [self.weixindicobjectForKey:@"package"];

            req.sign                = [self.weixindicobjectForKey:@"sign"];

            [WXApisendReq:req];

        }

//客户端提示信息

- (void)alert:(NSString *)title msg:(NSString *)msg

{

    UIAlertView *alter = [[UIAlertViewalloc]initWithTitle:titlemessage:msgdelegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil];

        [alter show];

}






可参考:http://blog.csdn.net/joonchen111/article/details/48287877

服务端参考:http://www.cnblogs.com/xyt-0412/p/4953748.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值