lionared пре 6 година
родитељ
комит
45e1b82da1
3 измењених фајлова са 118 додато и 80 уклоњено
  1. 79 0
      helper/kdn_helper.php
  2. 27 26
      mobile/kdniao_notify.php
  3. 12 54
      test/TestKdniao.php

+ 79 - 0
helper/kdn_helper.php

@@ -114,3 +114,82 @@ class kdn_helper
     }
 }
 
+class kdn_helper_new
+{
+    const EBusinessID = 1366783;
+    const AppKey    = "e62955d6-4871-4740-9ec9-62068a544ab5";
+    const ReqURL    = "http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";
+    const TRACEAPI  = "http://api.kdniao.cc/api/dis";
+
+    public static function cur_businessid()
+    {
+        return self::EBusinessID;
+    }
+
+    public static function query($shipperCode, $logisticCode)
+    {
+        $requestData = json_encode(array('ShipperCode' => $shipperCode,'LogisticCode' => $logisticCode));
+
+        Log::record("EBusinessID=". self::EBusinessID,Log::DEBUG);
+        $datas = array(
+            'EBusinessID' => self::EBusinessID,
+            'RequestType' => '8001',
+            'RequestData' => urlencode($requestData),
+            'DataType' => '2',
+            'DataSign' => self::encrypt($requestData, self::AppKey)
+        );
+        $result = http_post_data(self::ReqURL,$datas);
+        return $result;
+    }
+
+    public static function onCallback($data)
+    {
+        $order_sn = $data['CallBack'];
+        if(empty($order_sn)) return;
+
+        $e_code = $data['ShipperCode'];
+        $shipping_code = $data['LogisticCode'];
+        $key = "express_{$e_code}_{$shipping_code}";
+        wkcache($key, $data);
+        // 数据解析
+        switch ($data['State'])
+        {
+            case '3':	// 已签收, 设置为已收货
+                Log::record(__METHOD__ . " order_sn={$order_sn},code = {$data['LogisticCode']} 已签收.",Log::ERR);
+                break;
+            case '4':	// 问题件
+                Log::record(__METHOD__ . " order_sn={$order_sn},code = {$data['LogisticCode']} 问题件.",Log::ERR);
+                break;
+            default: {
+                Log::record(__METHOD__ . ' query_status false');
+                break;
+            }
+        }
+    }
+
+    public static function req_subscribe($e_code, $logisticCode, $order_sn)
+    {
+        $datas = [
+            'ShipperCode'   => $e_code,
+            'LogisticCode'  => $logisticCode,
+            'Callback'      => $order_sn
+        ];
+
+        $requestData = json_encode($datas);
+
+        $datas = array(
+            'EBusinessID' => self::EBusinessID,
+            'RequestType' => '8008',
+            'RequestData' => urlencode($requestData),
+            'DataType' => '2',
+            'DataSign' => self::encrypt($requestData, self::AppKey)
+        );
+        $result = http_post_data(self::ReqURL, $datas);
+        return $result;
+    }
+
+    private static function encrypt($data, $appkey)
+    {
+        return urlencode(base64_encode(md5($data . $appkey)));
+    }
+}

+ 27 - 26
mobile/kdniao_notify.php

@@ -2,42 +2,43 @@
 ini_set("display_errors",1);
 error_reporting(E_ALL);
 
+defined('BASE_ROOT_PATH') or define('BASE_ROOT_PATH',str_replace('/mobile','',dirname(__FILE__)));
 //require_once(BASE_MOBILE_PATH . '/control/app_pay.php');
-//require_once (BASE_DATA_PATH . '/logic/delivery.logic.php');
-//require_once(BASE_ROOT_PATH . '/helper/kdn_helper.php');
-require_once(BASE_ROOT_PATH . '/test/TestKdniao.php');
-
-use Log;
+require_once (BASE_DATA_PATH . '/logic/delivery.logic.php');
+require_once(BASE_ROOT_PATH . '/helper/kdn_helper.php');
 
 Log::record("快递鸟回调:", Log::DEBUG);
-$requestData = $_REQUEST['RequestData'];  // 快递鸟数据
-$deliver_info = json_decode(urldecode($requestData),true);
+$requestData = isset($_POST['RequestData']) ? $_POST['RequestData'] : null;  // 快递鸟数据
 
