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

微信支付调用

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

    [superviewDidLoad];

//监听一个通知

     [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(getOrderPayResult:)name:@"wx_pay"object:nil];


}


#pragma mark -支付结果回调

-(void)getOrderPayResult:(NSNotification *)notification

{

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

    {

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

       PaySuccessViewController*paySuccessVC =[[PaySuccessViewControlleralloc]init];

        paySuccessVC.isPaySuccess = YES;

        paySuccessVC.tidNUM = _tidNum;

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

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

        [view_alert show];


    }

    else

    {

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

       OrderDetailViewController*orderDetailVC = [[OrderDetailViewControlleralloc]init];

        orderDetailVC.tid = _tidNum;

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

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

        [view_alert show];


    }

}



在AppDelegate里面遵守 WXApiDelegate

@interfaceAppDelegate :UIResponder<UIApplicationDelegate,WXApiDelegate>

#import"WXApi.h"

#import"ApiXml.h"

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

//向微信注册

    [WXApiregisterApp:@"wx0fd7bd08928b91er"withDescription:@"支付"];


}


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

{

return[WXApihandleOpenURL:urldelegate:self];


}


- (BOOL)application:(UIApplication *)application

            openURL:(NSURL *)url

  sourceApplication:(NSString *)sourceApplication

         annotation:(id)annotation

{

return[WXApihandleOpenURL:urldelegate: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;

        }

    }

}







版权声明:本文为huanghaiyan_123原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。