Files
hyperf_service/app/Service/ServiceTrait/Api/RefundOrderTrait.php
2025-03-26 16:55:31 +08:00

89 lines
3.5 KiB
PHP

<?php
namespace App\Service\ServiceTrait\Api;
use App\Constants\Common\OrderCode;
use App\Constants\Common\PayCode;
use App\Constants\Common\RefundCode;
use App\Exception\ErrException;
use App\Model\OrderGood;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
trait RefundOrderTrait
{
/**
* @return void
*/
protected function checkRefundOrder(): void
{
$this->refundInfo = $this->refundOrderModel->getInfoByOrderSnoAndType($this->orderSno,$this->payType);
$this->orderInfo = $this->orderModel->getInfoById($this->refundInfo->order_id);
$this->payInfo = $this->payOrderModel->getInfoByOrderIdAndTypeAndRType($this->orderInfo->id,$this->orderType,$this->payType);
if (empty($this->orderInfo) || empty($this->payInfo) || empty($this->refundInfo)) throw new ErrException('订单不存在');
if ($this->refundInfo->refund_status != RefundCode::WAIT_BY_PAY_TOOL) throw new ErrException('订单已退款成功');
if ($this->refundInfo->refund_type != PayCode::WECHAT_PAY) throw new ErrException('订单退款渠道错误');
}
/**
* @return void
*/
protected function manageRefundOrder(): void
{
try {
$this->refundInfo->refund_status = RefundCode::REFUND_SUCCESS;
$this->refundInfo->refund_time = date('Y-m-d H:i:s');
$this->refundInfo->wx_transaction_id = $this->callbackData['transaction_id'];
$this->refundInfo->notify_json = json_encode($this->callbackData);
$this->refundInfo->remark = '退款成功';
if (!$this->refundInfo->save()) throw new Exception('更新退款订单失败');
}catch (Exception $e) {
// $this->log->error('Function:RefundFinish:'.$e->getMessage().':orderId:'.$this->orderInfo->id);
throw new ErrException('Function:RefundFinish:'.$e->getMessage().':orderId:'.$this->orderInfo->id);
}
}
/**
* @var OrderGood
*/
#[Inject]
protected OrderGood $orderGoodModel;
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function manageOrderByRefund(): void
{
try {
$isRefundMoney = $this->refundOrderModel->getSuccessMoneyByOrderId($this->refundInfo->order_id,$this->refundInfo->order_type);
if ($isRefundMoney <= 0) throw new Exception('订单退款金额错误');
if ($isRefundMoney == $this->payInfo->pay_money) {
$this->orderInfo->status = OrderCode::FINISH_REFUND;
} else {
$this->orderInfo->status = OrderCode::UNCOMPLETED_REFUND;
}
if (!$this->orderInfo->save()) throw new Exception('更新退款订单失败');
match ($this->refundInfo->type) {
RefundCode::FULL_GOOD_REFUND => $this->orderGoodModel->where('order_id',$this->refundInfo->order_id)->update(['refund_status' => OrderCode::FINISH_REFUND]),
RefundCode::PARTIAL_GOOD_REFUND => $this->orderGoodModel->whereIn('id',json_decode($this->refundInfo->good_ids,true))->update(['refund_status' => OrderCode::FINISH_REFUND]),
};
}catch (Exception $e) {
$this->log->error(__CLASS__.':Function:manageGoodOrder:'.$e->getMessage().':orderId:'.$this->orderInfo->id);
throw new ErrException('Function:RefundFinish:'.$e->getMessage().':orderId:'.$this->orderInfo->id);
}
}
}