ソースを参照

refactor(backend): 重构 domesticRefunds 方法,简化参数并优化金额处理逻辑
- 改进异步请求错误处理,增加日志记录并优化异常抛出信息
- 在 Config 类中添加退款回调地址和调试模式属性
- 更新单元测试,增加退款功能测试用例

runphp 6 ヶ月 前
コミット
87ed2d7616

+ 2 - 0
src/Config.php

@@ -20,6 +20,8 @@ use WeChatPay\Crypto\Rsa;
  * @property string $public_key 微信支付公钥
  * @property string $platform_no 平台证书序列号
  * @property string $platform_cert 平台证书
+ * @property string $refund_notify_url 退款回调地址
+ * @property bool $debug 调试模式
  *
  */
 class Config

+ 10 - 9
src/Trait/ApiTrait.php

@@ -72,26 +72,27 @@ trait ApiTrait
      * 退款申请
      *
      * @param string $outRefundNo 商户退款单号
-     * @param string $outTradeNo 商户订单号
-     * @param array $amount 退款金额信息
-     * @param string $reason 退款原因
-     * @return mixed
      */
-    private function domesticRefunds(string $outRefundNo, string $outTradeNo, array $amount, string $reason)
+    private function domesticRefunds(string $outRefundNo, string $outTradeNo, float $refund, float $total = null, string $reason = '交易取消')
     {
+        $total = $total ?? $refund;
         // https://pay.weixin.qq.com/doc/v3/merchant/4012791862
         // 【POST】/v3/refund/domestic/refunds
-        return $this->handleAsyncRequest(function (BuilderChainable $builder, Config $config) use ($outRefundNo, $outTradeNo, $amount, $reason): PromiseInterface {
+        return $this->handleAsyncRequest(function (BuilderChainable $builder, Config $config, $outRefundNo, $outTradeNo, $refund, $total, $reason) : PromiseInterface {
             return $builder->v3->refund->domestic->refunds->postAsync([
                 'json' => [
                     'out_refund_no' => $outRefundNo,
                     'out_trade_no' => $outTradeNo,
                     'reason' => $reason,
-                    'amount' => $amount,
-                    'notify_url' => $config->notifyUrl
+                    'amount' => [
+                        'refund' => (int)($refund * 100),
+                        'total' => (int)($total * 100),
+                        'currency' => 'CNY'
+                    ],
+                    'notify_url' => $config->refund_notify_url
                 ]
             ]);
-        });
+        }, $outRefundNo, $outTradeNo, $refund, $total, $reason);
     }
 
     /**

+ 5 - 3
src/Trait/HandleAsyncRequestTrait.php

@@ -25,9 +25,11 @@ trait HandleAsyncRequestTrait
             })
             ->otherwise(function ($e) {
                 if ($e instanceof RequestException && $e->hasResponse()) {
-                    if (json_validate((string)$e->getResponse()->getBody())) {
-                        $errorBody = json_decode((string)$e->getResponse()->getBody());
-                        throw new ErrorException(message: trim($errorBody->message));
+                    $content = $e->getResponse()->getBody()->getContents();
+                    Log::error('微信支付异步回调错误:' . $content);
+                    if (json_validate($content)) {
+                        $errorBody = json_decode($content);
+                        throw new ErrorException(severity:$e->getCode(), message: trim($errorBody->message));
                     }
                 }
                 throw $e;

+ 9 - 0
test/WechatPayBuilderTest.php

@@ -4,12 +4,15 @@ declare(strict_types=1);
 namespace SixShop\WechatPay;
 
 use PHPUnit\Framework\TestCase;
+use SixShop\WechatPay\Trait\ApiTrait;
 use SixShop\WechatPay\Trait\MiniAppTrait;
 use WeChatPay\BuilderChainable;
 
 class WechatPayBuilderTest extends TestCase
 {
     use MiniAppTrait;
+
+    use ApiTrait;
     public function testCreate()
     {
         $result = \SixShop\WechatPay\Facade\WechatPayBuilder::getBuilderChainable();
@@ -23,4 +26,10 @@ class WechatPayBuilderTest extends TestCase
         $this->expectExceptionCode(268546002);
         $result = $this->opSpecialOrder('errororderid', 2);
     }
+
+    public function testDemo()
+    {
+        $result = $this->domesticRefunds('20250904123534022660', '20250904123534022660', 0.01);
+        dump($result);
+    }
 }