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

/**
 * 转换 原始结算数据 ,适应模板
 * TODO: 其他结算,如果有 相似的 结构,可以移出去
 * @param data 结算页面数据
 * @param orderInfo
 *    orderInfo.paymentType 用户支付常用方式
 */
function tranformPayment(data, orderInfo) {
    orderInfo = orderInfo || {};
    let result = {};

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

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

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

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

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

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

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

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

        result.dispatchMode = deliveryWay;
    }

    // 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.delivery_time_id === cookieTimeId);

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

        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.last_price;
            obj.isLimitSkn = good.is_limited === 'Y';

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

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

            return obj;
        });

        result.goods = goods;
        result.goodsPrice = goodsPrice;
    }

    // 支付方式
    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;
    }

    // 有货币
    result.yohoCoin = data.yoho_coin;
    result.useYohoCoin = false; //data.use_yoho_coin;

    // 发票
    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);

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

        // 拆单逻辑 not here
        // you can add to here , if you need
        // 这块数据处理, 要改写,请参照Cart.php @cartPay
        // -----------------------

        result.cartPayData = cartData.promotion_formula_list;
        result.num = cartData.goods_count;
        result.goodsPrice = cartData.str_order_amount;
        result.price = cartData.last_order_amount;

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

    }

    // 优惠券 not here
    // 你可以在此添加, 但不是所有的结算类型都需要购物券.
    // 也可以在函数的返回值上追加
    // -----------------------

    return result;
}

module.exports = {
    tranformPayment
};