Notify.php 5.0 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 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'];

            // 处理订单逻辑
            $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()]);
            }
        } catch (Exception $e) {
        }
        echo $pay->success();
    }

    /**
     * 密卷
     */
    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'];

            // 处理订单逻辑
            $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()]);
            }
        } catch (Exception $e) {
        }
        echo $pay->success();
    }

    /**
     * 积分
     */
    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'];

            // 处理订单逻辑
            $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'],'充值积分');
            }
        } catch (Exception $e) {
        }
        echo $pay->success();
    }

    /**
     * 七牛云通知回调
     * @ApiInternal
     */
    public function notifyQiniu()
    {
        $config = get_addon_config('qiniu');
        $auth = new Auth($config['app_key'], $config['secret_key']);
        $contentType = 'application/x-www-form-urlencoded';
        $authorization = isset($_SERVER['HTTP_AUTHORIZATION']) ? $_SERVER['HTTP_AUTHORIZATION'] : '';
        if (!$authorization && function_exists('apache_request_headers')) {
            $headers = apache_request_headers();
            $authorization = isset($headers['Authorization']) ? $headers['Authorization'] : '';
        }

        $url = $this->request->root(true) . '/mobile/notify/notifyQiniu';
        $body = file_get_contents('php://input');
        $ret = $auth->verifyCallback($contentType, $authorization, $url, $body);
        if ($ret) {
            parse_str($body, $arr);
            $admin_id = isset($arr['admin']) ? $arr['admin'] : 0;
            $user_id = isset($arr['user']) ? $arr['user'] : 0;
            $imageInfo = json_decode($arr['imageInfo'], true);
            $params = array(
                'admin_id'    => (int)$admin_id,
                'user_id'     => (int)$user_id,
                'filesize'    => $arr['filesize'],
                'imagewidth'  => isset($imageInfo['width']) ? $imageInfo['width'] : 0,
                'imageheight' => isset($imageInfo['height']) ? $imageInfo['height'] : 0,
                'imagetype'   => isset($imageInfo['format']) ? $imageInfo['format'] : '',
                'imageframes' => 1,
                'mimetype'    => "image/" . (isset($imageInfo['format']) ? $imageInfo['format'] : ''),
                'extparam'    => '',
                'url'         => '/' . $arr['key'],
                'uploadtime'  => time(),
                'storage'     => 'qiniu'
            );
            Attachment::create($params);
            return json(['ret' => 'success', 'code' => 1, 'data' => ['url' => $params['url']]]);
        }
        return json(['ret' => 'failed']);
    }
}