CNBBASMPaymentHander.m 4.94 KB
//
//  CNBBASMPaymentHander.m
//  BBASMDemo
//
//  Created by CNLive on 2021/10/29.
//

#import "CNBBASMPaymentHander.h"
#import <BainuoCashierSDK/BDNCashierSDK.h>
#import <BainuoCashierSDK/BDNCashierSDKAlipayService.h>
#import <BainuoCashierSDK/BDNCashierSDKWechatService.h>
#import <BainuoCashierSDK/BDNCashierSDKBaifubaoService.h>
#import <CNLiveEnvironment/CNLiveEnvironment.h>
#import "CNBainuoPayManagerDelegate.h"

static NSString * const CNPAY_WEICHAT_PAY_SCHEME = @"cnlive.baidu.com://";
@implementation CNBBASMPaymentHander
+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    static CNBBASMPaymentHander *hander = nil;
    dispatch_once(&onceToken, ^{
        hander = [[self alloc] init];
        [hander registerPayment];
    });
    return hander;
}

- (void )registerPayment{
    // 按需实现和注册支付能力(收银台SDK提供了微信、支付宝、百付宝的支付能力的默认实现)
    [BDNCashierSDK registerPaymentServices:@[[BDNCashierSDKAlipayService defaultService],[BDNCashierSDKWechatService defaultService],[BDNCashierSDKBaifubaoService defaultService]]];
    // 使用收银台提供的支付宝支付能力时,需要注册对应Scheme
    [[BDNCashierSDKAlipayService defaultService] registerScheme:@"cnlive"];
    // 使用收银台提供的微信支付能力时,需要注册对应的宿主App的universalLink
    [[BDNCashierSDKWechatService defaultService] registerUniversalLink:[NSString stringWithFormat:@"%@/",API_Host]];
    // 使用收银台提供的百付宝支付能力时,需要注册实现BDNCashierSDKBaifubaoDelegate协议的代理类
    [[BDNCashierSDKBaifubaoService defaultService] registerDelegate:[CNBainuoPayManagerDelegate shareInstance]];
}

-(void)requestBainuoCashierPayment:(NSDictionary *)params bannedChannels:(NSArray *)bannedChannels successCallBack:(BBASMCashierPaymentCallBack)successCallBack loadingCallBack:(BBASMCashierPaymentCallBack)loadingCallBack errorCallBack:(BBASMCashierPaymentCallBack)errorCallBack cancelCallBack:(BBASMCashierPaymentCallBack)cancelCallBack{
    [self doBainuoCashierPayment:params bannedChannels:bannedChannels callback:^(NSDictionary *info) {
        // 支付渠道响应结果
        NSNumber *resultCode = info[BDNCashierPaymentRespCodeKey];
        NSString *payDesc = info[BDNCashierPaymentRespMsgKey];
        NSDictionary *data = info[BDNCashierPaymentRespDataKey];
        switch (resultCode.integerValue) {
            case BDNCashierPaymentRespCodeSucceeded:
            {
                successCallBack(payDesc, data);
                break;
            }
            case BDNCashierPaymentRespCodeLoading:
            {
                loadingCallBack(payDesc, data);
                break;
            }
            case BDNCashierPaymentRespCodeCanceled:{
                cancelCallBack(payDesc, data);
                break;
            }
            case BDNCashierPaymentRespCodeFailed:{
                errorCallBack(payDesc, data);
                break;
            }
                
            default:
                break;
        }
    }];
}
- (void)doBainuoCashierPayment:(NSDictionary *)params
                bannedChannels:(NSArray *)bannedChannels
                      callback:(void (^)(NSDictionary *info))callback{
    BDNCashierDealReq *dealReq = [[BDNCashierDealReq alloc] init];
    dealReq.params = params;
    dealReq.bannedChannels = bannedChannels;
    dealReq.schemeForWechatH5Pay = CNPAY_WEICHAT_PAY_SCHEME;
    dealReq.completion = ^(NSDictionary *info) {
        callback ? callback(info) : nil;
    };
    // 调起收银台
    [BDNCashierSDK showCashierDesk:dealReq];
}

-(void)requestBainuoPolymerPaymentChannelInfoWithParams:(NSDictionary *)params completion:(void (^)(NSDictionary * _Nullable, NSError * _Nullable))completionBlock
{
    [BDNCashierSDK requestChannelInfoWithParams:params responseBlock:^(NSDictionary *response) {
        completionBlock ? completionBlock(response, nil) : nil;
    } failedBlock:^(NSError *error) {
        completionBlock ? completionBlock (nil, error) : nil;
    }];
}
- (void)calculateBainuoPolymerPaymentPriceWithParams:(NSDictionary *)params
                                          completion:(void (^)(NSDictionary *_Nullable, NSError *_Nullable))completionBlock{
    [BDNCashierSDK calculatePriceWithParams:params responseBlock:^(NSDictionary *response) {
        completionBlock ? completionBlock(response, nil) : nil;
    } failedBlock:^(NSError *error) {
        completionBlock ? completionBlock(nil, error) : nil;
    }];
}
- (void)showBainuoCashierCouponPannel:(NSDictionary *)params
                   fromViewController:(UIViewController *)fromViewController
                             callback:(void (^)(NSDictionary * _Nonnull))callback{
    BDNCashierCouponReq *req = [BDNCashierCouponReq new];
    req.params = params;
    req.fromViewController = fromViewController;
    req.completion = ^(NSDictionary *info) {
        callback ? callback(info) : nil;
    };
    [BDNCashierSDK showCashierCoupon:req];
}
@end