Bläddra i källkod

feat(wechatpay): 调试模式进行特殊发货报备

runphp 6 månader sedan
förälder
incheckning
36b8aed775

+ 2 - 1
src/Config.php

@@ -28,7 +28,7 @@ class Config
 
     public function __construct(
         private readonly ExtensionManager $extensionManager,
-        private ?array $options = null
+        private ?array                    $options = null
     )
     {
     }
@@ -42,6 +42,7 @@ class Config
             }
         }
         return match ($name) {
+            'debug' => $this->options['debug'] ?? false,
             'apiclient_cert' => $this->getKeyPath($this->options['apiclient_cert'][0]),
             'apiclient_key' => Rsa::from($this->getKeyPath($this->options['apiclient_key'][0])),
             'public_key' => Rsa::from($this->getKeyPath($this->options['public_key'][0]), Rsa::KEY_TYPE_PUBLIC),

+ 3 - 0
src/Facade/WechatPayBuilder.php

@@ -1,7 +1,9 @@
 <?php
 declare(strict_types=1);
+
 namespace SixShop\WechatPay\Facade;
 
+use SixShop\Wechat\Service\MiniApp;
 use SixShop\WechatPay\Config;
 use think\Facade;
 use WeChatPay\BuilderChainable;
@@ -9,6 +11,7 @@ use WeChatPay\BuilderChainable;
 /**
  * @method static BuilderChainable getBuilderChainable()
  * @method static Config getConfig()
+ * @method static MiniApp getMiniApp()
  */
 class WechatPayBuilder extends Facade
 {

+ 28 - 0
src/Hook/SpecialOrderHook.php

@@ -0,0 +1,28 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\WechatPay\Hook;
+
+use SixShop\Payment\Event\PaymentSuccessEvent;
+use SixShop\WechatPay\Config;
+use SixShop\WechatPay\Trait\MiniAppTrait;
+
+class SpecialOrderHook
+{
+    use MiniAppTrait;
+
+    public function __construct(private Config $config)
+    {
+    }
+
+    /**
+     * 调试模式进行测试报备
+     */
+    #[Hook(PaymentSuccessEvent::class)]
+    public function debugMode(PaymentSuccessEvent $event): void
+    {
+        if ($this->config->isDebug()) {
+            $this->opSpecialOrder($event->orderNo, 2);
+        }
+    }
+}

+ 1 - 1
src/Hook/WechatpayHook.php

@@ -20,6 +20,6 @@ class WechatpayHook
             'wechatpay',
             '微信支付',
             '微信支付',
-           );
+        );
     }
 }

+ 1 - 3
src/PaymentProvider.php

@@ -20,8 +20,6 @@ use SixShop\Wechat\Facade\WechatUser;
 use SixShop\WechatPay\Trait\ApiTrait;
 use SixShop\WechatPay\Trait\PaymentParamsTrait;
 use think\facade\Event;
-use WeChatPay\Crypto\Rsa;
-use WeChatPay\Formatter;
 
 class PaymentProvider implements PaymentProviderInterface
 {
@@ -103,7 +101,7 @@ class PaymentProvider implements PaymentProviderInterface
             if ($paymentResult->return_code === 'SUCCESS' && $paymentResult->result_code === 'SUCCESS' && $paymentResult->trade_state === 'SUCCESS') {
                 $payment->status = PaymentStatusEnum::SUCCESS;
                 $payment->save();
-                Event::trigger(new PaymentSuccessEvent($order['order_sn'], self::PAYMENT_TYPE, $payment->toArray(), $payment->biz_type));
+                Event::trigger(new PaymentSuccessEvent($payment['order_sn'], self::PAYMENT_TYPE, $payment->toArray(), $payment->biz_type));
             }
         }
 

+ 0 - 4
src/Trait/ApiTrait.php

@@ -5,11 +5,7 @@ namespace SixShop\WechatPay\Trait;
 
 use app\xyz\exception\ApiException;
 use GuzzleHttp\Promise\PromiseInterface;
