审查视图

application/api/controller/Notify.php 5.6 KB
李忠强 authored
1 2 3 4 5 6 7 8 9
<?php


namespace app\api\controller;


use addons\epay\library\Service;
use app\common\controller\Api;
use Exception;
何书鹏 authored
10
use GuzzleHttp\Client;
李忠强 authored
11
use think\Db;
李忠强 authored
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

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

    /**
     * 订单支付回调
     */
    public function orderNotify()
    {
        $paytype = $this->request->param('type');
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
李忠强 authored
29
            return '签名错误';
李忠强 authored
30 31
        }
        $data = $pay->verify();
李忠强 authored
32
        if (!is_array($data)) $data = json_decode($data,true);
李忠强 authored
33
        $model = new \app\api\model\Order();
李忠强 authored
34 35
        $goodsmodel = new \app\api\model\OrderGoods();
        $skumodel = new \app\api\model\GoodsSpec();
李忠强 authored
36
        $goods_model = new \app\api\model\Goods();
李忠强 authored
37
        $buymodel = new \app\api\model\UserBuylist();
李忠强 authored
38 39 40 41 42 43 44 45 46 47 48 49
        try {
            $out_trade_no = $data['out_trade_no'];
            $order = $model
                ->where('order_no',$out_trade_no)
                ->where('pay_status','10')
                ->where('order_status','10')
                ->find();
            if (!$order) $this->error('订单不存在');
            $order->pay_status = '20';
            $order->transaction_id = $data['transaction_id'];
            $order->pay_time = time();
            $order->isUpdate()->save();
李忠强 authored
50
            // 减少库存
李忠强 authored
51
            $sales_actual = 0;
李忠强 authored
52
            $goods_ids = [];
李忠强 authored
53 54
            $list = $goodsmodel->where('order_id',$order['id'])->select();
            foreach ($list as $key => $value){
李忠强 authored
55
                $goods_ids[] = $value['goods_id'];
李忠强 authored
56
                $sales_actual+=$value['total_num'];
李忠强 authored
57 58 59 60
                if ($value['deduct_stock_type'] == 20){
                    $skumodel->where('goods_spec_id',$value['goods_spec_id'])->setDec('stock_num',$value['total_num']);
                }
            }
李忠强 authored
61
            // 增加销量
李忠强 authored
62
            $goods_model->whereIn('goods_id',$goods_ids)->setInc('sales_actual',$sales_actual);
李忠强 authored
63 64 65 66 67
            // 判断是否邀请成功
            $user = \app\api\model\User::get($order['user_id']);
            if ($user['invite_user_id'] > 0 && $user['invite_status'] != 1) {
                $user->invite_status = '1';
                $user->isUpdate()->save();
何书鹏 authored
68
                $coupon = Db::name('coupon')->where('id',1)->find();
何书鹏 authored
69
                if ($coupon['endtime'] > time()){
李忠强 authored
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
                    $data = [];
                    $time = time();
                    for ($i=1;$i<=$coupon['send_number'];$i++){
                        $data[] = [
                            'user_id' => $user->invite_user_id,
                            'coupon_id' => $coupon['id'],
                            'name' => $coupon['name'],
                            'price' => $coupon['price'],
                            'full_price' => $coupon['full_price'],
                            'endtime' => $time+$coupon['days']*86400,
                            'createtime' => $time,
                        ];
                    }
                    Db::name('user_coupon')->insertAll($data);
                }
李忠强 authored
85
            }
李忠强 authored
86 87 88 89 90 91 92 93 94 95
            // 加入我常买
            $data = [];
            foreach ($list as $key => $value){
                $data[] = [
                    'user_id' => $order['user_id'],
                    'goods_id' => $value['goods_id'],
                    'sku_id' => $value['goods_spec_id'],
                ];
            }
            $buymodel->isUpdate(false)->saveAll($data);
何书鹏 authored
96 97 98
            // 给后台发送新订单提醒
            $client = new Client();
            $domain = config('socketio.domain');
99
            $http_port = config('socketio.http_port');
100
            $client->request('POST', $domain.":{$http_port}/", [
何书鹏 authored
101 102
                'form_params' => [
                    'type' => 'publish',
103
                    'content' => '您有新的订单,请注意查收',
何书鹏 authored
104 105 106
                    'to' => '',
                ]
            ]);
李忠强 authored
107 108
        } catch (Exception $e) {
        }
李忠强 authored
109
        return $pay->success()->send();
李忠强 authored
110
    }
李忠强 authored
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


    /**
     * 订单退款回调
     */
    public function refund()
    {
        $paytype = $this->request->param('type');
        $pay = Service::checkNotify($paytype,'refund');
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        $data = json_decode($data,true);
        $model = new \app\api\model\Order();
        $goodsmodel = new \app\api\model\OrderGoods();
        $skumodel = new \app\api\model\GoodsSpec();
        $goods_model = new \app\api\model\Goods();
        try {
            $out_trade_no = $data['out_refund_no'];
            $order = $model
                ->where('refund_no',$out_trade_no)
                ->where('pay_status','20')
                ->where('order_status','10')
                ->find();
            if (!$order) $this->error('订单不存在');
            $order->refund_id = $data['refund_id'];
            $order->refund_time = time();
            $order->order_status = '20';
            $order->isUpdate()->save();
            // 增加库存
            $sales_actual = 0;
            $list = $goodsmodel->where('order_id',$order['id'])->select();
            foreach ($list as $key => $value){
                $sales_actual+=$value['total_num'];
                if ($value['deduct_stock_type'] == 20){
                    $skumodel->where('goods_spec_id',$value['goods_spec_id'])->setInc('stock_num',$value['total_num']);
                }
            }
            // 减少销量
            $goods_model->setDec('sales_actual',$sales_actual);
            //你可以在此编写订单逻辑
        } catch (Exception $e) {
        }
        echo $pay->success()->send();
    }
李忠强 authored
158
}