<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\api\controller;

use app\api\controller\Base;
use think\Db;
use think\Cache;
use think\Session;
use app\common\model\bbfxshop\Wxpay;
use app\common\model\bbfxshop\User;
use app\common\model\bbfxshop\Order;
use app\common\model\bbfxshop\CloudWarehouse;
use app\common\model\bbfxshop\Alipay;
use app\common\model\bbfxshop\Dougong;
use app\common\model\bbfxshop\Payeasenet;

class Pay extends Base
{ 
    protected $noNeedLogin = ['notify','sxNotify1','sxNotify','sxRefundNotify','alipayLink','alipayNotify','checkSubmit','dgNotify','dgRefundNotify','dgRuzhuNotify','dgQuxianNotify','dgFenzhangNotify','dgBalancePayNotify'];
    protected $noNeedRight = ['*'];
    protected $dk_paytype = [1,50,60,5,61,62];
    
    public function checkSubmit(){
        $order_no = $this->param("order_no/s");
        
        
        if(empty($order_no)){
            __error("订单号错误");
        }
        
        Cache::set($order_no,time());
        
        __success('操作成功');
    }
    
    public function check(){
        $order_no = $this->param("order_no/s");
        if(empty($order_no)){
            __error("订单号错误");
        }
        
        $pay_order = Db::name("bbfx_pay_order")->where(['order_trade_no'=>$order_no,'uniacid'=>UNIACID])->find();
        if(empty($pay_order)){
            __error("订单不存在");
        }
        
        if($pay_order['is_pay'] == 1){
            __success('操作成功',['is_pay'=>1]);
        }
        
        $u_time = Cache::get($order_no);
        if(time() - $u_time > 60){
            Cache::rm($order_no);
            __error("操作失败");
        }
        
        __success('操作成功',['is_pay'=>0]);
    }
    
    public function order(){
        global $_W;
        $params = $this->param();
        $platform_config = $_W['platform'];
        $order_type = intval($params['order_type']);
        $order_no = $params['order_no'];
        $user = $this->user;
        $base = $_W['base'];
        
        $is_wxpay = intval($_W['base']['is_wxpay']);
        $is_balancepay = intval($_W['base']['is_balancepay']);
        $is_deposit_release_pay = intval($_W['base']['deposit_release']['status']);
        $is_repurchase_currency_pay = 0;
        $is_alipay = intval($_W['base']['is_alipay']);
        $is_cashpay = intval($_W['base']['is_cashpay']);
        $is_scorepay = intval($_W['base']['is_scorepay']);
        $plugin_tongue_diagnosis = 0;

        if($order_type == 1){
            $title = "商品支付";
            $order = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("订单不存在");
            }
            if($order['is_pay'] == 1){
                __error("订单已支付");
            }
            if($order['status'] == -1){
                __error("订单已关闭");
            }
            if($order['status'] != 0){
                __error("订单已支付");
            }
            $goods_info = iunserializer($order['goods_info']);
            if(!empty($goods_info)){
                $title = $goods_info[0]['title'];
            }
            else{
                $goods_info = [['thumb'=>'']];
            }
            $order_goods = Db::name("bbfx_order_goods")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","left")->where(['a.order_id'=>$order['id']])->field("b.id,b.plugin")->select();
            foreach ($order_goods as $key => $val){
                $plugin = iunserializer($val['plugin']);
                $pay_set = $plugin['pay'];
                
                if($plugin['tongue_diagnosis']['status'] == 1){
                    $plugin_tongue_diagnosis = 1;
                }
                if(!empty($pay_set)){
                    if(is_null($pay_set['weixin_pay'])) $pay_set['weixin_pay'] = 1;
                    if(is_null($pay_set['balance_pay'])) $pay_set['balance_pay'] = 1;
                    if(is_null($pay_set['repurchase_currency_pay'])) $pay_set['repurchase_currency_pay'] = 1;
                    if(is_null($pay_set['zfb_pay'])) $pay_set['zfb_pay'] = 1;
                    if(is_null($pay_set['cash_pay'])) $pay_set['cash_pay'] = 1;
                    if(is_null($pay_set['score_pay'])) $pay_set['score_pay'] = 0;
                    if($pay_set['weixin_pay'] != 1){
                        $is_wxpay = 0;
                    }
                    if($pay_set['balance_pay'] != 1){
                        $is_balancepay = 0;
                    }
                    if($pay_set['deposit_release_pay'] != 1){
                        $is_deposit_release_pay = 0;
                    }
                    if($pay_set['zfb_pay'] != 1){
                        $is_alipay = 0;
                    }
                    if($pay_set['cash_pay'] != 1){
                        $is_cashpay = 0;
                    }
                    if($pay_set['score_pay'] != 1){
                        $is_scorepay = 0;
                    }
                    if($is_wxpay == 0 && $is_balancepay == 0 && $is_alipay == 0){
                        $is_repurchase_currency_pay = 0;
                        if($pay_set['repurchase_currency_pay'] == 1 && $_W['fenxiao']['is_repurchase_gold'] == 1){
                            $is_repurchase_currency_pay = 1;
                        }
                    }
                }
            }
            foreach ($goods_info as $key => &$val){
                $val['thumb'] = cdnurl($val['thumb']);
            }
            unset($val);
            
            $price = floatval($order['price']);
            $repurchase_currency = floatval($order['repurchase_currency']);
            if($repurchase_currency > 0){
                $price = round($price - $repurchase_currency,2);
            }
        }
        else if($order_type == 2){
            $title = "充值余额";
            $order = Db::name("bbfx_recharge_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("订单不存在");
            }
            if($order['is_pay'] == 1){
                __error("订单已支付");
            }
            if($order['type'] == 2){
                $title = "充值积分";
            }
            $price = floatval($order['money']);
            $is_balancepay = 0;
            $is_deposit_release_pay = 0;
            $is_cashpay = 0;
            $is_scorepay = 0;
            $is_repurchase_currency_pay = 0;
            if($order['mode'] == 1){
                $is_alipay = 0;
            }
            else{
                $is_wxpay = 0;
            }
        }
        else if($order_type == 3){
            $title = "购买会员卡";
            $order = Db::name("bbfx_member_card_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("订单不存在");
            }
            if($order['status'] == 1){
                __error("订单已支付");
            }
            
            $price = floatval($order['price']);
            $is_cashpay = 0;
            $is_scorepay = 0;
        }
        else if($order_type == 4){
            $title = $_W['base']['fenxiao']['texts']['predeposit']."充值";
            $order = Db::name("bbfx_deposit_release_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("订单不存在");
            }
            if($order['status'] == 1){
                __error("订单已支付");
            }
            
            $price = floatval($order['money']);
            $is_deposit_release_pay = 0;
            $is_cashpay = 0;
            $is_scorepay = 0;
            
            $deposit_paytype = $_W['base']['deposit_release']['deposit_paytype'];
            if(empty($deposit_paytype)){
                $is_wxpay = 0;
                $is_balancepay = 0;
                $is_alipay = 0;
            }
            else{
                if(!in_array(1,$deposit_paytype) && $is_wxpay == 1){
                    $is_wxpay = 0;
                }
                if(!in_array(2,$deposit_paytype) && $is_balancepay == 1){
                    $is_balancepay = 1;
                }
                if(!in_array(3,$deposit_paytype) && $is_alipay == 1){
                    $is_alipay = 1;
                }
            }
        }
        else if($order_type == 5){
            $order = Db::name("bbfx_sconnection_upgrade_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("订单不存在");
            }
            if($order['status'] == 1){
                __error("订单已支付");
            }
            $title = $order['title'];
            $price = floatval($order['price']);
            $is_cashpay = 0;
            $is_scorepay = 0;
        }
        else if($order_type == 6){
            $title = "积分商品支付";
            $order = Db::name("bbfx_scoreshop_order")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("订单不存在");
            }
            if($order['status'] == -1){
                __error("订单已关闭");
            }
            if($order['status'] != 0){
                __error("订单已支付");
            }
            $goods_info = iunserializer($order['goods_info']);
            if(!empty($goods_info)){
                $title = $goods_info[0]['title'];
            }
            
