feat : refund
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Amqp\Refund;
|
||||
|
||||
class BalanceOrderRefundService
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public int $orderId;
|
||||
|
||||
public function handle()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Amqp\Refund;
|
||||
|
||||
use App\Constants\Common\OrderCode;
|
||||
use App\Model\Order;
|
||||
use App\Model\PayOrder;
|
||||
use Exception;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class GoodOrderAllRefundService extends RefundBaseService
|
||||
{
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->type = OrderCode::ORDER_TYPE_GOOD;
|
||||
}
|
||||
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$this->getOrderInfo();
|
||||
|
||||
$this->getPayOrder();
|
||||
|
||||
$this->getRefundAmount();
|
||||
|
||||
if ($this->refundAmount <= 0) throw new Exception('退款金额不能小于等于0');
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Amqp\Refund;
|
||||
|
||||
use App\Model\Order;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class GoodOrderPartRefundService
|
||||
{
|
||||
public function handle()
|
||||
{
|
||||
//todo Write logic
|
||||
}
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Amqp\Refund;
|
||||
|
||||
use App\Constants\Common\OrderCode;
|
||||
use App\Model\Order;
|
||||
use App\Model\PayOrder;
|
||||
use Exception;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class RefundBaseService
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public int $orderId;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected int $type;
|
||||
|
||||
/**
|
||||
* @var PayOrder
|
||||
*/
|
||||
#[Inject]
|
||||
protected PayOrder $payOrderModel;
|
||||
|
||||
/**
|
||||
* @var Order
|
||||
*/
|
||||
#[Inject]
|
||||
protected Order $orderModel;
|
||||
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected mixed $payInfo;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected mixed $orderInfo;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
protected float $refundAmount;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function getPayOrder()
|
||||
{
|
||||
$this->payInfo = $this->payOrderModel->getInfoByOrderIdAndType($this->orderId, $this->type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function getOrderInfo()
|
||||
{
|
||||
$this->orderInfo = match ($this->type) {
|
||||
OrderCode::ORDER_TYPE_GOOD => $this->orderModel->getInfoById($this->orderId),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
protected function getAllRefundByOrderId()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function checkInfo()
|
||||
{
|
||||
if (!$this->payInfo || !$this->orderInfo) {
|
||||
throw new Exception('订单信息不存在');
|
||||
}
|
||||
}
|
||||
|
||||
protected function getUser()
|
||||
{
|
||||
//todo 获取用户信息
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function getRefundAmount(): void
|
||||
{
|
||||
//todo 等于支付金额 减去 已退款金额
|
||||
$this->refundAmount = 0;
|
||||
}
|
||||
|
||||
protected function insertRefund()
|
||||
{
|
||||
//todo 写入退款记录
|
||||
}
|
||||
|
||||
protected function updateOrderInfo()
|
||||
{
|
||||
//todo 更新订单信息
|
||||
}
|
||||
|
||||
protected function refund()
|
||||
{
|
||||
//todo 退款 参考调起支付
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Amqp\Refund;
|
||||
|
||||
class RefundFactory
|
||||
{
|
||||
/**
|
||||
* @return GoodOrderAllRefundService
|
||||
*/
|
||||
public function newGoodOrderRefundAll(): GoodOrderAllRefundService
|
||||
{
|
||||
return new GoodOrderAllRefundService();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return GoodOrderPartRefundService
|
||||
*/
|
||||
public function newGoodOrderRefundPart(): GoodOrderPartRefundService
|
||||
{
|
||||
return new GoodOrderPartRefundService();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BalanceOrderRefundService
|
||||
*/
|
||||
public function newBalanceOrderRefund(): BalanceOrderRefundService
|
||||
{
|
||||
return new BalanceOrderRefundService();
|
||||
}
|
||||
}
|
||||
218
app/Service/Amqp/Refund/RefundService.php
Normal file
218
app/Service/Amqp/Refund/RefundService.php
Normal file
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Amqp\Refund;
|
||||
|
||||
use App\Constants\Common\OrderCode;
|
||||
use App\Constants\Common\PayCode;
|
||||
use App\Constants\Common\RefundCode;
|
||||
use App\Lib\Log;
|
||||
use App\Model\Order;
|
||||
use App\Model\PayOrder;
|
||||
use App\Model\RefundOrder;
|
||||
use App\Service\Common\Pay\Wx\WxJsRechargeOrderService;
|
||||
use Exception;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class RefundService
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public int $orderId;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected int $type;
|
||||
|
||||
/**
|
||||
* @var PayOrder
|
||||
*/
|
||||
#[Inject]
|
||||
protected PayOrder $payOrderModel;
|
||||
|
||||
/**
|
||||
* @var Order
|
||||
*/
|
||||
#[Inject]
|
||||
protected Order $orderModel;
|
||||
|
||||
/**
|
||||
* @var RefundOrder
|
||||
*/
|
||||
#[Inject]
|
||||
protected RefundOrder $refundOrderModel;
|
||||
|
||||
/**
|
||||
* @var Log
|
||||
*/
|
||||
#[Inject]
|
||||
protected Log $log;
|
||||
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected mixed $payInfo;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected mixed $orderInfo;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected mixed $refundInfo;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
public float $refundAmount;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
protected float $isRefundMoney;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$errMsg = null;
|
||||
|
||||
try {
|
||||
$this->getOrderInfo();
|
||||
|
||||
$this->checkOrder();
|
||||
|
||||
$this->getPayAndRefundOrder();
|
||||
|
||||
$this->getAllRefundMoneyByOrderId();
|
||||
|
||||
$this->checkRefundAmount();
|
||||
|
||||
$this->refund();
|
||||
} catch (Exception $e) {
|
||||
$this->log->error('RefundOrderConsumer:RefundService:error:'.$e->getMessage());
|
||||
$errArr = explode(":", $e->getMessage());
|
||||
$errMsg = $errArr[0];
|
||||
}
|
||||
|
||||
$this->updateRefund($errMsg);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getPayAndRefundOrder(): void
|
||||
{
|
||||
$this->payInfo = $this->payOrderModel->getInfoByOrderIdAndType($this->orderId, $this->type);
|
||||
|
||||
$this->refundInfo = $this->refundOrderModel->getInfoByOrderIdAndTypeWaitRefund($this->orderId,$this->type);
|
||||
|
||||
if (!$this->payInfo || !$this->refundInfo) throw new Exception('订单信息不存在');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function getOrderInfo()
|
||||
{
|
||||
$this->orderInfo = match ($this->type) {
|
||||
OrderCode::ORDER_TYPE_GOOD => $this->orderModel->getInfoById($this->orderId),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function checkOrder()
|
||||
{
|
||||
if (empty($orderInfo)) throw new Exception('订单信息不存在:'.json_encode(['order_id' => $this->orderId,'order_type' => $this->type]));
|
||||
|
||||
if (!in_array($orderInfo->status,OrderCode::CAN_REFUND_STATUS)) throw new Exception('订单状态不能退款:'.json_encode($this->orderInfo->toArray()));
|
||||
|
||||
//余额订单必须退款全部金额
|
||||
if ($this->type == OrderCode::ORDER_TYPE_BALANCE && $this->refundAmount != $this->orderInfo->actual_price) throw new Exception('余额订单必须退款全部金额:'.json_encode($this->orderInfo->toArray()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function getAllRefundMoneyByOrderId(): void
|
||||
{
|
||||
$this->isRefundMoney = $this->refundOrderModel->getSuccessMoneyByOrderId($this->orderId,$this->type);
|
||||
}
|
||||
|
||||
protected function getUser()
|
||||
{
|
||||
//todo 获取用户信息
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function checkRefundAmount(): void
|
||||
{
|
||||
// 等于支付金额 减去 已退款金额 减去 本次退款金额 小于 0 就是错误的 大于等于 0 就是正确的
|
||||
if ($this->orderInfo->actual_price - $this->isRefundMoney - $this->refundAmount < 0) throw new Exception('退款金额不能大于订单金额');
|
||||
|
||||
$this->refundAmount = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function updateRefund($errMsg = null)
|
||||
{
|
||||
$status = RefundCode::REFUND_FAIL;
|
||||
|
||||
if (empty($errMsg)) {
|
||||
$status = RefundCode::WAIT_BY_PAY_TOOL;
|
||||
$errMsg = '等待支付工具退款';
|
||||
}
|
||||
|
||||
$this->refundOrderModel->where('id',$this->refundInfo->id)->update([
|
||||
'refund_status' => $status,
|
||||
'remark' => $errMsg
|
||||
]);
|
||||
}
|
||||
|
||||
protected function refund()
|
||||
{
|
||||
$rechargeService = match ($this->refundInfo->refund_type)
|
||||
{
|
||||
PayCode::ALIPAY => $this->setAliPayRefund(),
|
||||
PayCode::WECHAT_PAY => $this->setWechatPayRefund(),
|
||||
};
|
||||
|
||||
$rechargeService->setConfig();
|
||||
$rechargeService->setNotify();
|
||||
|
||||
$payData = $rechargeService->pay(
|
||||
(float)$this->orderInfo->actual_price,
|
||||
$this->request->input('body','订单支付'),
|
||||
$this->orderInfo->order_no,
|
||||
$this->userId
|
||||
);
|
||||
}
|
||||
|
||||
protected function setAliPayRefund()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return WxJsRechargeOrderService
|
||||
*/
|
||||
protected function setWechatPayRefund(): WxJsRechargeOrderService
|
||||
{
|
||||
return new WxJsRechargeOrderService();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user