微信支付回调处理

1. 在AppDelegate.h中

(1) 导入微信类 #import @"WXApi.h".
(2) 遵守微信代理方法

2. 在APPDelegate.m中

(1) 注册微信
#pragma mark 注册微信

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{
//注册 微信
/**
参数1 : 微信Appid
参数2 : 对项目的描述信息(用项目名称)
*/
[WXApi registerApp:@"微信Appid" withDescription:@"云宴"];
return YES;

}
(2) 跳转方法,并设置代理
#pragma mark 跳转处理

//被废弃的方法. 但是在低版本中会用到.建议写上

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ 

  return [WXApi handleOpenURL:url delegate:self];

}

//被废弃的方法. 但是在低版本中会用到.建议写上

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{

    return [WXApi handleOpenURL:url delegate:self];

}

//新的方法

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

{

    return [WXApi handleOpenURL:url delegate:self];

}
(3) 微信回调方法 (注意: 不要写成Req方法了)
#pragma mark 微信回调方法

- (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;

   //判断是否是微信支付回调 (注意是PayResp 而不是PayReq)
   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];
    }
}

3. 在ViewController.h (进行支付请求的类)

暂时没有任何操作

4. 在ViewController.m中(进行支付的请求的类)

(1) 导入微信库 #import @"WXApi.h"
(2) 监听APPDelegate.m中发送的通知

#pragma mark 监听通知

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

    //检测是否装了微信软件
    if ([WXApi isWXAppInstalled])
    {
    //监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"WXPay" object:nil];
    }

}
#pragma mark - 事件

- (void)getOrderPayResult:(NSNotification *)notification
{

    NSLog(@"userInfo: %@",notification.userInfo);

    if ([notification.object isEqualToString:@"success"])
    {

        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
    else{
        [self alert:@"提示" msg:@"支付失败"];
    }

}

//客户端提示信息

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

    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alter show];

}

(3) 移除通知

#pragma mark 移除通知
- (void)dealloc
{
    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

你可能感兴趣的:(微信支付回调处理)