编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

laravel德邦快递封装SDK(德邦快递包装服务)

wxchong 2024-07-05 02:13:21 开源技术 12 ℃ 0 评论

前一段时间对接德邦快递,就简单地封装了一下,现在分享给大家

<?php

namespace DepponSdk;
use GuzzleHttp\Client;
use DepponSdk\Lib\Exception;

class Deppon
{
    private $guzzleHttpClient = null;
    private $url;
    private $companyCode;
    private $appkey;
    private $logisticID;
    private $customerCode;
    private $timestamp;
    private function httpClient():Client
    {
        if (!$this->guzzleHttpClient instanceof Client) {
            $this->guzzleHttpClient = new Client();
        }
        return $this->guzzleHttpClient;
    }
    public function __construct()
    {
//        $this->url = 'http://dpsanbox.deppon.com/sandbox-web';//沙箱测试地址
        $this->url = 'http://dpapi.deppon.com/dop-interface-sync';//生产地址
        $this->companyCode = '';//替换自己的companyCode
        $this->appkey = '';//替换自己的appkey
        $this->logisticID = 'MRJW'.date('ymdhis');
        $this->customerCode = '';//替换自己的customerCode
        $this->logisticCompanyID = 'DEPPON';
    }
    private function makeBasisData($params)
    {
        $this->timestamp = $this->getMillisecond();
        return base64_encode(md5($params.$this->appkey.$this->timestamp));
    }
    //createOrderNotify.action
    public function createOrder($mustData)
    {
        $mustDataIdx = [
            'orderType', 'transportType','payType'
        ];
        foreach ($mustDataIdx as $v) {
            if (!isset($mustData[$v]) || !$mustData[$v]) {
                Exception::throw("the {$v} is empty");
            }
        }
        $addressIdx = [
            'name', 'mobile', 'province', 'city', 'county', 'address'
        ];
        foreach ($addressIdx as $v) {
            if (!isset($mustData['sender'][$v]) || !$mustData['sender'][$v]) {
                Exception::throw("the sender.{$v} is empty");
            }
            if (!isset($mustData['receiver'][$v]) || !$mustData['receiver'][$v]) {
                Exception::throw("the receiver.{$v} is empty");
            }
        }
        $packageInfoIdx = [
            'cargoName', 'totalNumber', 'totalWeight', 'deliveryType'
        ];
        foreach ($packageInfoIdx as $v) {
            if (!isset($mustData['packageInfo'][$v]) || !$mustData['packageInfo'][$v]) {
                Exception::throw("the packageInfo.{$v} is empty");
            }
        }
        if(!isset($mustData['addServices']['backSignBill']) || !$mustData['addServices']['backSignBill']){
            Exception::throw("the addServices.backSignBill is empty");
        }
        $data = [
            'logisticID' => $this->logisticID,
            'mailNo' => $mustData['mailNo'],
            'companyCode' => $this->companyCode,
            'customerCode' => $this->customerCode,
            'orderType' => $mustData['orderType'],
            'transportType' => $mustData['transportType'],
            'sender' => $mustData['sender'],
            'receiver' => $mustData['receiver'],
            'packageInfo' => $mustData['packageInfo'],
            'gmtCommit' => date('Y-m-d H:i:s'),
            'payType' => $mustData['payType'],
            'addServices' => $mustData['addServices'],
        ];
        $params = json_encode($data);
        $dd['params'] = $params;
        $dd['timestamp'] = $this->getMillisecond();
        $dd['companyCode'] = $this->companyCode;
        $dd['digest'] = base64_encode(md5($params.$this->appkey.$dd['timestamp']));
        return $this->post($this->url.'/dop-standard-ewborder/createOrderNotify.action', $dd);
    }

