feat : refund
This commit is contained in:
@@ -10,11 +10,16 @@ use App\Model\Order;
|
||||
use App\Model\PayOrder;
|
||||
use App\Model\RefundOrder;
|
||||
use App\Service\Common\Pay\Wx\WxJsRechargeOrderService;
|
||||
use App\Service\ServiceTrait\Api\OrderTrait;
|
||||
use Exception;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class RefundService
|
||||
{
|
||||
use OrderTrait;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
@@ -23,7 +28,12 @@ class RefundService
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected int $type;
|
||||
public int $type;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public string $reason;
|
||||
|
||||
/**
|
||||
* @var PayOrder
|
||||
@@ -76,7 +86,10 @@ class RefundService
|
||||
protected float $isRefundMoney;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return void
|
||||
* @throws Exception
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
@@ -93,6 +106,8 @@ class RefundService
|
||||
|
||||
$this->checkRefundAmount();
|
||||
|
||||
$this->insertRefundOrder();
|
||||
|
||||
$this->refund();
|
||||
} catch (Exception $e) {
|
||||
$this->log->error('RefundOrderConsumer:RefundService:error:'.$e->getMessage());
|
||||
@@ -103,6 +118,27 @@ class RefundService
|
||||
$this->updateRefund($errMsg);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
private function insertRefundOrder(): void
|
||||
{
|
||||
$this->refundInfo = new RefundOrder();
|
||||
|
||||
$this->refundInfo->user_id = $this->orderInfo->user_id;
|
||||
$this->refundInfo->order_type = $this->type;
|
||||
$this->refundInfo->order_id = $this->orderId;
|
||||
$this->refundInfo->pay_id = $this->payInfo->id;
|
||||
$this->refundInfo->refund_status = RefundCode::WAIT_REFUND;
|
||||
$this->refundInfo->refund_money = $this->refundAmount;
|
||||
$this->refundInfo->refund_type = $this->payInfo->recharge_type;
|
||||
$this->refundInfo->refund_order_sno = $this->generateRefundOrderNo($this->type, $this->orderInfo->user_id);
|
||||
$this->refundInfo->reason = $this->reason;
|
||||
|
||||
if (!$this->refundInfo->save()) throw new Exception('退款订单创建失败');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws Exception
|
||||
@@ -111,15 +147,17 @@ class RefundService
|
||||
{
|
||||
$this->payInfo = $this->payOrderModel->getInfoByOrderIdAndType($this->orderId, $this->type);
|
||||
|
||||
$this->refundInfo = $this->refundOrderModel->getInfoByOrderIdAndTypeWaitRefund($this->orderId,$this->type);
|
||||
// $this->refundInfo = $this->refundOrderModel->getInfoByOrderIdAndTypeWaitRefund($this->orderId,$this->type);
|
||||
|
||||
if (!$this->payInfo || !$this->refundInfo) throw new Exception('订单信息不存在');
|
||||
if (!$this->payInfo) throw new Exception('订单信息不存在');
|
||||
|
||||
if ($this->payInfo->status != PayCode::FINISH_PAY) throw new Exception('订单支付状态不是成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function getOrderInfo()
|
||||
protected function getOrderInfo(): void
|
||||
{
|
||||
$this->orderInfo = match ($this->type) {
|
||||
OrderCode::ORDER_TYPE_GOOD => $this->orderModel->getInfoById($this->orderId),
|
||||
@@ -131,7 +169,7 @@ class RefundService
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function checkOrder()
|
||||
protected function checkOrder(): void
|
||||
{
|
||||
if (empty($orderInfo)) throw new Exception('订单信息不存在:'.json_encode(['order_id' => $this->orderId,'order_type' => $this->type]));
|
||||
|
||||
@@ -149,10 +187,10 @@ class RefundService
|
||||
$this->isRefundMoney = $this->refundOrderModel->getSuccessMoneyByOrderId($this->orderId,$this->type);
|
||||
}
|
||||
|
||||
protected function getUser()
|
||||
{
|
||||
//todo 获取用户信息
|
||||
}
|
||||
// protected function getUser()
|
||||
// {
|
||||
// //todo 获取用户信息
|
||||
// }
|
||||
|
||||
/**
|
||||
* @return void
|
||||
@@ -167,9 +205,11 @@ class RefundService
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $errMsg
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function updateRefund($errMsg = null)
|
||||
protected function updateRefund($errMsg = null): void
|
||||
{
|
||||
$status = RefundCode::REFUND_FAIL;
|
||||
|
||||
@@ -178,12 +218,17 @@ class RefundService
|
||||
$errMsg = '等待支付工具退款';
|
||||
}
|
||||
|
||||
$this->refundOrderModel->where('id',$this->refundInfo->id)->update([
|
||||
'refund_status' => $status,
|
||||
'remark' => $errMsg
|
||||
]);
|
||||
$this->refundInfo->refund_status = $status;
|
||||
$this->refundInfo->remark = $errMsg;
|
||||
|
||||
if (!$this->refundInfo->save()) throw new Exception('退款订单更新失败');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
protected function refund(): void
|
||||
{
|
||||
$rechargeService = match ($this->refundInfo->refund_type)
|
||||
@@ -203,6 +248,9 @@ class RefundService
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
*/
|
||||
protected function setAliPayRefund()
|
||||
{
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user