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

微信支付调用

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

        }

    }

}






©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页