            foreach ($goods_info as $key => &$val){
                $val['thumb'] = cdnurl($val['thumb']);
            }
            unset($val);
            
            $price = floatval($order['price']);
            $is_cashpay = 0;
            $is_scorepay = 0;
        }
        else if($order_type == 7){
            $title = "押金支付";
            $order = Db::name("bbfx_yajin_record")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("记录不存在");
            }
            if($order['status'] != 0){
                __error("押金已支付");
            }
            
            $price = floatval($order['money']);
            $is_cashpay = 0;
            $is_scorepay = 0;
        }
        else if($order_type == 8){
            $title = "报名支付";
            $order = Db::name("bbfx_activity_baoming")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("记录不存在");
            }
            if($order['is_pay'] != 0){
                __error("报名已支付");
            }
            
            $price = floatval($order['baoming_fee']);
            $is_cashpay = 0;
            $is_scorepay = 0;
        }
        else if($order_type == 9){
            $title = "舌诊支付";
            $order = Db::name("bbfx_tongue_diagnosis_buy_record")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("记录不存在");
            }
            if($order['status'] != 0){
                __error("舌诊已支付");
            }
            
            $price = floatval($order['money']);
            $is_cashpay = 0;
            $is_scorepay = 0;
        }
        else{
            __error("订单类型错误");
        }
        
        if($params['platform'] != 'wxapp' && $params['platform'] != 'GZH'){
            // $is_wxpay = 0;
        }

        $thirdparty_pay = iunserializer($platform_config['thirdparty_pay']);
        
        $is_dougong_pay = 0;
        if($platform_config['dg_status'] == 1){
            $is_dougong_pay = 1;
        }

        if($params['platform'] == 'APP'){
            $is_dougong_pay = 0;
            // $is_wxpay = 0;
        }

        $is_wxpay_shouxin = 0;
        $is_alipay_shouxin = 0;
        $is_bankcardpay_shouxin = 0;
        if($thirdparty_pay['shouxin']['status'] == 1 && !empty($thirdparty_pay['shouxin']['pay_method'])){
            $pay_method = $thirdparty_pay['shouxin']['pay_method'];
            if(in_array(1,$pay_method) && ($params['platform'] == 'wxapp' || $params['platform'] == 'GZH')){
                $is_wxpay_shouxin = 1;
            }
            if(in_array(2,$pay_method)){
                $is_alipay_shouxin = 1;
            }
            if(in_array(3,$pay_method)){
                $is_bankcardpay_shouxin = 1;
            }
        }
        
        $pay_list = array(
            array(
                'paytype'=>1,
                'icon'=>cdnurl('/assets/images/wxzf.png'),
                'title'=>'微信支付',
                'status'=>$is_wxpay
            ),
            array(
                'paytype'=>50,
                'icon'=>cdnurl('/assets/images/wxzf.png'),
                'title'=>'汇付微信',
                'status'=>$is_dougong_pay
            ),
            array(
                'paytype'=>60,
                'icon'=>cdnurl('/assets/images/wxzf.png'),
                'title'=>'首信微信',
                'status'=>$is_wxpay_shouxin
            ),
            array(
                'paytype'=>2,
                'icon'=>cdnurl('/assets/images/yezf.png'),
                'title'=>$base['texts']['balance'].'支付',
                'status'=>$is_balancepay
            ),
            array(
                'paytype'=>3,
                'icon'=>cdnurl('/assets/images/yezf.png'),
                'title'=>$_W['base']['fenxiao']['texts']['predeposit'].'支付',
                'status'=>$is_deposit_release_pay
            ),
            array(
                'paytype'=>4,
                'icon'=>cdnurl('/assets/images/yezf.png'),
                'title'=>$_W['base']['fenxiao']['texts']['repurchase_currency'].'支付',
                'status'=>$is_repurchase_currency_pay
            ),
            array(
                'paytype'=>5,
                'icon'=>cdnurl('/assets/images/zfb.png'),
                'title'=>'支付宝支付',
                'status'=>$is_alipay
            ),
            array(
                'paytype'=>61,
                'icon'=>cdnurl('/assets/images/zfb.png'),
                'title'=>'首信支付宝',
                'status'=>$is_alipay_shouxin
            ),
            array(
                'paytype'=>62,
                'icon'=>cdnurl('/assets/images/xxzf.png'),
                'title'=>'首信银行卡',
                'status'=>$is_bankcardpay_shouxin
            ),
            array(
                'paytype'=>6,
                'icon'=>cdnurl('/assets/images/xxzf.png'),
                'title'=>'线下支付',
                'status'=>$is_cashpay
            ),
            array(
                'paytype'=>7,
                'icon'=>cdnurl('/assets/images/jfzf.png'),
                'title'=>$base['texts']['score'].'支付',
                'status'=>$is_scorepay
            )
        );
        
        $result = array(
            "title"=>$title,
            "price"=>$price,
            "order_no"=>$order_no,
            "order_id"=>$order['id'],
            "order_type"=>$order_type,
            "list"=>$pay_list,
            "dk_paytype"=>$this->dk_paytype
        );
        
        if($order_type == 1){
            $result['goods'] = $goods_info;
            $result['plugin_tongue_diagnosis'] = $plugin_tongue_diagnosis;
        }
        
        __success("操作成功",$result);
    }

    public function pay(){
        global $_W;
        $params = $this->param();
        $platform_config = $_W['platform'];
        $order_type = intval($params['order_type']);
        $paytype = max(1,intval($params['paytype']));
        $order_no = $params['order_no'];
        $pay_voucher = $params['pay_voucher'];
        $dk_type = intval($params['dk_type']);
        $user = $this->user;
        $pay_result = -1;
        $pay_order = [];
        $result = __json(1,"支付成功");
        try{
            Db::startTrans();
            if($order_type == 1){
                $title = "购买商品";
                $order = Db::name("bbfx_order")->lock(true)->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'order_no'=>$order_no])->find();
                if(!empty($order)){
                    $goods_info = iunserializer($order['goods_info']);
                    $title = $goods_info[0]['title'];
                    
                    foreach ($goods_info as $key => $item){
                        $res = CloudWarehouse::checkStock($user['id'],$item['total'],$item['id']);
                        if($res['code'] != 1){
                            throw new \Exception($res['message']);
                        }
                    }
                }
                if($order['is_pay'] == 1){
                    throw new \Exception("订单已支付");
                }
                if($order['status'] == -1){
                    throw new \Exception("订单已关闭");
                }
                if($order['status'] != 0){
                    throw new \Exception("订单已支付");
                }
                if($order['cash_pay_shenhe'] >= 0){
                    throw new \Exception("已线下支付");
                }

                if($order['order_source'] == 3){
                    $pintuan_order = Db::name("bbfx_pintuan_order")->where(['uniacid'=>UNIACID,'id'=>$order['pintuan_order_id']])->find();
                    if(empty($pintuan_order)){
                        throw new \Exception("拼团订单信息错误");
                    }

                    if($pintuan_order['status'] == 1 || $pintuan_order['status'] == 2){
                        throw new \Exception("该拼团订单已结束");
                    }

                    if($pintuan_order['status'] !== 0 && $pintuan_order['status'] !== -1){
                        throw new \Exception("您参与的拼团已结束");
                    }
                    if($pintuan_order['residue'] == 0){
                        throw new \Exception("您参与的拼团人数已满");
                    }
                }

                $order_goods = Db::name("bbfx_order_goods")->lock(true)->where(['uniacid'=>UNIACID,'order_id'=>$order['id']])->select();
                if(empty($order_goods)){
                    throw new \Exception("订单商品信息错误");
                }

                if($order['deduct_stock'] == 0){
                    foreach ($order_goods as $key => $item){
                        $goods = Db::name("bbfx_goods")->lock(true)->where(['uniacid'=>UNIACID,'id'=>$item['goods_id']])->find();
                        if(empty($goods)){
                            throw new \Exception("商品信息错误");
                        }
                        if($goods['stock'] < $item['total']){
                            throw new \Exception("商品库存不足");
                        }
                        $res = \app\common\model\bbfxshop\Goods::setStock($item['goods_id'],intval($item['sku_id']),-$item['total'],$user['id'],1,1,"用户下单",$item,false);
                    
                        if($res['code'] != 1){
                            throw new \Exception($res['message']."【{$item['title']}】");
                        }

                        if($order['order_source'] == 3){
                            $pintuan_goods = Db::name("bbfx_pintuan_goods")->where(['uniacid'=>UNIACID,'id'=>$item['pintuan_goods_id']])->find();
                            if(empty($pintuan_goods)){
                                throw new \Exception("拼团订单信息错误");
                            }
                            if($pintuan_goods['status'] != 1){
                                throw new \Exception("拼团活动已结束。");
                            }

                            if($pintuan_goods['is_sync_stock'] != 1){
                                if($pintuan_goods['stock'] < $item['total']){
                                    throw new \Exception("拼团商品库存不足。");
                                }
                                Db::name("bbfx_pintuan_goods")->where(['uniacid'=>UNIACID,'id'=>$item['pintuan_goods_id']])->setDec('stock',$item['total']);
                            }
                        }
                        else if($order['order_source'] == 10){
                            $bargain_goods = Db::name("bbfx_bargain_goods")->where(['uniacid'=>UNIACID,'id'=>$item['bargain_goods_id']])->find();
                            if(empty($bargain_goods)){
                                throw new \Exception("砍价订单信息错误");
                            }
                            if($bargain_goods['status'] != 1 || $bargain_goods['etime'] < time()){
                                throw new \Exception("砍价活动已结束。");
                            }
                            if($bargain_goods['stock'] < $item['total']){
                                throw new \Exception("砍价商品库存不足。");
                            }
                            Db::name("bbfx_bargain_goods")->where(['uniacid'=>UNIACID,'id'=>$item['bargain_goods_id']])->setDec('stock',$item['total']);
                        }
                    }
                }
                

                Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'id'=>$order['id']])->update(['deduct_stock'=>1]);

                $price = floatval($order['price']);
                $repurchase_currency = floatval($order['repurchase_currency']);
                if($repurchase_currency > 0){
                    $price = round($price - $repurchase_currency,2);
                }

                $dk_paytype = $this->dk_paytype;
                if(in_array($paytype,$dk_paytype) && $dk_type == 1){
                    // throw new \Exception("金额111");
                    if($price > $user['balance']){
                        $price = round($price - $user['balance'],2);
                        Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'id'=>$order['id']])->update(['balance'=>$user['balance'],'score'=>0]);
                    }
                    else{
                        $paytype = 2;
                        Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'id'=>$order['id']])->update(['balance'=>0,'score'=>0]);
                    }
                }
                else if(in_array($paytype,$dk_paytype) && $dk_type == 2){
                    // throw new \Exception("金额222");
                    if($price > $user['score']){
                        $price = round($price - $user['score'],2);
                        Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'id'=>$order['id']])->update(['score'=>$user['score'],'balance'=>0]);
                    }
                    else{
                        $paytype = 7;
                        Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'id'=>$order['id']])->update(['balance'=>0,'score'=>0]);
                    }
                }
                else{
                    Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'id'=>$order['id']])->update(['balance'=>0,'score'=>0]);
                }

                // throw new \Exception("金额".$price);
                
                if(empty($title)){
                    $title = "购买商品";
                }

                if($_W['base']['fenxiao']['fenzhang_status'] == 1 && $order['is_fenzhang'] == 0){
                    Db::name("bbfx_order")->where(['id'=>$order['id']])->update(['is_fenzhang'=>1]);
                    $order['is_fenzhang'] = 1;
                }
                else{
                    if($_W['base']['fenxiao']['fenzhang_status'] == 0 && $order['is_fenzhang'] == 1){
                        Db::name("bbfx_order")->where(['id'=>$order['id']])->update(['is_fenzhang'=>0]);
                        $order['is_fenzhang'] = 0;
                    }
                }
            }
            else if($order_type == 2){
                $title = "充值余额";
                $order = Db::name("bbfx_recharge_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
                if($order['type'] == 2){
                    $title = "充值积分";
                }
                if(empty($order)){
                    throw new \Exception("订单不存在");
                }
                if($order['is_pay'] == 1){
                    throw new \Exception("订单已支付");
                }
                if($order['type'] == 2){
                    $title = "充值积分";
                }
                $price = floatval($order['money']);
            }
            else if($order_type == 3){
                $title = "购买会员卡";
                $order = Db::name("bbfx_member_card_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
                if(empty($order)){
                    throw new \Exception("订单不存在");
                }
                if($order['status'] == 1){
                    throw new \Exception("订单已支付");
                }

                if($_W['base']['fenxiao']['fenzhang_status'] == 1 && $order['is_fenzhang'] == 0){
                    Db::name("bbfx_member_card_order")->where(['id'=>$order['id']])->update(['is_fenzhang'=>1]);
                    $order['is_fenzhang'] = 1;
                }
                else{
                    if($_W['base']['fenxiao']['fenzhang_status'] == 0 && $order['is_fenzhang'] == 1){
                        Db::name("bbfx_member_card_order")->where(['id'=>$order['id']])->update(['is_fenzhang'=>0]);
                        $order['is_fenzhang'] = 0;
                    }
                }
                
                $price = floatval($order['price']);
            }
            else if($order_type == 4){
                $title = $_W['base']['fenxiao']['texts']['predeposit']."充值";
                $order = Db::name("bbfx_deposit_release_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
                if(empty($order)){
                    throw new \Exception("订单不存在");
                }
                if($order['status'] == 1){
                    throw new \Exception("订单已支付");
                }
                
                $price = floatval($order['money']);
            }
            else if($order_type == 5){
                $order = Db::name("bbfx_sconnection_upgrade_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
                if(empty($order)){
                    throw new \Exception("订单不存在");
                }
                if($order['status'] == 1){
                    throw new \Exception("订单已支付");
                }
                $title = $order['title'];
                $price = floatval($order['price']);
            }
            else if($order_type == 6){
                $title = "购买积分商品";
                $order = Db::name("bbfx_scoreshop_order")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'order_no'=>$order_no])->find();
                if(empty($order)){
                    throw new \Exception("订单不存在");
                }
                if($order['status'] == -1){
                    throw new \Exception("订单已关闭");
                }
                if($order['status'] != 0){
                    throw new \Exception("订单已支付");
                }
                $goods_info = iunserializer($order['goods_info']);
                if(!empty($goods_info)){
                    $title = $goods_info[0]['title'];
                }
                $price = floatval($order['price']);
                if($this->user['score'] < $order['score']){
                    throw new \Exception("您的积分不足");
                }
            }
            else if($order_type == 7){
                $title = "押金支付";
                $order = Db::name("bbfx_yajin_record")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'order_no'=>$order_no])->find();
                if(empty($order)){
                    throw new \Exception("记录不存在");
                }
                if($order['status'] != 0){
                    throw new \Exception("押金已支付");
                }
                
                $price = floatval($order['money']);
            }
            else if($order_type == 8){
                $title = "报名支付";
                $order = Db::name("bbfx_activity_baoming")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'order_no'=>$order_no])->find();
                if(empty($order)){
                    throw new \Exception("记录不存在");
                }
                if($order['is_pay'] != 0){
                    throw new \Exception("报名已支付");
                }
                
                $price = floatval($order['baoming_fee']);
                // if($paytype == 1 && $dk_type == 1){
                //     if($price > $user['balance']){
                //         $price = round($price - $user['balance'],2);
                //     }
                //     else{
                //         $paytype = 2;
                //     }
                // }
            }
            else if($order_type == 9){
                $title = "舌诊支付";
                $order = Db::name("bbfx_tongue_diagnosis_buy_record")->where(['uniacid'=>UNIACID,'user_id'=>$user['id'],'order_no'=>$order_no])->find();
                if(empty($order)){
                    __error("记录不存在");
                }
                if($order['status'] != 0){
                    __error("舌诊已支付");
                }
                
                $price = floatval($order['money']);
            }
            else{
                throw new \Exception("订单类型错误");
            }
            
            if(empty($order)){
                throw new \Exception("订单信息错误");
            }

            if($order_type == 1){
                $productDetails = [];
                foreach($order_goods as $key => $val){
                    $goods1 = iunserializer($val['goods_info']);
                    $productDetails[] = ['name'=>$goods1['title'],'amount'=>intval($goods1['price']*100),'quantity'=>$val['total']];
                }
            }
            else{
                $productDetails = [['name'=>$title,'amount'=>intval($price*100),'quantity'=>1]];
            }
            
            $openid = $this->param('openid/s');
            if($params['platform'] == 'H5'){
                if(!empty($openid)){
                    $params['platform'] = 'GZH';
                    if($order_type == 1){
                        Db::name("bbfx_order")->where(['id'=>$order['id']])->update(['platform'=>$params['platform']]);
                    }
                }
            }

            if($paytype == 6 && empty($pay_voucher)){
                throw new \Exception("请上传支付凭证");
            }
            
            $order_trade_no = date('YmdHis').random(8,true);
            $pay_order = Db::name("bbfx_pay_order")->where(['order_no'=>$order_no,"order_type"=>$order_type,"order_id"=>$order['id'],'is_pay'=>1,'uniacid'=>UNIACID])->find();
            if(!empty($pay_order)){
                $pay_result = 2;
                throw new \Exception("订单已支付");
            }

            $pay_order = Db::name("bbfx_pay_order")->where(['order_no'=>$order_no,"order_type"=>$order_type,"order_id"=>$order['id'],'price'=>$price,'uniacid'=>UNIACID])->find();
            
            if(!empty($pay_order)){
                if(!in_array($paytype,[60,61,62])){
                    $order_trade_no = $pay_order['order_trade_no'];
                }
            }
            else{
                $pay_order = [];
            }
            if(empty($pay_order)){
                $data = array(
                    "uniacid"=>UNIACID,
                    "openid"=>$openid,
                    "order_trade_no"=>$order_trade_no,
                    "order_no"=>$order_no,
                    "order_id"=>$order['id'],
                    "price"=>$price,
                    "order_type"=>$order_type,
                    "pay_voucher"=>"",
                    "platform"=>$params['platform'],
                    "createtime"=>time()
                );
                if($order['is_fenzhang'] == 1){
                    $data['fenzhang_status'] = 0;
                }
                $data['paytype'] = 0;
                if($paytype == 6){
                    $pay_voucher = replaceSiteroot($pay_voucher);
                    $data['pay_voucher'] = $pay_voucher;
                    $data['paytype'] = $paytype;
                }
                $pay_order_id = Db::name("bbfx_pay_order")->insertGetId($data);
                if(empty($pay_order_id)){
                    throw new \Exception("创建支付订单失败");
                }
                $pay_order = Db::name("bbfx_pay_order")->where(['id'=>$pay_order_id])->find();
            }
            else{
                $pdata = ['order_trade_no'=>$order_trade_no,"platform"=>$params['platform'],'openid'=>$openid];
                $pdata['paytype'] = 0;
                if($paytype == 6){
                    $pay_voucher = replaceSiteroot($pay_voucher);
                    $pdata['pay_voucher'] = $pay_voucher;
                    $pdata['paytype'] = $paytype;
                }
                if($paytype == 1 && $dk_type == 1 && $order_type == 1 && $pay_order['price'] != $price){
                    // $pdata['price'] = $price;
                    // $order_no = date('YmdHis').random(8,true);  
                }

                Db::name("bbfx_pay_order")->where(['id'=>$pay_order['id']])->update($pdata);
            }
            
            if($price <= 0 && $paytype != 6){
                $paytype = 2;
            }

            $user_level = -1;
            if($user['is_distributor'] == 1 && $user['distributor_status'] == 1){
                $user_level = $user['level'];
            }
            
            if($paytype == 1){
                
                $pay_data = array(
                    'order_trade_no' => $order_trade_no,
                    'order_no' => $order_no,
                    'fee' => floatval($price),
                    'title' => $title,
                    'openid'=>$openid,
                    "profit_sharing"=>"N",
                    'level'=>$user_level,
                    'type'=>$params['platform']
                );

                
                if($order['is_fenzhang'] == 1){
                    $pay_data['profit_sharing'] = "Y";
                }
                

                $res = Wxpay::pay($pay_data);
                if ($res['code'] != 1) {
                    throw new \Exception($res['message']);
                }
                $pay_data = $res['data'];
                $pay_data['order_no'] = $order_no;
                $result = __json(1,"操作成功",$pay_data);
                $pay_result = 0;
            }
            else if($paytype == 50){
                if($platform_config['dg_status'] != 1){
                    throw new \Exception("未开启汇付支付");
                }

                $pay_data = array(
                    'order_trade_no' => $order_trade_no,
                    'order_no' => $order_no,
                    'fee' => floatval($price),
                    'title' => $title,
                    'openid'=>$openid,
                    "profit_sharing"=>"N",
                    'type'=>$params['platform']
                );

                if($params['platform'] == 'H5' || $params['platform'] == 'APP'){
                    // __error("当前平台不支持汇付支付");
                }

                $dougong = new Dougong();
                $result = $dougong->pay($pay_data,1);
                if ($result['code'] != 1) {
                    throw new \Exception($result['message']);
                }
                $result['data']['order_no'] = $order_no;
                $pay_result = 0;
            }
            else if($paytype == 60){
                $shouxin = iunserializer($platform_config['thirdparty_pay'])['shouxin'];
                if($shouxin['status'] != 1){
                    throw new \Exception("未开启首信易支付");
                }
                // $params['platform'] = 'GZH';
                // $openid = $user['openid'];
                $pay_data = array(
                    'order_trade_no' => $order_trade_no,
                    'fee' => floatval($price),
                    'productDetails' => $productDetails,
                    'openid'=>$openid,
                    'platform'=>$params['platform'],
                    'paytype'=>1
                );

                if($params['platform'] == 'H5' || $params['platform'] == 'APP'){
                    __error("当前平台不支持首信易支付微信支付");
                }

                $payeasenet = new Payeasenet();
                $result = $payeasenet->pay($pay_data);
                if ($result['code'] != 1) {
                    throw new \Exception($result['message']);
                }
                $result['data']['order_no'] = $order_no;
                $pay_result = 0;
            }
            else if($paytype == 2){
                
                
                $res = User::checkPayPassword($user['id'],$params['pay_password']);
                if($res['code'] != 1){
                    throw new \Exception($res['message']);
                }
                
                
                if(!in_array($order_type,[1,3,4,5,6,7,8,9])){
                    throw new \Exception("不支持该支付方式");
                }
                
                if($user['balance'] < $price){
                    throw new \Exception("余额不足".$user['balance']);
                }

                if(empty($title)){
                    $title = "购买商品";
                }
                
                if($price > 0){
                    $res = User::setUserMoney($user['id'],-$price,1,1,$title,$order);
                    if(empty($res)){
                        throw new \Exception("余额不足");
                    }
                }

                $pay_result = 1;
            }
            else if($paytype == 7){
                
                $res = User::checkPayPassword($user['id'],$params['pay_password']);
                if($res['code'] != 1){
                    throw new \Exception($res['message']);
                }
                
                if(!in_array($order_type,[1,3,4,5,6,7,8,9])){
                    throw new \Exception("不支持该支付方式");
                }
                
                if($user['score'] < $price){
                    throw new \Exception("积分不足".$user['balance']);
                }

                if(empty($title)){
                    $title = "购买商品";
                }
                
                if($price > 0){
                    $res = User::setUserMoney($user['id'],-$price,2,1,$title,$order);
                    if(empty($res)){
                        throw new \Exception("积分不足");
                    }
                }

                $pay_result = 1;
            }
            else if($paytype == 3){
                
                $res = User::checkPayPassword($user['id'],$params['pay_password']);
                if($res['code'] != 1){
                    throw new \Exception($res['message']);
                }
                
                if(!in_array($order_type,[1,3,5,7,8,9])){
                    throw new \Exception("不支持该支付方式");
                }
                
                if($user['deposit_money'] < $price){
                    throw new \Exception($_W['base']['fenxiao']['texts']['predeposit']."不足");
                }
                
                if($price > 0){
                    $res = User::setUserMoney($user['id'],-$price,6,1,$title,$order);
                    if(empty($res)){
                        throw new \Exception($_W['base']['fenxiao']['texts']['predeposit']."不足");
                    }
                }

                $pay_result = 1;
            }
            else if($paytype == 4){
                $res = User::checkPayPassword($user['id'],$params['pay_password']);
                if($res['code'] != 1){
                    throw new \Exception($res['message']);
                }
                
                if(!in_array($order_type,[1])){
                    throw new \Exception("不支持该支付方式");
                }
                
                if($user['repurchase_currency'] < $price){
                    throw new \Exception($_W['base']['fenxiao']['texts']['repurchase_currency']."不足");
                }
                
                if($price > 0){
                    $res = User::setUserMoney($user['id'],-$price,4,1,$title,$order);
                    if(empty($res)){
                        throw new \Exception($_W['base']['fenxiao']['texts']['repurchase_currency']."不足");
                    }
                }

                $pay_result = 1;
            }
            else if($paytype == 5){

                $pay_data = array(
                    'order_trade_no' => $order_trade_no,
                    'order_no' => $order_no,
                    'fee' => round($price,2),
                    'title' => $title,
                    'level'=>$user_level,
                    'platform'=>$params['platform'],
                );

                if($platform_config['dg_status'] == 1){
                    // if(!empty($platform_config['dg_huifu_id'])){
                    //     $dougong = new Dougong();
                    //     $result = $dougong->pay($pay_data,5);
                    //     __result($result);
                    // }
                }
                
                $result = Alipay::wapPay($pay_data);

                if($result['code'] != 1){
                    throw new \Exception($result['message']);
                }
                $pay_result = 0;
            }
            else if($paytype == 61){
                $shouxin = iunserializer($platform_config['thirdparty_pay'])['shouxin'];
                if($shouxin['status'] != 1){
                    throw new \Exception("未开启首信易支付");
                }

                $pay_data = array(
                    'order_trade_no' => $order_trade_no,
                    'fee' => floatval($price),
                    'productDetails' => $productDetails,
                    'openid'=>$openid,
                    'platform'=>$params['platform'],
                    'paytype'=>5
                );

                $payeasenet = new Payeasenet();
                $result = $payeasenet->pay($pay_data);
                if ($result['code'] != 1) {
                    throw new \Exception($result['message']);
                }
                $result['data']['order_no'] = $order_no;
                $pay_result = 0;
            }
            else if($paytype == 62){
                $shouxin = iunserializer($platform_config['thirdparty_pay'])['shouxin'];
                if($shouxin['status'] != 1){
                    throw new \Exception("未开启首信易支付");
                }

                $pay_data = array(
                    'order_trade_no' => $order_trade_no,
                    'fee' => floatval($price),
                    'productDetails' => $productDetails,
                    'openid'=>$openid,
                    'platform'=>$params['platform'],
                    'realname'=>$params['realname'],
                    'idcard'=>$params['idcard'],
                    'paytype'=>9
                );

                $payeasenet = new Payeasenet();
                $result = $payeasenet->pay($pay_data);
                if ($result['code'] != 1) {
                    throw new \Exception($result['message']);
                }
                $result['data']['order_no'] = $order_no;
                $pay_result = 0;

                $user['withdrawal_info']['sx_pay_realname'] = $params['realname'];
                $user['withdrawal_info']['sx_pay_idcard'] = $params['idcard'];
                Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'id'=>$user['id']])->update(['withdrawal_info'=>iserializer($user['withdrawal_info'])]);
            }
            else if($paytype == 6){

                if($order_type == 1){
                    $res = Db::name("bbfx_order")->where(['id'=>$order['id'],'is_pay'=>0,'cash_pay_shenhe'=>-1])->update(['paytype'=>6,'cash_pay_shenhe'=>0]);
                }
                else{
                    throw new \Exception("暂不支持该支付方式");
                }

                if(empty($res)){
                    throw new \Exception("提交失败");
                }
                
                $pay_result = 0;
                $result = __json(1,"提交成功");
            }
            else{
                throw new \Exception("不支持该支付方式");
            }
            
            Db::commit();
        }
        catch(\Exception $e){
            Db::rollback();
            $result = __json(0,$e->getMessage());
        }

        $result['data']['pay_result'] = $pay_result;
        
        echo toJsonen($result);
        bbfx_finish_request();

        if($pay_result == 2){
            Order::payResultOK($pay_order['id']);
        }
        else if($pay_result == 1){
            Order::payResult($order_trade_no,$paytype);
        }
        
        __result($result);
    }

    public function alipayLink(){
        global $_W;
        $params = $this->param();
        $order_type = intval($params['order_type']);
        $paytype = 5;
        $order_no = $params['order_no'];
        $dk_type = intval($params['dk_type']);
        $user = [];
        if($order_type == 1){
            $title = "购买商品";
            $order = Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
            $user = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'id'=>$order['user_id']])->find();
            if(!empty($order)){
                $goods_info = iunserializer($order['goods_info']);
                $title = $goods_info[0]['title'];
                
                foreach ($goods_info as $key => $item){
                    $result = CloudWarehouse::checkStock($user['id'],$item['total'],$item['id']);
            	    if($result['code'] != 1){
            	        __error($result['message']);
            	    }
                }
            }
            if($order['is_pay'] == 1){
                __error("订单已支付");
            }
            if($order['status'] == -1){
                __error("订单已关闭");
            }
            if($order['status'] != 0){
                __error("订单已支付");
            }
            if($order['cash_pay_shenhe'] >= 0){
                __error("已线下支付");
            }
            if($order['order_source'] == 3){
                $pintuan_order = Db::name("bbfx_pintuan_order")->where(['uniacid'=>UNIACID,'id'=>$order['pintuan_order_id']])->find();
                if(empty($pintuan_order)){
                    __error("拼团订单信息错误");
                }

                if($pintuan_order['status'] == 1 || $pintuan_order['status'] == 2){
                    __error("该拼团订单已结束，请重新下单支付");
                }
            }
            $price = floatval($order['price']);
            $repurchase_currency = floatval($order['repurchase_currency']);
            if($repurchase_currency > 0){
                $price = round($price - $repurchase_currency,2);
            }
            if($paytype == 1 && $dk_type == 1){
                if($price > $user['balance']){
                    $price = round($price - $user['balance'],2);
                    Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'id'=>$order['id']])->update(['balance'=>$user['balance'],'score'=>0]);
                }
                else{
                    $paytype = 2;
                }
            }
            else if($paytype == 1 && $dk_type == 2){
                if($price > $user['score']){
                    $price = round($price - $user['score'],2);
                    Db::name("bbfx_order")->where(['uniacid'=>UNIACID,'id'=>$order['id']])->update(['score'=>$user['score'],'balance'=>0]);
                }
                else{
                    $paytype = 7;
                }
            }
            if(empty($title)){
                $title = "购买商品";
            }

            if($_W['base']['fenxiao']['fenzhang_status'] == 1 && $order['is_fenzhang'] == 0){
                Db::name("bbfx_order")->where(['id'=>$order['id']])->update(['is_fenzhang'=>1]);
                $order['is_fenzhang'] = 1;
            }
            else{
                if($_W['base']['fenxiao']['fenzhang_status'] == 0 && $order['is_fenzhang'] == 1){
                    Db::name("bbfx_order")->where(['id'=>$order['id']])->update(['is_fenzhang'=>0]);
                    $order['is_fenzhang'] = 0;
                }
            }
        }
        else if($order_type == 2){
            $title = "充值余额";
            $order = Db::name("bbfx_recharge_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
            if($order['type'] == 2){
                $title = "充值积分";
            }
            if(empty($order)){
                __error("订单不存在");
            }
            if($order['is_pay'] == 1){
                __error("订单已支付");
            }
            if($order['type'] == 2){
                $title = "充值积分";
            }
            $price = floatval($order['money']);
        }
        else if($order_type == 3){
            $title = "购买会员卡";
            $order = Db::name("bbfx_member_card_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("订单不存在");
            }
            if($order['status'] == 1){
                __error("订单已支付");
            }

            if($_W['base']['fenxiao']['fenzhang_status'] == 1 && $order['is_fenzhang'] == 0){
                Db::name("bbfx_member_card_order")->where(['id'=>$order['id']])->update(['is_fenzhang'=>1]);
                $order['is_fenzhang'] = 1;
            }
            else{
                if($_W['base']['fenxiao']['fenzhang_status'] == 0 && $order['is_fenzhang'] == 1){
                    Db::name("bbfx_member_card_order")->where(['id'=>$order['id']])->update(['is_fenzhang'=>0]);
                    $order['is_fenzhang'] = 0;
                }
            }
            
            $price = floatval($order['price']);
        }
        else if($order_type == 4){
            $title = $_W['base']['fenxiao']['texts']['predeposit']."充值";
            $order = Db::name("bbfx_deposit_release_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("订单不存在");
            }
            if($order['status'] == 1){
                __error("订单已支付");
            }
            
            $price = floatval($order['money']);
        }
        else if($order_type == 5){
            $order = Db::name("bbfx_sconnection_upgrade_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("订单不存在");
            }
            if($order['status'] == 1){
                __error("订单已支付");
            }
            $title = $order['title'];
            $price = floatval($order['price']);
        }
        else if($order_type == 6){
            $title = "购买积分商品";
            $order = Db::name("bbfx_scoreshop_order")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("订单不存在");
            }
            $user = Db::name("bbfx_user")->where(['id'=>$order['user_id']])->find();
            if($order['status'] == -1){
                __error("订单已关闭");
            }
            if($order['status'] != 0){
                __error("订单已支付");
            }
            $goods_info = iunserializer($order['goods_info']);
            if(!empty($goods_info)){
                $title = $goods_info[0]['title'];
            }
            $price = floatval($order['price']);
            if($user['score'] < $order['score']){
                __error("您的积分不足");
            }
        }
        else if($order_type == 7){
            $title = "押金支付";
            $order = Db::name("bbfx_yajin_record")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("记录不存在");
            }
            if($order['status'] != 0){
                __error("押金已支付");
            }
            
            $price = floatval($order['money']);
        }
        else if($order_type == 8){
            $title = "报名支付";
            $order = Db::name("bbfx_activity_baoming")->where(['uniacid'=>UNIACID,'order_no'=>$order_no])->find();
            if(empty($order)){
                __error("记录不存在");
            }
            if($order['is_pay'] != 0){
                __error("报名已支付");
            }
            
            $price = floatval($order['baoming_fee']);
        }
        else{
            __error("订单类型错误");
        }
        
        if(empty($order)){
            __error("订单信息错误");
        }
        
        $order_trade_no = date('YmdHis').random(8,true);
        $pay_order = Db::name("bbfx_pay_order")->where(['order_no'=>$order_no,"order_type"=>$order_type,"order_id"=>$order['id'],'is_pay'=>1,'uniacid'=>UNIACID])->find();
        if(!empty($pay_order)){
            Order::payResultOK($pay_order['id']);
            __error("订单已支付");
        }

        $pay_order = Db::name("bbfx_pay_order")->where(['order_no'=>$order_no,"order_type"=>$order_type,"order_id"=>$order['id'],'price'=>$price,'uniacid'=>UNIACID])->find();
        
        if(!empty($pay_order)){
            $order_trade_no = $pay_order['order_trade_no'];
        }
        else{
            $pay_order = [];
        }

        if(empty($user)){
           $user = Db::name("bbfx_user")->where(['uniacid'=>UNIACID,'id'=>$order['user_id']])->find(); 
        }
        
		if(empty($pay_order)){
		    $data = array(
		        "uniacid"=>UNIACID,
		        "openid"=>$user['openid'],
                "order_trade_no"=>$order_trade_no,
		        "order_no"=>$order_no,
		        "order_id"=>$order['id'],
		        "price"=>$price,
		        "order_type"=>$order_type,
                "pay_voucher"=>"",
		        "platform"=>$params['platform'],
		        "createtime"=>time()
		    );
            if($order['is_fenzhang'] == 1){
                $data['fenzhang_status'] = 0;
            }
            $res = Db::name("bbfx_pay_order")->insert($data);
		    if(empty($res)){
		        __error("创建支付订单失败");
		    }
		}
		else{
		    if($pay_order['is_pay'] == 1){
		        __error("订单已支付");
		    }

            $pdata = ['order_trade_no'=>$order_trade_no,"platform"=>$params['platform']];
            if($paytype == 1 && $dk_type == 1 && $order_type == 1 && $pay_order['price'] != $price){
                // $pdata['price'] = $price;
            }

		    Db::name("bbfx_pay_order")->where(['id'=>$pay_order['id']])->update($pdata);

		}
		
		if($price <= 0){
		    __error("订单金额错误");;
		}

        $user_level = -1;
        if($user['is_distributor'] == 1 && $user['distributor_status'] == 1){
            $user_level = $user['level'];
        }
		
		$pay_data = array(
            'order_trade_no' => $order_trade_no,
            'order_no' => $order_no,
            'fee' => round($price,2),
            'title' => $title,
			'level'=>$user_level,
            'platform'=>$params['platform'],
        );

        $result = Alipay::wapPay($pay_data);
        
        __result($result);
    }

    public function notify()
    {
        
        $params = Wxpay::payVerify();
        addFileLog("pay111",json_encode($params));
        if(empty($params)){
            echo array2xml(['return_code'=>'FAIL','return_msg'=>'数据不存在']);
    		exit();
        }
        $order_trade_no = $params['out_trade_no'];
        
        echo array2xml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']);
        
        bbfx_finish_request();

        Order::payResult($order_trade_no,1,$params);
    }
    
    
    public function alipayNotify(){
        $params = $this->paramPost();
        
        if(empty($params['sign'])){
            echo "fail";die;
        }
        
        foreach ($params as $key => &$val){
            $val = htmlspecialchars_decode($val);
        }
        unset($val);
        
        addFileLog("alipaynn1",json_encode($params));
        
        
        $result = Alipay::payVerify($params);
        if($result['code'] == 1){

            echo "success";

            bbfx_finish_request();

            $order_trade_no = $params['out_trade_no'];
            if($params['trade_status'] == 'TRADE_SUCCESS'){
                Order::payResult($order_trade_no,5,$params);
            }
            
        }
        else{
            echo "fail";
        }
    }
    
    public function dgNotify(){
        $params = $this->paramPost();

        if(empty($params['sign'])){
            echo "fail";die;
        }

        $params['resp_data'] = htmlspecialchars_decode($params['resp_data']);
        addFileLog("dgnn1",json_encode($params));

        if($params['resp_code'] != '00000000'){
            echo "success";die;
        }
        
        $Dougong = new Dougong();
        $result = $Dougong->payVerify($params['sign'],$params['resp_data']);
        
        if($result['code'] == 1){
             
            if(is_string($params['resp_data'])){
                $resp_data = json_decode($params['resp_data'],true);
            }
            else{
                $resp_data = $params['resp_data'];
            }
            
            if($resp_data['trans_stat'] == 'S'){
                $trade_type = $resp_data['trade_type'];
                if($trade_type == 'A_NATIVE' || $trade_type == 'A_JSAPI' || $trade_type == 'A_MICROPAY'){
                    Order::payResult($resp_data['req_seq_id'],5,$resp_data,1);
                }
                else if($trade_type == 'T_JSAPI' || $trade_type == 'T_JSAPI' || $trade_type == 'T_MINIAPP' || $trade_type == 'T_MICROPAY'){
                    Order::payResult($resp_data['req_seq_id'],1,$resp_data,1);
                }
            }
        }

        echo "success";die;
    }

    public function dgRefundNotify(){
        $params = $this->paramPost();

        if(empty($params['sign'])){
            echo "fail";die;
        }

        $params['resp_data'] = htmlspecialchars_decode($params['resp_data']);
        addFileLog("dgrnn1",json_encode($params));

        if($params['resp_code'] != '00000000'){
            echo "success";die;
        }
        
        $Dougong = new Dougong();
        $result = $Dougong->payVerify($params['sign'],$params['resp_data']);
        
        if($result['code'] == 1){
             
            if(is_string($params['resp_data'])){
                $resp_data = json_decode($params['resp_data'],true);
            }
            else{
                $resp_data = $params['resp_data'];
            }
            
            if($resp_data['trans_stat'] != 'S'){
                Order::refundFail($params);
            }

        }

        echo "success";die;
    }

    public function dgRuzhuNotify(){

        $params = $this->paramPost();

        if(empty($params['sign'])){
            echo "fail";die;
        }

        $params['resp_data'] = htmlspecialchars_decode($params['resp_data']);
        addFileLog("dgrznn1",json_encode($params));

        if($params['resp_code'] != '00000000'){
            echo "success";die;
        }
        
        $Dougong = new Dougong();
        $result = $Dougong->payVerify($params['sign'],$params['resp_data']);
        
        if($result['code'] == 1){
             
            if(is_string($params['resp_data'])){
                $resp_data = json_decode($params['resp_data'],true);
            }
            else{
                $resp_data = $params['resp_data'];
            }
            
            if($resp_data['notify_type'] == 'A'){
                if($resp_data['audit_info']['audit_status'] == 'Y'){
                    $txaccount = Db::name("bbfx_user_txaccount")->where(['uniacid'=>UNIACID,'req_seq_id'=>$resp_data['req_seq_id']])->find();
                    if(!empty($txaccount)){
                        Db::name("bbfx_user_txaccount")->where(['id'=>$txaccount['id']])->update(['rz_status'=>1,'rz_time'=>time()]);
                    }
                }
            }
            
        }

        echo "success";die;
    }

    

    public function dgFenzhangNotify(){

        $params = $this->paramPost();

        if(empty($params['sign'])){
            echo "fail";die;
        }

        $params['resp_data'] = htmlspecialchars_decode($params['resp_data']);
        addFileLog("dgfznn1",json_encode($params));

        if($params['resp_code'] != '00000000'){
            echo "success";die;
        }
        
        $Dougong = new Dougong();
        $result = $Dougong->payVerify($params['sign'],$params['resp_data']);
        
        if($result['code'] == 1){
             
            if(is_string($params['resp_data'])){
                $resp_data = json_decode($params['resp_data'],true);
            }
            else{
                $resp_data = $params['resp_data'];
            }
            
            
            
        }

        echo "success";die;
    }

    public function dgTixianNotify(){

        $params = $this->paramPost();

        if(empty($params['sign'])){
            echo "fail";die;
        }

        $params['resp_data'] = htmlspecialchars_decode($params['resp_data']);
        addFileLog("dgtxnn1",json_encode($params));

        if($params['resp_code'] != '00000000'){
            echo "success";die;
        }
        
        $Dougong = new Dougong();
        $result = $Dougong->payVerify($params['sign'],$params['resp_data']);
        
        if($result['code'] == 1){
             
            if(is_string($params['resp_data'])){
                $resp_data = json_decode($params['resp_data'],true);
            }
            else{
                $resp_data = $params['resp_data'];
            }
            
            $log = Db::name("bbfx_withdrawal_log")->where(['uniacid'=>UNIACID,'withdrawal_no'=>$resp_data['req_seq_id'],'withdrawal_type'=>['<>',8],'dk_status'=>0])->find();
            if(!empty($log)){
                
                if($resp_data['trans_status'] == 'S' || $resp_data['trans_status'] == 'F'){
                    $data = ['dk_time'=>time()];
                    if($resp_data['trans_status'] == 'S'){
                        $data['dk_status'] = 1;
                    }
                    else{
                        $data['dk_status'] = 2;
                        $data['status'] = 2;
                        $data['paytime'] = time();
                        $data['remarks'] = $resp_data['sub_resp_desc'];
                    }

                    Db::name("bbfx_withdrawal_log")->where(['id'=>$log['id']])->update($data);
                }
            }
            
        }

        echo "success";die;
    }

    public function dgYewuNotify(){

        $params = $this->paramPost();

        if(empty($params['sign'])){
            echo "fail";die;
        }

        $params['resp_data'] = htmlspecialchars_decode($params['resp_data']);
        addFileLog("dgywnn1",json_encode($params));

        if($params['resp_code'] != '00000000'){
            echo "success";die;
        }
        
        $Dougong = new Dougong();
        $result = $Dougong->payVerify($params['sign'],$params['resp_data']);
        
        if($result['code'] == 1){
             
            if(is_string($params['resp_data'])){
                $resp_data = json_decode($params['resp_data'],true);
            }
            else{
                $resp_data = $params['resp_data'];
            }
            
            
        }

        echo "success";die;
    }

    public function dgYewuNotify1(){

        $params = $this->paramPost();

        if(empty($params['sign'])){
            echo "fail";die;
        }

        $params['resp_data'] = htmlspecialchars_decode($params['resp_data']);
        addFileLog("dgywnn2",json_encode($params));

        if($params['resp_code'] != '00000000'){
            echo "success";die;
        }
        
        $Dougong = new Dougong();
        $result = $Dougong->payVerify($params['sign'],$params['resp_data']);
        
        if($result['code'] == 1){
             
            if(is_string($params['resp_data'])){
                $resp_data = json_decode($params['resp_data'],true);
            }
            else{
                $resp_data = $params['resp_data'];
            }
            
            
        }

        echo "success";die;
    }
    
    public function dgQuxianNotify(){

        $params = $this->paramPost();

        if(empty($params['sign'])){
            echo "fail";die;
        }

        $params['resp_data'] = htmlspecialchars_decode($params['resp_data']);
        addFileLog("dgqxnn1",json_encode($params));

        if($params['resp_code'] != '00000000'){
            // echo "success";die;
        }
        
        $Dougong = new Dougong();
        $result = $Dougong->payVerify($params['sign'],$params['resp_data']);
        
        if($result['code'] == 1){
             
            if(is_string($params['resp_data'])){
                $resp_data = json_decode($params['resp_data'],true);
            }
            else{
                $resp_data = $params['resp_data'];
            }
            if($resp_data['sub_resp_code'] == '00000000'){
                $log = Db::name("bbfx_withdrawal_log")->where(['uniacid'=>UNIACID,'withdrawal_no'=>$resp_data['req_seq_id'],'withdrawal_type'=>8,'dk_status'=>0])->find();
                if(!empty($log)){
                    
                    if($resp_data['trans_status'] == 'S' || $resp_data['trans_status'] == 'F'){
                        $data = ['dk_time'=>time()];
                        if($resp_data['trans_status'] == 'S'){
                            $data['dk_status'] = 1;
                        }
                        else{
                            $data['dk_status'] = 2;
                            $data['status'] = 2;
                            $data['paytime'] = time();
                            $data['remarks'] = $resp_data['sub_resp_desc'];
                        }
    
                        Db::name("bbfx_withdrawal_log")->where(['id'=>$log['id']])->update($data);
                    }
                }
    
                $log = Db::name("bbfx_withdrawal_log")->where(['uniacid'=>UNIACID,'dg_qx_no'=>$resp_data['req_seq_id'],'yezf_dk'=>1,'dk_status'=>['in',[-1,0,2]]])->find();
                if(!empty($log)){
                    if($resp_data['trans_status'] == 'S' || $resp_data['trans_status'] == 'F'){
                        $data = ['dk_time'=>time()];
                        if($resp_data['trans_status'] == 'S'){
                            $data['dk_status'] = 1;
                        }
                        else{
                            $data['dk_status'] = 2;
                            // $data['status'] = 2;
                            $data['paytime'] = time();
                            $data['remarks'] = $resp_data['sub_resp_desc'];
                        }
    
                        Db::name("bbfx_withdrawal_log")->where(['id'=>$log['id']])->update($data);
                    }
                }
            }
            
        }

        echo "success";die;
    }
    
    public function dgBalancePayNotify(){

        $params = $this->paramPost();

        if(empty($params['sign'])){
            echo "fail";die;
        }

        $params['resp_data'] = htmlspecialchars_decode($params['resp_data']);
        addFileLog("dgyezfnn2",json_encode($params));

        if($params['resp_code'] != '00000000'){
            echo "success";die;
        }
        
        $Dougong = new Dougong();
        $result = $Dougong->payVerify($params['sign'],$params['resp_data']);
        
        if($result['code'] == 1){
             
            if(is_string($params['resp_data'])){
                $resp_data = json_decode($params['resp_data'],true);
            }
            else{
                $resp_data = $params['resp_data'];
            }
            
            $log = Db::name("bbfx_withdrawal_log")->where(['uniacid'=>UNIACID,'withdrawal_no'=>$resp_data['req_seq_id'],'yezf_dk'=>1])->find();
            if(!empty($log)){
                if($resp_data['trans_stat'] == 'S' || $resp_data['trans_stat'] == 'F'){
                    if($resp_data['trans_status'] == 'S'){
                        $data['yezf_dk_status'] = 1;
                    }
                    else{
                        $data['yezf_dk_status'] = 2;
                        // $data['status'] = 2;
                        $data['paytime'] = time();
                        $data['remarks'] = $resp_data['sub_resp_desc'];
                    }

                    Db::name("bbfx_withdrawal_log")->where(['id'=>$log['id']])->update($data);

                    if($data['yezf_dk_status'] == 1){
                        $txaccount = iunserializer($log['info']);
                        $dg_qx_no = date('YmdHis').random(8,true);
                        Db::name("bbfx_withdrawal_log")->where(['id'=>$log['id']])->update(['dg_qx_no'=>$dg_qx_no,'dk_status'=>0]);
                        $params = [
                            'cash_amt'=>$log['realmoney'],
                            'huifu_id'=>$txaccount['huifu_id'],
                            'token_no'=>$txaccount['token_no'],
                            'req_seq_id'=>$dg_qx_no,
                        ];
                        $result = $Dougong->quxian($params);
                        if($result['code'] != 1){
                            Db::name("bbfx_withdrawal_log")->where(['id'=>$log['id']])->update(['dk_status'=>-1]);
                        }
                    }
                }
            }
        }

        echo "success";die;
    }

    public function sxNotify(){
        header("Content-Type:text/html; charset=utf-8");
        $raw_post_data = file_get_contents('php://input', 'r');

	    $responseText = "encryptKey: ".$_SERVER['HTTP_ENCRYPTKEY']." merchantId: ".$_SERVER['HTTP_MERCHANTID']." requestId: ".$_SERVER['HTTP_REQUESTID']." ".$raw_post_data;
        addFileLog("sxxx111",$responseText);
        $Payeasenet = new Payeasenet();
        $result = $Payeasenet->payVerify($responseText);
        if($result['code'] == 1){
            $params = $result['data'];
            echo "SUCCESS";
			echo "<br>";
			echo toJsonen($result['data']);

            bbfx_finish_request();

            $paymentModeAlias = $params['paymentModeAlias'];
            if(in_array($paymentModeAlias,['DEBIT_EXPRESS_PAY','BANK_CARD-EXPRESS_DEBIT'])){
                $paytype = 9;
            }
            else if(in_array($paymentModeAlias,['WEIXIN_OFFICIAL_PAY','MINIAPPS_WEIXIN_PAY','MINIAPPS-WEIXIN_PAY-P2P','WECHAT-OFFICIAL_PAY-P2P'])){
                 $paytype = 1;
            }
            else if(in_array($paymentModeAlias,['ALIPAY-WAP-P2P','ALIPAY_WAP_PAY','APP_ALIPAY','ALIPAY_B2C_PAY','MINIAPPS_ALI_PAY','ALIPAY-OFFICIAL-P2P','MINIAPPS-ALI_PAY-P2P'])){
                 $paytype = 5;
            }

            Order::payResult($params['requestId'],$paytype,$params,2);
        }
        else{
            echo "CFCA验证失败";
			echo "<br>";
			echo toJsonen($result['data']);
        }
    }

    public function sxRefundNotify(){
        header("Content-Type:text/html; charset=utf-8");
        $raw_post_data = file_get_contents('php://input', 'r');
	    $responseText = "encryptKey: ".$_SERVER['HTTP_ENCRYPTKEY']." merchantId: ".$_SERVER['HTTP_MERCHANTID']." requestId: ".$_SERVER['HTTP_REQUESTID']." ".$raw_post_data;
        addFileLog("sxxx222",$responseText); 
        $Payeasenet = new Payeasenet();
        $result = $Payeasenet->payRefundVerify($responseText);
        if($result['code'] == 1){
            $params = $result['data'];
            echo "SUCCESS";
			echo "<br>";
			echo toJsonen($result['data']);

            // bbfx_finish_request();
            // $order_refund = Db::name("bbfx_order_refund")->where(['uniacid'=>UNIACID,'order_no'=>$params['requestId']])->find();
            // if(!empty($order_refund)){
            //     Db::name("bbfx_order_refund")->where(['id'=>$order_refund['id']])->update(['status'=>2]);
            //     Db::name("bbfx_order")->where(['id'=>$order_refund['order_id']])->update(['refund_status'=>2]);
            // }
        }
        else{
            echo "CFCA验证失败";
			echo "<br>";
			echo toJsonen($result['data']);
        }
    }
}
