feat : refund

This commit is contained in:
2025-02-24 15:06:26 +08:00
parent 6754d2969e
commit 27a6ad3e2e
15 changed files with 591 additions and 281 deletions

View File

@@ -6,6 +6,9 @@ use App\Exception\ErrException;
trait CheckOrderCallBackTrait
{
/**
* @return void
*/
public function checkWxCallBackOrder(): void
{
if (
@@ -24,4 +27,23 @@ trait CheckOrderCallBackTrait
$this->orderNo = $this->callbackData['out_trade_no'];
}
/**
* @return void
*/
public function checkWxRefundCallBackOrder(): void
{
if (
!isset($this->callbackData['return_code']) ||
$this->callbackData['return_code']!= 'SUCCESS' ||
!isset($this->callbackData['out_trade_no']) ||
empty($this->callbackData['out_trade_no']) ||
!isset($this->callbackData['mch_id']) ||
empty($this->callbackData['mch_id'])
) {
throw new ErrException('此订单回调异常');
}
$this->orderNo = $this->callbackData['out_trade_no'];
}
}

View File

@@ -4,9 +4,11 @@ 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\Order;
use App\Model\PayOrder;
use App\Model\RefundOrder;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
@@ -27,6 +29,12 @@ trait GoodOrderTrait
#[Inject]
protected PayOrder $payOrderModel;
/**
* @var RefundOrder
*/
#[Inject]
protected RefundOrder $refundOrderModel;
/**
* @return void
* @throws ContainerExceptionInterface
@@ -53,6 +61,8 @@ trait GoodOrderTrait
}
}
/**
* @return void
* @throws ContainerExceptionInterface

View File

@@ -0,0 +1,89 @@
<?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
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function checkRefundOrder(): void
{
$this->refundInfo = $this->refundOrderModel->getInfoByOrderSnoAndType($this->orderNo,self::OrderType);
$this->orderInfo = $this->orderModel->getInfoById($this->refundInfo->order_id);
$this->payInfo = $this->payOrderModel->getInfoByOrderIdAndType($this->orderInfo->id,self::OrderType);
if (empty($this->orderInfo) || empty($this->payInfo) || empty($this->refundInfo)) {
$this->log->debug(__CLASS__.':订单不存在,订单号:'.$this->orderNo);
throw new ErrException('订单不存在');
}
if ($this->refundInfo->refund_status != RefundCode::WAIT_BY_PAY_TOOL) {
$this->log->debug(__CLASS__.':订单已退款成功,订单信息:'.json_encode($this->refundInfo->toArray()));
throw new ErrException('订单已退款成功');
}
if ($this->refundInfo->refund_type != PayCode::WECHAT_PAY) {
$this->log->debug(__CLASS__.':订单退款渠道错误,订单信息:'.json_encode($this->refundInfo->toArray()));
throw new ErrException('订单退款渠道错误');
}
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
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);
if (!$this->refundInfo->save()) throw new Exception('更新退款订单失败');
}catch (Exception $e) {
$this->log->error(__CLASS__.':Function:manageGoodOrder:'.$e->getMessage().':orderId:'.$this->orderInfo->id);
throw new ErrException($e->getMessage());
}
}
/**
* @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->orderInfo->actual_price) {
$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($e->getMessage());
}
}
}