فهرست منبع

feat(wechatpay): 新增 QueryRefundJob 类用于异步查询退款状态

runphp 6 ماه پیش
والد
کامیت
c6f8745ef9
3فایلهای تغییر یافته به همراه42 افزوده شده و 10 حذف شده
  1. 20 0
      src/Job/QueryRefundJob.php
  2. 21 9
      src/PaymentProvider.php
  3. 1 1
      src/Trait/HandleAsyncRequestTrait.php

+ 20 - 0
src/Job/QueryRefundJob.php

@@ -0,0 +1,20 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\WechatPay\Job;
+
+use SixShop\Core\Job\BaseJob;
+use SixShop\WechatPay\PaymentProvider;
+use think\facade\Log;
+
+class QueryRefundJob extends BaseJob
+{
+    public function __construct(private PaymentProvider $paymentProvider)
+    {
+    }
+
+    public function execute(int $refundID):void
+    {
+        $this->paymentProvider->refundQuery($refundID);
+    }
+}

+ 21 - 9
src/PaymentProvider.php

@@ -21,6 +21,7 @@ use SixShop\Payment\Enum\RefundStatusEnum;
 use SixShop\Payment\Event\PaymentSuccessEvent;
 use SixShop\Payment\Event\RefundSuccessEvent;
 use SixShop\Wechat\Facade\WechatUser;
+use SixShop\WechatPay\Job\QueryRefundJob;
 use SixShop\WechatPay\Trait\ApiTrait;
 use SixShop\WechatPay\Trait\PaymentParamsTrait;
 use think\facade\Db;
@@ -73,7 +74,7 @@ class PaymentProvider implements PaymentProviderInterface
             $payment->expire_time = $expireTime;
             $openid = WechatUser::openid($order['user_id']);
             if ($openid === null) {
-                Helper::throw_logic_exception('用户需要先使用微信登录绑定微信身份');
+                throw_logic_exception('用户需要先使用微信登录绑定微信身份');
             }
             $payment->payment_param = $this->wechatPay(
                 openid: $openid,
@@ -127,8 +128,8 @@ class PaymentProvider implements PaymentProviderInterface
     public function refund(int $recordID, PaymentRefundRequest $param): PaymentRefundResult
     {
         $payment = $this->extensionPaymentEntity->find($recordID);
-        $reund = Db::transaction(function () use ($param, $payment) {
-            $reund = $this->extensionRefundEntity->create([
+        $refund = Db::transaction(function () use ($param, $payment) {
+            $refund = $this->extensionRefundEntity->create([
                 'payment_id' => $payment->id,
                 'order_sn' => $payment->out_trade_no,
                 'reason' => $param->getReason(),
@@ -137,16 +138,28 @@ class PaymentProvider implements PaymentProviderInterface
                 'refund_param' => $param->getRaw(),
                 'status_desc' => '正在申请微信接口退款',
             ]);
-            $this->domesticRefunds(
-                $reund->out_refund_no,
+            $result = $this->domesticRefunds(
+                $refund->out_refund_no,
                 $payment->out_trade_no,
                 $param->getAmount(),
                 $payment->amount,
                 $param->getReason()
             );
-            return $reund;
+            $refund->refund_id = $result->refund_id;
+            $refund->refund_result = $result;
+            $refund->save();
+            if ($result->status === 'SUCCESS' || $result->status === 'PROCESSING') {
+                QueryRefundJob::dispatch($refund->id)->delay(10);
+            } else {
+                throw new \RuntimeException(match ($result->status) {
+                    'CLOSED' => '退款关闭',
+                    'ABNORMAL' => '退款异常',
+                    default => '未知错误',
+                });
+            }
+            return $refund;
         });
-        return new PaymentRefundResult($reund);
+        return new PaymentRefundResult($refund);
     }
 
     public function refundQuery(int $refundID): PaymentRefundResult
@@ -154,10 +167,9 @@ class PaymentProvider implements PaymentProviderInterface
         $refund = $this->extensionRefundEntity->with('payment')->find($refundID);
         if ($refund->status === RefundStatusEnum::REFUNDING) {
             $result = $this->queryRefund($refund->out_refund_no);
-            $refund->refund_id = $result->refund_id;
             $refund->refund_result = $result;
             if ($result->status === 'SUCCESS') {
-                $refund->status = RefunddStatusEnum::SUCCESS;
+                $refund->status = RefundStatusEnum::SUCCESS;
                 $refund->success_time = strtotime($result->success_time);
                 Event::trigger(new RefundSuccessEvent(
                         $refund->model(),

+ 1 - 1
src/Trait/HandleAsyncRequestTrait.php

@@ -6,7 +6,7 @@ namespace SixShop\WechatPay\Trait;
 use GuzzleHttp\Exception\RequestException;
 use GuzzleHttp\Promise\PromiseInterface;
 use SixShop\WechatPay\Facade\WechatPayBuilder;
-use think\exception\ErrorException;
+use ErrorException;
 use think\facade\Log;
 
 trait HandleAsyncRequestTrait