Pay0wx.php 26.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
<?php

namespace app\index\controller;

use app\api\model\Active2Bonus;
use app\api\model\Active2BonusUser;
use app\api\model\Cm0bao0good;
use app\api\model\Cm0bao0user;
use app\api\model\Cm0bi;
use app\api\model\Cm0che;
use app\api\model\Cm0fen0log;
use app\api\model\Cm0fw;
use app\api\model\Cm0gou;
use app\api\model\Cm0order;
use app\api\model\Cm0pei0good;
use app\api\model\Cm0s40active0bao;
use app\api\model\Cm0s40active2;
use app\api\model\Cm0s40gou;
use app\api\model\Cm0s40order;
use app\api\model\Cm0zan0good;
use app\api\model\Cm0zan0user;
use app\api\model\Litestoregoods;
use app\api\model\Litestoregoodsspec;
use app\api\model\OrderTuan;
use app\api\model\OrderTuanUser;
use app\api\model\S4Qiang;
use app\common\controller\Frontend;
use fast\Http;
use think\Db;
use think\Model;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Log;
use EasyWeChat\Foundation\Application;


class Pay0wx
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';


    public $config = [
        'appid' => 'wx11011d1f9492b8e0', // APP APPID
        'app_id' => 'wx11011d1f9492b8e0', // 公众号 APPID
        'miniapp_id' => 'wx11011d1f9492b8e0', // 小程序 APPID

        'mch_id' => '1597125841',
        'key' => 'o1AEIYPLxtLGVp8r5TV6PHbysKIXZzRW',
//        'notify_url' => 'http://myqfd.com/index/pay0wx/notify0',
//        'cert_client' => ROOT_PATH . 'public' . DS . 'assets' . DS . 'wx0pem' . DS . 'apiclient_cert.pem', // optional, 退款,红包等情况时需要用到
//        'cert_key' => ROOT_PATH . 'public' . DS . 'assets' . DS . 'wx0pem' . DS . 'apiclient_key.pem',// optional, 退款,红包等情况时需要用到
        'cert_client' => APP_PATH . 'cert/apiclient_cert.pem', // optional, 退款,红包等情况时需要用到
        'cert_key' => APP_PATH . 'cert/apiclient_key.pem',// optional, 退款,红包等情况时需要用到
        'log' => [ // optional
            'file' => './logs/wechat.log',
            'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
            'type' => 'single', // optional, 可选 daily.
            'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
        ],
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
        ],
        //重定向支付完成路径