-use SixShop\Payment\Entity\ExtensionPaymentEntity;
 use SixShop\WechatPay\Config;
-use think\exception\ErrorException;
-use think\facade\Log;
-use WeChatPay\Builder;
 use WeChatPay\BuilderChainable;
 
 trait ApiTrait

+ 1 - 0
src/Trait/HandleAsyncRequestTrait.php

@@ -1,5 +1,6 @@
 <?php
 declare(strict_types=1);
+
 namespace SixShop\WechatPay\Trait;
 
 use GuzzleHttp\Exception\RequestException;

+ 31 - 0
src/Trait/MiniAppTrait.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace SixShop\WechatPay\Trait;
+
+use SixShop\WechatPay\Facade\WechatPayBuilder;
+
+trait MiniAppTrait
+{
+
+    /**
+     * 特殊发货报备
+     * @see https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html#%E5%8D%81%E3%80%81%E7%89%B9%E6%AE%8A%E5%8F%91%E8%B4%A7%E6%8A%A5%E5%A4%87
+     * @param string $orderID 订单号
+     * @param int $type 订单类型 1为预售商品订单,2为测试订单
+     * @param int $delayTo 预计发货时间的unix时间戳,type为1时必填,type为2可省略
+     */
+    private function opSpecialOrder(string $orderID, int $type, int $delayTo = null): bool
+    {
+        $response = WechatPayBuilder::getMiniApp()->getClient()->postJson('/wxa/sec/order/opspecialorder', [
+
+            'order_id' => $orderID,
+            'type' => $type,
+            'delay_to' => $delayTo,
+        ]);
+        $result = json_decode($response->getContent());
+        if ($result->errcode != 0) {
+            throw new  \InvalidArgumentException($result->errmsg, $result->errcode);
+        }
+        return true;
+    }
+}

+ 17 - 4
src/WechatPayBuilder.php

@@ -3,6 +3,7 @@ declare(strict_types=1);
 
 namespace SixShop\WechatPay;
 
+use SixShop\Wechat\Service\MiniApp;
 use think\Facade;
 use WeChatPay\Builder;
 use WeChatPay\BuilderChainable;
@@ -12,17 +13,19 @@ use WeChatPay\BuilderChainable;
  */
 class WechatPayBuilder extends Facade
 {
-    protected static function getFacadeAccessor(): string
-    {
-        return self::class;
-    }
     public function __construct(
         private readonly Config   $config,
         private ?BuilderChainable $builderChainable = null,
+        private ?MiniApp          $miniApp = null
     )
     {
     }
 
+    protected static function getFacadeAccessor(): string
+    {
+        return self::class;
+    }
+
     public function getBuilderChainable(): BuilderChainable
     {
 
@@ -44,4 +47,14 @@ class WechatPayBuilder extends Facade
     {
         return $this->config;
     }
+
+    public function getMiniApp(): MiniApp
+    {
+        if ($this->miniApp === null) {
+            $this->miniApp = app()->make(MiniApp::class, [
+                $this->config->app_id,
+            ]);
+        }
+        return $this->miniApp;
+    }
 }

+ 10 - 0
test/WechatPayBuilderTest.php

@@ -4,13 +4,23 @@ declare(strict_types=1);
 namespace SixShop\WechatPay;
 
 use PHPUnit\Framework\TestCase;
+use SixShop\WechatPay\Trait\MiniAppTrait;
 use WeChatPay\BuilderChainable;
 
 class WechatPayBuilderTest extends TestCase
 {
+    use MiniAppTrait;
     public function testCreate()
     {
         $result = \SixShop\WechatPay\Facade\WechatPayBuilder::getBuilderChainable();
         $this->assertInstanceOf(BuilderChainable::class, $result);
     }
+
+    public  function testOpSpecialOrder()
+    {
+
+        $this->expectException(\InvalidArgumentException::class);
+        $this->expectExceptionCode(268546002);
+        $result = $this->opSpecialOrder('errororderid', 2);
+    }
 }