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

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'),
    Alert = dialog.Alert,
    C_ID;

require('../simple-header');

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/newpay/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/newpay/go',
                type: 'POST',
                dataType: 'json',
                async: false,
                timeout: 6000,
                data: {
                    code: orderCode,
                    payType: payType
                },
                success: function(d) {
                    $this.data('use', false);

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

                    d = d.data;

                    if (d.reqType) {
                        $lightBox.show();
                        $formGo.attr('action', d.reqType);
                        $.each(d, function(key, val) {
                            if (key === 'reqType') {
                                return true;
                            }
                            $formGo.append('<input type="text" name="' + key + '" value="' + val + '" />');
                        });
                        $formGo.submit();
                        setTimeout(function() {
                            $formGo.attr('action', '');
                            $formGo.html('');
                        }, 10);
                        return true;
                    } else if (!d.href) {
                        return new Alert('订单信息获取失败!').show();
                    }

                    $lightBox.show();

                    $open = window.open();
                    $open.location = d.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);
});