Files
hyperf_service/app/Service/ServiceTrait/Api/RefundOrderTrait.php
2025-03-25 15:54:34 +08:00

77 lines
3.0 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 Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
trait RefundOrderTrait
{
/**
* @return void
*/
protected function checkRefundOrder(): void
{
$this->refundInfo = $this->refundOrderModel->getInfoByOrderSnoAndType($this->orderNo,self::PayType);
$this->orderInfo = $this->orderModel->getInfoById($this->refundInfo->order_id);
$this->payInfo = $this->payOrderModel->getInfoByOrderIdAndTypeAndRType($this->orderInfo->id,self::OrderType,self::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);
}
}
/**
* @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;
$this->orderInfo->is_refund_all = 1;//todo 感觉可以删除这个值
} else {
$this->orderInfo->status = OrderCode::UNCOMPLETED_REFUND;
}
if (!$this->orderInfo->save()) throw new Exception('更新退款订单失败');
}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);
}
}
}