payment-process.js 11.4 KB
'use strict';
const helpers = global.yoho.helpers;
const crypto = global.yoho.crypto;
const _ = require('lodash');


/**
*有货币使用前端方案显示及是否可单击判断
*/
function yohoCoinCompute(orderCompute) {

    let yohoCoinData = {
        totalYohoCoinNum: (orderCompute.total_yoho_coin_num || 0) * 1,
        yohoCoin: (orderCompute.yoho_coin || 0) * 1,
        useYohoCoin: (orderCompute.use_yoho_coin || 0) * 1,
        yohoCoinClick: 0,
        yohoCoinMsg: '',
        yoho_coin_pay_rule: orderCompute.yoho_coin_pay_rule || {}
    };

    if (!orderCompute) {
        return yohoCoinData;
    }

    if (yohoCoinData.yohoCoin > 0) {
        yohoCoinData.yohoCoin = yohoCoinData.yohoCoin.toFixed(2);
    }

    if (yohoCoinData.useYohoCoin > 0) {
        yohoCoinData.useYohoCoin = yohoCoinData.useYohoCoin.toFixed(2);
    }

    if (yohoCoinData.totalYohoCoinNum < 100) {
        yohoCoinData.yohoCoinMsg = `共${yohoCoinData.totalYohoCoinNum}有货币,满${
            _.get(orderCompute, 'yoho_coin_pay_rule.num_limit', '100')}可用`;
    } else if (yohoCoinData.useYohoCoin > 0 || yohoCoinData.yohoCoin > 0) {
        yohoCoinData.yohoCoinMsg = '可抵¥' + (yohoCoinData.useYohoCoin > 0 ?
            yohoCoinData.useYohoCoin : yohoCoinData.yohoCoin);
        yohoCoinData.yohoCoinClick = 1;
    } else {
        yohoCoinData.yohoCoinMsg = '不满足有货币使用条件';
    }

    return yohoCoinData;
}

/**
 * 转换 原始结算数据 ,适应模板
 * TODO: 其他结算,如果有 相似的 结构,可以移出去
 * @param data 结算页面数据
 * @param orderInfo
 *    orderInfo.paymentType 用户支付常用方式
 * @param cartType
 * @param skuList
 * @param orderComputeData [option]  app.shopping.compute 返回的data数据
 *
 * TODO:
 *     与秒杀结算 一起测试
 */
