소스 검색

feat(alibaba): 添加免密支付功能并完善订单服务

- 在 OrderService 中新增 prepareProtocolPay 方法实现免密支付准备功能
- 添加 formatProtocolPayResult 方法格式化免密支付返回结果
- 在 StoreGroupOrderRepository 中添加 alibabaOrderId 字段存储1688订单ID
- 在 StoreOrderEntity 中增加 alibabaOrderId 属性及对应的 getter/setter 方法
- 完善日志记录,添加预下单和创建订单的API调用成功日志
- 实现免密支付接口的参数校验和业务逻辑处理
shichen 3 달 전
부모
커밋
7613584a71

+ 3 - 1
app/common/repositories/store/order/StoreGroupOrderRepository.php

@@ -742,7 +742,7 @@ class StoreGroupOrderRepository extends BaseRepository
742 742
             //     $addressEntity->getCityId()
743 743
             // );
744 744
             $postage = 0.00; // 默认全都包邮s
745
-
745
+            $alibabaOrderId = null;
746 746
             // 1688商品要获取运费
747 747
             if ($productEntity->getIsAlibaba() == 1) {
748 748
                 // 地址数据
@@ -767,6 +767,7 @@ class StoreGroupOrderRepository extends BaseRepository
767 767
                 // 创建1688订单
768 768
                 $flow = $alibabaOrder['preview']['flowFlag'] ?? 'general';
769 769
                 $createOrderRes = $orderService->fastCreateOrder(['cargoParamList' => $cargoParamList, 'addressParam' => $addressParam,'flow'=>$flow]);
770
+                if ($createOrderRes['success']) $alibabaOrderId = $createOrderRes['orderId'];
770 771
             }
771 772
 
772 773
 
@@ -836,6 +837,7 @@ class StoreGroupOrderRepository extends BaseRepository
836 837
                         ->setAttrValuePensionNum($productAttrValueEntity->getPensionNum())
837 838
                         ->toArray()
838 839
                 )->setPickUpId($storeOrderCreateRequestEntity->getPickUpId())
840
+                ->setAlibabaOrderId($alibabaOrderId)
839 841
                 ->setMark($storeOrderCreateRequestEntity->getMark());
840 842
             $storeOrderEntityList[] = $storeOrderEntity;
841 843
         }

+ 13 - 0
app/entity/data/store/StoreOrderEntity.php

@@ -103,6 +103,8 @@ class StoreOrderEntity extends DataEntity
103 103
 
104 104
     public $pickUpId = null;
105 105
 
106
+    public $alibabaOrderId = null;
107
+
106 108
     /**
107 109
      * @return mixed
108 110
      */
@@ -1784,6 +1786,17 @@ class StoreOrderEntity extends DataEntity
1784 1786
         return $this;
1785 1787
     }
1786 1788
 
1789
+    public function getAlibabaOrderId()
1790
+    {
1791
+        return $this->alibabaOrderId;
1792
+    }
1793
+
1794
+    public function setAlibabaOrderId($alibabaOrderId): StoreOrderEntity
1795
+    {
1796
+        $this->alibabaOrderId = $alibabaOrderId;
1797
+        return $this;
1798
+    }
1799
+
1787 1800
     public function deconstructionFzonePaytype()
1788 1801
     {
1789 1802
         if (!empty($this->getFzonePayType()) && is_array(json_decode($this->getFzonePayType(), true))) {

+ 81 - 0
app/services/ThirdParty/AlibabaAgent/OrderService.php

@@ -69,6 +69,8 @@ class OrderService extends AlibabaAgentBaseService
69 69
             return false;
70 70
         }
71 71
 
72
+        Log::channel('alibaba')->info('[预下单] API请求成功', ['result' => $result]);
73
+
72 74
         return $this->formatPreviewResult($result);
73 75
     }
74 76
 
@@ -198,10 +200,89 @@ class OrderService extends AlibabaAgentBaseService
198 200
             return false;
199 201
         }
200 202
 
203
+        Log::channel('alibaba')->info('[创建订单] API请求成功', ['result' => $result]);
204
+
201 205
         return $this->formatCreateOrderResult($result);
202 206
     }
203 207
 
204 208
     /**
209
+     * 免密支付准备
210
+     *
211
+     * API: com.alibaba.trade:alibaba.trade.pay.protocolPay.preparePay-1
212
+     *
213
+     * 接口文档: https://open.1688.com/api/apidocdetail.htm?id=com.alibaba.trade%3Aalibaba.trade.pay.protocolPay.preparePay-1
214
+     *
215
+     * 请求参数说明(接口文档定义):
216
+     *   - tradeWithholdPreparePayParam: 必填,类型 message:alibaba.trade.pay.withhold.preparePayParam
217
+     *     preparePayParam 结构:
218
+     *       - orderId: 订单ID(Long,必填)
219
+     *
220
+     * 调用示例:
221
+     *   $orderService->prepareProtocolPay([
222
+     *     'orderId' => 123456789,
223
+     *   ]);
224
+     *
225
+     * @param array $params 免密支付参数(orderId)
226
+     * @return array|false
227
+     */
228
+    public function prepareProtocolPay(array $params = [])
229
+    {
230
+        // --- 参数校验 ---
231
+        if (empty($params['orderId'])) {
232
+            Log::channel('alibaba')->error('[免密支付] orderId不能为空');
233
+            return false;
234
+        }
235
+
236
+        // --- 构建 tradeWithholdPreparePayParam(message类型需序列化为JSON字符串) ---
237
+        $businessParams = [
238
+            'tradeWithholdPreparePayParam' => json_encode([
239
+                'orderId' => (int)$params['orderId'],
240
+            ], JSON_UNESCAPED_UNICODE),
241
+        ];
242
+
243
+        // --- 调用API ---
244
+        $result = $this->executeParam2(
245
+            self::NAMESPACE,
246
+            'alibaba.trade.pay.protocolPay.preparePay',
247
+            1,
248
+            $businessParams
249
+        );
250
+
251
+        if ($result === false) {
252
+            Log::channel('alibaba')->error('[免密支付] API请求失败', $businessParams);
253
+            return false;
254
+        }
255
+
256
+        Log::channel('alibaba')->info('[免密支付] API请求成功', ['result' => $result]);
257
+
258
+        return $this->formatProtocolPayResult($result);
259
+    }
260
+
261
+    /**
262
+     * 格式化免密支付准备返回结果
263
+     *
264
+     * @param array $result API原始返回数据
265
+     * @return array
266
+     */
267
+    protected function formatProtocolPayResult(array $result): array
268
+    {
269
+        // 返回结构: { "result": { ... }, "success": true }
270
+        $data = $result['result'] ?? $result;
271
+        $success = $result['success'] ?? false;
272
+
273
+        Log::channel('alibaba')->info('[免密支付] 返回结果', [
274
+            'success' => $success,
275
+            'data'    => $data,
276
+        ]);
277
+
278
+        return [
279
+            'success' => $success,
280
+            'data'    => $data,
281
+            'rawData' => $result,
282
+        ];
283
+    }
284
+
285
+    /**
205 286
      * 格式化创建订单返回结果
206 287
      *
207 288
      * @param array $result API原始返回数据