CNLivePayCostModule.m 12 KB
//
//  CNLivePayCostModule.m
//  CNLivePayCostModule
//
//  Created by open on 2022/5/12.
//

#import "CNLivePayCostModule.h"
#import "CNLivePayCostContentView.h"
#import "CNLivePayCostModel.h"



@interface CNLivePayCostModule ()

@end

@implementation CNLivePayCostModule

/// 启动设置
+(void)finishLaunchingWithPayPluginSettings
{
    [UMSPPPayPluginSettings sharedInstance].umspEnviroment = UMSP_PROD;
    
    //注册微信支付
    [UMSPPPayUnifyPayPlugin registerApp:WeiChatAppId_Share universalLink:[NSString stringWithFormat:@"%@/",API_Host]];
}

/// 拉起网家家支付功能
/// @param paramDict 参数 (详细见.m中参数解释)
/// @param payCostType 支付来源路径
/// @param formVC 支付界面控制器
/// @param resultBlock 支付结果
+(void)showDefaultPayViwWithParamDict:(NSDictionary *)paramDict
                          PayCostType:(CNLivePayCostType)payCostType
                               FromVC:(UIViewController *)formVC
                          ResultBlock:(void(^)(CNLivePayCostTypeResult resultType))resultBlock
{
    [CNLivePayCostModel sharePayCostModel].protocol = [NSClassFromString(@"CNLivePayCostProtocolImpl") new];
    CNLivePayCostModel * payModel = [[CNLivePayCostModel alloc] init];
    //扫码支付
    if (payCostType == CNLivePayCostTypeSweep) {
        payModel.price = [paramDict floatValueForKey:@"price" default:0.00];
        payModel.expand = [@{@"fromType":@"1",@"toType":@"3",@"sourceType":@"order"} mj_JSONString];
        payModel.payType = payCostType;
        payModel.orderId = [paramDict stringValueForKey:@"orderId" default:@""];
        payModel.toId = [paramDict stringValueForKey:@"darenId" default:@""];
        payModel.shopName = [paramDict stringValueForKey:@"shopName" default:@""];
        payModel.face = [paramDict stringValueForKey:@"face" default:@""];
        payModel.remark = [paramDict stringValueForKey:@"remark" default:@""];
        if ([[paramDict allKeys] containsObject:@"paymentType"] && [paramDict dictionaryForKey:@"paymentType"] != nil) {
            payModel.isHiddenAlipay = ![[paramDict dictionaryForKey:@"paymentType"] boolForKey:@"ali"];
            payModel.isHiddenWechat = ![[paramDict dictionaryForKey:@"paymentType"] boolForKey:@"wechat"];
            payModel.isHiddenUnionpay = ![[paramDict dictionaryForKey:@"paymentType"] boolForKey:@"union"];
            payModel.isHiddenWjjpay = ![[paramDict dictionaryForKey:@"paymentType"] boolForKey:@"wjj"];
        }
    }
    //门票支付
    if (payCostType == CNLivePayCostTypeTicke) {
        payModel.orderId = [paramDict stringValueForKey:@"orderId" default:@""];
        payModel.expand = [@{@"fromType":@"1",@"toType":@"6",@"sourceType":@"jsfy_order"} mj_JSONString];
        payModel.toId = [paramDict stringValueForKey:@"toId" default:@""];
        payModel.price = [paramDict floatValueForKey:@"price" default:0.00];
        payModel.isHiddenWjjpay = YES;
        payModel.shopName = [paramDict stringValueForKey:@"shopName" default:@""];
        payModel.descStr = [paramDict stringValueForKey:@"descStr" default:@""];
        payModel.payType = payCostType;
    }
    //订单支付
    if (payCostType == CNLivePayCostTypeOrder) {
        payModel.orderId = [paramDict stringValueForKey:@"orderId" default:@""];
        payModel.userId = [paramDict stringValueForKey:@"userId" default:@""];
        payModel.expand = [@{@"fromType":@"1",@"toType":@"3",@"sourceType":@"order"} mj_JSONString];
        payModel.descStr = [paramDict stringValueForKey:@"descStr" default:@""];
        payModel.notifyUrl = [paramDict stringValueForKey:@"notifyUrl" default:@""];
        payModel.price = [paramDict floatValueForKey:@"price" default:0.00];
        if ([[paramDict stringValueForKey:@"orderType" default:@""] isEqualToString:@"3"]) payModel.isHiddenWjjpay =YES;
        payModel.payType = payCostType;
    }
    //群组支付
    if (payCostType == CNLivePayCostTypeGroup) {
        payModel.orderId = [paramDict stringValueForKey:@"groupId" default:@""];
        payModel.expand = [@{@"fromType":@"1",@"toType":@"2",@"sourceType":@"group_payment"} mj_JSONString];
        payModel.descStr = [paramDict stringValueForKey:@"descStr" default:@""];
        payModel.price = [paramDict floatValueForKey:@"price" default:0.00];
        payModel.fundsId = [paramDict stringValueForKey:@"fundsId" default:@""];
        payModel.payType = payCostType;
    }
    //上海电商支付
    if (payCostType == CNLivePayCostTypeSHShop) {
        payModel.orderId = [paramDict stringValueForKey:@"orderId" default:@""];
        payModel.toId = @"daren_10528143";//@"daren_10499273";
        payModel.userId = [paramDict stringValueForKey:@"userId" default:@""];
        payModel.notifyUrl = [paramDict stringValueForKey:@"notifyUrl" default:@""];
        payModel.descStr = [paramDict stringValueForKey:@"descStr" default:@""];
        payModel.price = [paramDict floatValueForKey:@"price" default:0.00];
        payModel.attach = @{@"plat":@"i"};;
        payModel.isHiddenWjjpay = NO;
        payModel.expand = [@{@"fromType":@"1",@"toType":@"3",@"sourceType":@"global_order",@"orderType":@"8"} mj_JSONString];
        payModel.payType = payCostType;
    }
    //社区支付
    if (payCostType == CNLivePayCostTypeCommunt) {
        payModel.orderId = [paramDict stringValueForKey:@"orderId" default:@""];
        payModel.isHiddenWjjpay = YES;
        payModel.price = [paramDict floatValueForKey:@"price" default:1];
        payModel.descStr = [paramDict stringValueForKey:@"body" default:@"网家家"];
        payModel.attach = @{@"plat":@"i"};
        payModel.notifyUrl = [paramDict stringValueForKey:@"notifyUrl" default:@""];
    }
    //网页支付
    if (payCostType == CNLivePayCostTypeHtml) {
        payModel.orderId = [paramDict stringValueForKey:@"orderId" default:@""];
        payModel.toId = [paramDict stringValueForKey:@"toId" default:@""];
        payModel.expand = [paramDict stringValueForKey:@"expand" default:@""];
        payModel.shopName = [paramDict stringValueForKey:@"toName" default:@""];
        payModel.isHiddenWjjpay = ![paramDict boolValueForKey:@"wjj" default:NO];
        payModel.isHiddenAlipay = ![paramDict boolValueForKey:@"ali" default:NO];
        payModel.isHiddenWechat = ![paramDict boolValueForKey:@"wechat" default:NO];
        payModel.isHiddenUnionpay = [paramDict boolValueForKey:@"union" default:NO];
        payModel.price = [paramDict floatValueForKey:@"price" default:0.00];
        payModel.descStr = [paramDict stringValueForKey:@"title" default:@"网家家"];
        
        NSDictionary * tempDict = [payModel.expand mj_JSONObject];
        NSString * productId = @"";
        if ([tempDict containsObjectForKey:@"prdId"]) productId = [tempDict stringValueForKey:@"prdId" default:@""];
        NSDictionary * attach = @{
                                @"sid":CNUserShareModel.uid?CNUserShareModel.uid:@"",
                                @"plat":@"i",
                                @"prdId":productId?productId:@""
                                };
        payModel.attach = attach;
        payModel.notifyUrl = [paramDict stringValueForKey:@"notify_url" default:@""];
        payModel.payType = payCostType;
    }
    
    if ([[CNUserInfoManager manager].userInfoModel.unionUserStatus isEqualToString:@"3"]) payModel.isHiddenWjjpay = YES;
    
    CNLivePayCostContentView * payCostView = [[CNLivePayCostContentView alloc] initWithFrame:AppKeyWindow.bounds PayModel:payModel FromVC:formVC CompleterBlock:^(CNLivePayCostTypeResult resultType) {
        if (resultBlock) resultBlock(resultType);
    }];
    [AppKeyWindow addSubview:payCostView];
    [payCostView show];
    
    if (payCostType == CNLivePayCostTypeCommunt) {
        //社区支付结果回调
        [[NSNotificationCenter defaultCenter] addObserver:payCostView selector:@selector(resultPayAction:) name:@"kIMAMSG_CommunityPaySuccessNotification" object:nil];
    }else if (payCostType == CNLivePayCostTypeSweep) {
        //二维码识别结果回调
        [[NSNotificationCenter defaultCenter] addObserver:payCostView selector:@selector(resultPayAction:) name:@"CNLiveContentSweepPaymentNotification" object:nil];
    }else{
        //支付成功通知回调
        [[NSNotificationCenter defaultCenter] addObserver:payCostView selector:@selector(resultPayAction:) name:@"kIMAMSG_ScanPaySuccessNotification" object:nil];
    }

}

