微信/支付宝 支付

头文件 包含
#import
#import "WXApi.h"
#import "WXApiObject.h"


//支付宝SDk  启动支付宝程序进行支付
-(void)zfbSDK:(NSString*)sing0 payorderId:(NSDictionary*)dic{

  //第二个参数订单id 成功之后用订单id 验证后台是否收到支付款  

    AlipaySDK *alipa = [AlipaySDK defaultService];
    [alipa payOrder:sing0 fromScheme:@"HappyCampus" callback:^(NSDictionary *resultDic) {
        NSString * status =   resultDic[@"resultStatus"];
        if ([status isEqualToString:@"9000"]) {
          
          // 网页支付成功
           
        }else{
           
            [SVProgressHUD showErrorWithStatus:@"支付未完成"];      
          }
       
    }];
}

//微信支付   启动微信程序进行支付

-(void)wxSdk:(NSDiction*)result{

                 if([WXApi isWXAppInstalled] ==false){
                       
                        [SVProgressHUD showErrorWithStatus:@"请先安装微信"];
                        return ;
                    }
                   //微信支付
                   [WXApi registerApp:wxAppId withDescription:@"demo 2.0"];
                
                    m_payID = result[@"payOrderID"]; //订单id 成功之后用订单id 验证后台是否收到支付款  
                    PayReq* req   = [[PayReq alloc] init];
                    req.openID     = result[@"wxAppId"];//[dict objectForKey:@"appid"];
                    req.partnerId  = result[@"wxPartnerid"];//[dict objectForKey:@"partnerid"];
                    req.prepayId    = result[@"wxPrepayid"];//[dict objectForKey:@"prepayid"];
                    req.nonceStr     = result[@"wxNonceStr"];//[dict objectForKey:@"noncestr"];
                    req.timeStamp     = [result[@"wxTimestamp"] intValue];//stamp.intValue;
                    req.package     = result[@"wxPackage"];//[dict objectForKey:@"package"];
                    req.sign      = result[@"wxSign"];//[dict objectForKey:@"sign"];
                    [WXApi sendReq:req];
}

//微信、支付宝 支付返回app后 AppDelete 回调  代码拷贝到 AppDelete类里面

//支付宝  微信回调
-(BOOL)application : (UIApplication*)application openURL : (NSURL *)url sourceApplication : (NSString *)sourceApplication annotation:(id)annotation{
   
    if ([[url scheme] isEqualToString:@"HappyCampus"] == YES) { // alipay
     
        [[AlipaySDK defaultService] processOderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            if ([resultDic[@"resultStatus"]  isEqualToString:@"9000"]) {
               
             在支付宝app中 支付成功
            }else{
           
              在支付宝app中 支付失败
            }
           
        }];

        return YES;
    }
    else if ([[url scheme] isEqualToString:wxAppId] ==YES){
   
        //微信支付回调
        return [WXApi handleOpenURL:url delegate:self];
          执行这句代码后接着执行 -(void)onResp:(BaseResp*)resp  该函数 返回微信支付结果
    }
   
   else{
 
   return;
  }
   
}

//微信支付结果回调
-(void)onResp:(BaseResp*)resp{

    if([resp isKindOfClass:[PayResp class]]) {
       
       
        switch (resp.errCode) {
               
            case WXSuccess: {
                NSLog(@"WXSuccess Pay");
                微信支付成功
                break;
            }
            case WXErrCodeUserCancel: {
                NSLog(@"WXErrCodeUserCancel");
                [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"%@",@"用户取消微信支付"] ];

                break;
            }
            default: {
                  [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"%@",@"微信支付失败"] ];
   
                NSLog(@"WXSuccess faled");
                break;
            }
        }}
}

你可能感兴趣的:(微信/支付宝 支付)