Notify.php 5.2 KB
<?php


namespace app\api\controller;


use addons\epay\library\Service;
use app\common\controller\Api;
use Exception;
use think\Db;

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

    /**
     * 订单支付回调
     */
    public function orderNotify()
    {
        $paytype = $this->request->param('type');
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
            return '签名错误';
        }
        $data = $pay->verify();
        if (!is_array($data)) $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();
        $buymodel = new \app\api\model\UserBuylist();
        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();
            // 减少库存
            $sales_actual = 0;
            $goods_ids = [];
            $list = $goodsmodel->where('order_id',$order['id'])->select();
            foreach ($list as $key => $value){
                $goods_ids[] = $value['goods_id'];
                $sales_actual+=$value['total_num'];
                if ($value['deduct_stock_type'] == 20){
                    $skumodel->where('goods_spec_id',$value['goods_spec_id'])->setDec('stock_num',$value['total_num']);
                }
            }
            // 增加销量
            $goods_model->whereIn('goods_id',$goods_ids)->setInc('sales_actual',$sales_actual);
            // 判断是否邀请成功
            $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();
                $coupon = Db::name('coupon')->find();
                if ($coupon['endtime'] > time()){
                    $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);
                }
            }
            // 加入我常买
            $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);
        } catch (Exception $e) {
        }
        return $pay->success()->send();
    }


    /**
     * 订单退款回调
     */
    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();
    }
}