feat : refund
This commit is contained in:
@@ -7,6 +7,7 @@ namespace App\Amqp\Consumer;
|
||||
use App\Constants\Common\OrderCode;
|
||||
use App\Lib\Log;
|
||||
use App\Model\Order;
|
||||
use App\Service\ServiceTrait\Api\OrderTrait;
|
||||
use Hyperf\Amqp\Message\ConsumerDelayedMessageTrait;
|
||||
use Hyperf\Amqp\Message\ProducerDelayedMessageTrait;
|
||||
use Hyperf\Amqp\Message\Type;
|
||||
@@ -20,6 +21,7 @@ use PhpAmqpLib\Message\AMQPMessage;
|
||||
class CancelOrderConsumer extends ConsumerMessage
|
||||
{
|
||||
use ProducerDelayedMessageTrait,ConsumerDelayedMessageTrait;
|
||||
use OrderTrait;
|
||||
|
||||
/**
|
||||
* @var Type|string 消息类型
|
||||
@@ -38,16 +40,6 @@ class CancelOrderConsumer extends ConsumerMessage
|
||||
#[Inject]
|
||||
protected Order $orderModel;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private int $orderId;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private int $orderType;
|
||||
|
||||
|
||||
public function consumeMessage($data, AMQPMessage $message): Result
|
||||
{
|
||||
@@ -56,11 +48,11 @@ class CancelOrderConsumer extends ConsumerMessage
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
$this->orderId = (int)$data['order_id'];
|
||||
$this->orderType = (int)$data['type'];
|
||||
$orderId = (int)$data['order_id'];
|
||||
$orderType = (int)$data['type'];
|
||||
|
||||
$orderInfo = match ($this->orderType) {
|
||||
OrderCode::ORDER_TYPE_GOOD => $this->orderModel->getInfoById($this->orderId),
|
||||
$orderInfo = match ($orderType) {
|
||||
OrderCode::ORDER_TYPE_GOOD => $this->orderModel->getInfoById($orderId),
|
||||
default => null,
|
||||
};
|
||||
|
||||
@@ -80,6 +72,7 @@ class CancelOrderConsumer extends ConsumerMessage
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
$this->sendStockMq($orderInfo->id,OrderCode::CANCEL);
|
||||
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
@@ -4,23 +4,107 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Amqp\Consumer;
|
||||
|
||||
use App\Constants\Common\OrderCode;
|
||||
use App\Lib\Log;
|
||||
use App\Model\Order;
|
||||
use App\Service\Amqp\Refund\GoodOrderAllRefundService;
|
||||
use App\Service\Amqp\Refund\RefundFactory;
|
||||
use App\Service\ServiceTrait\Api\OrderTrait;
|
||||
use Exception;
|
||||
use Hyperf\Amqp\Message\Type;
|
||||
use Hyperf\Amqp\Result;
|
||||
use Hyperf\Amqp\Annotation\Consumer;
|
||||
use Hyperf\Amqp\Message\ConsumerMessage;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
|
||||
#[Consumer(exchange: 'RefundOrder', routingKey: 'RefundOrder', queue: 'RefundOrder.direct', name: "RefundOrderConsumer", nums: 1)]
|
||||
class RefundOrderConsumer extends ConsumerMessage
|
||||
{
|
||||
use OrderTrait;
|
||||
|
||||
/**
|
||||
* @var Type|string 消息类型
|
||||
*/
|
||||
protected Type|string $type = Type::DIRECT;
|
||||
|
||||
|
||||
/**
|
||||
* @var Log $log
|
||||
*/
|
||||
#[Inject]
|
||||
protected Log $log;
|
||||
|
||||
/**
|
||||
* @var Order
|
||||
*/
|
||||
#[Inject]
|
||||
protected Order $orderModel;
|
||||
|
||||
/**
|
||||
* @var RefundFactory
|
||||
*/
|
||||
#[Inject]
|
||||
protected RefundFactory $refundFactory;
|
||||
|
||||
public function consumeMessage($data, AMQPMessage $message): Result
|
||||
{
|
||||
if (!$data['order_id'] || !$data['type'] || !$data['amount']) {
|
||||
$this->log->error('CancelOrderConsumer:error:NoData:'.json_encode($data));
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
$orderId = (int)$data['order_id'];
|
||||
$orderType = (int)$data['type'];
|
||||
$amount = $data['amount'];
|
||||
|
||||
$orderInfo = match ($orderType) {
|
||||
OrderCode::ORDER_TYPE_GOOD => $this->orderModel->getInfoById($orderId),
|
||||
default => null,
|
||||
};
|
||||
|
||||
if (empty($orderInfo)) {
|
||||
$this->log->debug('RefundOrderConsumer:error:NoOrderData:'.json_encode($data));
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
if (!in_array($orderInfo->status,OrderCode::CAN_REFUND_STATUS)) {
|
||||
$this->log->debug('CancelOrderConsumer:error:orderStatusError:'.json_encode($orderInfo->toArray()));
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
//余额订单必须退款全部金额
|
||||
if ($orderType == OrderCode::ORDER_TYPE_BALANCE && $amount != 0) {
|
||||
$this->log->debug('CancelOrderConsumer:error:orderStatusError:'.json_encode($orderInfo->toArray()));
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
$service = null;
|
||||
|
||||
switch ($orderType) {
|
||||
case OrderCode::ORDER_TYPE_GOOD:
|
||||
if ($amount == 0) {
|
||||
$service = $this->refundFactory->newGoodOrderRefundAll();
|
||||
} else {
|
||||
$service = $this->refundFactory->newGoodOrderRefundPart();
|
||||
}
|
||||
break;
|
||||
case OrderCode::ORDER_TYPE_BALANCE:
|
||||
$service = $this->refundFactory->newBalanceOrderRefund();
|
||||
break;
|
||||
default:
|
||||
throw new Exception('service not found or orderType error');
|
||||
}
|
||||
|
||||
|
||||
} catch (Exception $e) {
|
||||
$this->log->error('RefundOrderConsumer:error:'.$e->getMessage().':data:'.json_encode($data));
|
||||
}
|
||||
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,45 +7,57 @@ class OrderCode
|
||||
/**
|
||||
* @var int 状态:1=未支付,11=未出,21=出餐,31=已出发,41=待取餐,51=完成,97=未完成退款,98=完成退款,99=取消
|
||||
*/
|
||||
CONST WAIT_PAY = 1; //待支付
|
||||
CONST PAYED = 11; //已支付待出餐
|
||||
CONST PLAN = 21; //出餐未出发
|
||||
CONST DEPART = 31; //已出发
|
||||
CONST ARRIVE = 41; // 已送达(待取餐)
|
||||
CONST FINISH = 51; //已完成
|
||||
const UNCOMPLETED_REFUND = 97; //未完成退款 部分退款
|
||||
CONST FINISH_REFUND = 98; //完成后退款
|
||||
CONST CANCEL = 99; //取消
|
||||
CONST int WAIT_PAY = 1; //待支付
|
||||
CONST int PAYED = 11; //已支付待出餐
|
||||
CONST int PLAN = 21; //出餐未出发
|
||||
CONST int DEPART = 31; //已出发
|
||||
CONST int ARRIVE = 41; // 已送达(待取餐)
|
||||
CONST int FINISH = 51; //已完成
|
||||
const int UNCOMPLETED_REFUND = 97; //未完成退款 部分退款
|
||||
CONST int FINISH_REFUND = 98; //完成后退款
|
||||
CONST int CANCEL = 99; //取消
|
||||
|
||||
/**
|
||||
* 可以退款的订单状态
|
||||
*/
|
||||
const array CAN_REFUND_STATUS = [
|
||||
self::PAYED,
|
||||
self::PLAN,
|
||||
self::DEPART,
|
||||
self::ARRIVE,
|
||||
self::FINISH,
|
||||
self::UNCOMPLETED_REFUND,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var int 退款状态 1=未退款 2=已退款部分 3=全部退款
|
||||
*/
|
||||
CONST REFUND_NULL = 1; //未退款
|
||||
CONST REFUND_SEGMENT = 2; //部分退款
|
||||
CONST REFUND_ALL = 3; //全部退款
|
||||
CONST int REFUND_NULL = 1; //未退款
|
||||
CONST int REFUND_SEGMENT = 2; //部分退款
|
||||
CONST int REFUND_ALL = 3; //全部退款
|
||||
|
||||
/**
|
||||
* @var int 订单评价状态 1=未评价 2=部分评价 3=全部评价
|
||||
*/
|
||||
CONST ORDER_COMMENT_NULL = 1;
|
||||
CONST ORDER_COMMENT_SEGMENT = 2;
|
||||
CONST ORDER_COMMENT_FINISH = 3;
|
||||
CONST int ORDER_COMMENT_NULL = 1;
|
||||
CONST int ORDER_COMMENT_SEGMENT = 2;
|
||||
CONST int ORDER_COMMENT_FINISH = 3;
|
||||
|
||||
/**
|
||||
* @var int 商品评价状态 1=未评价 2=已评价
|
||||
*/
|
||||
CONST GOOD_COMMENT_NULL = 1;
|
||||
CONST GOOD_COMMENT_FINISH = 2;
|
||||
CONST int GOOD_COMMENT_NULL = 1;
|
||||
CONST int GOOD_COMMENT_FINISH = 2;
|
||||
|
||||
/**
|
||||
* @var int 订单类型 1=商品订单 2=充值订单
|
||||
*/
|
||||
const ORDER_TYPE_GOOD = 1;
|
||||
const ORDER_TYPE_BALANCE = 2;
|
||||
const int ORDER_TYPE_GOOD = 1;
|
||||
const int ORDER_TYPE_BALANCE = 2;
|
||||
|
||||
/**
|
||||
* @var string 订单类型前缀
|
||||
*/
|
||||
const ORDER_TYPE_GOOD_PREFIX = 'OG';
|
||||
const ORDER_TYPE_BALANCE_PREFIX = 'OB';
|
||||
const string ORDER_TYPE_GOOD_PREFIX = 'OG';
|
||||
const string ORDER_TYPE_BALANCE_PREFIX = 'OB';
|
||||
}
|
||||
@@ -6,9 +6,11 @@ namespace App\Controller\Api;
|
||||
|
||||
use App\Controller\AbstractController;
|
||||
use App\Middleware\Api\JwtAuthMiddleware;
|
||||
use App\Service\Api\Order\CancelOrderService;
|
||||
use App\Service\Api\Order\CheckCartService;
|
||||
use App\Service\Api\Order\ConfirmationOrderService;
|
||||
use App\Service\Api\Order\PlaceOrderService;
|
||||
use App\Service\Api\Order\RefundOrderService;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\Middlewares;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
@@ -57,4 +59,30 @@ class OrderController extends AbstractController
|
||||
{
|
||||
return (new PlaceOrderService)->handle();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消订单 type 1|2 good|balance
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
#[RequestMapping(path: 'cancel_order',methods: 'post')]
|
||||
#[Scene(scene: 'cancel_order')]
|
||||
public function cancelOrder()
|
||||
{
|
||||
return (new CancelOrderService)->handle();
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单退款(同理)
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
#[RequestMapping(path: 'refund_order',methods: 'post')]
|
||||
#[Scene(scene: 'refund_order')]
|
||||
public function refundOrder()
|
||||
{
|
||||
return (new RefundOrderService)->handle();
|
||||
}
|
||||
}
|
||||
|
||||
24
app/Service/Amqp/Refund/BalanceOrderRefundService.php
Normal file
24
app/Service/Amqp/Refund/BalanceOrderRefundService.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
41
app/Service/Amqp/Refund/GoodOrderAllRefundService.php
Normal file
41
app/Service/Amqp/Refund/GoodOrderAllRefundService.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?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');
|
||||
}
|
||||
}
|
||||
22
app/Service/Amqp/Refund/GoodOrderPartRefundService.php
Normal file
22
app/Service/Amqp/Refund/GoodOrderPartRefundService.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?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
|
||||
}
|
||||
}
|
||||
114
app/Service/Amqp/Refund/RefundBaseService.php
Normal file
114
app/Service/Amqp/Refund/RefundBaseService.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?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 退款 参考调起支付
|
||||
}
|
||||
}
|
||||
30
app/Service/Amqp/Refund/RefundFactory.php
Normal file
30
app/Service/Amqp/Refund/RefundFactory.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
51
app/Service/Api/Order/CancelOrderService.php
Normal file
51
app/Service/Api/Order/CancelOrderService.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Api\Order;
|
||||
|
||||
use App\Constants\Common\OrderCode;
|
||||
use App\Exception\ErrException;
|
||||
use App\Model\Order;
|
||||
use App\Service\Api\BaseService;
|
||||
use App\Service\ServiceTrait\Common\OrderChangeStatusTrait;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class CancelOrderService extends BaseService
|
||||
{
|
||||
use OrderChangeStatusTrait;
|
||||
|
||||
/**
|
||||
* @var Order
|
||||
*/
|
||||
#[Inject]
|
||||
protected Order $orderModel;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
$orderId = (int)$this->request->input('order_id');
|
||||
$type = (int)$this->request->input('type');
|
||||
|
||||
$orderInfo = $this->orderModel->getInfoById($orderId);
|
||||
|
||||
if ($orderInfo->status != OrderCode::WAIT_PAY) throw new ErrException('该订单状态已变更,请勿重复操作');
|
||||
|
||||
//立即取消
|
||||
$this->joinCancelDelayQueue($orderId, $type,1);
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ use App\Exception\ErrException;
|
||||
use App\Extend\DateUtil;
|
||||
use App\Model\Order;
|
||||
use App\Model\OrderGood;
|
||||
use App\Service\ServiceTrait\Common\OrderChangeStatusTrait;
|
||||
use Exception;
|
||||
use Hyperf\Amqp\Producer;
|
||||
use Hyperf\Context\ApplicationContext;
|
||||
@@ -26,6 +27,8 @@ use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class PlaceOrderService extends BaseOrderService
|
||||
{
|
||||
use OrderChangeStatusTrait;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
@@ -61,28 +64,17 @@ class PlaceOrderService extends BaseOrderService
|
||||
$this->placeOrder();
|
||||
|
||||
// 加入取消延迟队列
|
||||
$this->joinCancelDelayQueue();
|
||||
$this->joinCancelDelayQueue(
|
||||
$this->orderId,
|
||||
OrderCode::ORDER_TYPE_GOOD,
|
||||
(int)$this->configCache->getConfigValue(ConfigCode::ORDER_CANCEL_TIME_KEY) * DateUtil::MINUTE * DateUtil::MS
|
||||
);
|
||||
|
||||
$this->sendStockMq($this->orderId,OrderCode::WAIT_PAY);
|
||||
|
||||
return $this->return->success('success',$this->orderRes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入取消队列
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function joinCancelDelayQueue(): void
|
||||
{
|
||||
$message = new CancelOrderProducer([
|
||||
'order_id' => $this->orderId,
|
||||
'type' => OrderCode::ORDER_TYPE_GOOD
|
||||
]);
|
||||
$message->setDelayMs((int)$this->configCache->getConfigValue(ConfigCode::ORDER_CANCEL_TIME_KEY) * DateUtil::MINUTE * DateUtil::MS);
|
||||
$producer = ApplicationContext::getContainer()->get(Producer::class);
|
||||
$producer->produce($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
|
||||
51
app/Service/Api/Order/RefundOrderService.php
Normal file
51
app/Service/Api/Order/RefundOrderService.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Api\Order;
|
||||
|
||||
use App\Constants\Common\OrderCode;
|
||||
use App\Exception\ErrException;
|
||||
use App\Model\Order;
|
||||
use App\Service\Api\BaseService;
|
||||
use App\Service\ServiceTrait\Common\OrderChangeStatusTrait;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class RefundOrderService extends BaseService
|
||||
{
|
||||
use OrderChangeStatusTrait;
|
||||
|
||||
/**
|
||||
* @var Order
|
||||
*/
|
||||
#[Inject]
|
||||
protected Order $orderModel;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
$orderId = (int)$this->request->input('order_id');
|
||||
$type = (int)$this->request->input('type');
|
||||
|
||||
$orderInfo = $this->orderModel->getInfoById($orderId);
|
||||
|
||||
if ($orderInfo->status != OrderCode::PAYED) throw new ErrException('该订单状态已变更,请勿重复操作,刷新后无法退款请联系客服');
|
||||
|
||||
//立即取消
|
||||
$this->joinRefundQueue($orderId, $type);
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
}
|
||||
@@ -2,15 +2,29 @@
|
||||
|
||||
namespace App\Service\Common\Pay\Wx;
|
||||
|
||||
use App\Constants\Common\OrderCode;
|
||||
use App\Model\Order;
|
||||
use App\Service\ServiceTrait\Api\CheckOrderCallBackTrait;
|
||||
use App\Service\ServiceTrait\Api\GoodOrderTrait;
|
||||
use App\Service\ServiceTrait\Api\OrderTrait;
|
||||
use App\Service\ServiceTrait\Api\PayFinishTrait;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Yansongda\Artful\Exception\InvalidParamsException;
|
||||
use function Hyperf\Config\config;
|
||||
|
||||
class WxJsRechargeOrderService extends WxJsRechargeBaseService
|
||||
{
|
||||
use CheckOrderCallBackTrait,
|
||||
OrderTrait;
|
||||
GoodOrderTrait,
|
||||
PayFinishTrait;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
const int OrderType = OrderCode::ORDER_TYPE_GOOD;
|
||||
|
||||
/**
|
||||
* 订单 id
|
||||
@@ -28,17 +42,27 @@ class WxJsRechargeOrderService extends WxJsRechargeBaseService
|
||||
*/
|
||||
protected mixed $orderInfo;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected mixed $payInfo;
|
||||
|
||||
/**
|
||||
* @return ResponseInterface
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws InvalidParamsException
|
||||
*/
|
||||
public function callBackHandle()
|
||||
{
|
||||
$this-> getWechatData();
|
||||
|
||||
$this->checkWxCallBackOrder();
|
||||
|
||||
$this->checkGoodOrder();
|
||||
$this->checkOrder();
|
||||
|
||||
Db::transaction(function (){
|
||||
$this->manageGoodOrder();
|
||||
$this->manageOrder();
|
||||
|
||||
$this->manageWxOrder();
|
||||
});
|
||||
@@ -55,7 +79,10 @@ class WxJsRechargeOrderService extends WxJsRechargeBaseService
|
||||
return $this->returnSuccess();
|
||||
}
|
||||
|
||||
public function setNotify()
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setNotify(): void
|
||||
{
|
||||
//返回的回调地址
|
||||
$this->config['wechat']['default']['notify_url'] = config('system.api_url').'/common/wxPay/order/js/callBack';
|
||||
|
||||
73
app/Service/ServiceTrait/Api/GoodOrderTrait.php
Normal file
73
app/Service/ServiceTrait/Api/GoodOrderTrait.php
Normal 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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
31
app/Service/ServiceTrait/Api/PayFinishTrait.php
Normal file
31
app/Service/ServiceTrait/Api/PayFinishTrait.php
Normal 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('更新充值订单的状态失败');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
63
app/Service/ServiceTrait/Common/OrderChangeStatusTrait.php
Normal file
63
app/Service/ServiceTrait/Common/OrderChangeStatusTrait.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user