Kaynağa Gözat

feat(payment): 修改 PaymentProvider 接口,将 query 方法的参数从订单号改为记录 ID

runphp 6 ay önce
ebeveyn
işleme
ae5de02e99

+ 8 - 1
route/api.php

@@ -2,9 +2,16 @@
 declare(strict_types=1);
 
 
-use SixShop\Payment\Controller\PaymentController;
+use SixShop\Payment\Controller\{
+    PaymentController,
+    RecordController
+};
 use think\facade\Route;
 
 Route::resource('', PaymentController::class)->middleware([
     'auth'
 ]);
+
+Route::resource('record', RecordController::class)->middleware([
+    'auth'
+]);

+ 1 - 3
src/Contracts/PaymentProviderInterface.php

@@ -26,11 +26,9 @@ interface PaymentProviderInterface
 
     /**
      * 查询支付订单状态
-     * @param string $orderNo 订单号
-     * @return PaymentQueryResult
      * @throws PaymentException
      */
-    public function query(string $orderNo): PaymentQueryResult;
+    public function query(int $recordID): PaymentQueryResult;
 
     /**
      * 申请退款

+ 10 - 1
src/Contracts/PaymentResponse.php

@@ -3,9 +3,18 @@ declare(strict_types=1);
 
 namespace SixShop\Payment\Contracts;
 
-class PaymentResponse
+class PaymentResponse implements \JsonSerializable
 {
     public function __construct(public string $orderNo, public string $type, public array $raw)
     {
     }
+
+    public function jsonSerialize(): array
+    {
+        return [
+            'order_no' => $this->orderNo,
+            'type' => $this->type,
+            'raw' => $this->raw,
+        ];
+    }
 }

+ 2 - 1
src/Controller/PaymentController.php

@@ -6,11 +6,12 @@ use SixShop\Core\Helper;
 use SixShop\Payment\PaymentManager;
 use think\Response;
 use think\response\Json;
+use function SixShop\Core\success_response;
 
 class PaymentController
 {
     public function index(PaymentManager $paymentManager): Response
     {
-        return Helper::success_response($paymentManager->getAllPayment());
+        return success_response($paymentManager->getAllPayment());
     }
 }

+ 19 - 0
src/Controller/RecordController.php

@@ -0,0 +1,19 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\Payment\Controller;
+
+use SixShop\Payment\Entity\ExtensionPaymentEntity;
+use SixShop\Payment\PaymentManager;
+use think\Response;
+use function SixShop\Core\success_response;
+
+class RecordController
+{
+    public function read(int $id, ExtensionPaymentEntity $extensionPaymentEntity, PaymentManager $paymentManager): Response
+    {
+        $entity = $extensionPaymentEntity->read($id);
+
+        $paymentManager->query($entity->pay_type, $entity->id);
+        return success_response($entity);
+    }
+}

+ 14 - 0
src/Entity/ExtensionPaymentEntity.php

@@ -4,6 +4,7 @@ namespace SixShop\Payment\Entity;
 
 use SixShop\Core\Entity\BaseEntity;
 use SixShop\Payment\Model\ExtensionPaymentModel;
+use think\Model;
 
 /**
  * @mixin ExtensionPaymentModel
@@ -11,4 +12,17 @@ use SixShop\Payment\Model\ExtensionPaymentModel;
 class ExtensionPaymentEntity extends BaseEntity
 {
 
+    public function __construct(?Model $model = null)
+    {
+        parent::__construct($model);
+    }
+
+    public function read(int $id): self
+    {
+        $entity = $this->where('id', $id)->find();
+        if (!$entity) {
+            throw_logic_exception('支付记录不存在');
+        }
+        return $entity;
+    }
 }

+ 18 - 8
src/PaymentManager.php

@@ -2,6 +2,7 @@
 declare(strict_types=1);
 namespace SixShop\Payment;
 
+use SixShop\Payment\Contracts\PaymentQueryResult;
 use SixShop\Payment\Contracts\PaymentResponse;
 use SixShop\Payment\Entity\ExtensionPaymentEntity;
 use SixShop\Payment\Enum\PaymentBizEnum;
@@ -31,8 +32,8 @@ readonly class PaymentManager
     {
         /* @var PaymentInfo[] $paymentList */
         $paymentList = Event::trigger(GatheringPaymentEvent::class);
-        $paymentIds = array_column($paymentList, 'id');
-        $statusMap = ExtensionModel::whereIn('id', $paymentIds)->column(['status','id'], 'id', true);
+        $paymentIDs = array_column($paymentList, 'id');
+        $statusMap = ExtensionModel::whereIn('id', $paymentIDs)->column(['status','id'], 'id', true);
         $payTypeList = extension_config('payment', 'pay_type')??[];
         foreach ($paymentList as $payment) {
             $payment->status = $statusMap[$payment->id]['status'];
@@ -44,17 +45,26 @@ readonly class PaymentManager
     /**
      *  创建支付订单
      */
-    public function create($paymentId,  array $order, PaymentBizEnum $bizType = PaymentBizEnum::ORDER_PAY):  PaymentResponse
+    public function create($paymentID,  array $order, PaymentBizEnum $bizType = PaymentBizEnum::ORDER_PAY):  PaymentResponse
     {
-        $extension = $this->extensionManager->getExtension($paymentId);
-        Event::trigger(new BeforePayEvent($order, $paymentId, $bizType));
+        $extension = $this->extensionManager->getExtension($paymentID);
+        Event::trigger(new BeforePayEvent($order, $paymentID, $bizType));
         return $extension->getPaymentProvider()->create($order, $bizType);
     }
+    
+    /**
+     * 查询支付订单
+     */
+    public function query($paymentID, $recordID): PaymentQueryResult
+    {
+        $extension = $this->extensionManager->getExtension($paymentID);
+        return $extension->getPaymentProvider()->query($recordID);
+    }
 
     /**
      * 获取指定支付方式
      */
-    public function getPayment($paymentId):array
+    public function getPayment($paymentID):array
     {
         return [
             // todo
@@ -64,7 +74,7 @@ readonly class PaymentManager
     /**
      * 开启支付方式
      */
-    public function enablePayment($paymentId):bool
+    public function enablePayment($paymentID):bool
     {
         // todo
         return true;
@@ -73,7 +83,7 @@ readonly class PaymentManager
     /**
      * 关闭支付方式
      */
-    public function disablePayment($paymentId):bool
+    public function disablePayment($paymentID):bool
     {
         // todo
         return true;