kdn_helper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/3/30
  6. * Time: 下午5:10
  7. */
  8. require_once (BASE_CORE_PATH . "/framework/function/http.php");
  9. class kdn_helper
  10. {
  11. const req_url = 'http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx';
  12. //1256766 22c88d88-3565-4023-b5ac-0201792869ed
  13. //1256767 e02cd836-3146-4958-b1dc-c1585fa05b9e
  14. //1256768 e7eb7d2f-0525-484e-80ba-c8f5b5a90af3
  15. static public function query($shipperCode, $logisticCode)
  16. {
  17. $requestData = json_encode(array('ShipperCode' => $shipperCode,'LogisticCode' => $logisticCode));
  18. $account = self::get_account();
  19. Log::record("EBusinessID={$account}",Log::DEBUG);
  20. $datas = array(
  21. 'EBusinessID' => $account['EBusinessID'],
  22. 'RequestType' => '1002',
  23. 'RequestData' => urlencode($requestData),
  24. 'DataType' => '2',
  25. 'DataSign' => self::encrypt($requestData, $account['AppID'])
  26. );
  27. $result = http_post_data(self::req_url,$datas);
  28. return $result;
  29. }
  30. static public function cur_businessid() {
  31. $account = self::get_account();
  32. return $account['EBusinessID'];
  33. }
  34. static public function get_account()
  35. {
  36. if(defined('MOBILE_PUBLISH') && MOBILE_PUBLISH == true) {
  37. return array('EBusinessID' => '1256766','AppID' => '22c88d88-3565-4023-b5ac-0201792869ed');
  38. } else {
  39. return array('EBusinessID' => '1256051','AppID' => '6718d260-e2b6-4329-ad78-daff173309ac');
  40. }
  41. }
  42. static private function encrypt($data, $appkey)
  43. {
  44. return urlencode(base64_encode(md5($data . $appkey)));
  45. }
  46. static public function subscribe($order_sn)
  47. {
  48. $model_order = Model('order');
  49. $condition['order_sn'] = $order_sn;
  50. $order_info = $model_order->getOrderInfo($condition, array('order_common', 'order_goods'));
  51. // 发送快递鸟订阅回调
  52. $express = rkcache('express', true);
  53. $e_code = $express[$order_info['extend_order_common']['shipping_express_id']]['e_kdn_code'];
  54. if(empty($e_code)) {
  55. Log::record("找不到该单号的快递公司编码—— ordersn={$order_sn}",Log::ERR);
  56. return;
  57. }
  58. // 通知订阅, 快递鸟定时回调
  59. $ret = self::req_subscribe($e_code, $order_info['shipping_code'], $order_sn);
  60. if($ret === false) {
  61. Log::record("kdn_helper::subscribe order_sn={$order_sn} error.",Log::ERR);
  62. } else {
  63. Log::record("kdn_helper::subscribe ret={$ret}",Log::DEBUG);
  64. }
  65. }
  66. static public function req_subscribe($e_code, $logisticCode, $order_sn)
  67. {
  68. $datas = array(array('Code' => $e_code,'Item' => array(array('No' => $logisticCode,'Bk' => $order_sn))));
  69. $requestData = json_encode($datas);
  70. $account = self::get_account();
  71. $datas = array(
  72. 'EBusinessID' => $account['EBusinessID'],
  73. 'RequestType' => '1005',
  74. 'RequestData' => urlencode($requestData),
  75. 'DataType' => '2',
  76. 'DataSign' => self::encrypt($requestData, $account['AppID'])
  77. );
  78. $result = http_post_data(self::req_url, $datas);
  79. return $result;
  80. }
  81. static public function onCallback($data)
  82. {
  83. $order_sn = $data['CallBack'];
  84. if(empty($order_sn)) return;
  85. $e_code = $data['ShipperCode'];
  86. $shipping_code = $data['LogisticCode'];
  87. $key = "express_{$e_code}_{$shipping_code}";
  88. wkcache($key, $data);
  89. // 数据解析
  90. switch ($data['State'])
  91. {
  92. case '3': // 已签收, 设置为已收货
  93. {
  94. $model_order = Model('order');
  95. $update = $model_order->editOrder(array('order_state' => ORDER_STATE_SUCCESS), array('order_state' => ORDER_STATE_SEND,'order_sn' => $order_sn));
  96. if (!$update) {
  97. Log::record('更新订单状态为已收货状态时写入失败');
  98. }
  99. }
  100. break;
  101. case '4': // 问题件
  102. Log::record("order_sn={$order_sn},code = {$data['LogisticCode']}.",Log::ERR);
  103. break;
  104. default: {
  105. Log::record('query_status false');
  106. break;
  107. }
  108. }
  109. }
  110. }