kdn_helper.php 3.9 KB

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