CNBBASMPaymentImpl.m 6.97 KB
//
//  CNBBASMPaymentImpl.m
//  CNLiveNetAdd
//
//  Created by CNLive on 2021/10/16.
//  Copyright © 2021 cnlive. All rights reserved.
//

#import "CNBBASMPaymentImpl.h"
#import "CNBBASMPaymentHander.h"

/**
 * 小程序统计支付
 */
typedef NS_ENUM(NSInteger, BBASMPayStatisticsType) {
    BBASMPayStatisticsAliPay     = 0,     //支付宝直连
    BBASMPayStatisticsPolymer    = 1,     //聚合收银台
    BBASMPayStatisticsBDWallet   = 2,     //百度钱包
    BBASMPayStatisticsWechat     = 3,     //微信端能力调用
};
@implementation CNBBASMPaymentImpl
RegisterBBASMAdapter()

+ (void)calculateBainuoPolymerPaymentPriceWithParams:(nonnull NSDictionary *)params completion:(nonnull void (^)(NSDictionary * _Nonnull, NSError * _Nonnull))completionBlock {
    [[CNBBASMPaymentHander shareInstance] calculateBainuoPolymerPaymentPriceWithParams:params completion:completionBlock];
}


+ (void)requestBainuoPolymerPaymentChannelInfoWithParams:(nonnull NSDictionary *)params completion:(nonnull void (^)(NSDictionary * _Nonnull, NSError * _Nonnull))completionBlock {
    [[CNBBASMPaymentHander shareInstance] requestBainuoPolymerPaymentChannelInfoWithParams:params completion:completionBlock];
}


+ (void)requestBainuoPolymerPaymentWithParams:(nonnull NSDictionary *)params bannedChannels:(nullable NSArray *)bannedChannels detailResultCallback:(nonnull void (^)(BBASMPaymentStatus, NSString * _Nonnull, NSDictionary * _Nonnull))detailResultCallback {
    [[CNBBASMPaymentHander shareInstance] requestBainuoCashierPayment:[params copy]
                                                      bannedChannels:bannedChannels
                                                     successCallBack:^(NSString *payDesc, NSDictionary *data) {
        detailResultCallback(BBASMPaymentStatusSuccess, payDesc, data);
        [self cnPaymentUbcPayStatisticstype:BBASMPayStatisticsPolymer
                                    payInfo:payDesc
                                    succeed:YES];
    } loadingCallBack:^(NSString *payDesc, NSDictionary *data) {
        detailResultCallback(BBASMPaymentStatusLoading, payDesc, data);
    } errorCallBack:^(NSString *payDesc, NSDictionary *data) {
        detailResultCallback(BBASMPaymentStatusFailed, payDesc, data);
        [self cnPaymentUbcPayStatisticstype:BBASMPayStatisticsPolymer
                                    payInfo:payDesc
                                    succeed:NO];
    } cancelCallBack:^(NSString *payDesc, NSDictionary *data) {
        detailResultCallback(BBASMPaymentStatusCanceled, payDesc, data);
        [self cnPaymentUbcPayStatisticstype:BBASMPayStatisticsPolymer
                                    payInfo:payDesc
                                    succeed:NO];
    }];
}


+ (void)requestPaymentWithType:(BBASMPaymentType)type orderInfo:(nonnull NSString *)orderInfo completion:(nonnull void (^)(BBASMPaymentStatus, NSDictionary * _Nonnull))completion {
    
}


+ (void)requestWalletRiskInfo:(nonnull NSString *)parameter source:(nonnull NSString *)source controller:(nonnull UIViewController *)controller completion:(nonnull void (^)(NSInteger, NSString * _Nonnull))completion {
    
}


+ (void)showBainuoCashierCouponPannel:(nonnull NSDictionary *)params fromViewController:(nonnull UIViewController *)fromViewController callback:(nonnull void (^)(NSDictionary * _Nonnull))callback {
    [[CNBBASMPaymentHander shareInstance] showBainuoCashierCouponPannel:params fromViewController:fromViewController callback:callback];
}

