浏览代码

refactor(wechatpay):新增 waybillToken 方法,用于获取运单 token

runphp 6 月之前
父节点
当前提交
f0ad2d2727
共有 3 个文件被更改,包括 120 次插入69 次删除
  1. 27 3
      src/PaymentProvider.php
  2. 93 0
      src/Trait/MiniAppTrait.php
  3. 0 66
      src/Trait/UploadShippingInfoTrait.php

+ 27 - 3
src/PaymentProvider.php

@@ -23,6 +23,7 @@ use SixShop\Wechat\Facade\WechatUser;
 use SixShop\Wechat\Service\MiniApp;
 use SixShop\WechatPay\Job\QueryRefundJob;
 use SixShop\WechatPay\Trait\ApiTrait;
+use SixShop\WechatPay\Trait\MiniAppTrait;
 use SixShop\WechatPay\Trait\PaymentParamsTrait;
 use SixShop\WechatPay\Trait\UploadShippingInfoTrait;
 use think\facade\Db;
@@ -33,15 +34,15 @@ class PaymentProvider implements PaymentProviderInterface
 {
     private const string PAYMENT_TYPE = 'wechatpay';
     use ApiTrait;
-    use UploadShippingInfoTrait {
+    use MiniAppTrait {
         uploadShippingInfo as private uploadShippingInfoAPI;
+        waybillToken as private waybillTokenAPI;
     }
     use PaymentParamsTrait;
 
     public function __construct(
         private readonly ExtensionPaymentEntity $extensionPaymentEntity,
-        private readonly ExtensionRefundEntity  $extensionRefundEntity,
-        private readonly MiniApp                $miniApp,
+        private readonly ExtensionRefundEntity  $extensionRefundEntity
     )
     {
     }
@@ -217,4 +218,27 @@ class PaymentProvider implements PaymentProviderInterface
         }
         return $this->uploadShippingInfoAPI($order->out_trade_no, $order->user_id, $itemDesc, $trackingNo, $expressCompany, $receiverContact, $failException);
     }
+
+
+    /**
+     * 传运单接口 trace_waybill
+     */
+    public function waybillToken(
+        int $orderID,
+        string $receiverPhone,
+        string $waybillID,
+        string $deliveryID,
+        array $detailList,
+        bool   $failException = true
+    ): array
+    {
+        $order = $this->extensionPaymentEntity->where([
+            'order_id' => $orderID,
+            'status' => PaymentStatusEnum::SUCCESS
+        ])->findOrEmpty();
+        if ($order->isEmpty()) {
+            throw new \RuntimeException('支付订单不存在或未支付');
+        }
+        return $this->waybillTokenAPI($order->user_id, $receiverPhone, $waybillID, $deliveryID, $detailList, $failException);
+    }
 }

+ 93 - 0
src/Trait/MiniAppTrait.php

@@ -2,7 +2,9 @@
 
 namespace SixShop\WechatPay\Trait;
 
+use SixShop\Wechat\Facade\WechatUser;
 use SixShop\WechatPay\Facade\WechatPayBuilder;
+use think\facade\Log;
 
 trait MiniAppTrait
 {
@@ -28,4 +30,95 @@ trait MiniAppTrait
         }
         return true;
     }
+
+    /**
+     * 发货信息录入接口
+     *
+     * @param string $outTradeNo 商户订单号
+     * @param int $userID 用户ID
+     * @param string $itemDesc 商品描述
+     * @param string $trackingNo 物流单号
+     * @param string $expressCompany 物流公司编码
+     * @param string $receiverContact 收件人联系方式
+     * @param bool $failException 是否抛出异常
+     *
+     * @see https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html#%E4%B8%80%E3%80%81%E5%8F%91%E8%B4%A7%E4%BF%A1%E6%81%AF%E5%BD%95%E5%85%A5%E6%8E%A5%E5%8F%A3
+     */
+    private function uploadShippingInfo(
+        string $outTradeNo,
+        int    $userID,
+        string $itemDesc,
+        string $trackingNo,
+        string $expressCompany,
+        string $receiverContact,
+        bool   $failException = true
+    ): array
+    {
+        $data = [
+            'order_key' => [
+                'order_number_type' => 1, // 枚举值1,使用下单商户号和商户侧单号;枚举值2,使用微信支付单号
+                'mchid' => WechatPayBuilder::getConfig()->mchid,
+                'out_trade_no' => $outTradeNo,
+            ],
+            'logistics_type' => 1, // 物流模式,发货方式枚举值:1、实体物流配送采用快递公司进行实体物流配送形式 2、同城配送 3、虚拟商品,虚拟商品,例如话费充值,点卡等,无实体配送形式 4、用户自提
+            'delivery_mode' => 1, // 发货模式,发货模式枚举值:1、UNIFIED_DELIVERY(统一发货)2、SPLIT_DELIVERY(分拆发货) 示例值: UNIFIED_DELIVERY
+            'shipping_list' => [
+                [
+                    'tracking_no' => $trackingNo, // 物流单号
+                    'express_company' => $expressCompany, // 物流公司编码,快递公司ID
+                    'item_desc' => $itemDesc,
+                    'contact' => [
+                        'receiver_contact' => substr_replace($receiverContact, '****', -8, 4), // 收件人联系方式
+                    ],
+                ],
+            ],
+            'upload_time' => date('c'), // 上传时间,用于标识请求的先后顺序 示例值: `2022-12-15T13:29:35.120+08:00`
+            'payer' => [
+                'openid' => WechatUser::openid($userID), // 用户标识,用户在小程序appid下的唯一标识
+            ]
+        ];
+        $response = WechatPayBuilder::getMiniApp()->getClient()->postJson('wxa/sec/order/upload_shipping_info', $data);
+        $result = $response->getContent();
+        $result = json_decode($result, true);
+        if ($result['errcode'] !== 0) {
+            Log::error('更新发货信息失败{errcode}:{errmsg}:' . json_encode($data), $result);
+            $failException && throw new \RuntimeException($result['errmsg'], $result['errcode']);
+        }
+        return $result;
+    }
+
+    /**
+     * 传运单接口 trace_waybill
+     *
+     * @see https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/express/business/express_search.html
+     */
+    private function waybillToken(
+        int $userID,
+        string $receiverPhone,
+        string $waybillID,
+        string $deliveryID,
+        array $detailList,
+        bool   $failException = true
+    ): array
+    {
+        // POST https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/trace_waybill?access_token=XXX
+        $data = [
+            'openid' => WechatUser::openid($userID),
+            'receiver_phone' => $receiverPhone,
+            'waybill_id' => $waybillID,
+            'delivery_id' => $deliveryID,
+            'detail_list' => $detailList,
+            'goods_info' => [
+                'detail_list' => $detailList
+            ],
+        ];
+        $response = WechatPayBuilder::getMiniApp()->getClient()->postJson('cgi-bin/express/delivery/open_msg/trace_waybill', $data);
+        $result = $response->getContent();
+        $result = json_decode($result, true);
+        if ($result['errcode'] !== 0) {
+            Log::error('获取运单token失败{errcode}:{errmsg}:' . json_encode($data), $result);
+            $failException && throw new \RuntimeException($result['errmsg'], $result['errcode']);
+        }
+        return $result;
+    }
 }

