Răsfoiți Sursa

feat(wechatpay): 添加小程序发货信息录入接口

runphp 6 luni în urmă
părinte
comite
5994fc9e6e
2 a modificat fișierele cu 93 adăugiri și 14 ștergeri
  1. 27 14
      src/PaymentProvider.php
  2. 66 0
      src/Trait/UploadShippingInfoTrait.php

+ 27 - 14
src/PaymentProvider.php

@@ -3,7 +3,6 @@ declare(strict_types=1);
 
 namespace SixShop\WechatPay;
 
-use EasyWeChat\Kernel\HttpClient\Response;
 use GuzzleHttp\Exception\ClientException;
 use SixShop\Core\Exception\NotFoundException;
 use SixShop\Payment\Contracts\PaymentNotifyResult;
@@ -25,14 +24,18 @@ use SixShop\Wechat\Service\MiniApp;
 use SixShop\WechatPay\Job\QueryRefundJob;
 use SixShop\WechatPay\Trait\ApiTrait;
 use SixShop\WechatPay\Trait\PaymentParamsTrait;
+use SixShop\WechatPay\Trait\UploadShippingInfoTrait;
 use think\facade\Db;
 use think\facade\Event;
 use function SixShop\Core\throw_logic_exception;
 
 class PaymentProvider implements PaymentProviderInterface
 {
-    const string PAYMENT_TYPE = 'wechatpay';
+    private const string PAYMENT_TYPE = 'wechatpay';
     use ApiTrait;
+    use UploadShippingInfoTrait {
+        uploadShippingInfo as private uploadShippingInfoAPI;
+    }
     use PaymentParamsTrait;
 
     public function __construct(
@@ -189,19 +192,29 @@ class PaymentProvider implements PaymentProviderInterface
     }
 
     /**
-     * 订单发货
-     *
-     * @see https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/ministore/minishopopencomponent2/API/delivery/send.html
+     * 发货信息录入
+     * @param int $orderID 订单ID
+     * @param string $itemDesc 商品描述
+     * @param string $trackingNo 运单号
+     * @param string $expressCompany 快递公司ID
+     * @param string $receiverContact 收件人手机号码
      */
-    public function deliverySend(int $orderID, array $deliveryList, int $finishAll = 1): Response
+    public function uploadShippingInfo(
+        int    $orderID,
+        string $itemDesc,
+        string $trackingNo,
+        string $expressCompany,
+        string $receiverContact,
+        bool   $failException = true
+    ): array
     {
-        $order = $this->orderEntity->find($orderID);
-        return $this->miniApp->getClient()->postJson('/shop/delivery/send', [
-            'out_order_id' => $order->out_trade_no,
-            'openid' => WechatUser::openid($order->user_id),
-            'finish_all_delivery' => $finishAll,
-            'delivery_list' => $deliveryList,
-            'ship_done_time' => date('Y-m-d H:i:s')
-        ]);
+        $order = $this->extensionPaymentEntity->where([
+            'order_id' => $orderID,
+            'status' => PaymentStatusEnum::SUCCESS
+        ])->findOrEmpty();
+        if ($order->isEmpty()) {
+            throw new \RuntimeException('支付订单不存在或未支付');
+        }
+        return $this->uploadShippingInfoAPI($order->out_trade_no, $order->user_id, $itemDesc, $trackingNo, $expressCompany, $receiverContact, $failException);
     }
 }

+ 66 - 0
src/Trait/UploadShippingInfoTrait.php

@@ -0,0 +1,66 @@
+<?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;
+    }
+}