+ (void)requestBainuoPolymerPaymentWithParams:(NSDictionary *)params
                               bannedChannels:(nullable NSArray *)bannedChannels
                                   completion:(void (^)(BBASMPaymentStatus status, NSString *payResult))completionBlock{
    [[CNBBASMPaymentHander shareInstance] requestBainuoCashierPayment:[params copy] bannedChannels:bannedChannels successCallBack:^(NSString * _Nonnull payDesc, NSDictionary * _Nonnull data) {
        completionBlock(BBASMPaymentStatusSuccess, payDesc);
        [self cnPaymentUbcPayStatisticstype:BBASMPayStatisticsPolymer
                                      payInfo:payDesc
                                      succeed:YES];
    } loadingCallBack:^(NSString * _Nonnull payDesc, NSDictionary * _Nonnull data) {
        completionBlock(BBASMPaymentStatusLoading, payDesc);
    } errorCallBack:^(NSString * _Nonnull payDesc, NSDictionary * _Nonnull data) {
        completionBlock(BBASMPaymentStatusFailed, payDesc);
        [self cnPaymentUbcPayStatisticstype:BBASMPayStatisticsPolymer
                                      payInfo:payDesc
                                      succeed:NO];
    } cancelCallBack:^(NSString * _Nonnull payDesc, NSDictionary * _Nonnull data) {
        completionBlock(BBASMPaymentStatusCanceled, payDesc);
        [self cnPaymentUbcPayStatisticstype:BBASMPayStatisticsPolymer
                                      payInfo:payDesc
                                      succeed:NO];
    }];
}


#pragma mark - Private Method

/**
 * @brief 统计小程序带来的订单量和支付成功的订单金额
 *
 * @param type 区分支付方式:支付宝-直连,聚合收银台,百度钱包
 * @param payInfo 支付结果信息字符串
 * @param succeed 支付成功失败
 */
+ (void)cnPaymentUbcPayStatisticstype:(BBASMPayStatisticsType)type payInfo:(NSString *)payInfo succeed:(BOOL)succeed {
    NSString *value = succeed? @"success" : @"fail";
    Class<BBASMServiceProtocol> serviceClass = [Pyramid createClassService:@protocol(BBASMServiceProtocol)];
    NSString *appKey = [serviceClass currentBBASMAppId];
    NSString *moneyInfo = payInfo;
    if (!CHECK_STRING_VALID(moneyInfo)) {
        moneyInfo = @"";
    }
    if (!CHECK_STRING_VALID(appKey)) {
        appKey = @"";
    }
    //  支付方式
    NSString *source = @"";
    switch (type) {
        case BBASMPayStatisticsBDWallet:{
            source = @"baiduqianbao";
            break;
        }
        case BBASMPayStatisticsAliPay:{
            source = @"alipay";
            break;
        }
        case BBASMPayStatisticsPolymer:{
            source = @"nuomi";
        }   break;
        case BBASMPayStatisticsWechat:{
            source = @"wechatH5Action";
            break;
        }
        default:
            source = @"baiduqianbao";
            break;
    }
    if (CHECK_STRING_VALID(appKey)) {
        Class<BBASMServiceProtocol> serviceClass = [Pyramid createClassService:@protocol(BBASMServiceProtocol)];
        NSString *appTypeString = [serviceClass appTypeString:appKey];
        if (!appTypeString || [appTypeString isEqualToString:@"unknown"]) {
            return;
        }
        NSDictionary *ubcDict = @{@"type": @"pay",
                                  @"from": appTypeString,
                                  @"value": value,
                                  @"source": source,
                                  @"ext": @{@"appid": appKey, @"money": moneyInfo,@"source" : BBASM_LOWERCASE_STR}};
        [serviceClass sendWalletUBCMessage:ubcDict
                                     appId:appKey];
    }
}


@end