소스 검색

feat(lakala): 新增微信订单发货状态更新功能

- 在 WechatOrderCron 中引入并调用 ExpressService 获取物流信息
- 为 PaymentRecordService 添加 ExpressService 依赖注入
- 实现 updateWechatOrder 方法以同步微信订单发货状态
- 增加对订单发货信息上传逻辑处理及异常捕获
- 自动匹配物流公司并记录日志便于追踪未识别的物流公司
- 支持对微信平台上传包裹跟踪号、物流公司及商品描述等信息
runphp 4 달 전
부모
커밋
80760cef2b
2개의 변경된 파일68개의 추가작업 그리고 12개의 파일을 삭제
  1. 6 1
      src/Cron/WechatOrderCron.php
  2. 62 11
      src/Service/PaymentRecordService.php

+ 6 - 1
src/Cron/WechatOrderCron.php

@@ -5,13 +5,18 @@ namespace SixShop\Lakala\Cron;
 use SixShop\Core\Attribute\Cron;
 use SixShop\Lakala\Service\PaymentRecordService;
 use SixShop\Payment\Model\ExtensionPaymentModel;
+use SixShop\Wechat\Service\ExpressService;
 use think\db\Query;
 use think\facade\Db;
 
 class WechatOrderCron
 {
-    public function __construct(private PaymentRecordService $paymentRecordService)
+    public function __construct(
+        private PaymentRecordService $paymentRecordService,
+        private ExpressService $expressService,
+    )
     {
+        $this->expressService->getAllDelivery();
     }
 
     #[Cron('* */10 * * * *', 'lakala.shipping')]

+ 62 - 11
src/Service/PaymentRecordService.php

@@ -3,15 +3,24 @@ declare(strict_types=1);
 
 namespace SixShop\Lakala\Service;
 
+use app\model\Order;
 use app\model\OrderGoods;
+use SixShop\Core\Exception\LogicException;
 use SixShop\Lakala\Enum\WechatOrderStateEnum;
 use SixShop\Lakala\Model\LakalaWechatPaymentModel;
 use SixShop\Payment\Model\ExtensionPaymentModel;
+use SixShop\Wechat\Entity\WechatDeliveryEntity;
+use SixShop\Wechat\Service\ExpressService;
 use think\db\Query;
+use think\facade\Log;
 use think\Paginator;
 
 class PaymentRecordService
 {
+    public function __construct(private ExpressService $expressService)
+    {
+    }
+
     public function getTradeOrderList(array $params, array $pageAndLimit): Paginator
     {
         $paginator = ExtensionPaymentModel::alias('p')
@@ -51,20 +60,20 @@ class PaymentRecordService
             ->order('p.id', 'DESC')
             ->paginate($pageAndLimit);
         $paginator->each(function (ExtensionPaymentModel $item) {
-           if ($item['order_state'] == null) {
-               $wechatOrder = $this->createWechatOrder($item->id);
-               $item->setAttrs([
-                   'wechat_transaction_id' => $wechatOrder['wechat_transaction_id'],
-                   'order_state' => $wechatOrder['order_state'],
-                   'goods_desc' => $wechatOrder['goods_desc'],
-               ]);
-           }
-           $item->setAttr('order_state_text', WechatOrderStateEnum::from($item['order_state'])->toString());
+            if ($item['order_state'] == null) {
+                $wechatOrder = $this->createWechatOrder($item->id);
+                $item->setAttrs([
+                    'wechat_transaction_id' => $wechatOrder['wechat_transaction_id'],
+                    'order_state' => $wechatOrder['order_state'],
+                    'goods_desc' => $wechatOrder['goods_desc'],
+                ]);
+            }
+            $item->setAttr('order_state_text', WechatOrderStateEnum::from($item['order_state'])->toString());
         });
         return $paginator;
     }
 
-    public function createWechatOrder(int $paymentID):LakalaWechatPaymentModel
+    public function createWechatOrder(int $paymentID): LakalaWechatPaymentModel
     {
         $payment = ExtensionPaymentModel::find($paymentID);
         $goods = OrderGoods::where(['order_id' => $payment['order_id']])->find();
@@ -79,8 +88,50 @@ class PaymentRecordService
     /**
      * 更新微信发货状态
      */
-    public function updateWechatOrder(int $paymentID)
+    public function updateWechatOrder(int $paymentID): void
     {
+        $wechatPayment = LakalaWechatPaymentModel::where(['payment_id' => $paymentID])->find();
+        try {
+            $result = $this->expressService->queryOrder($wechatPayment->wechat_transaction_id);
+        } catch (LogicException $e) {
+            return;
+        }
+        $wechatState = WechatOrderStateEnum::from($result['order_state']);
+        if ($wechatState != $wechatPayment->order_state) {
+            $wechatPayment->order_state = $wechatState;
+            $wechatPayment->save();
+        }
+        if ($wechatState === WechatOrderStateEnum::PENDING_SHIPMENT) {
+            $payment = ExtensionPaymentModel::find($paymentID);
+            $goods = OrderGoods::where(['order_id' => $payment['order_id']])->find();
+            $order = Order::find($payment['order_id']);
+            if ($wechatPayment->express_company == null) {
+                $delivery = WechatDeliveryEntity::where(['delivery_name' => $order['express_name']])->findOrEmpty();
+                if ($delivery->isEmpty()) {
+                    Log::warning('未找到物流公司{transaction_id}', ['transaction_id' => $wechatPayment->wechat_transaction_id]);
+                    return;
+                }
+                $wechatPayment->express_company = $delivery['delivery_id'];
+                $wechatPayment->express_company_name = $delivery['delivery_name'];
+                $wechatPayment->save();
+            }
+            try {
+                $this->expressService->uploadShippingInfo(
+                    transactionID: $wechatPayment->wechat_transaction_id,
+                    openid: $payment['payment_result']['acc_resp_fields']['user_id'],
+                    shippingList: [
+                        'tracking_no' => $order['express_number'],
+                        'express_company' => $wechatPayment->express_company,
+                        'item_desc' => $goods['goods_name'],
+                        'contact' => [
+                            'receiver_contact' => substr_replace($order['mobile'], '****', -8, 4),
+                        ]
+                    ]
+                );
+            } catch (LogicException $e) {
+                return;
+            }
+        }
 
     }
 }