//        'return_url'=>'http://myqfd.com/W4O58hAYLo.php/auth/rule?ref=addtabs',
        //沙盒环境这个打开
        // 'mode' => 'dev',
    ];


    public function index($order = [])
    {
        $order['total_fee'] = $order['total_fee'] * 100;
        $ip = request()->ip();
        $gs_ip = Cm0fw::where('kw', 'IP地址')->value('content');//公司IP
        if ($ip == $gs_ip) {
            $order['total_fee'] = '1';
        }
        $result = Pay::wechat($this->config)->miniapp($order);
        return $result;
    }


    public function tui($order = [])
    {
//        $order2 = [
//            'out_trade_no' => $order['order_no'],
//            'out_refund_no' => time(),
//            'total_fee' => $order['shi_pay'],
//            'refund_fee' => $order['tui_pay'],
//            'refund_desc' => $order['cnt'],
//            'type' => 'miniapp',
//        ];
        $result = Pay::wechat($this->config)->refund($order);
        $return_code = $result['return_code'] ?? '';
        if ($return_code == 'SUCCESS') {
            return true;
        } else {
            return false;
        }
    }


    //H5的提现    //用的是同放的app_id  百荣的商户号
    public function h5_ti_xian($user_id, $money)
    {
        $options = [
//            'app_id' => 'wxa9f996fcd50636d4',  // 公众号appid
            'app_id' => 'wx11011d1f9492b8e0',         // AppID
//            'payment' => [
            //                'merchant_id' => '1386823202',
            //                'key' => 'myadrquavnbdgwuu5wshtbgxl0zzcd2w',
            //                'cert_path' => APP_PATH . 'cert_h5/apiclient_cert.pem',
            //                'key_path' => APP_PATH . 'cert_h5/apiclient_key.pem',
            //            ],//百荣的提现
            'payment' => [
                'merchant_id' => '1597125841',
                'key' => 'o1AEIYPLxtLGVp8r5TV6PHbysKIXZzRW',
                'cert_path' => APP_PATH . 'cert/apiclient_cert.pem', // optional, 退款,红包等情况时需要用到
                'key_path' => APP_PATH . 'cert/apiclient_key.pem',// optional, 退款,红包等情况时需要用到
            ],//同放的提现
        ];


        $order_no = cmf_get_order_sn();
        $openid = \app\admin\model\Third::where('user_id', $user_id)
            ->value('openid');
        $merchantPayData = [
            'partner_trade_no' => $order_no, //随机字符串作为订单号,跟红包和支付一个概念。
            'openid' => $openid, //收款人的openid
            'check_name' => 'NO_CHECK',  //文档中有三种校验实名的方法 NO_CHECK OPTION_CHECK FORCE_CHECK
            //'re_user_name'=>'张三',     //OPTION_CHECK FORCE_CHECK 校验实名的时候必须提交
            'amount' => $money * 100,  //单位为分
            'desc' => '企业付款',
            // 'spbill_create_ip' => '192.168.0.1',  //发起交易的IP地址
        ];

        $Wechat = new Application($options);
        $merchantPay = $Wechat->merchant_pay;
        $result = $merchantPay->send($merchantPayData);
        return $result;
    }

    public function test(){
        \think\Log::write(123123123,'callbackParam');

    }

    //平台的支付回调
    public function notify0()
    {
        $pay = Pay::wechat($this->config);
        \think\Db::startTrans();
        try {
            $data = $pay->verify(); // 是的,验签就这么简单!

            $find = Cm0order::where('order_no', $data['out_trade_no'])->find();
            $param['shi_pay'] = $data['total_fee'] * 0.01;
            $param['liu_no'] = $data['transaction_id'];
            $param['state'] = 1;
            $param['back'] = 1;
            Cm0order::where('order_no', $data['out_trade_no'])->update($param);
//            $user = \app\api\model\User::where('id', $find['user_id'])->find();//把下单用户翻出来

            //普通商品
            $pu_arr = ['0', '3'];
            if (in_array($find['type'], $pu_arr)) {
                $data99 = unserialize($find['data']);
                $good = $data99['order_good'];

                //两种销量
                foreach ($good as $k => $v) {
                    Litestoregoods::where('goods_id', $v['goods_id'])->setInc('sales_initial', $v['num']);//初始销量+
                    Litestoregoods::where('goods_id', $v['goods_id'])->setInc('sales_actual', $v['num']);//实际销量+

                    if ($find['s4_qiang_id'] == 1) {
                        S4Qiang::where('goods_id', $v['goods_id'])->setDec('sheng_num', $v['num']);
                    }
                }

                //清楚购物车数据
                if (isset($data99['gou_arr']) && !empty($data99['gou_arr'])) {
                    Cm0gou::where('id', 'in', $data99['gou_arr'])->delete();
                }
            }


            //点赞商品
            if ($find['type'] == '1') {
                //两种销量+num
                $good = unserialize($find['data']);
                Cm0zan0good::where('id', $good['good']['id'])->setInc('sales_initial', $good['good']['gou_num']);
                Cm0zan0good::where('id', $good['good']['id'])->setInc('sales_actual', $good['good']['gou_num']);

                //改变购买状态
                Cm0zan0user::where('goods_id', $good['good']['id'])
                    ->where('user_id', $find['user_id'])
                    ->update(['buy' => 1]);
            }


            //团购商品
            $arr_tuan = ['2', '4'];
            if (in_array($find['type'], $arr_tuan)) {
                $data99 = unserialize($find['data']);
                $good = $data99['order_good'];

                //两种销量
                foreach ($good as $k => $v) {
                    Litestoregoods::where('goods_id', $v['goods_id'])->setInc('sales_initial', $v['num']);//初始销量+
                    Litestoregoods::where('goods_id', $v['goods_id'])->setInc('sales_actual', $v['num']);//实际销量+
                }
                $this->notify0_tuan($find);//团购的其他逻辑
            }

            \think\Db::commit();
            Log::debug('Wechat notify', $data->all());
        } catch (\Exception $e) {
            \think\Log::write('支付回调异常','callbackParam');
            \think\Log::write($e->getMessage(),'callbackParams');
            \think\Db::rollback();
        }
        return $pay->success()->send();// laravel 框架中请直接
    }


    //创建团队 //添加成员
    private function notify0_tuan($find)
    {
        if ($find['tuan_id'] == 0) {
            $data99 = unserialize($find['data']);
            $good = $data99['order_good'];
            foreach ($good as $k => $v) {
                $tuan_num = Litestoregoods::where('goods_id', $v['goods_id'])->value('tuan_num');
                $res = OrderTuan::create([
                    'user_id' => $find['user_id'],
                    'goods_id' => $v['goods_id'],
                    'order_no' => $find['order_no'],
                    'num' => $tuan_num,
                ], true);
            }
            OrderTuanUser::create([
                'user_id' => $find['user_id'],
                'tuan_id' => $res['id'],
                'order_no' => $find['order_no'],
            ], true);
            $tuan_id = $res['id'];
        } else {
            OrderTuanUser::create([
                'user_id' => $find['user_id'],
                'tuan_id' => $find['tuan_id'],
                'order_no' => $find['order_no'],
            ], true);
            $tuan_id = $find['tuan_id'];
        }
        Cm0order::where('order_no', $find['order_no'])->update([
            'tuan_id' => $tuan_id,
        ]);
        $this->notify0_tuan1($tuan_id, $find['order_no']);
        $this->notify0_tuan2($tuan_id, $find['order_no']);
    }


    //改变团的状态
    private function notify0_tuan1($tuan_id, $order_no)
    {
        $tuan = OrderTuan::where('id', $tuan_id)->find();
        if ($tuan['state'] == 1) {
            return;
        }
        $num = OrderTuanUser::where('tuan_id', $tuan_id)->count();
        if ($num >= $tuan['num']) {
            OrderTuan::where('id', $tuan_id)->update([
                'state' => 1,
            ]);
            Cm0order::where('tuan_id', $tuan_id)->update([
                'tuan_state' => 2,
            ]);

            // 发送成团订阅消息
            $userlist = OrderTuanUser::where('tuan_id', $tuan_id)->select();
            $goods_id = Db::name('order_tuan')->where('id',$userlist[0]['tuan_id'])->value('goods_id');
            $goods = Db::name('litestore_goods')->where('goods_id',$goods_id)->find();
            $name4s = Db::name('cm0s4')->where('id',$goods['s4_id'])->value('name');
            $data = [
                'name10' => ['value'=>$goods['goods_name']], //商品名称
                'amount2' => ['value'=>'¥'.$goods['price']], //价格
                'number3' => ['value'=>$tuan['num']], //参团人数
                'name11' => ['value'=>'同放汽车'], //店家名称
                'thing5' => ['value'=>'拼团成功'], //温馨提示
            ];
            $token = $this->getAccessToken();
            foreach ($userlist as $key => $value){
                $openid = Db::name('third')->where('user_id',$value['user_id'])->value('openid');
                $this->sendmessage($data,$token,$openid);
            }
        }
    }


    //更新发起人
    private function notify0_tuan2($tuan_id, $order_no)
    {
        $tuan = OrderTuan::where('id', $tuan_id)->find();
        $nickname = \app\api\model\User::where('id', $tuan['user_id'])->value('nickname');
        Cm0order::where('order_no', $order_no)->update([
            'tuan_boss' => $nickname,
        ]);
    }

    //发送成团订阅消息
    protected function sendmessage($array,$access_token,$openid)
    {
        $data = [
            'touser' => $openid,
            'template_id' => 'Q_ecLnQbmEaiyVRYD7air0OVQnlF4daXW5WaXkx201U',
            'data' => $array,
        ];
        $data = json_encode($data);
        $http = Http::post('https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token,$data);
        return $http;
    }

    protected function getAccessToken()
    {
        $config = get_addon_config('third');
        $config = $config['wechat'];
        $params = [
            'grant_type' => 'client_credential',
            'appid' => $config['app_id'],
            'secret' => $config['app_secret']
        ];
        $http = Http::get('https://api.weixin.qq.com/cgi-bin/token',$params);
        $http = json_decode($http,true);
        return $http['access_token'];
    }

    //4s店的支付回调
    public function notify1()
    {
        $pay = Pay::wechat($this->config);
        \think\Db::startTrans();
        try {
            $data = $pay->verify(); // 是的,验签就这么简单!

            $find = Cm0s40order::where('order_no', $data['out_trade_no'])->find();
            $param['shi_pay'] = $data['total_fee'] * 0.01;
            $param['liu_no'] = $data['transaction_id'];
            $param['state'] = 1;
            $param['back'] = 1;
            Cm0s40order::where('order_no', $data['out_trade_no'])->update($param);

            $data99 = unserialize($find['data']);
            //保养预约
            if ($find['type'] == 0) {
                //无逻辑
            }
            //直购保养
            if ($find['type'] == 1) {
                //两种销量
                $id_arr = array_column($data99['bao'], 'id');
                Cm0bao0good::where('id', 'in', $id_arr)->setInc('sales_initial');//+1就行
                Cm0bao0good::where('id', 'in', $id_arr)->setInc('sales_actual');//+1就行
                //返还积分
                $dong = unserialize($find['dong']);
                $back_fen = 0;

                $dong['ping_fen'] = $dong['ping_fen'] ?? 0;

                if ($dong['ping_fen'] == 0 && empty($find['red'])) {//冻结积分0则可以反
                    foreach ($data99['bao'] as $k => $v) {
                        $back_fen += $v['fen'];
                    }
                    $back_fen = round($back_fen, 2);
                    s4userFen($find['s4_id'], $find['user_id'], $back_fen, '购买保养', "保养出售,订单号:{$find['order_no']}");
                }
            }

            //购车定金
            if ($find['type'] == 2) {

            }
            //购买配件
            if ($find['type'] == 3) {
                //两种销量
                foreach ($data99['order_good'] as $k => $v) {
                    Cm0pei0good::where('id', $v['good']['id'])->setInc('sales_initial', $v['good']['gou_num']);
                    Cm0pei0good::where('id', $v['good']['id'])->setInc('sales_actual', $v['good']['gou_num']);
                }
                //清购物车数据
                $gou_arr = $data99['gou_arr'] ?? [];
                Cm0s40gou::where('id', 'in', $gou_arr)->delete();
            }

            \think\Db::commit();
            Log::debug('Wechat notify', $data->all());
        } catch (\Exception $e) {
            \think\Db::rollback();
        }
        return $pay->success()->send();// laravel 框架中请直接
    }


    //4s店,保养支付尾款
    public function notify3()
    {
        $pay = Pay::wechat($this->config);
        \think\Db::startTrans();
        try {
            $data = $pay->verify(); // 是的,验签就这么简单!

            $find = Cm0s40order::where('order_end', $data['out_trade_no'])->find();
            $data99 = unserialize($find['data']);
            $data99['end']['shi_pay'] = $data['total_fee'] * 0.01;
            $data99['end']['liu_no'] = $data['transaction_id'];
            Cm0s40order::where('order_end', $data['out_trade_no'])->update([
                'state_end' => 1,
                'data' => serialize($data99),
                'type' => 5,//更改状态
            ]);

            //两种销量
            $id_arr = array_column($data99['bao'], 'id');
            Cm0bao0good::where('id', 'in', $id_arr)->setInc('sales_initial');//+1就行
            Cm0bao0good::where('id', 'in', $id_arr)->setInc('sales_actual');//+1就行
            //返还积分
            $dong = unserialize($find['dong']);
            $back_fen = 0;

            $dong['ping_fen'] = $dong['ping_fen'] ?? 0;
            if ($dong['ping_fen'] == 0 && empty($find['red'])) {//冻结积分0则可以反
                foreach ($data99['bao'] as $k => $v) {
                    $back_fen += $v['fen'];
                }
                $back_fen = round($back_fen, 2);
                s4userFen($find['s4_id'], $find['user_id'], $back_fen, '购买保养', "保养尾款结算完成,订单号:{$find['order_no']}");
            }

            \think\Db::commit();
            Log::debug('Wechat notify', $data->all());
        } catch (\Exception $e) {
            \think\Db::rollback();
        }
        return $pay->success()->send();// laravel 框架中请直接
    }


    //4S店活动报名支付
    public function notify5()
    {
        $pay = Pay::wechat($this->config);

        \think\Db::startTrans();
        try {
            $data = $pay->verify(); // 是的,验签就这么简单!

            $find = Cm0s40active0bao::where('order_no', $data['out_trade_no'])->find();
            $activeId=$find['active_id'];
            //=========发送活动邀请红包
            $this->sendActiveBonus($activeId,$find['inviter_id'],$find['user_id'],2);

            $param['shi_pay'] = $data['total_fee'] * 0.01;
            $param['liu_no'] = $data['transaction_id'];
            $param['state'] = 1;
            Cm0s40active0bao::where('order_no', $data['out_trade_no'])->update($param);
//            $data99 = unserialize($find['data']);
            $user = \app\api\model\User::where('id', $find['user_id'])->find();
            //扣除积分
            if ($find['di_pay'] != 0) {
                $money = round($user['ping_money'] - $find['di_pay'], 2);
                $fen = moneyFen($money);
                Cm0che::where('id', $user['che_id'])->update(['ping_fen' => $fen]);//放下面
                fenLog('活动报名费', $find['user_id'], $fen * (-1));
            }

            //========
            $msg = "亲,您已成功预约活动,请在活动有效期内使用相关权益。如价格或服务不满意可申请无条件退款或致电:400-0513-066";
            sendMsg($find['phone'], $msg);

            \think\Db::commit();
            Log::debug('Wechat notify', $data->all());
        } catch (\Exception $e) {
            \think\Log::write("写入Wechat notify日志错误".$e->getMessage());
            \think\Db::rollback();
        }

        $active = Db::name('cm0s40active2')->where('id',$find['active_id'])->find();
        $s4 = Db::name('cm0s4')->where('id',$active['s4_id'])->find();
        $data = [
            [
                'name' => $active['name'],
                'time' => $active['start_time'].' - '.$active['stop_time'],
                'business' => $s4['name'],
                'place' => $s4['address_detail'],
                'user_id' => $find['user_id'],
                'openid' => Db::name('third')->where('user_id',$find['user_id'])->value('openid'),
                'tips' => '',
                'createtime' => time(),
                'sendtime' => time()+(60*30),
                'type' =>1
            ],
            [
                'name' => $active['name'],
                'time' => $active['start_time'].' - '.$active['stop_time'],
                'business' => $s4['name'],
                'place' => $s4['address_detail'],
                'user_id' => $find['user_id'],
                'openid' => Db::name('third')->where('user_id',$find['user_id'])->value('openid'),
                'tips' => '',
                'createtime' => time(),
                'sendtime' => strtotime($active['stop_time']),
                'type' =>2
            ]
        ];
        Db::name('sendmessage')->insertAll($data);

        return $pay->success()->send();// laravel 框架中请直接
    }


    //被邀请人点击立即报名后 支付费用后可以 邀请人可以获得(固定还是怎么)红包

    //发送红包
    public function  transfer($order){
        $result = Pay::wechat($this->config)->transfer($order);
        $return_code = $result['return_code'] ?? '';
        if ($return_code == 'SUCCESS') {
            return true;
        } else {
            return false;
        }
    }


    //报名红包

    /**
     * 发放红包
     * @param int $activeId 活动id
     * @param int $inviterId 邀请者id
     * @param  $userId 被邀请人id
     * @param int $type 红包的类型 1进入红包2邀请红包
     * @return false
     */
    public function sendActiveBonus(int $activeId, int $inviterId,  $userId, int $type){

        \think\Log::init([
            'type' => 'File',
            'path' => LOG_PATH . 'pays/'
        ]);
        \think\Log::write("写入日志");
        \think\Log::write("相关数据:活动".$activeId."邀请者".$inviterId."被邀请者".$userId."类型".$type);


        //点击链接的是邀请人本人
        if ($inviterId == $userId) {
            \think\Log::write("点击链接的是邀请人本人".$inviterId);
            return false;
        }

        $cm0s40active2 = new Cm0s40active2();
        $isActive2 = $cm0s40active2
            ->where('start_time', '< time', time())
            ->where('stop_time', '> time', time())
            ->where('id', $activeId)->count();

        //活动已结束
        if ($isActive2 == 0) {
            return false;
        }

        $active2BonusModel = new Active2Bonus();
        $active2Bonus = $active2BonusModel
            ->where('cm0s40active2_id', $activeId)
            ->where('type', $type)
            ->find();

        //活动没有奖励
        if (!$active2Bonus) {
            return false;
        }

        $active2BonusUserModel = new Active2BonusUser();
        $isUser = $active2BonusUserModel
            ->where('invitee_id', $userId)  //被邀请人
            ->where('cm0s40active2_id', $activeId)  //活动
            ->where('type', $type)  //红包的类型
            ->count();

        //已领取过进入红包
        if ($isUser != 0) {
            return false;
        }

        //订单号
        $orderNo = uniqid();
        $inviterOpenId = Db::name('third')->where('user_id', $inviterId)->value('openid');

        if (empty($inviterOpenId)){
            return false;
        }

        //红包金额
        if ($active2Bonus['mold'] == 1) {
            $amount = $active2Bonus['price_max']*100;
        } else {
            $amount = rand($active2Bonus['price_min']*100, $active2Bonus['price_max']*100);
        }

        $desc=[1=>'邀请进入红包',2=>'报名红包'];
        //======================
        $order = [
            'partner_trade_no' => $orderNo,              //商户订单号
            'openid' => $inviterOpenId,                        //收款人的openid
            'check_name' => 'NO_CHECK',            //NO_CHECK:不校验真实姓名\FORCE_CHECK:强校验真实姓名
            // 're_user_name'=>'张三',              //check_name为 FORCE_CHECK 校验实名的时候必须提交
            'amount' => $amount,                       //企业付款金额,单位为分
            'desc' => $desc[$type],                  //付款说明
        ];

        try {
            //发送企业付款
            \think\Log::write('发送红包金额'.$amount);
            $result = Pay::wechat($this->config)->transfer($order);
            //===========================
            $return_code = $result['return_code'] ?? '';

            if ($return_code == 'SUCCESS') {//成功
                \think\Log::write('发送红包成功');
                $data['inviter_id'] = $inviterId;
                $data['cm0s40active2_id'] = $activeId;
                $data['order_no'] = $orderNo;
                $data['price'] = (float)$amount/100;
                $data['remark'] = $desc[$type];
                $data['invitee_id'] = $userId;
                $data['type'] = $type;
                $active2BonusUserModel->allowField(true)->save($data);
                \think\Log::write('微信企业付款成功,订单号:'.$orderNo.",转账金额:".$amount.",收款人openid:".$inviterOpenId);
                return true;
            }
        }catch (\Exception $exception){
            \think\Log::write('发送红包失败:'.$exception->getMessage());
        }

        return false;

    }


    //pc扫码支付
    public function pcScan($order = []){
            $order['total_fee'] = $order['total_fee'] * 100;
            $ip = request()->ip();
            $gs_ip = Cm0fw::where('kw', 'IP地址')->value('content');//公司IP
            if ($ip == $gs_ip) {
                $order['total_fee'] = '1';
            }
            $result = Pay::wechat($this->config)->scan($order);
            return $result;
    }

    //4S店活动报名支付pc
    public function notifyscan()
    {
        $pay = Pay::wechat($this->config);

        \think\Db::startTrans();
        try {
            $data = $pay->verify(); // 是的,验签就这么简单!

            $find = Cm0s40active0bao::where('order_no', $data['out_trade_no'])->find();

            $param['shi_pay'] = $data['total_fee'] * 0.01;
            $param['liu_no'] = $data['transaction_id'];
            $param['state'] = 1;
            Cm0s40active0bao::where('order_no', $data['out_trade_no'])->update($param);
//            $data99 = unserialize($find['data']);

            //========
            $msg = "亲,您已成功预约活动,请在活动有效期内使用相关权益。如价格或服务不满意可申请无条件退款或致电:400-0513-066";
            sendMsg($find['phone'], $msg);

            \think\Db::commit();
            Log::debug('Wechat notify', $data->all());
        } catch (\Exception $e) {
            \think\Log::write("写入Wechat notify日志错误".$e->getMessage());
            \think\Db::rollback();
        }
        return $pay->success()->send();// laravel 框架中请直接
    }







}