对微信支付和支付宝支付SDK的封装

作者: 洲洲哥 | 来源:发表于2018-09-03 20:32 被阅读98次

    本文首发地址
    微信支付有提供的pod库提供使用,但是只提供了唤起微信app进行支付。但是想要在唤起的时候获取支付结果,无法获取。当然你在分享的时候想获取分享结果也是一个未知数。so,今天来解决一下.

    notice:两平台处理方式基本一样

    1:支付平台的流程图

    微信支付流程图

    2:支付闭环图

    支付闭环图

    3:对于微信支付的封装

    3.1注册微信appid和遵循支付宝支付的网页支付结果的代理

    - (void)setPaySDKAppIDWithweChat:(NSString *)weChatID delegate:(id<GZIMPayWebAliPayDelegate>) delegate {
        [[GZPayConfigURL defaultMannage] configURL];
        if (weChatID) {
            [WXApi registerApp:weChatID];
            self.weCahtPayAppKey = weChatID;
        }
        self.AliPayHelper = [[MSAlipayHelper alloc] init];
        self.WebaliPayDelegate = delegate;
    }
    

    注意:这里的GZIMPayWebAliPayDelegate是针对支付宝网页支付的回调结果。

    3.2唤起微信支付

    /**
     唤起微信支付
     
     @param payRequest 唤起微信支付要传入的参数对象,
     **自行校验是否为空数据
     */
    - (void)WakeupWeChatPay:(GZIMPayPayRequest *)payRequest handler:(GZIMPayInfo *)userInfo weChatResult:(void(^)(GZIMPayErrorCode code,NSString * msg,GZIMPayInfo * userInfo))weChatResult {
        self.userInfo = userInfo;
        PayReq *request = [[PayReq alloc] init];
        request.openID = payRequest.openID;
        request.partnerId = payRequest.partnerId;
        request.prepayId= payRequest.prepayId;
        request.package = payRequest.package;
        request.nonceStr= payRequest.nonceStr;
        request.timeStamp = [payRequest.timeStamp intValue];
        request.sign= payRequest.sign;
        
        // 调用微信
        [WXApi sendReq:request];
        self.weChatResult = weChatResult;
    }
    

    3.3 获取微信支付结果回调

    /**
     微信支付回调
    
     @param url url
     @param weChatResult 处理结果回调
     @return bool
     */
    - (BOOL)weChatHandleOpenUrl:(NSURL *)url weChatResult:(void(^)(GZIMPayErrorCode code,NSString * msg,GZIMPayInfo * userInfo))weChatResult {
        // *****注意这里的block*****
        self.weChatDelegateResult = weChatResult;
        if ([url.scheme isEqualToString:self.weCahtPayAppKey] && [url.host isEqualToString:@"pay"]) {
          return  [WXApi handleOpenURL:url delegate:(id<WXApiDelegate>)self];
        }
        return NO;
    }
    

    3.4 唤起支付宝支付

    /**
     -唤起支付宝支付
     
     @param payOrderString 唤起支付宝要传入的参数,
     */
    - (void)WakeupAliPay:(NSString *)payOrderString handlerInfo:(GZIMPayInfo *)userInfo {
        if (payOrderString) {
            self.userInfo = userInfo;
            [self.AliPayHelper WakeupAliPayPay:payOrderString fromScheme:GZPAY_ALIPAY_SCHEME handler:^(NSDictionary *result) {
                
                GZIMPayErrorCode  payCode = GZIMPaySuccess;
                NSString * msg = @"用户支付成功";
                if([@"9000" isEqualToString:[result objectForKey:@"resultStatus"]]) {
                    payCode = GZIMPaySuccess;
                    msg = @"用户支付成功";
                } else if([@"6001" isEqualToString:[result objectForKey:@"resultStatus"]]) {
                    payCode = GZIMPayCancle;
                    msg = @"用户中途取消";
                } else {
                    payCode = GZIMPayFaile;
                    msg = @"用户支付失败";
                }
                if (self.WebaliPayDelegate && [self.WebaliPayDelegate respondsToSelector:@selector(GZIMPaySDK:payCode:payMsg:forUserInfo:)]) {
                    [self.WebaliPayDelegate GZIMPaySDK:self payCode:payCode payMsg:msg forUserInfo:self.userInfo];
                }
    
            }];
        }
    }
    

    3.5 唤起支付宝支付回调结果

    /**
     支付结果回调
     
     @param resultUrl handelrURL
     @param completionBlock 支付结果回调Block
     */
    - (void)aliPayHandlerResultURL:(NSURL *)resultUrl standbyCallback:(CompletionSDKBlock)completionBlock {
        if ([resultUrl.host isEqualToString:@"safepay"]) {
            [self.AliPayHelper aliPayHandlerResultURL:resultUrl standbyCallback:completionBlock];
        }
    }
    

    3.6 支付结果统一回调

    /**
     支付结果统一回调
    
     @param url url
     @param handler 回调
     */
    - (void)GZIMPayHandlerOpenUrl:(NSURL *)url handler:(void(^)(GZIMPayErrorCode code, NSString * msg,GZIMPayInfo* payInfo))handler {
        // 支付宝
        [self aliPayHandlerResultURL:url standbyCallback:^(NSDictionary *resultDic) {
            // 订单支付成功
            if([@"9000" isEqualToString:[resultDic objectForKey:@"resultStatus"]]) {
                if (handler) {
                    handler(GZIMPaySuccess,@"用户支付成功",self.userInfo);
                }
            } else if([@"6001" isEqualToString:[resultDic objectForKey:@"resultStatus"]]) {
                // 用户中途取消
                if (handler) {
                    handler(GZIMPayCancle,@"用户中途取消",self.userInfo);
                }
            } else {
                if (handler) {
                    handler(GZIMPayCancle,@"用户支付失败",self.userInfo);
                }
            }
        }];
        
        // 微信
        [self weChatHandleOpenUrl:url weChatResult:^(GZIMPayErrorCode code,NSString * msg, GZIMPayInfo *userInfo) {
            if (handler) {
                handler(code,msg,userInfo);
            } 
        }];
    }
    

    3.7 支付回调代理函数

    - (void)onResp:(BaseResp *)resp
    {
        if([resp isKindOfClass:[PayResp class]]){
            NSString *strMsg;
            switch (resp.errCode) {
                case WXSuccess:
                    strMsg = @"支付结果:成功!";
                    NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
                    if (self.weChatResult) {
                        self.weChatResult(GZIMPaySuccess,@"用户支付成功",self.userInfo);
                        self.weChatResult = nil;
                    }
                    if (self.weChatDelegateResult) {
                        self.weChatDelegateResult(GZIMPaySuccess,@"用户支付成功",self.userInfo);
                        self.weChatDelegateResult = nil;
                    }
                    break;
                case WXErrCodeUserCancel:
                    strMsg = @"支付结果:取消!";
                    NSLog(@"支付取消-PayCancle,retcode = %d", resp.errCode);
                    if (self.weChatResult) {
                        self.weChatResult(GZIMPayCancle,@"用户中途取消",self.userInfo);
                        self.weChatResult = nil;
                    }
                    if (self.weChatDelegateResult) {
                        self.weChatDelegateResult(GZIMPayCancle,@"用户中途取消",self.userInfo);
                        self.weChatDelegateResult = nil;
                    }
                    break;
                    
                default:
                    strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
                    NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
                    if (self.weChatResult) {
                        self.weChatResult(GZIMPayFaile,@"用户支付失败",nil);
                        self.weChatResult = nil;
                    }
                    if (self.weChatDelegateResult) {
                        self.weChatDelegateResult(GZIMPayFaile,@"用户支付失败",nil);
                        self.weChatDelegateResult = nil;
                    }
                    break;
            }
        }
    }
    

    注意这里的有两个block针对微信支付回调结果返回时.注意3.3步骤里的block。大概原因:由于两个block加载时机不同会被替换,由此引起的不会执行对应的block。

    有些地方说的不到位,还请各位看官指正。。。

    • 如有问题可添加QQ群:234812704
    • 欢迎各位一块学习,提高逼格!
    • 也可以添加洲洲哥的微信公众号

    可以来微信公众号(洲洲哥)后台给我们留言。 快来扫码关注我们吧!

    公众号二维码

    相关文章

      网友评论

        本文标题:对微信支付和支付宝支付SDK的封装

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