function tranformPayment(data, orderInfo, cartType, skuList, orderComputeData) {
    orderInfo = orderInfo || {};
    let result = {};
    let isSunfengSupport;

    // orderComputeData 覆盖 data中对应的值
    if (orderComputeData) {
        orderComputeData.delivery_way && _.set(data, 'delivery_way', orderComputeData.delivery_way);
        orderComputeData.promotion_formula_list &&
            _.set(data, 'shopping_cart_data.promotion_formula_list', orderComputeData.promotion_formula_list);
        orderComputeData.last_order_amount &&
            _.set(data, 'shopping_cart_data.last_order_amount', orderComputeData.last_order_amount);
    }

    // delivery_address 中 提取信息
    if (data.hasOwnProperty('delivery_address') && !_.isEmpty(data.delivery_address)) {
        let cookieAddress = orderInfo.address;
        let addressData = data.delivery_address;
        let isSupport = cookieAddress ? cookieAddress.is_support : addressData.is_support;

        result.name = cookieAddress ? cookieAddress.consignee : addressData.consignee;
        result.phoneNum = cookieAddress ? cookieAddress.mobile : addressData.mobile;
        result.addressId = cookieAddress ?
            parseInt(crypto.decrypt(null, cookieAddress.address_id), 10) : addressData.address_id;
        result.addressInfo = cookieAddress ?
            cookieAddress.address_info : [addressData.area, addressData.address].join(' ');

        result.addressId = crypto.encryption(null, result.addressId + '');
        isSunfengSupport = isSupport === 'Y';

    }

    // delivery_way 配送信息
    if (data.delivery_way) {
        let arr = [];
        let cookieWayId = orderInfo.deliveryId;
        let deliveryWay = data.delivery_way;
        let isDeliveryId = true;
        let defaultKey = 0;

        deliveryWay.forEach((way, index) => {
            if (way.delivery_way_name === '顺丰速运' && !isSunfengSupport) {
                return;
            }

            let obj = {};

            (way.default === 'Y') && (defaultKey = index);

            if (cookieWayId && (way.delivery_way_id === cookieWayId)) {
                obj.isSelected = true;
                isDeliveryId = false;
            }

            arr.push(
                Object.assign({
                    id: way.delivery_way_id,
                    name: way.delivery_way_name,
                    cost: way.delivery_way_cost,
                }, obj)
            );
        });

        if (isDeliveryId) {
            arr[defaultKey].isSelected = true;
        }

        result.dispatchMode = arr;
    }

    // delivery_time 配送时间
    if (data.delivery_time) {
        let cookieTimeId = orderInfo.deliveryTimeId;
        let defaultKey = 0;
        let times = data.delivery_time;

        times = times.map((time, index) => {
            (time.default === 'Y') && (defaultKey = index);

            return {
                isSelected: false,
                id: time.delivery_time_id,
                name: time.delivery_time_string
            };
        });

        times[defaultKey].isSelected = true;

        if (cookieTimeId) {
            let selectTime = times.find(time => time.id === cookieTimeId);

            if (selectTime) {
                times[defaultKey].isSelected = false;
                selectTime.isSelected = true;
            }
        }

        result.dispatchTime = times;
    }

    // goods_list 订单商品
    if (data.goods_list) {
        let goods = data.goods_list;
        let goodsPrice = 0;

        goods = goods.map(good => {
            var obj = {};

            obj.id = good.product_sku;
            obj.thumb = helpers.image(good.goods_images, 120, 160);
            obj.name = good.product_name;
            obj.color = good.color_name;
            obj.size = good.size_name;
            obj.count = good.buy_number;
            obj.price = good.sales_price;
            obj.isLimitSkn = good.is_limit_skn === 'Y';

            // obj.isVipPrice = good.sales_price !== good.last_vip_price && good.discount_tag === 'V';
            // obj.isStudebt = good.sales_price !== good.last_vip_price && good.discount_tag === 'S';

            if (good.goods_type === 'gift') {
                obj.gift = true;
                obj.price = good.is_advance === 'Y' ? good.sale_price : good.last_price;
            } else if (good.goods_type === 'price_gift') { // eslint-disable-line
                obj.advanceBuy = true;
                obj.price = good.last_price;
            }

            // Total Price
            goodsPrice += obj.count * obj.price;

            return obj;
        });

        result.goods = goods;
        result.goodsPrice = goodsPrice.toFixed(2);
    }

    // 支付方式
    if (data.payment_way) {
        let payway = data.payment_way;

        payway = payway.filter(obj => obj.is_support === 'Y')
            .map(way => {
                let obj = {};

                obj.id = way.payment_id;
                obj.paymentType = way.payment_type;
                obj.name = way.payment_type_name;
                obj.isSupport = way.is_support === 'Y';

                return obj;
            });

        payway.some(way => {
            let bool = way.paymentType === orderInfo.paymentType;

            if (bool) {
                way.recommend = true;
            }

            return bool;
        }) || (payway[0].recommend = true);

        result.paymentWay = payway;
    }

    // 是否是重新计算的
    let isOrderComputer = orderComputeData && !_.isEmpty(orderComputeData);

    // 有货币
    result.yohoCoinCompute = yohoCoinCompute(isOrderComputer ? orderComputeData : data);

    // 发票
    if (data.invoices) {
        let invTypes = data.invoices.invoiceContentList || [];

        invTypes = invTypes.map(type => {
            return {
                id: type.invoices_type_id,
                name: type.invoices_type_name
            };
        });

        result.invoice = invTypes;

        if (orderInfo.invoice) {
            result.needInvoice = orderInfo.invoice;
            result.invoiceText = orderInfo.invoiceText;
        }
    }

    // 留言
    orderInfo.msg && (result.msg = orderInfo.msg);
    result.isPrintPrice = true; // 是否不打印价格,默认不勾选,预留

    // 订单数据
    if (data.shopping_cart_data) {
        let cartData = data.shopping_cart_data;

        // 判断 是否为jit商品
        if (cartData.is_multi_package === 'Y') {
            result.isJit = true;

            let jitInfo = {};

            if (orderInfo) {
                jitInfo = {
                    deliveryId: orderInfo.deliveryId,
                    paymentType: orderInfo.paymentType,
                    couponCode: orderInfo.couponCode,
                    yohoCoin: orderInfo.yohoCoin
                };
            }

            let param = Object.assign({
                cartType: cartType,
                skuList: skuList
            }, jitInfo);

            result.jitDetailUrl = helpers.urlFormat('/cart/index/new/jitDetail', param);
            result.packageTitle = cartData.package_title;
        }

        result.cartPayData = cartData.promotion_formula_list;
        result.num = cartData.goods_count;
        result.goodsPrice = cartData.str_order_amount;
        result.price = orderComputeData && _.isNumber(orderComputeData.last_order_amount) ?
            orderComputeData.last_order_amount : cartData.last_order_amount;

        if (cartData.gain_yoho_coin > 0) {
            result.yohoCoinNum = cartData.gain_yoho_coin;
            result.returnYohoCoin = true;
        }

    }

    // Note: 优惠券的使用数据, 使用coupon函数,追加在result上
    // example:
    //  result = tranformPayment(....)
    //  result.couon = coupon(.....)

    return result;
}

