kdn_helper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/8/9
  6. * Time: 下午7:44
  7. */
  8. require_once (BASE_CORE_PATH . "/framework/function/http.php");
  9. class kdn_helper
  10. {
  11. private static $EBusinessID = 1366783;
  12. private static $AppKey = "e62955d6-4871-4740-9ec9-62068a544ab5";
  13. private static $ReqURL = "http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
  14. //const TRACEAPI = "http://api.kdniao.cc/api/dis";
  15. public static function cur_businessid()
  16. {
  17. return self::$EBusinessID;
  18. }
  19. public static function query($shipperCode, $logisticCode)
  20. {
  21. $requestData = json_encode(array('ShipperCode' => $shipperCode,'LogisticCode' => $logisticCode));
  22. Log::record("EBusinessID=". self::$EBusinessID,Log::DEBUG);
  23. $datas = array(
  24. 'EBusinessID' => self::$EBusinessID,
  25. 'RequestType' => '8001',
  26. 'RequestData' => urlencode($requestData),
  27. 'DataType' => '2',
  28. 'DataSign' => self::encrypt($requestData, self::$AppKey)
  29. );
  30. $result = http_post_data(self::$ReqURL,$datas);
  31. return $result;
  32. }
  33. public static function onCallback($data)
  34. {
  35. $order_sn = $data['CallBack'];
  36. if(empty($order_sn)) return;
  37. $e_code = $data['ShipperCode'];
  38. $shipping_code = $data['LogisticCode'];
  39. $key = "express_{$e_code}_{$shipping_code}";
  40. wkcache($key, $data);
  41. // 数据解析
  42. switch ($data['State'])
  43. {
  44. case '1': // 已揽收
  45. Log::record(__METHOD__ . " order_sn={$order_sn},code = {$data['LogisticCode']} 已揽收.",Log::DEBUG);
  46. break;
  47. case '2': // 在途中
  48. Log::record(__METHOD__ . " order_sn={$order_sn},code = {$data['LogisticCode']} 在途中.",Log::DEBUG);
  49. break;
  50. case '3': // 已签收, 设置为已收货
  51. Log::record(__METHOD__ . " order_sn={$order_sn},code = {$data['LogisticCode']} 已签收.",Log::DEBUG);
  52. break;
  53. case '4': // 问题件
  54. Log::record(__METHOD__ . " order_sn={$order_sn},code = {$data['LogisticCode']} 问题件.",Log::ERR);
  55. break;
  56. default: {
  57. Log::record(__METHOD__ . ' query_status false');
  58. }
  59. }
  60. }
  61. public static function subscribe($order_sn)
  62. {
  63. $model_order = Model('order');
  64. $condition['order_sn'] = $order_sn;
  65. $order_info = $model_order->getOrderInfo($condition, array('order_common', 'order_goods'));
  66. // 发送快递鸟订阅回调
  67. $express = rkcache('express', true);
  68. $e_code = $express[$order_info['extend_order_common']['shipping_express_id']]['e_kdn_code'];
  69. if(empty($e_code)) {
  70. Log::record("找不到该单号的快递公司编码—— ordersn={$order_sn}",Log::ERR);
  71. return;
  72. }
  73. // 通知订阅, 快递鸟定时回调
  74. $ret = self::req_subscribe($e_code, $order_info['shipping_code'], $order_sn);
  75. if($ret === false) {
  76. Log::record("kdn_helper::subscribe order_sn={$order_sn} error.",Log::ERR);
  77. } else {
  78. Log::record("kdn_helper::subscribe ret={$ret}",Log::DEBUG);
  79. }
  80. }
  81. public static function req_subscribe($e_code, $logisticCode, $order_sn)
  82. {
  83. $datas = [
  84. 'ShipperCode' => $e_code,
  85. 'LogisticCode' => $logisticCode,
  86. 'Callback' => $order_sn
  87. ];
  88. $requestData = json_encode($datas);
  89. $datas = array(
  90. 'EBusinessID' => self::$EBusinessID,
  91. 'RequestType' => '8008',
  92. 'RequestData' => urlencode($requestData),
  93. 'DataType' => '2',
  94. 'DataSign' => self::encrypt($requestData, self::$AppKey)
  95. );
  96. $result = http_post_data(self::$ReqURL, $datas);
  97. return $result;
  98. }
  99. private static function encrypt($data, $appkey)
  100. {
  101. return urlencode(base64_encode(md5($data . $appkey)));
  102. }
  103. }