用户下单6

Laravel框架
459
0
0
2022-11-12

用户下单6

<?php
namespace App\Services\Order;

use App\Contracts\Order\ReturnContracts;
use App\Models\Order;

class ReturnService implements ReturnContracts
{
    public function applyRefund(Order $order, $data)
    {
        if (!$order->paid_at){
            return ['code' => 400,'error' => '该订单未支付,不可退款'];
        }

        if ($order->refund_status != 0){
            return ['code' => 400,'error' => '该订单已经申请过退款,请勿重复申请'];
        }

        $extra = $order->extra ? :[];

        $extra['refund_reason'] = $data["reason"];

        $order->update([
            'refund_status' => 1,
            'extra'         => $extra
        ]);

        return $order;

    }

    public function handleRefund(Order $order, $data)
    {
        if ($order->refund_status != 1){
            return ['code' => 400,'error' => '订单状态不正确'];
        }

        if ($data['agree']){
            $extra = $order->extra ?: [];
            unset($extra['refund_disagree_reason']);
            $order->update([
                'extra' => $extra
            ]);

            $order = $this->_RefundOrder($order);

            return $order;
        }else{
            //拒绝退款 
            $extra = $order->extra ?: [];
            $extra['refund_disagree_reason'] = $data["reason"];

            $order->update([
                'refund_status' => 0,
                'extra' => $extra
            ]);

            return $order;
        }
    }

    public function _RefundOrder(Order $order)
    {
        //ERROR_GATEWAY: Get Alipay API Error:Business Failed - ACQ.TRADE_NOT_EXIST 
        //这个问题是支付宝中没有找到我们的订单编号 
        switch ($order->payment_method){
            case 'wechat':
                break;
            case 'alipay':
                $refundNo = Order::findAvailableNo();

                $ret = app('alipay')->refund([
                    'out_trade_no' => $order->no,
                    'refund_amount' => $order->total_amount,
                    'out_request_no' => $refundNo
                ]);

                if ($ret->sub_code){
                    $extra = $order->extra;
                    $extra['refund_failed_code'] = $ret->sub_code;
                    $order->update([
                        'refund_no' => $refundNo,
                        'refund_status' => 2,
                        'extra' => $extra
                    ]);
                }else{
                    $order->update([
                        'refund_no' => $refundNo,
                        'refund_status' => 1,
                    ]);
                }
                break;
            default:
                return [
                    'code' => 400,
                    'error' => '未知的订单支付方式: '.$order->payment_method,
                ];
                break;
        }
        return $order;
    }
}

用户下单6

用户下单6

用户下单6