/**
 *  优惠券数据
 *  @param count int 用户有效优惠券
 *  @param orderComputeData obj app.Shopping.compute返回的data数据
 *  @param orderInfo cookie中的数据
 *  @return obj
 */
function coupon(count, orderInfo, orderComputeData) {
    let coupons = {
        couponName: '',
        isCoupon: false,
        count: count
    };

    if (
        orderComputeData.coupon_amount ||
        (orderComputeData.coupon_amount === 0 && orderComputeData.shipping_cost === 0)
    ) {
        coupons.couponName = orderInfo.couponName;
    }

    // 选中已使用的优惠劵,则剩余多少张优惠劵,不提示
    if (!coupons.couponName || coupons.count) {
        coupons.isCoupon = true;
    }

    return coupons;
}

/**
 * JIT 拆单数据处理
 */
function transformJit(packageList) {
    let result = {
        packages: []
    };

    _.forEach(packageList, (pValue, pKey) => {
        result.packages[pKey] = {
            packageType: (pKey + 1) + ':' + pValue.title
        };

        _.forEach(pValue.goods_list, (gValue, gKey) => {
            result.packages[pKey].goods = [];
            result.packages[pKey].goods[gKey] = {
                thumb: gValue.goods_images
            };

            switch (gValue.goods_type) {
                case 'price_gift' :
                    // 加价购
                    result.packages[pKey].goods[gKey].isAdd = true;
                    break;
                case 'gift' :
                    // 赠品
                    result.packages[pKey].goods[gKey].isGift = true;
                    break;
                default:
                    break;
            }
        });

        if (pValue.shopping_cost !== '0.00') {
            result.packages[pKey].expressCost = pValue.shopping_cost;
        }
        if (pValue.shopping_cut_cost !== '0.00') {
            result.packages[pKey].discount = pValue.shopping_cut_cost;
        }
    });

    return result;
}

module.exports = {
    tranformPayment,
    yohoCoinCompute,
    coupon,
    transformJit
};