-$success = true;
-$reson = '';
-//数据不正确, 记录并退出
-if ($deliver_info === false || empty($deliver_info))
-{
-    $success = false;
-    $reson = 'data is empty.';
-    Log::record("kdniao_notify:cannot query delivery info from kuaidn.",Log::ERR);
-}
-else
+if (!empty($requestData))
 {
-    Log::record("start handle....",Log::DEBUG);
-    $count = intval($deliver_info['Count']);
-    if($count <= 0 || $deliver_info['EBusinessID'] != kdn_helper::cur_businessid()) {
+    $deliver_info = json_decode(urldecode($requestData),true);
+
+    $success = true;
+    $reson = '';
+//数据不正确, 记录并退出
+    if ($deliver_info === false || empty($deliver_info))
+    {
         $success = false;
-        $reson = "count={$count} or EBusinessID = {$deliver_info['EBusinessID']} != " . kdn_helper::cur_businessid();
+        $reson = 'data is empty.';
+        Log::record("kdniao_notify:cannot query delivery info from kuaidn.",Log::ERR);
     }
     else
     {
-        foreach($deliver_info['Data'] as $item) {
-            kdn_helper::onCallback($item);
+        Log::record("start handle....",Log::DEBUG);
+        $count = intval($deliver_info['Count']);
+        if($count <= 0 || $deliver_info['EBusinessID'] != kdn_helper_new::cur_businessid()) {
+            $success = false;
+            $reson = "count={$count} or EBusinessID = {$deliver_info['EBusinessID']} != " . kdn_helper_new::cur_businessid();
+        }
+        else
+        {
+            foreach($deliver_info['Data'] as $item) {
+                kdn_helper_new::onCallback($item);
+            }
         }
     }
-}
-
-echo (json_encode(array('EBusinessID' => kdn_helper::cur_businessid(),'UpdateTime' => strftime("%Y-%m-%d %H:%M:%S",time()), 'Success' => $success,'Reason' => $reson)));
 
+    echo (json_encode(array('EBusinessID' => kdn_helper_new::cur_businessid(),'UpdateTime' => strftime("%Y-%m-%d %H:%M:%S",time()), 'Success' => $success,'Reason' => $reson)));
+}
 ?>

+ 12 - 54
test/TestKdniao.php

@@ -6,69 +6,27 @@
  * Time: 上午10:27
  */
 
-define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
+defined('BASE_ROOT_PATH') or define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
 
 require_once(BASE_ROOT_PATH . '/fooder.php');
-require_once (BASE_CORE_PATH . "/framework/function/http.php");
-use Log;
+require_once(BASE_ROOT_PATH . '/helper/kdn_helper.php');
 
-class kdn_helper
+class TestKdniao extends PHPUnit_Framework_TestCase
 {
-    const EBusinessID = 1366783;
-    const AppKey    = "e62955d6-4871-4740-9ec9-62068a544ab5";
-    const ReqURL    = "http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";
-    const TRACEAPI  = "http://api.kdniao.cc/api/dis";
-
-    public static function cur_businessid()
-    {
-        return self::EBusinessID;
-    }
-
-    public static function onCallback($data)
-    {
-        $order_sn = $data['CallBack'];
-        if(empty($order_sn)) return;
-
-        $e_code = $data['ShipperCode'];
-        $shipping_code = $data['LogisticCode'];
-        $key = "express_{$e_code}_{$shipping_code}";
-
-        Log::record("kdn_helper::onCallback: key === {$key}, full data ===".json_encode($data));
-    }
-
-    public static function req_subscribe($e_code, $logisticCode, $order_sn)
+    public static function setUpBeforeClass()
     {
-        $datas = [
-            'ShipperCode'   => $e_code,
-            'LogisticCode'  => $logisticCode,
-            'Callback'      => $order_sn
-        ];
-
-        $requestData = json_encode($datas);
-
-        $datas = array(
-            'EBusinessID' => self::EBusinessID,
-            'RequestType' => '8008',
-            'RequestData' => urlencode($requestData),
-            'DataType' => '2',
-            'DataSign' => self::encrypt($requestData, self::AppKey)
-        );
-        $result = http_post_data(self::ReqURL, $datas);
-        return $result;
+        Base::run_util();
     }
 
-    private static function encrypt($data, $appkey)
+    public function testQuery()
     {
-        return urlencode(base64_encode(md5($data . $appkey)));
-    }
-}
+        //$order_sn   = "9000000002470048";
+        $e_code     = "YTO";
+        $logisticCode   = "886670260037510426";
 
+        $ret = kdn_helper_new::query($e_code, $logisticCode);
 
-class TestKdniao extends PHPUnit_Framework_TestCase
-{
-    public static function setUpBeforeClass()
-    {
-        Base::run_util();
+        var_dump($ret);
     }
 
     public function testSubscribe()
@@ -77,7 +35,7 @@ class TestKdniao extends PHPUnit_Framework_TestCase
         $e_code     = "YTO";
         $logisticCode   = "886670260037510426";
 
-        $subs = kdn_helper::req_subscribe($e_code, $logisticCode, $order_sn);
+        $subs = kdn_helper_new::req_subscribe($e_code, $logisticCode, $order_sn);
 
         var_dump($subs);
     }