pay.page.js 4.14 KB
var $ = require('yoho-jquery'),
    dialog = require('../common/dialog'),
    Alert = dialog.Alert;

var payPage = {};
var $btnby = $('input.btnby'),
    $tabConbox = $('ul.tab-conbox'),
    $payWay = $('.pay-way'),
    $lightBox = $('.light-box'),
    orderCode = $btnby.data('order'),
    amount = $btnby.data('amount'),
    $formGo = $('.form-go'),
    C_ID;

require('../common');

payPage = {
    init: function() {
        C_ID = window._ChannelVary[window.cookie('_Channel')] || 1;
        this.setEvent();
        this.goPay();
    },
    setEvent: function() {
        // tab切 事件
        $('ul.tabs').on('click', function(e) {
            var $li = $(e.target).closest('li');

            if ($li.length <= 0) {
                return false;
            }

            $li.addClass('active').siblings().removeClass('active');
            $tabConbox.find('li').eq($li.index()).removeClass('hide').siblings().addClass('hide');
        });

        // 支付方式选中事件
        $tabConbox.on('click', '.mode', function() {
            $(this).closest('.tab-conbox').find('.mode').removeClass('active');
            $(this).addClass('active');
            $payWay.find('img').attr('src', $(this).find('img').attr('src'));
            $btnby.val('前往' + $(this).data('name'));
        });

        // 支付弹框隐藏
        $lightBox.find('.close,.change').click(function() {
            $lightBox.hide();
        });

        // 单击完成,发送支付确认
        $lightBox.find('.over').on('click', function() {
            $.post('//www.yohobuy.com/shopping/pay/sendPayConfirm', {
                code: orderCode,
                payment: $tabConbox.find('.mode.active').data('id')
            }, function() {
                document.location.href = '//www.yohobuy.com/home/orders';
            });
        });
    },
    goPay: function() {
        var payType, $open;

        $btnby.on('click', function() {
            var $this = $(this);

            // 防止频繁点击
            if ($this.data('use')) {
                return false;
            }

            $this.data('use', true);
            payType = $tabConbox.find('.mode.active').data('value');

            // 埋点
            window._yas && window._yas.sendCustomInfo && window._yas.sendCustomInfo({
                op: 'YB_SC_PAYMENT_TOPAY_CLICK',
                param: JSON.stringify({
                    C_ID: C_ID,
                    ORDER_CODE: orderCode,
                    ORDER_AMOUNT: amount,
                    PAY_STYLE: $tabConbox.find('.mode.active').data('id')
                })
            }, true);

            $.ajax({
                url: '//www.yohobuy.com/shopping/pay/go',
                type: 'POST',
                dataType: 'json',
                timeout: 7000,
                data: {
                    code: orderCode,
                    payType: payType
                },
                success: function(d) {
                    $this.data('use', false);

                    if (d.code !== 200) {
                        return new Alert(d.message).show();
                    }

                    if (!d.data.href) {
                        return new Alert('订单信息获取失败!').show();
                    }

                    $lightBox.show();

                    if (payType === '25_platform') {
                        $formGo.attr('action', d.data.href);
                        $formGo.submit();
                        setTimeout(function() {
                            $formGo.attr('action', '');
                        }, 10);
                        return true;
                    }

                    $open = window.open();
                    $open.location = d.data.href;
                },
                error: function() {
                    $this.data('use', false);
                }
            });
        });
    }
};

$(function() {
    payPage.init();

    // 默认埋点
    window._yas && window._yas.sendCustomInfo && window._yas.sendCustomInfo({
        op: 'YB_SC_PAYMENT',
        param: JSON.stringify({
            C_ID: C_ID,
            ORDER_CODE: orderCode,
            ORDER_AMOUNT: amount
        })
    }, true);
});