/// 拉起苹果支付功能
/// @param paramDict 参数
/// @param formVC 支付界面控制器
/// @param resultBlock 支付结果
+(void)showApplePayViwWithParamDict:(NSDictionary *)paramDict
                             FromVC:(UIViewController *)formVC
                        ResultBlock:(void(^)(CNLivePayCostTypeResult resultType))resultBlock
{
    [CNLivePayCostModel sharePayCostModel].protocol = [NSClassFromString(@"CNLivePayCostProtocolImpl") new];
    
    NSString * notify_url = [paramDict stringValueForKey:@"notify_url" default:@""];
    NSString * total_fee = [paramDict stringValueForKey:@"total_fee" default:@""];
    NSString * body = [paramDict stringValueForKey:@"body" default:@""];
    NSString * productId = [paramDict stringValueForKey:@"prdId" default:@""];
    int moneyInt = [total_fee intValue];
    if (((int)moneyInt/100)<=0) {
        [QMUITips showWithText:@"付款金额不正确" inView:formVC.view hideAfterDelay:1.5f];
        return;
    }
    NSString *appleProductId = [NSString stringWithFormat:@"%@%d",CNLiveWjjApplePayAVProductIdBase,(int)moneyInt/100];
    
    CNLivePayArg *arg = [[CNLivePayArg alloc] init];
    arg.account = @"";
    arg.type = CNLivePayTypeInAppPurchase;
    arg.out_trade_no = [CNLivePayCostModel payCost_getRandomOrderId];
    arg.total_fee = moneyInt;
    arg.notify_url = notify_url;
    arg.user_id = CNUserShareModel.uid?CNUserShareModel.uid:@"";
    arg.attach = @{
                   @"sid":CNUserShareModel.uid?CNUserShareModel.uid:@"",
                   @"plat":@"i",
                   @"prdId":productId?productId:@""
                   };
    arg.body = [body isNotBlank]?body:[NSString stringWithFormat:@"您购买了付费服务"];
    [QMUITips showLoadingInView:formVC.view];
    [[CNLivePaySDK sharedCNLivePayManager] pay:arg isDebug:TestEnvironment productId:appleProductId completion:^(CNLivePayResult *cnlivePayResult) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [QMUITips hideAllTipsInView:formVC.view];
            if ([cnlivePayResult.channelBackCode isEqualToString:@"0"]) {
                if (resultBlock) resultBlock(CNLivePayCostTypeResultSucc);
            } else {
                [QMUITips showWithText:cnlivePayResult.backDescription inView:formVC.view hideAfterDelay:1.5f];
            }
        });
    }];
}