    //standard-order/queryOrder.action
    public function queryOrder($number) {
        if (!isset($number) || !$number) {
            Exception::throw("the logisticID is empty");
        }
        $data = [
            'logisticCompanyID' => $this->logisticCompanyID,
            'logisticID' => $number,
        ];
        $dd['params'] = json_encode($data);
        $dd['timestamp'] = $this->getMillisecond();
        $dd['companyCode'] = $this->companyCode;
        $dd['digest'] = base64_encode(md5($dd['params'].$this->appkey.$dd['timestamp']));
        $return = $this->post($this->url.'/standard-order/queryOrder.action', $dd);
        return json_decode($return, true);
    }
    //standard-order/newTraceQuery.action
    public function newTraceQuery($number) {
        if (!isset($number) || !$number) {
            Exception::throw("the logisticID is empty");
        }
        $data = [
            'mailNo' => $number
        ];
        $dd['params'] = json_encode($data);
        $dd['timestamp'] = $this->getMillisecond();
        $dd['companyCode'] = $this->companyCode;
        $dd['digest'] = base64_encode(md5($dd['params'].$this->appkey.$dd['timestamp']));
        $return = $this->post($this->url.'/standard-query/newTraceQuery.action', $dd);
        return json_decode($return, true);
    }
    /**
     * @param $mustData
     * @return mixed
     * @throws \Exception
     * 更新订单
     */
    //standard-order/updateOrder.action
    public function updateOrder($mustData)
    {
        $mustDataIdx = [
            'logisticID','cargoName','payType', 'transportType', 'vistReceive'
        ];
        foreach ($mustDataIdx as $v) {
            if (!isset($mustData[$v]) || !$mustData[$v]) {
                Exception::throw("the {$v} is empty");
            }
        }
        $addressIdx = [
            'name', 'mobile', 'province', 'city', 'county', 'address'
        ];
        foreach ($addressIdx as $v) {
            if (!isset($mustData['sender'][$v]) || !$mustData['sender'][$v]) {
                Exception::throw("the sender.{$v} is empty");
            }
            if (!isset($mustData['receiver'][$v]) || !$mustData['receiver'][$v]) {
                Exception::throw("the receiver.{$v} is empty");
            }
        }
        if(!isset($mustData['addServices']['backSignBill']) || !$mustData['addServices']['backSignBill']){
            Exception::throw("the addServices.backSignBill is empty");
        }
        $data = [
            'logisticCompanyID' => $this->logisticCompanyID,
            'logisticID' => $mustData['logisticID'],
            'orderSource' => $this->companyCode,
            'customerCode' => $this->customerCode,
            'sender' => $mustData['sender'],
            'receiver' => $mustData['receiver'],
            'gmtCommit' => date('Y-m-d H:i:s'),
            'cargoName' => $mustData['cargoName'],
            'payType' => $mustData['payType'],
            'transportType' => $mustData['transportType'],
            'vistReceive' => $mustData['vistReceive']
        ];
        $params = json_encode($data);
        $dd['params'] = $params;
        $dd['timestamp'] = $this->getMillisecond();
        $dd['companyCode'] = $this->companyCode;
        $dd['digest'] = base64_encode(md5($params.$this->appkey.$dd['timestamp']));
        $return =  $this->post($this->url.'/standard-order/updateOrder.action', $dd);
        return json_decode($return, true);
    }
    /**
     * 取消订单
     */
    //standard-order/cancelOrder.action
    public function cancelOrder($orderNumber, $memo)
    {
        if (!isset($orderNumber) || !$orderNumber) {
            Exception::throw("the mailNo is empty");
        }
        if (!isset($memo) || !$memo) {
            Exception::throw("the remark is empty");
        }
        $data = [
            'logisticCompanyID' => $this->logisticCompanyID,
            'mailNo' => $orderNumber,
            'cancelTime' => date('Y-m-d H:i:s'),
            'remark' => $memo,
        ];
        $params = json_encode($data);
        $dd['params'] = $params;
        $dd['timestamp'] = $this->getMillisecond();
        $dd['companyCode'] = $this->companyCode;
        $dd['digest'] = base64_encode(md5($params.$this->appkey.$dd['timestamp']));
        $return = $this->post($this->url.'/standard-order/cancelOrder.action', $dd);
        return json_decode($return, true);
    }
    private function getMillisecond() {
        list($t1, $t2) = explode(' ', microtime());
        return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
    }
    public function post($url, $params) {
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query($params) );
        $return = curl_exec ( $ch );
        curl_close ( $ch );
        return $return;
    }
}
/**
     * @param $data
     * @return mixed
     * 下单
     */
private function depponOrderCreate($data)
    {
        $deppon = new Deppon();
        $order = [
            'mailNo' => 'DB'.$data['order_no'],
            'orderType' => 2,
            'transportType' => 'RCP',//具体传值请与月结合同签订约定的为准
            'sender' => [
                'name' => '张三',
                'mobile' => '13575745195',
                'province' => '上海',
                'city' => '上海',
                'county' => '浦东区',
                'address' => '新龙科技大厦9层',
            ],
            'receiver' => [
                'name' => $data['customer']['name'],
                'mobile' => $data['customer']['contact_mobilephone'],
                'province' => $data['customer']['province'],
                'city' => $data['customer']['city'],
                'county' => $data['customer']['county'],
                'address' => $data['customer']['address'],
            ],
            'packageInfo' => [
                'cargoName'=>$data['products'][0]['item_name'],
                'totalNumber'=>$data['totalNumber'],
                'totalWeight'=>$data['totalWeight'],
                'deliveryType'=>4,
            ],
            'payType' => 2,
            'addServices' => ['backSignBill' => 2]
        ];
        $result =$deppon->createOrder($order);
        return json_decode($result, true);
    }
/**
     * 取消订单
     * order_number   快递单号
     * remark    备注
     * @param FormRequest $request
     * @return mixed
     */
    public function cancelDepponExpress($orderNumber, $memo) {
        $remark = $memo ? $memo : '订单取消';
        $yto = new Deppon();
        $result = $yto->cancelOrder($orderNumber, ['remark'=> $remark]);
        if($result['result']){
            //成功
            return $this->failed('取消订单成功!');
        }else{
            return $this->failed('取消订单失败!'.$result['msg']);
        }
    }
/**
     * @param FormRequest $request
     * @return mixed
     * 查询
     */
    public function queryDeppon(FormRequest $request){
        $orderNumber = $request->input('express_number');
        $yto = new Deppon();
        $result = $yto->newTraceQuery($orderNumber);
        if($result['result'] == 'true'){
            $newarr = [];
            foreach($result['responseParam']['trace_list'] as $v){
                $arr['Waybill_No'] = $orderNumber;
                $arr['Upload_Time'] = $v['time'];
                $arr['ProcessInfo'] = $v['description'];
                array_push($newarr, $arr);
            }
            $return = [
                'express_type'=>2,
                'list'=>$newarr,
            ];
            return $this->success($return);
        }else{
            return $this->failed('查询失败2!'.$result['reason']);
        }
    }

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表