达达快递API接口示例

2023-12-13 09:29:16
<?php

function generateSignedStr($app_key, $body, $format, $version, $source_id, $app_secret) {
    // 获取当前时间的时间戳
    $timestamp = time();

    // 构建数据数组
    $data = array(
        "app_key" => $app_key,
        "body" => $body,
        "format" => $format,
        "source_id" => $source_id,
        "timestamp" => $timestamp,
        "v" => $version
    );

    // 按照键名对数组进行字典顺序排序
    ksort($data);

    // 初始化字符串
    $concatenatedString = '';

    // 遍历数组并拼接键值对
    foreach ($data as $key => $value) {
        $concatenatedString .= $key . $value;
    }

    // 拼接app_secret
    $finalString = $app_secret . $concatenatedString . $app_secret;

    // 对字符串进行MD5加密并转换为大写
    return strtoupper(md5($finalString));
}

// 示例使用
$app_key = 'dada6612f445e22d755'; //开发文档中查看
$body = '{"shop_no":"721330-9952885","origin_id":"20231211172336","cargo_price":19.99,"is_prepay":0,"receiver_name":"接收人姓名","receiver_address":"郑州市中原区","receiver_lat":"34.783583","receiver_lng":"113.597151","receiver_phone":"15225162266","callback":"http://www.xxxx.com/cb.php","cargo_weight":1}';
$format = 'json';
$version = '1.0';
$source_id = ''; //开发文档中查看
$app_secret = 'edeb3632c1107860bef8d4a9663a8054';

// 调用函数并输出结果
$sign = generateSignedStr($app_key, $body, $format, $version, $source_id, $app_secret);

// API 的 URL
$url = "https://newopen.imdada.cn/api/order/queryDeliverFee";

// 请求头信息
$headers = array(
    "Content-Type: application/json"
);

// 请求体
$jsonData = array(
    "app_key" => $app_key,
    "body" => $body,
    "format" => $format,
    "source_id" => $source_id,
    "signature" => $sign,
    "timestamp" => time(),
    "v" => $version
);

// 初始化 curl
$ch = curl_init($url);

// 设置 curl 选项
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // 设置请求头
curl_setopt($ch, CURLOPT_POST, true); // 设置为 POST 请求
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($jsonData)); // 设置 POST 数据
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回响应,而不是输出
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 如果你的 PHP 环境不支持 SSL,你可能需要关闭 SSL 验证

// 发送请求并获取响应
$response = curl_exec($ch);

// 关闭 curl
curl_close($ch);

// 输出响应
echo $response;

备注:callback为回调URL;

<?php

class DaDaCallbackListener {

    /**
     * 接收并处理达达回调消息
     *
     * @param string $dadaCallbackStr 达达回调原始参数的 JSON 字符串
     */
    public static function onReceiveMsg($dadaCallbackStr) {
        // JSON 字符串转对象
        $callbackParam = json_decode($dadaCallbackStr);

        // 验证签名
        self::verifySignature($callbackParam);

        // 签名通过,进行自己的业务逻辑处理
        self::doBizLogic($callbackParam);
    }

    /**
     * 执行商户自己的回调业务逻辑(比如落表)
     * @param object $callbackParam
     */
    private static function doBizLogic($callbackParam) {
        // 使用error_log或者其他日志记录方法记录信息
        error_log("收到达达回调, 订单号: {$callbackParam->order_id}, 达达单号: {$callbackParam->client_id}, 订单状态: {$callbackParam->order_status}, 时间: " . self::parseUpdateTime($callbackParam) . ", 骑士id: {$callbackParam->dm_id}, 骑士姓名: {$callbackParam->dm_name}, 骑士手机号: {$callbackParam->dm_mobile}");
    }

    /**
     * 解析更新时间
     *
     * @param object $callbackParam
     * @return string
     */
    private static function parseUpdateTime($callbackParam) {
        $updateTime = $callbackParam->update_time;
        if ($callbackParam->order_status == 1000) { // 达达创建运单失败的情况下单位是毫秒
            return date("Y-m-d H:i:s", $updateTime / 1000);
        } else { // 其他情况下单位是秒
            return date("Y-m-d H:i:s", $updateTime);
        }
    }

    /**
     * 验证签名是否正确
     */
    private static function verifySignature($callbackParam) {
        // 根据参数计算出期望的正确签名
        $expectSign = self::generateSignature($callbackParam);

        // 实际收到的签名
        $actualSign = $callbackParam->signature;

        // 期望签名结果和实际签名结果比较
        if ($expectSign !== $actualSign) {
            throw new Exception("签名校验不通过");
        }
    }

    /**
     * 根据相关参数计算回调的签名
     */
    private static function generateSignature($callbackParam) {
        $clientId = isset($callbackParam->client_id) ? $callbackParam->client_id : "";
        $orderId = isset($callbackParam->order_id) ? $callbackParam->order_id : "";
        $updateTime = isset($callbackParam->update_time) ? $callbackParam->update_time : "";

        // 将签名相关字段加入数组
        $list = array($clientId, $orderId, $updateTime);

        // 将参与签名的字段的值进行升序排列
        sort($list);

        // 将排序过后的参数,进行字符串拼接
        $joinedStr = implode("", $list);

        // 对拼接后的字符串进行md5加密
        return md5($joinedStr);
    }
}

// 示例使用
$arriveShopCallbackStr = "{\"signature\":\"2b3aa825562bcea9471fe860b3f8a8fa\",\"client_id\":\"1357061741749469184\",\"order_id\":\"Cu16538768403935339353\",\"order_status\":100,\"cancel_reason\":\"\",\"cancel_from\":0,\"dm_id\":170900,\"dm_name\":\"张三\",\"dm_mobile\":\"18500000000\",\"update_time\":1653876860,\"transporter_lng\":121.526008,\"transporter_lat\":31.220394,\"transporter_type\":1,\"is_finish_code\":false}"; // 骑士到店消息示例 JSON 字符串
DaDaCallbackListener::onReceiveMsg($arriveShopCallbackStr);

$orderCreateFailedCallbackStr = "{\"signature\":\"cd6339c2b4f481d5515831d53ff43ccc\",\"client_id\":\"\",\"order_id\":\"948220531221045216\",\"order_status\":1000,\"cancel_reason\":\"您所选的发货地址和收货地址处于疫情封控中,达达骑?将?法为您提供服务\",\"cancel_from\":3,\"dm_id\":0,\"dm_name\":\"\",\"dm_mobile\":\"\",\"update_time\":1654006323146}"; // 订单创建失败消息示例 JSON 字符串
DaDaCallbackListener::onReceiveMsg($orderCreateFailedCallbackStr);

文章来源:https://blog.csdn.net/quweiie/article/details/134962864
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。