/// 拉起电商支付功能
/// @param paramDict 参数
/// @param payType 支付界面控制器
/// @param resultBlock 支付结果
+(void)showShopPayViwWithParamDict:(NSDictionary *)paramDict
                           PayType:(CNLivePayCostPayType)payType
                       ResultBlock:(void(^)(CNLivePayCostTypeResult resultType))resultBlock
{
    NSString * orderId = [paramDict stringValueForKey:@"orderId" default:@""];
    NSString * body = [paramDict stringValueForKey:@"body" default:@""];
    NSString * price = [paramDict stringValueForKey:@"price" default:@"0.00"];
    NSString * couponFee = [paramDict stringValueForKey:@"coupon_fee" default:@"0.00"];
    NSString * balanceFee = [paramDict stringValueForKey:@"balance_fee" default:@"0.00"];
    NSString * notifyUrl = [paramDict stringValueForKey:@"notifyUrl" default:@""];
    NSString * password = [paramDict stringValueForKey:@"password" default:@""];
    
    NSString * p = [NSString stringWithFormat:@"%d",(NSInteger)([price floatValue]*100)];
    
    [CNLivePayCostNetwork requestWithUMSPayForPrePayWithOrderId:orderId Body:body Price:p CouponFee:couponFee BalanceFee:balanceFee NotifyUrl:notifyUrl Type:payType ThreePayType:CNLivePayCostThreePayTypeShop FrmId:@"debug" PassWord:password CompleterBlcok:^(NSInteger statue) {
        if (resultBlock) {
            if (statue == 0) resultBlock(statue == 0?CNLivePayCostTypeResultSucc:CNLivePayCostTypeResultFail);
        }
    }];
}
@end