Notify.php 9.1 KB
<?php
namespace app\mobile\controller;

use think\Db;
use app\common\controller\Api;
use app\mobile\model\CourseOrder;
use app\mobile\model\SecretOrder;
use app\mobile\model\ScoreOrder;
use app\mobile\model\PackageOrder;
use addons\qiniu\library\Auth;
use app\common\model\Attachment;

/**
 * 异步接口
 * @ApiInternal
 */
class Notify extends Api
{
	protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 课程---------------------------------------------------------------------
     */
    public function notifyCourse()
    {
        $paytype = $this->request->param('paytype');
        $pay = \addons\epay\library\Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        try {
            $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
            $out_trade_no = $data['out_trade_no'];
            // 处理订单
            $this->handleCourseOrder($out_trade_no,$payamount,$paytype);
        } catch (Exception $e) {
        }
        echo $pay->success();
    }

    /**
     * 课程-零元支付
     */
    public function notifyCourseZero($out_trade_no,$payamount,$paytype)
    {
        Db::startTrans();
        try {
            $this->handleCourseOrder($out_trade_no,$payamount,$paytype);
            Db::commit();
        } catch (\think\exception\PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
            return false;
        } catch (\think\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
            return false;
        }
        return true;
    }

    /**
     * 课程-处理订单
     */
    private function handleCourseOrder($out_trade_no,$payamount,$paytype){
        // 处理订单逻辑
        $order = CourseOrder::get(['order_sn'=>$out_trade_no,'pay_price'=>$payamount,'pay_type'=>$paytype]);
        if($order && $order['pay_status'] != '1'){
            $order->save(['pay_status'=>'1','pay_time'=>time()]);
            // 学习人数加1
            Db::name('mobile_course')->where('id',$order['course_id'])->setInc('study_num_real');
        }
        return true;
    }

    /**
     * 密卷-----------------------------------------------------------------------
     */
    public function notifySecret()
    {
        $paytype = $this->request->param('paytype');
        $pay = \addons\epay\library\Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        try {
            $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
            $out_trade_no = $data['out_trade_no'];

            //处理订单
            $this->handleSecretOrder($out_trade_no,$payamount,$paytype);
        } catch (Exception $e) {
        }
        echo $pay->success();
    }

    /**
     * 密卷-零元支付
     */
    public function notifySecretZero($out_trade_no,$payamount,$paytype)
    {
        Db::startTrans();
        try {
            $this->handleSecretOrder($out_trade_no,$payamount,$paytype);
            Db::commit();
        } catch (\think\exception\PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
            return false;
        } catch (\think\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
            return false;
        }
        return true;
    }

    /**
     * 密卷-处理订单
     */
    private function handleSecretOrder($out_trade_no,$payamount,$paytype){
        // 处理订单逻辑
        $order = SecretOrder::get(['order_sn'=>$out_trade_no,'pay_price'=>$payamount,'pay_type'=>$paytype]);
        if($order && $order['pay_status'] != '1'){
            $order->save(['pay_status'=>'1','pay_time'=>time()]);
            // 购买量加1
            Db::name('mobile_secret')->where('id',$order['course_id'])->setInc('buy_num_real');
        }
        return true;
    }

    /**
     * 积分----------------------------------------------------------------------
     */
    public function notifyScore()
    {
        $paytype = $this->request->param('paytype');
        $pay = \addons\epay\library\Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        try {
            $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
            $out_trade_no = $data['out_trade_no'];

            // 处理订单
            $this->handleScoreOrder($out_trade_no,$payamount,$paytype);
        } catch (Exception $e) {
        }
        echo $pay->success();
    }

    /**
     * 积分-零元支付
     */
    public function notifyScoreZero($out_trade_no,$payamount,$paytype)
    {
        Db::startTrans();
        try {
            $this->handleScoreOrder($out_trade_no,$payamount,$paytype);
            Db::commit();
        } catch (\think\exception\PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
            return false;
        } catch (\think\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
            return false;
        }
        return true;
    }

    /**
     * 积分-处理订单
     */
    private function handleScoreOrder($out_trade_no,$payamount,$paytype){
        // 处理订单逻辑
        $order = ScoreOrder::get(['order_sn'=>$out_trade_no,'pay_price'=>$payamount,'pay_type'=>$paytype]);
        if($order && $order['pay_status'] != '1'){
            $order->save(['pay_status'=>'1','pay_time'=>time()]);
            // 增加积分
            \app\common\model\User::score($order['score'],$order['user_id'],'充值积分');
        }
        return true;
    }

    /**
     * 企业套餐---------------------------------------------------------------------
     */
    public function notifyPackage()
    {
        $paytype = $this->request->param('paytype');
        $pay = \addons\epay\library\Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        try {
            $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
            $out_trade_no = $data['out_trade_no'];

            // 处理订单逻辑
            $this->handlePackageOrder($out_trade_no,$payamount,$paytype);
        } catch (Exception $e) {
        }
        echo $pay->success();
    }

    /**
     * 企业套餐-零元支付
     */
    public function notifyPackageZero($out_trade_no,$payamount,$paytype)
    {
        Db::startTrans();
        try {
            $this->handlePackageOrder($out_trade_no,$payamount,$paytype);
            Db::commit();
        } catch (\think\exception\PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
            return false;
        } catch (\think\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
            return false;
        }
        return true;
    }

    /**
     * 企业套餐-处理订单
     */
    private function handlePackageOrder($out_trade_no,$payamount,$paytype){
        // 处理订单逻辑
        $order = PackageOrder::get(['order_sn'=>$out_trade_no,'pay_price'=>$payamount,'pay_type'=>$paytype]);
        if($order && $order['pay_status'] != '1'){
            $order->save(['pay_status'=>'1','pay_time'=>time()]);
            foreach ($order['package'] as $v){
                // 购买量加1
                Db::name('mobile_package')->where('id',$v['package_id'])->setInc('buy_num_real');
            }
        }
        return true;
    }

    /**
     * 七牛云音频转码通知回调
     * @ApiInternal
     */
    public function notifyQiniuAvthumb()
    {
        if($this->request->isPost()) {
            $arr = $this->request->param();
            // 判断七牛云回调code是否为完成
            if(isset($arr['code']) && $arr['code'] == 0) {
                $url = '/' . $arr['items'][0]['key'];
                $info = Attachment::get(['url'=>$url]);
                if(!$info){
                    $params = array(
                        'admin_id'    => 0,
                        'user_id'     => 0,
                        'filesize'    => 0,
                        'imagewidth'  => 0,
                        'imageheight' => 0,
                        'imagetype'   => 'pcm',
                        'imageframes' => 0,
                        'mimetype'    => 'application/octet-stream',
                        'extparam'    => '',
                        'url'         => $url,
                        'uploadtime'  => time(),
                        'storage'     => 'qiniu'
                    );
                    Attachment::create($params);
                }
                return json(['ret' => 'success', 'code' => 1, 'data' => ['url' => $url]]);
            }
            return json(['ret' => 'failed']);
        }
    }
}