+ 0 - 66
src/Trait/UploadShippingInfoTrait.php

@@ -1,66 +0,0 @@
-<?php
-
-namespace SixShop\WechatPay\Trait;
-
-use SixShop\Wechat\Facade\WechatUser;
-use SixShop\WechatPay\Facade\WechatPayBuilder;
-use think\facade\Log;
-
-trait UploadShippingInfoTrait
-{
-    /**
-     * 发货信息录入接口
-     *
-     * @param string $outTradeNo 商户订单号
-     * @param int    $userID     用户ID
-     * @param string $itemDesc   商品描述
-     * @param string $trackingNo 物流单号
-     * @param string $expressCompany 物流公司编码
-     * @param string $receiverContact 收件人联系方式
-     * @param bool   $failException 是否抛出异常
-     *
-     * @see https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html#%E4%B8%80%E3%80%81%E5%8F%91%E8%B4%A7%E4%BF%A1%E6%81%AF%E5%BD%95%E5%85%A5%E6%8E%A5%E5%8F%A3
-     */
-    private function uploadShippingInfo(
-        string $outTradeNo,
-        int    $userID,
-        string $itemDesc,
-        string $trackingNo,
-        string $expressCompany,
-        string $receiverContact,
-        bool   $failException = true
-    ): array
-    {
-        $data = [
-            'order_key' => [
-                'order_number_type' => 1, // 枚举值1,使用下单商户号和商户侧单号;枚举值2,使用微信支付单号
-                'mchid' => WechatPayBuilder::getConfig()->mchid,
-                'out_trade_no' => $outTradeNo,
-            ],
-            'logistics_type' => 1, // 物流模式,发货方式枚举值:1、实体物流配送采用快递公司进行实体物流配送形式 2、同城配送 3、虚拟商品,虚拟商品,例如话费充值,点卡等,无实体配送形式 4、用户自提
-            'delivery_mode' => 1, // 发货模式,发货模式枚举值:1、UNIFIED_DELIVERY(统一发货)2、SPLIT_DELIVERY(分拆发货) 示例值: UNIFIED_DELIVERY
-            'shipping_list' => [
-                [
-                    'tracking_no' => $trackingNo, // 物流单号
-                    'express_company' => $expressCompany, // 物流公司编码,快递公司ID
-                    'item_desc' => $itemDesc,
-                    'contact' => [
-                        'receiver_contact' => substr_replace($receiverContact, '****', -8, 4), // 收件人联系方式
-                    ],
-                ],
-            ],
-            'upload_time' => date('c'), // 上传时间,用于标识请求的先后顺序 示例值: `2022-12-15T13:29:35.120+08:00`
-            'payer' => [
-                'openid' => WechatUser::openid($userID), // 用户标识,用户在小程序appid下的唯一标识
-            ]
-        ];
-        $response = WechatPayBuilder::getMiniApp()->getClient()->postJson('wxa/sec/order/upload_shipping_info', $data);
-        $result = $response->getContent();
-        $result = json_decode($result, true);
-        if ($result['errcode'] !== 0) {
-            Log::error('更新发货信息失败{errcode}:{errmsg}:' . json_encode($data), $result);
-            $failException && throw new \RuntimeException($result['errmsg'], $result['errcode']);
-        }
-        return $result;
-    }
-}