微信支付调用和支付成功后的跳转

微信支付调用

#import "payRequsestHandler.h"

#import "WXApi.h"


NSMutableString *stamp  = [payInfoDict objectForKey:@"timeStamp"];


 //调起微信支付

        PayReq* request      = [[PayReq alloc] init];

        request.openID              = [payInfoDict objectForKey:@"appId"];

        request.partnerId           = [payInfoDict objectForKey:@"partnerId"];

        request.prepayId            = [payInfoDict objectForKey:@"prepayId"];

        request.nonceStr            = [payInfoDict objectForKey:@"nonceStr"];

        request.timeStamp           = stamp.intValue;

        request.package             = [payInfoDict objectForKey:@"packageValue"];

        request.sign                = [payInfoDict objectForKey:@"sign"];

        [WXApi sendReq:request];


- (void)viewDidLoad {

    [super viewDidLoad];

//监听一个通知

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"wx_pay" object:nil];


}


#pragma mark -支付结果回调

-(void)getOrderPayResult:(NSNotification *)notification

{

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

    {

        NSLog(@"success: 支付成功");

        PaySuccessViewController *paySuccessVC =[[PaySuccessViewController alloc]init];

        paySuccessVC.isPaySuccess = YES;

        paySuccessVC.tidNUM = _tidNum;

        [self.navigationController pushViewController:paySuccessVC animated:YES];

        UIAlertView *view_alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"订单支付成功!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];

        [view_alert show];


    }

    else

    {

        NSLog(@"fail: 支付失败");

        OrderDetailViewController *orderDetailVC = [[OrderDetailViewController alloc]init];

        orderDetailVC.tid = _tidNum;

        [self.navigationController pushViewController:orderDetailVC animated:YES];

        UIAlertView *view_alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"订单取消支付!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];

        [view_alert show];


    }

}



在AppDelegate里面遵守 WXApiDelegate

@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>

#import "WXApi.h"

#import "ApiXml.h"

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

//向微信注册

    [WXApi registerApp:@"wx0fd7bd08928b91er" withDescription:@"支付"];


}


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

{

return [WXApi handleOpenURL:url delegate:self];


}


- (BOOL)application:(UIApplication *)application

            openURL:(NSURL *)url

  sourceApplication:(NSString *)sourceApplication

         annotation:(id)annotation

{

return [WXApi handleOpenURL:url delegate:self];

}


-(void)onResp:(BaseResp*)resp{

    if ([resp isKindOfClass:[PayResp class]]){

        PayResp*response=(PayResp*)resp;

        NSLog(@"微信支付状态response.errCode%d",response.errCode);

        switch (resp.errCode) {

            case WXSuccess:

            {

                NSNotification *notification = [NSNotification notificationWithName:@"wx_pay" object:@"success"];

                [[NSNotificationCenter defaultCenter] postNotification:notification];

                

            }

                break;

            default:

            {

                NSNotification *notification = [NSNotification notificationWithName:@"wx_pay" object:@"fail"];

                [[NSNotificationCenter defaultCenter] postNotification:notification];

            }

                break;

        }

    }

}






  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值