feat : refund

This commit is contained in:
2025-02-21 19:10:58 +08:00
parent ab0aff0d7f
commit 6754d2969e
17 changed files with 694 additions and 127 deletions

View File

@@ -0,0 +1,73 @@
<?php
namespace App\Service\ServiceTrait\Api;
use App\Constants\Common\OrderCode;
use App\Constants\Common\PayCode;
use App\Exception\ErrException;
use App\Model\Order;
use App\Model\PayOrder;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
trait GoodOrderTrait
{
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
/**
* @var PayOrder
*/
#[Inject]
protected PayOrder $payOrderModel;
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function checkOrder(): void
{
$this->orderInfo = $this->orderModel->getInfoByOrderSno($this->orderNo);
$this->payInfo = $this->payOrderModel->getInfoByOrderIdAndType($this->orderInfo->id,self::OrderType);
if (empty($this->orderInfo) || empty($this->payInfo)) {
$this->log->debug(__CLASS__.':订单不存在,订单号:'.$this->orderNo);
throw new ErrException('订单不存在');
}
if ($this->orderInfo->status != OrderCode::WAIT_PAY) {
$this->log->debug(__CLASS__.':订单已充值成功或取消,订单信息:'.json_encode($this->orderInfo->toArray()));
throw new ErrException('订单已充值成功');
}
if ($this->orderInfo->action != PayCode::WECHAT_PAY) {
$this->log->debug(__CLASS__.':订单充值渠道错误,订单信息:'.json_encode($this->orderInfo->toArray()));
throw new ErrException('订单充值渠道错误');
}
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function manageOrder(): void
{
try {
$this->orderInfo->status = OrderCode::PAYED;
$this->orderInfo->pay_time = date('Y-m-d H:i:s');
}catch (Exception $e) {
$this->log->error(__CLASS__.':Function:manageGoodOrder:'.$e->getMessage().':orderId:'.$this->orderInfo->id);
throw new ErrException($e->getMessage());
}
}
}

View File

@@ -21,6 +21,7 @@ use App\Constants\Common\PayCode;
use App\Constants\ConfigCode;
use App\Exception\ErrException;
use App\Model\Order;
use App\Model\PayOrder;
use Exception;
use Hyperf\Amqp\Producer;
use Hyperf\Context\ApplicationContext;
@@ -299,74 +300,4 @@ trait OrderTrait
$producer->produce($message);
}
/**
* @var Order
*/
#[Inject]
protected Order $orderModel;
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function checkGoodOrder(): void
{
$this->orderInfo = $this->orderModel->getInfoByOrderSno($this->orderNo);
if (empty($this->orderInfo)) {
$this->log->debug(__CLASS__.':订单不存在,订单号:'.$this->orderNo);
throw new ErrException('订单不存在');
}
if ($this->orderInfo->status != OrderCode::WAIT_PAY) {
$this->log->debug(__CLASS__.':订单已充值成功或取消,订单信息:'.json_encode($this->orderInfo->toArray()));
throw new ErrException('订单已充值成功');
}
if ($this->orderInfo->action != PayCode::WECHAT_PAY) {
$this->log->debug(__CLASS__.':订单充值渠道错误,订单信息:'.json_encode($this->orderInfo->toArray()));
throw new ErrException('订单充值渠道错误');
}
}
/**
* 操作微信订单
* @return void
*/
protected function manageWxOrder(): void
{
// $this->orderInfo->status = OrderCode::STATUS_PAY;
// $this->orderInfo->isAdd = 1;
// $this->orderInfo->tradeno = $this->callbackData['transaction_id'];
//// $this->orderInfo->payTime = date('Y-m-d H:i:s',strtotime($this->callbackData['time_end']));
// $this->orderInfo->payTime = strtotime($this->callbackData['success_time']);
// $this->orderInfo->jsonData = [
// 'payData' => $this->callbackData,
// 'jsonData' => $this->orderInfo->jsonData
// ];
if (!$this->orderInfo->save()) {
$this->log->debug(__CLASS__.':Function:manageWxOrder:更新充值订单的状态失败:'.$this->orderInfo->id);
throw new ErrException('更新充值订单的状态失败');
}
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function manageGoodOrder(): void
{
try {
$this->orderInfo->status = OrderCode::PAYED;
// $this->orderInfo->pay_time =
}catch (Exception $e) {
$this->log->error(__CLASS__.':Function:manageGoodOrder:'.$e->getMessage().':orderId:'.$this->orderInfo->id);
throw new ErrException($e->getMessage());
}
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Service\ServiceTrait\Api;
use App\Constants\Common\PayCode;
use App\Exception\ErrException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
trait PayFinishTrait
{
/**
* 操作微信订单
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function manageWxOrder(): void
{
$this->payInfo->status = PayCode::FINISH_PAY;
$this->payInfo->wx_transaction_id = $this->callbackData['transaction_id'];
$this->payInfo->notify_json = json_encode($this->callbackData);
if (!$this->orderInfo->save()) {
$this->log->debug(__CLASS__.':Function:manageWxOrder:更新充值订单的状态失败:'.$this->payInfo->id);
throw new ErrException('更新充值订单的状态失败');
}
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Service\ServiceTrait\Common;
use App\Amqp\Producer\CancelOrderProducer;
use App\Amqp\Producer\RefundOrderProducer;
use App\Cache\Redis\Common\ConfigCache;
use App\Constants\Common\OrderCode;
use App\Constants\ConfigCode;
use App\Extend\DateUtil;
use Hyperf\Amqp\Producer;
use Hyperf\Context\ApplicationContext;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
trait OrderChangeStatusTrait
{
/**
* @var ConfigCache $configCache
*/
#[Inject]
protected ConfigCache $configCache;
/**
* 加入取消队列
* @param int $orderId
* @param int $type
* @param int $millisecond
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function joinCancelDelayQueue(int $orderId,int $type,int $millisecond): void
{
$message = new CancelOrderProducer([
'order_id' => $orderId,
'type' => $type
]);
$message->setDelayMs($millisecond);
$producer = ApplicationContext::getContainer()->get(Producer::class);
$producer->produce($message);
}
/**
* @param int $orderId
* @param int $type
* @param float|int $amount amount = 0 全部 amount > 0 部分
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function joinRefundQueue(int $orderId, int $type, float|int $amount = 0): void
{
$message = new RefundOrderProducer([
'order_id' => $orderId,
'type' => $type,
'amount' => $amount,
]);
$producer = ApplicationContext::getContainer()->get(Producer::class);
$producer->produce($message);
}
}