Browse Source

中通快递接入

ANG YU 5 năm trước cách đây
mục cha
commit
a182d051bb

+ 1 - 1
app/Services/LogisticQiaoSFService.php → app/Services/LogisticSFService.php

@@ -8,7 +8,7 @@ use Exception;
 use Illuminate\Http\Client\Response;
 use Illuminate\Support\Facades\Http;
 
-class LogisticQiaoSFService
+class LogisticSFService
 {
     /**
      * 顺丰字段与数据库字段的映射关心

+ 33 - 0
app/Services/LogisticZopService.php

@@ -0,0 +1,33 @@
+<?php
+
+
+namespace App\Services;
+
+
+use App\library\zop\ZopClient;
+use App\library\zop\ZopProperties;
+use App\library\zop\ZopRequest;
+
+class LogisticZopService
+{
+    public function get(array $logisticNums)
+    {
+
+        $url = config('api_logistic.ZTO.url');
+        $xAppKey = config('api_logistic.ZTO.x-appKey');
+        $xDataDigest = config('api_logistic.ZTO.x-dataDigest');
+
+        $properties = new ZopProperties($xAppKey, $xDataDigest);
+        $client = new ZopClient($properties);
+        $request = new ZopRequest();
+
+        $request->setUrl($url);
+        $body = json_encode([
+            'type' => 0,
+            'orderCode' => '200824000005397109',
+            'billCode' => '73110263178916',
+        ]);
+        $request->setBody($body);
+        return json_decode($client->execute($request));
+    }
+}

+ 1 - 1
app/Services/OrderPackageReceivedSyncService.php

@@ -17,7 +17,7 @@ class OrderPackageReceivedSyncService
      */
     public function get(array $request)
     {
-        $this->logisticQiaoSFService = new LogisticQiaoSFService();
+        $this->logisticQiaoSFService = new LogisticSFService();
         $resultSF = [];
         $resultZT = [];
         $resultYD = [];

+ 23 - 0
app/library/zop/README.md

@@ -0,0 +1,23 @@
+# 中通开放平台SDK(php版)
+
+## 使用方式
+
+```php
+use App\library\zop\ZopClient;
+use App\library\zop\ZopProperties;
+use App\library\zop\ZopRequest;
+
+// ZopProperties类的构造方法接收两个参数,分别是companyid和key,都需要注册中通开放平台后到个人中心查看
+$properties = new ZopProperties("kfpttestCode", "kfpttestkey==");
+$client = new ZopClient($properties);
+
+$request = new ZopRequest();
+$request->setUrl("http://58.40.16.120:9001/submitOrderCode");
+$request->setData('{"data":{"content":{"branchId":"","buyer":"","collectMoneytype":"CNY","collectSum":"12.00","freight":"10.00","id":"xfs2018031500002222333","orderSum":"0.00","orderType":"1","otherCharges":"0.00","packCharges":"1.00","premium":"0.50","price":"126.50","quantity":"2","receiver":{"address":"育德路XXX号","area":"501022","city":"四川省,XXX,XXXX","company":"XXXX有限公司","email":"yyj@abc.com","id":"130520142097","im":"yangyijia-abc","mobile":"136*****321","name":"XXX","phone":"010-222***89","zipCode":"610012"},"remark":"请勿摔货","seller":"","sender":{"address":"华新镇华志路XXX号","area":"310118","city":"上海,上海市,青浦区","company":"XXXXX有限公司","email":"ll@abc.com","endTime":1369033200000,"id":"131*****010","im":"1924656234","mobile":"1391***5678","name":"XXX","phone":"021-87***321","startTime":1369022400000,"zipCode":"610012"},"size":"12,23,11","tradeId":"2701843","type":"1","typeId":"","weight":"0.753"},"datetime":"2018-3-30 12:00:00","partner":"test","verify":"ZTO123"}}');
+
+echo $client->execute($request);
+```
+
+## 其他
+1. 该项目刚创建,功能还很不完善,如有问题请提issue
+2. 由于中通开放平台各个接口返回值格式没有统一,所以未封装返回值,未来可能会做封装

+ 61 - 0
app/library/zop/ZopClient.php

@@ -0,0 +1,61 @@
+<?php
+
+
+namespace App\library\zop;
+use App\library\zop\ZopHttpUtil;
+
+class ZopClient
+{
+    private $zopProperties;
+
+    private $httpClient;
+    /**
+     * ZopClient constructor.
+     * @param $zopProperties
+     */
+    public function __construct($zopProperties)
+    {
+        $this->zopProperties = $zopProperties;
+        $this->httpClient = new ZopHttpUtil();
+    }
+
+    public function execute($zopRequest)
+    {
+        if($zopRequest->getBody()==null) {
+            $url = $zopRequest->getUrl();
+            $params = $zopRequest->getParams();
+            $fixedParams = array();
+            foreach ($params as $k => $v) {
+                if (gettype($v) != "string") {
+                    $fixedParams += [$k => json_encode($v)];
+                } else {
+                    $fixedParams += [$k => $v];
+                }
+            }
+            $str_to_digest = "";
+            foreach ($fixedParams as $k => $v) {
+                $str_to_digest = $str_to_digest .$k ."=" .$v ."&";
+            }
+            $str_to_digest = substr($str_to_digest, 0, -1) .$this->zopProperties->getKey();
+            $data_digest = base64_encode(md5($str_to_digest, TRUE));
+            $headers = array(
+                "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
+                "x-companyid: " .$this->zopProperties->getCompanyid(),
+                "x-datadigest: " .$data_digest
+            );
+            return $this->httpClient->post($url, $headers, http_build_query($fixedParams), 2000);
+        } else{
+            $url = $zopRequest->getUrl();
+            $body = $zopRequest->getBody();
+            $str_to_digest = $body.$this->zopProperties->getKey();
+            $data_digest = base64_encode(md5($str_to_digest, TRUE));
+            $headers = array(
+                "Content-Type: application/json; charset=UTF-8",
+                "x-companyid: ".$this->zopProperties->getCompanyid(),
+                "x-datadigest: ".$data_digest
+            );
+            return $this->httpClient->post($url, $headers, $body, 2000);
+
+        }
+    }
+}

+ 20 - 0
app/library/zop/ZopHttpUtil.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace App\library\zop;
+
+class ZopHttpUtil
+{
+    public function post($url, $headers, $querystring, $timeout)
+    {
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);//设置链接
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置HTTP头
+        curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
+        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $querystring);
+        $response = curl_exec($ch);
+        return $response;
+    }
+
+}

