美文网首页
微信支付

微信支付

作者: 古月思吉 | 来源:发表于2018-09-11 13:47 被阅读0次

    微信支付 VS 支付宝支付
    (1)没有安装微信app,是不能进行微信支付的
    (2)没有安装支付宝app,也能支付,支付宝SDK提供了支付的H5页

    一、项目配置:

    二、 代码:

    1. Targets/Info/URL Types 中添加URL Schemes:
    • 备注:这里的 URLShemes 就是你在做微信登录功能时,在微信开放平台上注册应用生成的 appid 。微信支付&微信登录,用的是同一个 URLShemes ,都是为了从微信 app 跳转回自己的 app 。


      微信URLSchemes

    2.在需要的地方导入微信支付的SDK头文件:

    #import <WXApi.h>
    
    1. AJGoodsCashierController.m 文件:
      (1)增加&移除 微信支付结果的观察者:
    //添加“微信支付结果”通知的观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getWeiXinpayOrderPayResult:) name:@"kWeiXinPayResultNotification" object:nil];
    
    -(void)dealloc {
        //移除观察者
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    

    (2)微信支付相关:

    #pragma mark - 微信支付相关
    /**
     微信支付
    
     @param model 后台返回的微信支付需要的参数model
     */
    - (void)weiXinPayWithModel:(AJWeiXinPayModel *)model {
        //注册
        if ([WXApi registerApp:model.appid withDescription:@"手报"]) {
            //调起支付
            PayReq *request = [[PayReq alloc] init];
            request.partnerId = model.partnerid;
            request.prepayId= model.prepayid;
            request.package = model.package;
            request.nonceStr= model.noncestr;
            request.timeStamp= model.timestamp;
            request.sign= model.sign;
            
            [WXApi sendReq:request];
        }
    }
    /**接收到通知的回调 - “微信支付结果”*/
    -(void)getWeiXinpayOrderPayResult:(NSNotification *)notification {
        if (notification.userInfo != nil) {
            NSDictionary * resultDic = notification.userInfo;
            NSString * resultStatus = [resultDic valueForKey:@"isSuccess"];
            if ([resultStatus isEqualToString:@"1"]) {//支付成功
                [self setPayResultUIWithIsSuccess:true];
            } else {//支付失败
                [self setPayResultUIWithIsSuccess:false];
            }
        }
    }
    
    1. AJWeiXinPayModel.h 文件:
    @property (nonatomic ,copy)NSString * appid;//微信开放平台中注册的id
    @property (nonatomic ,copy)NSString * noncestr;
    @property (nonatomic ,copy)NSString * package;
    @property (nonatomic ,copy)NSString * partnerid;
    @property (nonatomic ,copy)NSString * prepayid;
    @property (nonatomic ,copy)NSString * sign;
    @property (nonatomic, assign)UInt32 timestamp;
    
    1. AppDelegate.m文件:
    • 注:AppDelegate 需要遵守协议 WXApiDelegate
    #pragma mark - 微信支付的接入
    // NOTE: 9.0之前使用的API接口
    - (BOOL)application:(UIApplication *)application
                openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication
             annotation:(id)annotation
    {
        //微信支付
        if ([[url host] isEqualToString:@"pay"]) {
            [WXApi handleOpenURL:url delegate:self];
        }
        
        return YES;
    }
    // NOTE: 9.0及以后使用新API接口
    - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
    {
        //微信支付
        if ([[url host] isEqualToString:@"pay"]) {
            [WXApi handleOpenURL:url delegate:self];
        }
        
        return YES;
    }
    /**微信支付结果回调*/
    - (void)onResp:(BaseResp *)resp{
        if ([resp isKindOfClass:[PayResp class]]){
            PayResp * response = (PayResp *)resp;
            switch(response.errCode){
                case WXSuccess:
                    //服务器端查询支付通知或查询API返回的结果再提示成功
                    NSLog(@"支付成功");
                    [self postWeiXinPayResultNotificationWithIsSuccess:true];
                    break;
                default:
                    NSLog(@"支付失败,retcode=%d",resp.errCode);
                    [self postWeiXinPayResultNotificationWithIsSuccess:false];
                    break;
            }
        }
    }
    /**发送微信支付结果的通知*/
    -(void)postWeiXinPayResultNotificationWithIsSuccess:(BOOL)isSuccess {
        NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
        if (isSuccess) {
            [dict setValue:@"1" forKey:@"isSuccess"];//成功
        } else {
            [dict setValue:@"0" forKey:@"isSuccess"];//失败
        }
        [NSNotificationCenter.defaultCenter postNotificationName:@"kWeiXinPayResultNotification" object:nil userInfo:dict];
    }
    

    相关文章

      网友评论

          本文标题:微信支付

          本文链接:https://www.haomeiwen.com/subject/mequgftx.html