上传物流信息到微信公众平台发货管理

上传物流信息到微信公众平台发货管理
日出的幻景   Common    6个月前
dd1jr

上传物流信息到微信公众平台发货管理

if (!function_exists('uploadShippingInfo')) {
    /**
     * 上传物流信息到微信公众平台发货管理
     *
     * @param int $order_id 订单Id
     * @return bool
     */
    function uploadShippingInfo(int $order_id = 0)
    {
        $client = new \GuzzleHttp\Client();
        $appid = config('appid');
        $secret = config('appsecret');
        $mchid = config('mch_id');
        $access_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid .
            '&secret=' . $secret;
        $accessTokenResponseData = $client->request('GET', $access_token_url);
        $accessTokenResponse = $accessTokenResponseData->getBody();
        $accessTokenResponse = json_decode($accessTokenResponse);
        $orderInfo=model('app\api\model\Order')
            ->field('id,user_id,order_no,transaction_id')
            ->where(['id' => $order_id])
            ->select();
        $orderGoods = model('app\api\model\OrderGoods')
            ->field('id,order_id,title,sku,number')
            ->where(['id' => $order_id])
            ->select();
        if (!empty($orderInfo['transaction_id'])) {
            $accessToken = $accessTokenResponse->access_token;
            $uploadShippingInfoUrl = 'https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=' . $accessToken;
            $openid = model('app\api\model\User')->where(['id' => $orderInfo['user_id']])->value('openid');
            $item_desc = '';
            foreach ($orderGoods as $item) {
                $item_desc .= $item['title']  . (!empty($item['sku']) ? ('_' . $item['sku']) : '')
                    . ' * ' . $item['number'] . '件;';
            }
            $query = [
                'body' => json_encode([
                    'order_key' => [
                        // 订单单号类型:1=商户侧单号,2=微信支付单号。
                        'order_number_type' => 1,
                        // 商户号
                        'mchid' => $mchid,
                        // 商户系统内部订单号,只能是数字、大小写字母`_-*`且在同一个商户号下唯一
                        'out_trade_no' => $orderInfo['order_no'],
                    ],
                    // 物流模式:1=物流配送,2=同城配送,3=虚拟商品,4=用户自提
                    'logistics_type' => 4,
                    // 发货模式:1=UNIFIED_DELIVERY(统一发货),2=SPLIT_DELIVERY(分拆发货)
                    'delivery_mode' => 1,
                    // 物流信息列表
                    'shipping_list' => [['item_desc' => $item_desc]],
                    // 上传时间: `2022-12-15T13:29:35.120+08:00`
                    'upload_time' => date('Y-m-d\TH:i:sP'),
                    // 支付者信息
                    'payer' => [
                        // 用户标识,用户在小程序appid下的唯一标识
                        'openid' => $openid
                    ]
                ], JSON_UNESCAPED_UNICODE)
            ];
            // dump($query);exit;
            $response = $client->request('POST', $uploadShippingInfoUrl, $query);
            $result = $response->getBody()->getContents();
            $result = !empty($result) ? json_decode($result) : null;
            if (!empty($result->errcode)) {
                write_log($result->errmsg);
            }
        }
        return true;
    }
}

小程序上传物流信息文档:https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html

参考链接:https://developers.weixin.qq.com/community/develop/doc/0002229d99c9d051155015d7f61800