easypay.js 6.56 KB
/**
 * 立即购买controller
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/9/23
 */
'use strict';

const _ = require('lodash');

const helper = global.yoho.helpers;
const crypto = global.yoho.crypto;

const easypayApi = require('./easypay-api');

const _handelViewPrice = list => {
    if (list) {
        _.forEach(list, val => {
            if (val.promotion === '商品金额') {
                val.promotion = '商品总价';
                val.promotion_amount = `+ ${val.promotion_amount}`;
            } else {
                val.promotion_amount = _.replace(val.promotion_amount, '+', '+ ');
                val.promotion_amount = _.replace(val.promotion_amount, '-', '- ');
            }
        });
    }
};

const _handelUseYohoCoin = (info) => {
    let resData = {};
    let limitCoin = _.get(info, 'yoho_coin_pay_rule.num_limit', 0);

    if (info) {
        Object.assign(resData, {
            yoho_coin: info.yoho_coin.toFixed(2),
            use_yoho_coin: info.use_yoho_coin,
            total_yoho_coin_num: info.total_yoho_coin_num,
            yoho_coin_pay_rule: info.yoho_coin_pay_rule,
            canUseCoinNum: _.round(info.yoho_coin * 100),
            usedCoinNum: _.round(info.use_yoho_coin * 100)
        });

        if (!resData.canUseCoinNum) {
            let coinErrorTip = '';

            if (info.total_yoho_coin_num > limitCoin) {
                coinErrorTip = '抱歉,您的订单实付款不满足有货币使用条件';
            } else {
                coinErrorTip = `抱歉,您的有货币不足,有货币满${limitCoin}个方可使用`;
            }

            resData.coinErrorTip = coinErrorTip;
        }
    }

    return resData;
};

const _handelPaymentInfo = (d) => {
    let resData = {};

    if (d.delivery_address) {
        // 地址信息加密
        d.delivery_address.id = crypto.encryption('', `${d.delivery_address.address_id}`);
        delete d.delivery_address.address_id;
        resData.deliveryAddress = d.delivery_address;
    }

    d.shopping_cart_data.hasCoin = _.round(d.yoho_coin * 100); // 有货币稀释

    let supportLine = [];

    _.forEach(['zhifubao', 'zaixianzhifu', 'weixinzhifu', 'wangyinzaixian',
        'caifutong', 'shengfutong', 'tonglianzhifu'], sl => {
        supportLine.push(`//static.yohobuy.com/images/pay/icon/${sl}.png`);
    }
    );
    resData.supportLine = supportLine;

    let supportBank = [];

    _.forEach(
        ['BOC', 'ICBC', 'CMB', 'CCB', 'ABC', 'SPDB', 'CIB', 'GDB', 'SDB', 'CMBC',
        'COMM', 'CITIC', 'HZCBB2C', 'CEB', 'SHBANK', 'NBBANK', 'SZPAB', 'BJRCB', 'FDB', 'PSBC'],
        sb => {
            supportBank.push(`//static.yohobuy.com/images/bankico/${sb}.gif`);
        }
    );
    resData.supportBank = supportBank;

    _.forEach(d.goods_list, g => {
        // link to goods
        g.linkToGoods = helper.getUrlBySkc(g.product_id, g.goods_id, g.cn_alphabet);
    });
    resData.goodsList = d.goods_list;

    _.remove(d.payment_way, function(n) {
        return n.is_support === 'N';
    });

    let dPw = _.find(d.payment_way, {default: 'Y'});
    let dDt = _.find(d.delivery_time, {default: 'Y'});

    resData.defaultPayDelivery = {
        paymentTypeName: dPw ? dPw.payment_type_name : '在线支付(推荐)',
        paymentType: dPw ? dPw.payment_type : 1,
        paymentTypeId: dPw ? dPw.payment_id : 15,
        deliveryTimeStr: dDt ? dDt.delivery_time_string : '送货时间不限',
        deliveryTimeId: dDt ? dDt.delivery_time_id : 2,
        contractMe: false
    };

    if (d.shopping_cart_data && d.shopping_cart_data.promotion_formula_list) {
        _handelViewPrice(d.shopping_cart_data.promotion_formula_list);
    }

    Object.assign(resData, {
        paymentWay: d.payment_way,
        deliveryTime: d.delivery_time,
        deliveryWay: d.delivery_way,
        shoppingCartData: Object.assign(d.shopping_cart_data, _handelUseYohoCoin(d)),
        invoices: d.invoices
    });

    return resData;
};

const _getLimitProductData = (params) => {
    let resList = [];

    if (params.limitcode && params.sku && params.skn) {
        resList.push({
            type: 'limitcode',
            buy_number: 1,
            sku: params.sku,
            skn: params.skn,
            limitproductcode: params.limitcode
        });
    } else if (params.bundle && params.sku) {
        resList = [];

        _.forEach(_.split(params.sku, ',', 10), val => {
            resList.push({
                type: 'bundle',
                sku: parseInt(val, 10),
                buy_number: 1
            });
        });
    }

    return resList.length ? JSON.stringify(resList) : false;
};

const getEasypayOrderData = (params, uid) => {
    let resData = {};
    let strInfo = _getLimitProductData(params);
    let bundle = params.bundle ? parseInt(params.bundle, 10) : false;

    return easypayApi.getEasyPaymentAsync(uid, strInfo, bundle).then(result => {
        let d = _.get(result, 'data', false);

        if (d) {
            Object.assign(resData, _handelPaymentInfo(d), {
                productSkuList: crypto.encryption('', strInfo)
            });
        }

        return resData;
    });
};


const getOrderComputeData = (uid, cartType, params) => {
    params.productSkuList = _getLimitProductData(params);

    return easypayApi.getEasypayComputeAsync(uid, cartType, params.paymentType, params.deliveryWay, params).then(result => { // eslint-disable-line
        if (result.code === 200) {
            if (_.has(result, 'data.last_order_amount')) {
                result.data.last_order_amount = (result.data.last_order_amount).toFixed(2);
            }

            if (_.has(result, 'data.promotion_formula_list')) {
                _handelViewPrice(result.data.promotion_formula_list);
            }

            result.data = Object.assign(result.data, _handelUseYohoCoin(result.data));
        }
        return result;
    });
};

// 订单提交
const easypayOrderSubmit = (uid, cartType, params, remoteIp) => {
    params.addressId = crypto.decrypt('', params.addressId);
    params.productSkuList = _getLimitProductData(params);

    return easypayApi.easypayOrderSubmitAsync(uid, cartType, params.addressId, params.deliveryTime,
        params.deliveryWay, params.paymentType, params.paymentId, params.printPrice, params, remoteIp).then(result => {
            if (result.code === 200) {
                let d = result.data;

                d.url = helper.urlFormat('/shopping/pay', {
                    ordercode: d.order_code
                });
            }
            return result;
        }
    );
};

module.exports = {
    getEasypayOrderData,
    getOrderComputeData,
    easypayOrderSubmit
};