From 914ddb5abf36fc1d5b35d1331fa9fa2a4e6004ef Mon Sep 17 00:00:00 2001 From: wucongxing8150 <815046773@qq.com> Date: Wed, 26 Jun 2024 14:18:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=86=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E6=94=AF=E4=BB=98=20=E5=85=B3=E9=97=AD=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E3=80=82=E5=9C=A8=E5=8F=96=E6=B6=88=E6=9C=AA=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E4=B8=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CancelUnpayOrdersDelayDirectConsumer.php | 7 +++ app/Model/Order.php | 1 - extend/Wechat/WechatPay.php | 43 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/app/Amqp/Consumer/CancelUnpayOrdersDelayDirectConsumer.php b/app/Amqp/Consumer/CancelUnpayOrdersDelayDirectConsumer.php index 064dc92..6e9e1d9 100644 --- a/app/Amqp/Consumer/CancelUnpayOrdersDelayDirectConsumer.php +++ b/app/Amqp/Consumer/CancelUnpayOrdersDelayDirectConsumer.php @@ -19,6 +19,7 @@ use App\Services\OrderProductService; use App\Services\OrderService; use App\Utils\Log; use App\Utils\Utils; +use Extend\Wechat\WechatPay; use Hyperf\Amqp\Message\ConsumerDelayedMessageTrait; use Hyperf\Amqp\Message\ProducerDelayedMessageTrait; use Hyperf\Amqp\Result; @@ -96,6 +97,12 @@ class CancelUnpayOrdersDelayDirectConsumer extends ConsumerMessage return Result::ACK; } + // 关闭支付订单 + $WechatPay = new WechatPay("1", 1); + + // 关闭订单 + $WechatPay->closeOrder($data['order_no']); + Db::commit(); Log::getInstance("queue-CancelUnpayOrders")->info("成功"); return Result::ACK; diff --git a/app/Model/Order.php b/app/Model/Order.php index 6bed845..283f92a 100644 --- a/app/Model/Order.php +++ b/app/Model/Order.php @@ -323,7 +323,6 @@ class Order extends Model ->where($params) ->whereIn('order_type',[1,4,5]); - // 问诊订单 $query = $query->where(function ($query) use ($is_platform_deep_cooperation){ $query->whereExists(function ($subQuery) use ($is_platform_deep_cooperation){ $subQuery->from('order_inquiry'); diff --git a/extend/Wechat/WechatPay.php b/extend/Wechat/WechatPay.php index 18b276c..8c318bf 100644 --- a/extend/Wechat/WechatPay.php +++ b/extend/Wechat/WechatPay.php @@ -250,4 +250,47 @@ class WechatPay throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR); } } + + /** + * 关闭订单 + * @param string $out_trade_no 商户系统内部订单号 + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function closeOrder(string $out_trade_no): array + { + $app = $this->createApp(); + + $options = [ + "mchid" => $this->pay_config['mch_id'], // <---- 商户号 + ]; + + Log::getInstance()->info(json_encode($options,JSON_UNESCAPED_UNICODE)); + try { + $url = "v3/pay/transactions/out-trade-no/" . $out_trade_no . "/close"; + + $response = $app->getClient()->postJson($url, $options); + if ($response->isFailed()) { + // 出错了,处理异常 + $result = $response->toArray(false); + Log::getInstance()->info(json_encode($result,JSON_UNESCAPED_UNICODE)); + if(empty($result)){ + // 返回值为空 + throw new BusinessException("发起支付失败"); + } + if (!empty($result['code'])){ + if ($result['code'] != 400){ + throw new BusinessException($result['message']); + } + }else{ + throw new BusinessException("关闭支付订单失败"); + } + } + + return $response->toArray(false); + } catch (\Exception $e) { + throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR); + } + } } \ No newline at end of file