Parcourir la source

feat(lakala): 增加商户余额校验逻辑

- 在分账订单审核时增加对商户电子钱包余额的查询
- 新增 LaepIndustryService 服务类用于调用拉卡拉行业接口
- 实现 ewalletBalanceQuery 方法查询商户余额
- 增加商户账户状态检查,支持多种异常状态提示
- 添加单元测试用例验证 ewalletBalanceQuery 方法
- 创建 LaepIndustryService 门面类便于调用
runphp il y a 3 mois
Parent
commit
07a4e2e4f3

+ 20 - 1
src/Controller/Admin/ProfitShareOrderController.php

@@ -7,6 +7,7 @@ use SixShop\Balpay\Enum\BalpayLogTypeEnum;
 use SixShop\Core\Request;
 use SixShop\Lakala\Entity\ProfitShareOrderEntity;
 use SixShop\Lakala\Enum\ProfitShareOrderStatusEnum;
+use SixShop\Lakala\Service\LaepIndustryService;
 use think\Response;
 use function SixShop\Core\page_response;
 use function SixShop\Core\success_response;
@@ -31,12 +32,30 @@ class ProfitShareOrderController
     public function approve(
         int $id,
         ProfitShareOrderEntity $profitShareOrderEntity,
-        ExtensionBalpayLogEntity $extensionBalpayLogEntity): Response
+        ExtensionBalpayLogEntity $extensionBalpayLogEntity,
+        LaepIndustryService $laepIndustryService
+    ): Response
     {
         $entity = $profitShareOrderEntity->where(['id' => $id, 'status' => ProfitShareOrderStatusEnum::PENDING])->findOrEmpty();
         if ($entity->isEmpty()) {
             throw_logic_exception('订单不存在或状态异常');
         }
+        $balance = $laepIndustryService->ewalletBalanceQuery([
+            'merchantNo' => $entity->merchant_no,
+            'payType' => '03',
+        ]);
+        if ($balance->acctSt != 'NORMAL') {
+            throw_logic_exception(match ($balance->acctSt){
+                'CLOSE' => '商户已经销户',
+                'FREEZE' => '商户账户被冻结',
+                'STOPPAY' => '商户账户被停付',
+                default => '商户账户异常'
+            });
+        }
+        $curBalance = (int)round($balance->curBalance * 100,0);
+        if ($curBalance < $entity->total_amt) {
+            throw_logic_exception('商户余额不足');
+        }
         $entity->status = ProfitShareOrderStatusEnum::PROCESSING;
         $entity->save();
 

+ 18 - 0
src/Facade/LaepIndustryService.php

@@ -0,0 +1,18 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\Lakala\Facade;
+
+use think\Facade;
+
+/**
+ * @mixin \SixShop\Lakala\Service\LaepIndustryService
+ */
+class LaepIndustryService extends Facade
+{
+
+    protected static function getFacadeClass()
+    {
+        return \SixShop\Lakala\Service\LaepIndustryService::class;
+    }
+}

+ 35 - 0
src/Service/LaepIndustryService.php

@@ -0,0 +1,35 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\Lakala\Service;
+
+use SixShop\Lakala\Config;
+use SixShop\Lakala\OpenAPISDK\V2\Api\V2LakalaApi;
+use SixShop\Lakala\OpenAPISDK\V2\Model\V2ModelRequest;
+use function SixShop\Core\throw_logic_exception;
+
+class LaepIndustryService
+{
+    private V2LakalaApi $v2LakalaApi;
+
+    public function __construct(private Config $config)
+    {
+        $this->v2LakalaApi = new V2LakalaApi($config->getV2Config());
+    }
+
+    public function ewalletBalanceQuery(array $reqData)
+    {
+        $request = new V2ModelRequest();
+        $reqData['orgNo'] = $this->config->org_code;
+        $request->setReqData($reqData);
+        $response = $this->v2LakalaApi->tradeApi('/api/v2/laep/industry/ewalletBalanceQuery', $request);
+        if ($response->getRetCode() == '000000') {
+            return $response->getRespData();
+        } else {
+            throw_logic_exception(
+                msg:$response->getRetMsg(),
+                code: (int)$response->getRetCode(),
+                data: $response->getRespData(),
+            );
+        }
+    }
+}

+ 27 - 0
tests/Service/LaepIndustryServiceTest.php

@@ -0,0 +1,27 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\Lakala\Service;
+
+use PHPUnit\Framework\Attributes\Test;
+use PHPUnit\Framework\TestCase;
+
+class LaepIndustryServiceTest extends TestCase
+{
+    private LaepIndustryService $laepIndustryService;
+
+    protected function setUp(): void
+    {
+        $this->laepIndustryService = app(LaepIndustryService::class);
+    }
+
+    #[Test]
+    public function ewalletBalanceQuery()
+    {
+        $response = $this->laepIndustryService->ewalletBalanceQuery([
+            'merchantNo' => '822451048160BXH',
+            'payType' => '03',
+        ]);
+        dump($response);
+    }
+}