审查视图

application/api/controller/Pay.php 37.4 KB
郭盛 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace app\api\controller;

use app\common\controller\Api;
use EasyWeChat\Factory;
use function GuzzleHttp\Promise\inspect;
use think\Db;
use app\admin\model\User;
use think\Log;
use fast\Http;
use think\Validate;
use Exception;
use function EasyWeChat\Kernel\Support\generate_sign;
郭盛 authored
15
use app\nsms\nsms;
郭盛 authored
16 17 18 19 20
/**
 * 支付接口
 */
class Pay extends Api
{
郭盛 authored
21 22
    protected $noNeedLogin = ['payOrderNotify','vippayOrderNotify','picOrderNotify','videoOrderNotify'];
    protected $noNeedRight = ['*'];
郭盛 authored
23 24 25 26 27 28 29 30 31
    protected $uid = '';//token存贮uid
    protected $order_status = [];//订单状态
    public function _initialize()
    {
        parent::_initialize();
        $this->uid = $this->auth->getUserId();
    }

    /**
32
     * @ApiTitle    (充值支付)
郭盛 authored
33 34 35 36 37
     * @ApiSummary  (充值支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/pay/pay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
38
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
郭盛 authored
39 40 41
     *
     */
    public function pay(){
42
        $rule = config('verify.path');
郭盛 authored
43
        if($this->request->isPost()){
44 45
            $order_id = $this->request->post('order_id');
            $openid = $this->getopenid();
郭盛 authored
46 47 48
            if(empty($openid)){
                $this->error(['code'=>2,'msg'=>'openid不能为空']);
            }
49 50 51 52
            $data = Db::name('toporder')->where('id',$order_id)->find();
            if($data){
                if($data['status'] == 2){
                    $this->error('该订单已支付过了');
郭盛 authored
53 54 55 56 57 58 59 60
                }else{
                    //创建支付对象
                    $config = config('verify.wx_pay');
                    $app = Factory::payment($config);
                    $result = $app->order->unify([
                        'body' => '山东航拍',
                        'out_trade_no' => $data['num'],//支付订单号
                        'total_fee' => $data['money']*100,//单位分
郭盛 authored
61
                        'notify_url' => 'http://areial.w.brotop.cn/api/pay/payOrderNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
郭盛 authored
62
                        'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
郭盛 authored
63
                        'openid' => $openid['openid'],
郭盛 authored
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
                    ]);
                    if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
                        $params = [
                            'appId'     => $config['app_id'],
                            'timeStamp' => time(),
                            'nonceStr'  => $result['nonce_str'], // 统一下单返回的随机字符串
                            'package'   => 'prepay_id='.$result['prepay_id'], // 统一下单Id
                            'signType'  => 'MD5', // 签名方法
                        ];
                        // 注意这里用的是商户平台的Key进行二次签名
                        $params['paySign'] = generate_sign($params, $config['key']);
                        $this->success('成功',$params);
                    }
                    $this->error($result['err_code_des']);
                }
            }else{
                $this->error('无效的订单');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
郭盛 authored
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
     * @ApiTitle    (开通会员支付)
     * @ApiSummary  (开通会员支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/pay/vippay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     *
     */
    public function vippay(){
        $rule = config('verify.path');
        if($this->request->isPost()){
            $order_id = $this->request->post('order_id');
            $openid = $this->getopenid();
            if(empty($openid)){
                $this->error(['code'=>2,'msg'=>'openid不能为空']);
            }
            $data = Db::name('viporder')->where('id',$order_id)->find();
            if($data){
                if($data['status'] == 2){
                    $this->error('该订单已支付过了');
                }else{
                    //创建支付对象
                    $config = config('verify.wx_pay');
                    $app = Factory::payment($config);
                    $result = $app->order->unify([
                        'body' => '山东航拍',
                        'out_trade_no' => $data['num'],//支付订单号
                        'total_fee' => $data['money']*100,//单位分
郭盛 authored
117
                        'notify_url' => 'http://areial.w.brotop.cn/api/pay/vippayOrderNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
郭盛 authored
118
                        'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
郭盛 authored
119
                        'openid' => $openid['openid'],
郭盛 authored
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
                    ]);
                    if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
                        $params = [
                            'appId'     => $config['app_id'],
                            'timeStamp' => time(),
                            'nonceStr'  => $result['nonce_str'], // 统一下单返回的随机字符串
                            'package'   => 'prepay_id='.$result['prepay_id'], // 统一下单Id
                            'signType'  => 'MD5', // 签名方法
                        ];
                        // 注意这里用的是商户平台的Key进行二次签名
                        $params['paySign'] = generate_sign($params, $config['key']);
                        $this->success('成功',$params);
                    }
                    $this->error($result['err_code_des']);
                }
            }else{
                $this->error('无效的订单');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
郭盛 authored
144 145 146 147 148 149 150
     * @ApiTitle    (图片购买支付)
     * @ApiSummary  (图片购买支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/pay/picpay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
郭盛 authored
151
     * @ApiParams   (name="type", type="inter", required=true, description="支付方式 1 为余额支付 2为微信支付")
郭盛 authored
152 153 154 155 156 157
     *
     */
    public function picpay(){
        $rule = config('verify.path');
        if($this->request->isPost()){
            $order_id = $this->request->post('order_id');
郭盛 authored
158 159 160 161
            $type = $this->request->post('type');
            if(empty($type)){
                $this->error(['code'=>2,'msg'=>'缺少必要参数']);
            }
郭盛 authored
162 163 164 165 166
            $openid = $this->getopenid();
            if(empty($openid)){
                $this->error(['code'=>2,'msg'=>'openid不能为空']);
            }
            $data = Db::name('order')->where('id',$order_id)->find();
郭盛 authored
167
            $pic_id = explode(',',$data['pic_id']);
郭盛 authored
168
            $video_id = unserialize($data['video_id']);
郭盛 authored
169 170 171 172 173

            $user = Db::name('user')->where('id',$data['user_id'])->find();
            $mobile = $user['mobile'];
            $str = "【仁甲看见】";
郭盛 authored
174 175 176 177 178
            if($type == 1){
                if($data){
                    if($data['status'] == 2){
                        $this->error('该订单已支付过了');
                    }else{
郭盛 authored
179
                        //查看该用户的信息
郭盛 authored
180 181 182
                        $res = Db::name('user')
                            ->where('id',$data['user_id'])
                            ->find();
郭盛 authored
183
                        //判断总价是否正确
郭盛 authored
184 185 186 187
                        if($data['total'] > $res['money']){
                            $this->error(['code'=>2,'msg'=>'余额不足,请充值后重试']);
                        }
                        $money = $res['money'] - $data['total'];
郭盛 authored
188
郭盛 authored
189 190 191
                        // 启动事务
                        Db::startTrans();
                        try{
郭盛 authored
192
                            if(!empty($pic_id)){
郭盛 authored
193 194 195 196 197 198
                                //将购买次数加1
                                foreach ($pic_id as $key => $v){
                                    $pic = Db::name('pic')->field('id,buy_num')->where('id',$v)->find();
                                    Db::name('pic')->where('id',$pic['id'])->update(['buy_num'=>$pic['buy_num']+1]);
                                }
                                //拼接发送短信的内容
郭盛 authored
199 200 201 202 203 204
                                foreach ($pic_id as $key => $v){
                                    $pic = Db::name('pic')->field('id,title,url,code')->where('id',$v)->find();
                                    $str .= "图片标题:$pic[title],云盘地址:$pic[url],提取码:$pic[code],";
                                }
                            }
                            if(!empty($video_id)){
郭盛 authored
205 206 207 208 209 210
                                //将购买次数加1
                                foreach ($video_id as $key => $val){
                                    $video = Db::name('video')->field('id,buy_num')->where('id',$val['id'])->find();
                                    Db::name('video')->where('id',$video['id'])->update(['buy_num'=>$video['buy_num']+1]);
                                }
                                //判断视频的属性
郭盛 authored
211 212 213 214 215 216 217 218 219
                                foreach ($video_id as &$val){
                                    if($val['attr'] == 1){
                                        $val['attr'] = '2k';
                                    }elseif ($val['attr'] == 2){
                                        $val['attr'] = '4k';
                                    }else{
                                        $val['attr'] = '8k';
                                    }
                                }
郭盛 authored
220
                                //拼接发送短信的内容
郭盛 authored
221 222 223 224 225 226 227 228 229 230 231 232 233
                                foreach ($video_id as $k=>$v){
                                    if($v['attr'] == '4k'){
                                        $video = Db::name('video')->field('id,title,four_url,four_code')->where('id',$v['id'])->find();
                                        $str .= "视频标题:$video[title],云盘地址:$video[four_url],提取码:$video[four_code]";
                                    }elseif ($v['attr'] == '2k'){
                                        $video = Db::name('video')->field('id,title,two_url,two_code')->where('id',$v['id'])->find();
                                        $str .= "视频标题:$video[title],云盘地址:$video[two_url],提取码:$video[two_code]";
                                    }elseif ($v['attr'] == '8k'){
                                        $video = Db::name('video')->field('id,title,eight_url,eight_code')->where('id',$v['id'])->find();
                                        $str .= "视频标题:$video[title],云盘地址:$video[eight_url],提取码:$video[eight_code]";
                                    }
                                }
                            }
郭盛 authored
234
                            //发送短信
郭盛 authored
235 236 237
                            if(!empty($mobile)){
                                $result = $this->sms($mobile,$str);
                            }
郭盛 authored
238
郭盛 authored
239 240 241 242 243 244 245 246 247 248 249
                            $arr = Db::name('user')->where('id',$data['user_id'])->update(['money'=>$money]);
                            $res = Db::name('order')->where('id',$order_id)->update(['status'=>2,'paytime'=>time()]);
                            if($res && $arr){
                                true;
                            }
                            // 提交事务
                            Db::commit();
                        } catch (\Exception $e) {
                            dump($e->getMessage());
                            // 回滚事务
                            Db::rollback();
郭盛 authored
250
                            $this->error('购买失败');
郭盛 authored
251
                        }
郭盛 authored
252
                        $this->success('SUCCESS','支付成功');
253
                    }
郭盛 authored
254 255
                }else{
                    $this->error('无效的订单');
郭盛 authored
256
                }
257
            }else{
郭盛 authored
258 259 260 261 262 263 264 265 266 267 268
                if($data){
                    if($data['status'] == 2){
                        $this->error('该订单已支付过了');
                    }else{
                        //创建支付对象
                        $config = config('verify.wx_pay');
                        $app = Factory::payment($config);
                        $result = $app->order->unify([
                            'body' => '山东航拍',
                            'out_trade_no' => $data['num'],//支付订单号
                            'total_fee' => $data['total']*100,//单位分
郭盛 authored
269
                            'notify_url' => 'http://areial.w.brotop.cn/api/pay/picOrderNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
郭盛 authored
270
                            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
郭盛 authored
271
                            'openid' => $openid['openid'],
郭盛 authored
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
                        ]);
                        if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
                            $params = [
                                'appId'     => $config['app_id'],
                                'timeStamp' => time(),
                                'nonceStr'  => $result['nonce_str'], // 统一下单返回的随机字符串
                                'package'   => 'prepay_id='.$result['prepay_id'], // 统一下单Id
                                'signType'  => 'MD5', // 签名方法
                            ];
                            // 注意这里用的是商户平台的Key进行二次签名
                            $params['paySign'] = generate_sign($params, $config['key']);
                            $this->success('成功',$params);
                        }
                        $this->error($result['err_code_des']);
                    }
                }else{
                    $this->error('无效的订单');
                }
郭盛 authored
290 291 292 293 294 295
            }
        }else{
            $this->error('请求方式错误');
        }
    }
郭盛 authored
296 297 298 299 300
    //发送短信
    public function sms($mobile,$content){
        $note = array(
            'content' 	=> $content,//短信内容
            'mobile' 	=> $mobile,//手机号码
郭盛 authored
301
            'productid' => '887361',//产品id
郭盛 authored
302 303 304 305 306 307 308 309 310 311 312 313
            'xh'		=> ''//小号
        );
        $result = $this->send_sms($note);
        return $result;
    }

    function send_sms($data){
        //todo 短信账号密码
//        require_once EXTEND_PATH . 'nsms/nsms.php';
        $url 		= "http://www.ztsms.cn/sendNSms.do";
        $username 	= 'hangpai';
        $password 	= 'Cxz307312';
郭盛 authored
314
        $sendAPI = new nsms($url, $username, $password);
郭盛 authored
315
        $sendAPI->data = $data;/*初始化数据包*/
郭盛 authored
316
        $return = $sendAPI->sendSMS('POST');
郭盛 authored
317 318 319
        return $return;
    }
郭盛 authored
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    /**
     * @ApiTitle    (视频购买支付)
     * @ApiSummary  (视频购买支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/pay/videopay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiParams   (name="order_id", type="inter", required=true, description="订单id")
     * @ApiParams   (name="type", type="inter", required=true, description="支付方式 1 为余额支付 2为微信支付")
     *
     */
    public function videopay(){
        $rule = config('verify.path');
        if($this->request->isPost()){
            $order_id = $this->request->post('order_id');
            $openid = $this->getopenid();
            if(empty($openid)){
                $this->error(['code'=>2,'msg'=>'openid不能为空']);
            }
郭盛 authored
339 340 341
            $data = Db::name('order')->where('id',$order_id)->find();
            $pic_id = explode(',',$data['pic_id']);
            $video_id = unserialize($data['video_id']);
郭盛 authored
342 343 344
            $user = Db::name('user')->where('id',$data['user_id'])->find();
            $mobile = $user['mobile'];
            $str = "【仁甲看见】";
郭盛 authored
345 346 347 348
            $type = $this->request->post('type');
            if(empty($type)){
                $this->error(['code'=>2,'msg'=>'缺少必要参数']);
            }
郭盛 authored
349
            $data = Db::name('order')->where('id',$order_id)->find();
郭盛 authored
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
            if($type == 1){
                if($data){
                    if($data['status'] == 2){
                        $this->error('该订单已支付过了');
                    }else{
                        $res = Db::name('user')
                            ->where('id',$data['user_id'])
                            ->find();
                        if($data['total'] > $res['money']){
                            $this->error(['code'=>2,'msg'=>'余额不足,请充值后重试']);
                        }
                        $money = $res['money'] - $data['total'];
                        // 启动事务
                        Db::startTrans();
                        try{
郭盛 authored
365
                            if(!empty($pic_id)){
郭盛 authored
366 367 368 369 370 371
                                //修改购买次数
                                foreach ($pic_id as $key => $v){
                                    $pic = Db::name('pic')->field('id,buy_num')->where('id',$v)->find();
                                    Db::name('pic')->where('id',$pic['id'])->update(['buy_num'=>$pic['buy_num']+1]);
                                }
                                //拼接发送短信内容
郭盛 authored
372 373 374 375 376 377
                                foreach ($pic_id as $key => $v){
                                    $pic = Db::name('pic')->field('id,title,url,code')->where('id',$v)->find();
                                    $str .= "图片标题:$pic[title],云盘地址:$pic[url],提取码:$pic[code],";
                                }
                            }
                            if(!empty($video_id)){
郭盛 authored
378 379 380 381 382 383
                                //修改购买次数
                                foreach ($video_id as $key => $val){
                                    $video = Db::name('video')->field('id,buy_num')->where('id',$val['id'])->find();
                                    Db::name('video')->where('id',$video['id'])->update(['buy_num'=>$video['buy_num']+1]);
                                }
                                //判断视频的属性
郭盛 authored
384 385 386 387 388 389 390 391 392
                                foreach ($video_id as &$val){
                                    if($val['attr'] == 1){
                                        $val['attr'] = '2k';
                                    }elseif ($val['attr'] == 2){
                                        $val['attr'] = '4k';
                                    }else{
                                        $val['attr'] = '8k';
                                    }
                                }
郭盛 authored
393
                                //拼接发送短信的内容
郭盛 authored
394 395 396 397 398 399 400 401 402 403 404 405 406
                                foreach ($video_id as $k=>$v){
                                    if($v['attr'] == '4k'){
                                        $video = Db::name('video')->field('id,title,four_url,four_code')->where('id',$v['id'])->find();
                                        $str .= "视频标题:$video[title],云盘地址:$video[four_url],提取码:$video[four_code]";
                                    }elseif ($v['attr'] == '2k'){
                                        $video = Db::name('video')->field('id,title,two_url,two_code')->where('id',$v['id'])->find();
                                        $str .= "视频标题:$video[title],云盘地址:$video[two_url],提取码:$video[two_code]";
                                    }elseif ($v['attr'] == '8k'){
                                        $video = Db::name('video')->field('id,title,eight_url,eight_code')->where('id',$v['id'])->find();
                                        $str .= "视频标题:$video[title],云盘地址:$video[eight_url],提取码:$video[eight_code]";
                                    }
                                }
                            }
郭盛 authored
407
                            //发送短信
郭盛 authored
408 409 410
                            if(!empty($mobile)){
                                $result = $this->sms($mobile,$str);
                            }
郭盛 authored
411
郭盛 authored
412
                            $arr = Db::name('user')->where('id',$data['user_id'])->update(['money'=>$money]);
郭盛 authored
413
                            $res = Db::name('order')->where('id',$order_id)->update(['status'=>2,'paytime'=>time()]);
郭盛 authored
414 415 416 417 418 419 420 421 422
                            if($res && $arr){
                                true;
                            }
                            // 提交事务
                            Db::commit();
                        } catch (\Exception $e) {
                            dump($e->getMessage());
                            // 回滚事务
                            Db::rollback();
郭盛 authored
423
                            $this->error('购买失败');
郭盛 authored
424
                        }
郭盛 authored
425
                        $this->success('SUCCESS','支付成功');
郭盛 authored
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
                    }
                }else{
                    $this->error('无效的订单');
                }
            }else{
                if($data){
                    if($data['status'] == 2){
                        $this->error('该订单已支付过了');
                    }else{
                        //创建支付对象
                        $config = config('verify.wx_pay');
                        $app = Factory::payment($config);
                        $result = $app->order->unify([
                            'body' => '山东航拍',
                            'out_trade_no' => $data['num'],//支付订单号
                            'total_fee' => $data['total']*100,//单位分
郭盛 authored
442
                            'notify_url' => 'http://areial.w.brotop.cn/api/pay/videoOrderNotify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
郭盛 authored
443
                            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
郭盛 authored
444
                            'openid' => $openid['openid'],
郭盛 authored
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
                        ]);
                        if($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS'){
                            $params = [
                                'appId'     => $config['app_id'],
                                'timeStamp' => time(),
                                'nonceStr'  => $result['nonce_str'], // 统一下单返回的随机字符串
                                'package'   => 'prepay_id='.$result['prepay_id'], // 统一下单Id
                                'signType'  => 'MD5', // 签名方法
                            ];
                            // 注意这里用的是商户平台的Key进行二次签名
                            $params['paySign'] = generate_sign($params, $config['key']);
                            $this->success('成功',$params);
                        }
                        $this->error($result['err_code_des']);
                    }
                }else{
                    $this->error('无效的订单');
                }
            }
郭盛 authored
464
郭盛 authored
465 466 467
        }else{
            $this->error('请求方式错误');
        }
郭盛 authored
468 469
    }
470
    //支付订单结果通知(无需调用)
郭盛 authored
471 472 473 474
    public function payOrderNotify(){
        $config = config('verify.wx_pay');
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function($message, $fail){
475 476 477
            $data = Db::name('toporder')
                ->where('num',$message['out_trade_no'])
                ->find();
郭盛 authored
478 479 480 481 482
            //return_code 表示通信状态
            if ($message['return_code'] === 'SUCCESS') {

                if ($message['result_code'] === 'SUCCESS') {
                    //支付成功,更新订单状态
483 484 485 486 487 488
                    $toporder['status'] = 2;
                    $toporder['paytime'] = time();
                    $money = $data['money'];
                    // 启动事务
                    Db::startTrans();
                    try{
郭盛 authored
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
                        //判断用户充值金额是否大于等于8000
                        if($data['money']>=8000){
                            //大于等于的情况
                            $res = Db::name('toporder')->where('num',$message['out_trade_no'])->update($toporder);
                            $arr = Db::name('user')->where('id',$data['user_id'])->find();
                            //判断用户是否已经是会员了
                            if($arr['is_vip'] == 0){
                                //已经是会员
                                $list['money'] = $arr['money'] + $money;
                                $info = Db::name('user')->where('id',$data['user_id'])->update(['money'=>$list['money']]);
                                if($res && $info){
                                    true;
                                }
                                // 提交事务
                                Db::commit();
                            }else{
                                //不是会员
                                $list['money'] = $arr['money'] + $money;
                                $list['is_vip'] = 1;
                                $info = Db::name('user')->where('id',$data['user_id'])->update($list);
                                if($res && $info){
                                    true;
                                }
                                // 提交事务
                                Db::commit();
                            }
515
                        }else{
郭盛 authored
516 517 518 519 520 521
                            //小于8000的情况
                            $res = Db::name('toporder')->where('num',$message['out_trade_no'])->update($toporder);
                            $arr = Db::name('user')->where('id',$data['user_id'])->find();
                            $list['money'] = $arr['money'] + $money;
                            $info = Db::name('user')->where('id',$data['user_id'])->update(['money'=>$list['money']]);
                            if($res && $info){
郭盛 authored
522 523 524 525 526 527 528 529 530
                                true;
                            }
                            // 提交事务
                            Db::commit();
                        }
                    } catch (\Exception $e) {
                        dump($e->getMessage());
                        // 回滚事务
                        Db::rollback();
郭盛 authored
531
                        $this->error(['code'=>2,'msg'=>'充值失败']);
郭盛 authored
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
                    }
                }else{
                    //支付失败
                    Db::name('toporder')->where('num',$message['out_trade_no'])->update(['status'=>1]);
                }
            }else{
                return $fail('通信失败,请稍后再通知我');
            }
            return true; // 返回处理完成
        });
        $response->send();
    }


    //支付开通会员结果通知(无需调用)
    public function vippayOrderNotify(){
        $config = config('verify.wx_pay');
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function($message, $fail){
            $data = Db::name('viporder')
                ->where('num',$message['out_trade_no'])
                ->find();
            //return_code 表示通信状态
            if ($message['return_code'] === 'SUCCESS') {

                if ($message['result_code'] === 'SUCCESS') {
                    //支付成功,更新订单状态
                    $toporder['status'] = 2;
                    $toporder['paytime'] = time();
                    $money = $data['money'];
                    // 启动事务
                    Db::startTrans();
                    try{
                        //判断用户充值金额是否大于等于8000
                        if($data['money']>=8000){
                            //大于等于的情况
郭盛 authored
568
                            $res = Db::name('viporder')->where('num',$message['out_trade_no'])->update($toporder);
郭盛 authored
569 570 571 572 573 574 575 576 577 578 579 580
                            $arr = Db::name('user')->where('id',$data['user_id'])->find();
                            //不是会员
                            $list['money'] = $arr['money'] + $money;
                            $list['is_vip'] = 1;
                            $info = Db::name('user')->where('id',$data['user_id'])->update($list);
                            if($res && $info){
                                true;
                            }
                            // 提交事务
                            Db::commit();
                        }else{
                            //小于8000的情况
郭盛 authored
581
                            $res = Db::name('viporder')->where('num',$message['out_trade_no'])->update($toporder);
郭盛 authored
582 583 584 585
                            $arr = Db::name('user')->where('id',$data['user_id'])->find();
                            $list['money'] = $arr['money'] + $money;
                            $info = Db::name('user')->where('id',$data['user_id'])->update(['money'=>$list['money']]);
                            if($res && $info){
郭盛 authored
586 587 588 589
                                true;
                            }
                            // 提交事务
                            Db::commit();
590 591 592 593 594
                        }
                    } catch (\Exception $e) {
                        dump($e->getMessage());
                        // 回滚事务
                        Db::rollback();
郭盛 authored
595
                        $this->error(['code'=>2,'msg'=>'开通会员失败']);
596
                    }
郭盛 authored
597
                    $this->success('SUCCESS','支付成功');
598
                }else{
郭盛 authored
599
                    //支付失败
郭盛 authored
600 601
                    Db::name('viporder')->where('num',$message['out_trade_no'])->update(['status'=>1]);
                    $this->error(['code'=>2,'msg'=>'支付失败']);
郭盛 authored
602
                }
603
            }else{
郭盛 authored
604 605 606 607 608 609
                return $fail('通信失败,请稍后再通知我');
            }
            return true; // 返回处理完成
        });
        $response->send();
    }
郭盛 authored
610 611 612 613 614 615 616 617 618 619 620 621

    //图片支付订单结果通知(无需调用)
    public function picOrderNotify(){
        $config = config('verify.wx_pay');
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function($message, $fail){
            //return_code 表示通信状态
            if ($message['return_code'] === 'SUCCESS') {
                if ($message['result_code'] === 'SUCCESS') {
                    //支付成功,更新订单状态
                    $toporder['status'] = 2;
                    $toporder['paytime'] = time();
郭盛 authored
622 623 624

                    $data = Db::name('order')->where('id',$message['out_trade_no'])->find();
                    $pic_id = explode(',',$data['pic_id']);
郭盛 authored
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
                    $video_id = unserialize($data['video_id']);
                    $user = Db::name('user')->where('id',$data['user_id'])->find();
                    $mobile = $user['mobile'];
                    $str = "【仁甲看见】";
                    if(!empty($pic_id)){
                        foreach ($pic_id as $key => $v){
                            $pic = Db::name('pic')->field('id,buy_num')->where('id',$v)->find();
                            Db::name('pic')->where('id',$pic['id'])->update(['buy_num'=>$pic['buy_num']+1]);
                        }
                        foreach ($pic_id as $key => $v){
                            $pic = Db::name('pic')->field('id,title,url,code')->where('id',$v)->find();
                            $str .= "图片标题:$pic[title],云盘地址:$pic[url],提取码:$pic[code],";
                        }
                    }
                    if(!empty($video_id)){
                        foreach ($video_id as &$val){
                            if($val['attr'] == 1){
                                $val['attr'] = '2k';
                            }elseif ($val['attr'] == 2){
                                $val['attr'] = '4k';
                            }else{
                                $val['attr'] = '8k';
                            }
                        }
                        foreach ($video_id as $k=>$v){
                            if($v['attr'] == '4k'){
                                $video = Db::name('video')->field('id,title,four_url,four_code')->where('id',$v['id'])->find();
                                $str .= "视频标题:$video[title],云盘地址:$video[four_url],提取码:$video[four_code]";
                            }elseif ($v['attr'] == '2k'){
                                $video = Db::name('video')->field('id,title,two_url,two_code')->where('id',$v['id'])->find();
                                $str .= "视频标题:$video[title],云盘地址:$video[two_url],提取码:$video[two_code]";
                            }elseif ($v['attr'] == '8k'){
                                $video = Db::name('video')->field('id,title,eight_url,eight_code')->where('id',$v['id'])->find();
                                $str .= "视频标题:$video[title],云盘地址:$video[eight_url],提取码:$video[eight_code]";
                            }
                        }
                    }
郭盛 authored
662
                    //发送短信
郭盛 authored
663 664
                    if(!empty($mobile)){
                        $result = $this->sms($mobile,$str);
郭盛 authored
665
                    }
郭盛 authored
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
                }else{
                    //支付失败
                    $toporder['status'] = 1;
                }
            }else{
                return $fail('通信失败,请稍后再通知我');
            }
            Db::name('order')->where('num',$message['out_trade_no'])->update($toporder);
            return true; // 返回处理完成
        });
        $response->send();
    }

    //视频支付订单结果通知(无需调用)
    public function videoOrderNotify(){
        $config = config('verify.wx_pay');
        $app = Factory::payment($config);
        $response = $app->handlePaidNotify(function($message, $fail){
            //return_code 表示通信状态
            if ($message['return_code'] === 'SUCCESS') {
                if ($message['result_code'] === 'SUCCESS') {
                    //支付成功,更新订单状态
                    $toporder['status'] = 2;
                    $toporder['paytime'] = time();
郭盛 authored
690
                    $data = Db::name('order')->where('id',$message['out_trade_no'])->find();
郭盛 authored
691
                    $pic_id = explode(',',$data['pic_id']);
郭盛 authored
692
                    $video_id = unserialize($data['video_id']);
郭盛 authored
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735

                    $user = Db::name('user')->where('id',$data['user_id'])->find();
                    $mobile = $user['mobile'];
                    $str = "【仁甲看见】";
                    if(!empty($pic_id)){
                        foreach ($pic_id as $key => $v){
                            $pic = Db::name('pic')->field('id,buy_num')->where('id',$v)->find();
                            Db::name('pic')->where('id',$pic['id'])->update(['buy_num'=>$pic['buy_num']+1]);
                        }
                        foreach ($pic_id as $key => $v){
                            $pic = Db::name('pic')->field('id,title,url,code')->where('id',$v)->find();
                            $str .= "图片标题:$pic[title],云盘地址:$pic[url],提取码:$pic[code],";
                        }
                    }
                    if(!empty($video_id)){
                        foreach ($video_id as $key => $val){
                            $video = Db::name('video')->field('id,buy_num')->where('id',$val['id'])->find();
                            Db::name('video')->where('id',$video['id'])->update(['buy_num'=>$video['buy_num']+1]);
                        }
                        foreach ($video_id as &$val){
                            if($val['attr'] == 1){
                                $val['attr'] = '2k';
                            }elseif ($val['attr'] == 2){
                                $val['attr'] = '4k';
                            }else{
                                $val['attr'] = '8k';
                            }
                        }
                        foreach ($video_id as $k=>$v){
                            if($v['attr'] == '4k'){
                                $video = Db::name('video')->field('id,title,four_url,four_code')->where('id',$v['id'])->find();
                                $str .= "视频标题:$video[title],云盘地址:$video[four_url],提取码:$video[four_code]";
                            }elseif ($v['attr'] == '2k'){
                                $video = Db::name('video')->field('id,title,two_url,two_code')->where('id',$v['id'])->find();
                                $str .= "视频标题:$video[title],云盘地址:$video[two_url],提取码:$video[two_code]";
                            }elseif ($v['attr'] == '8k'){
                                $video = Db::name('video')->field('id,title,eight_url,eight_code')->where('id',$v['id'])->find();
                                $str .= "视频标题:$video[title],云盘地址:$video[eight_url],提取码:$video[eight_code]";
                            }
                        }
                    }
                    if(!empty($mobile)){
                        $result = $this->sms($mobile,$str);
郭盛 authored
736
                    }
郭盛 authored
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
                }else{
                    //支付失败
                    $toporder['status'] = 1;
                }
            }else{
                return $fail('通信失败,请稍后再通知我');
            }
            Db::name('vorder')->where('num',$message['out_trade_no'])->update($toporder);
            return true; // 返回处理完成
        });
        $response->send();
    }

    //获取用户openid
    public function getopenid()
    {
        $user_id = $this->uid;
        $openid = Db::name('user')
            ->where('id',$user_id)
            ->field('openid')
            ->find();

        return $openid;
    }
郭盛 authored
761
}