+ 46 - 0
app/library/zop/ZopProperties.php

@@ -0,0 +1,46 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: choco
+ * Date: 6/13/18
+ * Time: 5:47 PM
+ */
+namespace App\library\zop;
+
+
+class ZopProperties
+{
+    private $companyid;
+    private $key;
+
+    /**
+     * ZopProperties constructor.
+     * @param $companyid
+     * @param $key
+     */
+    public function __construct($companyid, $key)
+    {
+        $this->companyid = $companyid;
+        $this->key = $key;
+    }
+
+    /**
+     * @return mixed
+     */
+    public function getCompanyid()
+    {
+        return $this->companyid;
+    }
+
+    /**
+     * @return mixed
+     */
+    public function getKey()
+    {
+        return $this->key;
+    }
+
+
+
+
+}

+ 59 - 0
app/library/zop/ZopRequest.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace App\library\zop;
+
+
+class ZopRequest
+{
+    private $url;
+    private $params = Array();
+    private $body;
+
+    public function addParam($k, $v)
+    {
+        $this->params += [$k => $v];
+    }
+
+    /**
+     * @param mixed $url
+     */
+    public function setUrl($url)
+    {
+        $this->url = $url;
+    }
+
+    public function setData($data)
+    {
+        $this->params = json_decode($data);
+    }
+
+    public function setBody($body)
+    {
+        $this->body = $body;
+    }
+
+    public function getBody()
+    {
+        return $this->body;
+    }
+
+
+
+    /**
+     * @return mixed
+     */
+    public function getUrl()
+    {
+        return $this->url;
+    }
+
+    /**
+     * @return array
+     */
+    public function getParams()
+    {
+        return $this->params;
+    }
+
+
+}

+ 6 - 2
composer.json

@@ -59,7 +59,11 @@
             "database/factories"
         ],
         "files": [
-            "app/Utils/helpers.php"
+            "app/Utils/helpers.php",
+            "app/library/zop/ZopClient.php",
+            "app/library/zop/ZopHttpUtil.php",
+            "app/library/zop/ZopProperties.php",
+            "app/library/zop/ZopRequest.php"
         ]
     },
     "autoload-dev": {
@@ -81,4 +85,4 @@
             "@php artisan key:generate --ansi"
         ]
     }
-}
+}

+ 7 - 0
config/api_logistic.php

@@ -1,5 +1,6 @@
 <?php
 return [
+    //丰桥
     'SF' => [
         'url' =>  'https://bspsw.sf-express.com/sfexpressService',
         'head' =>  'BSGYLGL',
@@ -24,4 +25,10 @@ return [
         'received_code' => 80,
         'error_code' => 33,
     ],
+    //中通 https://op.zto.cn/#/Console?type=API
+    'ZTO' => [
+        'url' =>  'https://japi-test.zto.com/zto.open.getOrderInfo',
+        'x-appKey' => '67af93a877a027581b775',
+        'x-dataDigest' => 'c47eda396cc4d0739c45524a839f48c9',
+    ],
 ];

+ 2 - 2
tests/Services/LogisticQiaoSFService/LogisticQiaoSFServiceTest.php

@@ -2,7 +2,7 @@
 
 namespace LogisticQiaoSFService;
 
-use App\Services\LogisticQiaoSFService;
+use App\Services\LogisticSFService;
 use Tests\TestCase;
 
 class LogisticQiaoSFServiceTest extends TestCase
@@ -12,7 +12,7 @@ class LogisticQiaoSFServiceTest extends TestCase
     protected function setUp(): void
     {
         parent::setUp(); // TODO: Change the autogenerated stub
-        $this->logisticQiaoSFService = new LogisticQiaoSFService();
+        $this->logisticQiaoSFService = new LogisticSFService();
     }
 
 

+ 32 - 0
tests/Services/LogisticZopService/LogisticZopServiceTest.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace LogisticZopService;
+
+use App\Services\LogisticZopService;
+use Tests\TestCase;
+
+class LogisticZopServiceTest extends TestCase
+{
+    protected $logisticZopService;
+
+    protected function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        $this->logisticZopService = new LogisticZopService();
+    }
+
+    protected function tearDown(): void
+    {
+        parent::tearDown(); // TODO: Change the autogenerated stub
+    }
+
+    /**
+     * @test
+     */
+    public function get_test()
+    {
+        $result = $this->logisticZopService->get([]);
+        dd($result);
+    }
+
+}