iOS第三方APP接入微信支付流程

使用pod下载微信支付SDK

platform:ios, ‘7.0‘
use_frameworks!
target ‘你的工程名’ do
   pod 'WeChat_SDK', '~> 1.7.5.1'
end

AppDelegate.m中注册你申请的微信app_id

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //1 微信pay
    [WXApi registerApp:@"你的微信app_id" withDescription:@"你的项目名称"];
  }

重写AppDelegateopenURL方法

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options

{

    //设置微信WXApi的代理是当前控制器
    return [WXApi handleOpenURL:url delegate:self];

}

AppDelegate继承代理WXApiDelegate

@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>

AppDelegate.m中重写WXApiDelegateonResp方法

- (void)onResp:(BaseResp *)resp{
    NSString * strMsg = [NSString stringWithFormat:@"errorCode: %d",resp.errCode];
    NSLog(@"strMsg: %@",strMsg);
    
    NSString * errStr      = [NSString stringWithFormat:@"errStr: %@",resp.errStr];
    NSLog(@"errStr: %@",errStr);
    
    NSString * strTitle;
    
    //判断是微信消息的回调 --> 是支付回调回来的还是消息回调回来的.
    if ([resp isKindOfClass:[SendMessageToWXResp class]])
    {
        
        strTitle = [NSString stringWithFormat:@"发送媒体消息的结果"];
        
    }
    
    NSString * wxPayResult;
    
    //判断是否是微信支付回调
    if ([resp isKindOfClass:[PayResp class]])
    {
        
        //支付返回的结果, 实际支付结果需要去微信服务器端查询
        strTitle = [NSString stringWithFormat:@"支付结果"];
        switch (resp.errCode)
        {
            case WXSuccess:
            {
                strMsg = @"支付结果:";
                NSLog(@"支付成功: %d",resp.errCode);
                wxPayResult = @"success";
                break;
            }
            case WXErrCodeUserCancel:
            {
                strMsg = @"用户取消了支付";
                NSLog(@"用户取消支付: %d",resp.errCode);
                wxPayResult = @"cancel";
                break;
            }
            default:
            {
                strMsg = [NSString stringWithFormat:@"支付失败! code: %d  errorStr: %@",resp.errCode,resp.errStr];
                NSLog(@":支付失败: code: %d str: %@",resp.errCode,resp.errStr);
                wxPayResult = @"faile";
                break;
            }
        }
        
        //全局广播
        NSNotification * notification = [NSNotification notificationWithName:@"WXPay" object:wxPayResult];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
    }

}

在调起支付的控制器XXX.m中接受广播

- (void)viewDidLoad {
    [super viewDidLoad];
    //是否安装微信
    if ([WXApi isWXAppInstalled])
    {
        //监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"WXPay" object:nil];
    }

}

处理广播内容

- (void)getOrderPayResult:(NSNotification *)notification
{    
    if ([notification.object isEqualToString:@"success"])
    {
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        [self paySuccessHandler];
    }
    else{
        [self alert:@"提示" msg:@"支付失败"];
        [self payFailHandler];
    }
    
}

//客户端提示信息
- (void)alert:(NSString *)title msg:(NSString *)msg
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alter show];
    
}

请求服务器对微信支付签名后的数据

-(void)wechat{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    dict[@"nos"] = self.payNos; //订单号
    dict[@"token"] = [self token];
    NSString* urlString = @"http://api.xxxxx.com/order/pay_wx";
    [manager GET:urlString parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        WXDataModel *dataModel = [WXDataModel mj_objectWithKeyValues:responseObject[@"data"]];
        
        WXSelectedModel *selectModel = [WXSelectedModel mj_objectWithKeyValues:dataModel.selected_pay_data];
        WechatModel *weChatModel = [WechatModel mj_objectWithKeyValues:selectModel.native];
        
        //调起支付
        BOOL result = [self wechatPay:weChatModel];
        NSLog(@"result = %d",result?YES:NO);
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

}

调起支付,WechatModel是数据模型,封装了服务器sign签名后返回的数据

// 微信支付
-(BOOL)wechatPay:(WechatModel *)dict{
    PayReq *req             = [[PayReq alloc]init];
    req.partnerId           = dict.partnerid;
    req.prepayId            = dict.prepayid;
    req.nonceStr            = dict.noncestr;
    req.timeStamp           = dict.timestamp.intValue;
    req.package             = dict.package;
    req.sign                = dict.sign;
    return [WXApi sendReq:req];
}

销毁广播

/**
 销毁广播
 */
-(void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self]; 
}

推荐阅读更多精彩内容

  • 微信支付 商家系统和微信支付系统主要交互说明: 步骤1:用户在商品APP选择商品,提交订单,选择微信支付.步骤2:...
    Jack_Baiyx阅读 1,371评论 2 12
  • iOS支付 iOS支付分为两类,第三方支付和应用内支付(内购)。 第三方支付包括:支付宝支付、微信支付、银联支付、...
    请输入账号名阅读 3,883评论 3 19
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 127,464评论 18 546
  • 实现支付宝支付的准备工作: 1.向支付宝签约,成为支付宝的商户 签约完成后,支付宝会提供一些必要的数据给我们 商户...
    Anson杨春安阅读 6,493评论 0 3
  • 万事开头难,很多事没做前总觉得不可能成功、难度很大,没开始就已经被吓住了而一直没有付诸行动。然而一旦行动,会发现...
    若垂天云阅读 22评论 0 0