pay.js 11.3 KB
var t = getApp(), n = t.request;
var wxlog = require("wxlog.js");

module.exports = {
    wx_paying:0,
    set_fir(){
       this.wx_paying=0;
    },
    url: "/api/weshop/order/pay/createOrder",
    rechange: function(t, e, a) {
        var i = this;
        n.post(this.url, {
            data: {
                account: t
            },
            success: function(t) {
                i.weixinPay(t.data.result, e, a);
            },
            fail: function() {
                "function" == typeof a && a();
            },
            failStatus: function() {
                "function" == typeof a && a();
            }
        });
    },
    pay: function(t, e, a,sto,type) {

        if(this.wx_paying) return false;
        this.wx_paying=1;

        var i = this;
        var dd = {
          parent_sn: t,
          store_id: sto,
          type: 2,
        };
        if (type == 1) dd.type = 1;

        if(getApp().globalData.scene)
            dd.scene=getApp().globalData.scene;

        console.log(dd,'支付场景');

        getApp().globalData.is_tonglian_pay=0; //是通联支付的时候

        wx.showLoading();
        n.post(this.url, {
            data: dd,
            success: function(t) {
              console.log('======', t)
                wx.hideLoading();
                if(t.data.code==0){

                    var is_pos_pay=t.data.data.is_pos_pay;
                    //-- 富有的插件支付 --
                    if(t.data.data.is_fuioupay){
                        const plugin = requirePlugin('fuiou-pay')
                        getApp().globalData.no_clear=1;
                        getApp().globalData.fuiou_pay=1;


                        var paramsJson={
                            reqType:"11",//必填 01-商户模式,11-机构模式
                            reqData:t.data.data.reqData,//必填 富友加密参数
                        }
                        console.log(plugin, plugin.fuioupay)
                        plugin.fuioupay(parseInt(t.data.data.amt),paramsJson,function (res){
                          console.log(res);
                            getApp().globalData.fuiou_pay=0;
                            i.wx_paying=0;
                            getApp().showSuccess("支付成功!", e);

                        },function (error){
                          console.log("插件支付错误",error);
                            //支付失败的时候
                            i.wx_paying=0;
                            getApp().globalData.fuiou_pay=0;
                            var msg='取消支付';
                            if(error.code){
                                msg=error.code+":"+error.message;
                            }
                            getApp().confirmBox(msg);

                            setTimeout(function(){
                                wx.redirectTo({
                                    url: "/pages/user/order_list/order_list",
                                });
                            },1000)

                        })
                    }
                    //-- 通联的支付插件 --
                    else if(is_pos_pay){

                        var p_data=t.data.data;
                        delete p_data.is_pos_pay; //清理掉,不然签名不通过

                        var err_txt="";

                        if(!p_data.acct && !err_txt){
                            err_txt="未找到acct";
                        }
                        if(!p_data.appid && !err_txt){
                            err_txt="未找到appid";
                        }
                        if(!p_data.cusid && !err_txt){
                            err_txt="未找到cusid";
                        }
                        if(!p_data.notify_url && !err_txt){
                            err_txt="未找到notify_url";
                        }
                        if(!p_data.orgid && !err_txt){
                            err_txt="未找到orgid";
                        }
                        if(!p_data.paytype && !err_txt){
                            err_txt="未找到paytype";
                        }
                        if(!p_data.randomstr && !err_txt){
                            err_txt="未找到randomstr";
                        }
                        if(!p_data.remark && !err_txt){
                            err_txt="未找到remark";
                        }
                        if(!p_data.reqsn && !err_txt){
                            err_txt="未找到reqsn";
                        }
                        if(!p_data.sign && !err_txt){
                            err_txt="未找到sign";
                        }
                        if(!p_data.signtype && !err_txt){
                            err_txt="未找到signtype";
                        }
                        if(!p_data.trxamt && !err_txt){
                            err_txt="未找到trxamt";
                        }
                        if(!p_data.validtime && !err_txt){
                            err_txt="未找到validtime";
                        }
                        if(!p_data.version && !err_txt){
                            err_txt="未找到version";
                        }

                        if(err_txt){
                            i.wx_paying=0;
                            getApp().confirmBox(err_txt);
                            setTimeout(function(){
                                wx.redirectTo({
                                    url: "/pages/user/order_list/order_list",
                                });
                            },1000)
                            return false;
                        }

                        getApp().globalData.is_tonglian_pay=1; //是通联支付的时候

                        wx.openEmbeddedMiniProgram({
                            appId: 'wxef277996acc166c3',
                            extraData: p_data,
                            success:function(res){
                                
                               wxlog.info('通联支付跳转'+JSON.stringify(res));
                               wxlog.info(JSON.stringify(p_data));
                            },
                            fail(){
                                //-- 取消不支付 --
                                i.wx_paying=0;
                                "function" == typeof a && a();
                            }
                        })

                    }else{
                        i.weixinPay(t.data.data, e, a);
                    }

                }else{
                    i.wx_paying=0;
                    getApp().confirmBox(t.data.msg);
                    setTimeout(function(){
                        wx.redirectTo({
                            url: "/pages/user/order_list/order_list",
                        });
                    },1000)

                }
                return false;
            },
            fail: function() {
              console.log('======fail', )
              wx.hideLoading();
                i.wx_paying=0;
                "function" == typeof a && a();
            },
            failStatus: function() {
                wx.hideLoading();
                i.wx_paying=0;
                "function" == typeof a && a();
            }
        });
    },

    //-- 这里是专享礼包的调用 --
    pay_data:function (t,succ,fail){
        var th=this;
        var ob=t.data.data;
        if(!ob){
            this.set_fir();
            fail('支付参数错误');
            return false;
        }

        getApp().globalData.is_tonglian_pay=0; //是通联支付的时候

        //富友插件的优化
        if(ob.is_fuioupay){
            const plugin = requirePlugin('fuiou-pay')

            var paramsJson={
                reqType:"11",//必填 01-商户模式,11-机构模式
                reqData:ob.reqData,//必填 富友加密参数
            }
            getApp().globalData.no_clear=1;
            console.log(plugin, plugin.fuioupay)
            plugin.fuioupay(parseInt(ob.amt),paramsJson,function (res){
                console.log(res);
                th.wx_paying=0;
                "function" == typeof succ && succ();
            },function (error){
                th.wx_paying=0;
                console.log("插件支付错误",error);
                //支付失败的时候
                //getApp().confirmBox(error.code+error.message);
                var msg='取消支付';
                if(error && error.code){
                    msg=error.code+":"+error.message;
                }
                fail(msg)

            })
        }
        //-- 通联的支付插件 --
        else if(ob.is_pos_pay){

            getApp().globalData.is_tonglian_pay=1; //是通联支付的时候
            var p_data=t.data.data;
            delete p_data.is_pos_pay; //清理掉,不然签名不通过
            wx.openEmbeddedMiniProgram({
                appId: 'wxef277996acc166c3',
                extraData: p_data,
                fail(){
                    //-- 取消不支付 --
                    th.wx_paying=0;
                    "function" == typeof fail && fail();
                }
            })

        }
        else {
            this.weixinPay(ob, succ, fail);
        }
    },

    weixinPay: function(n, e, a) {
       var th=this;
	   if(!n) return false;

       getApp().globalData.no_clear=1;

        //有的话用最新的支付方式
       if(n.orderInfo) {
           wx.requestOrderPayment({
               timeStamp: String(n.timeStamp),
               nonceStr: n.nonceStr,
               package: n.packageValue,
               signType: n.signType,
               paySign: n.paySign,
               orderInfo: n.orderInfo, // 需要新增的 订单 信息
               success (n) {
                   th.wx_paying=0;
                   t.showSuccess("支付成功!", e);
               },
               fail (n) {
                   th.wx_paying=0;
                   if(!n.errMsg )  {
                       getApp().showWarning("支付失败");
                       "function" == typeof fail && fail();
                       return false;
                   }

                   console.log(n), "requestOrderPayment:fail" == n.errMsg ? t.showWarning("支付失败") : "requestOrderPayment:fail cancel" == n.errMsg ? t.showWarning("您已取消支付") :
                           t.confirmBox("支付失败:" + n.errMsg.substr("requestOrderPayment:fail ".length)),
                   "function" == typeof a && a();
               }
           })

       }else{
           wx.requestPayment({
               timeStamp: String(n.timeStamp),
               nonceStr: n.nonceStr,
               package: n.packageValue,
               signType: n.signType,
               paySign: n.paySign,
               success: function (n) {
                   th.wx_paying=0;
                   console.log(n), t.showSuccess("支付成功!", e);
               },
               fail: function (n) {
                   th.wx_paying=0;
                   console.log(n), "requestPayment:fail" == n.errMsg ? t.showWarning("支付失败") : "requestPayment:fail cancel" == n.errMsg ? t.showWarning("您已取消支付") :
                           t.confirmBox("支付失败:" + n.errMsg.substr("requestPayment:fail ".length)),
                   "function" == typeof a && a();
               }
           });
       }








    }
};