Imdada.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author cy 2020-08-05
  12. *
  13. */
  14. /*
  15. * 达达配送接口
  16. */
  17. namespace Lib\Localtown;
  18. class Imdada
  19. {
  20. //正式地址
  21. private $reqUrl = "";
  22. //达达开发者app_key
  23. private $app_key = "";
  24. //达达开发者app_secret
  25. private $app_secret = "";
  26. //商户id
  27. private $source_id = "";
  28. //门店编号
  29. private $shop_no = "";
  30. //订单所在城市的code
  31. private $city_code = "";
  32. /**
  33. * api版本
  34. */
  35. public $v = "1.0";
  36. /**
  37. * 数据格式
  38. */
  39. public $format = "json";
  40. //新增配送单地址
  41. private $add_order_url = "/api/order/addOrder";
  42. //配送单重发地址
  43. private $re_add_order_url = "/api/order/reAddOrder";
  44. private $cancel_order_url = "/api/order/formalCancel";
  45. private $query_delivery_url = "/api/order/queryDeliverFee";
  46. private $notify_url = "";
  47. /**
  48. * 构造函数
  49. */
  50. public function __construct(){
  51. $merchant_id = D('Home/Front')->get_config_by_name('localtown_imdada_merchant_id');
  52. $this->app_key = D('Home/Front')->get_config_by_name('localtown_imdada_appkey');
  53. $this->app_secret = D('Home/Front')->get_config_by_name('localtown_imdada_appsecret');
  54. $this->shop_no = D('Home/Front')->get_config_by_name('localtown_imdada_shop_no');
  55. $this->source_id = $merchant_id;
  56. $shop_domain = D('Home/Front')->get_config_by_name('shop_domain');
  57. $this->notify_url = $shop_domain.'delivery_notify.php';
  58. $city_name = D('Home/Front')->get_config_by_name('localtown_shop_city_id');
  59. $city_name = str_replace('市','',$city_name);
  60. $area_info = M('lionfish_comshop_imdada_area_code')->where( array('city_name' => $city_name) )->find();
  61. $this->city_code = $area_info['city_code'];
  62. if ($merchant_id == '73753') {//测试账号
  63. $this->shop_no = "11047059";//测试门店号
  64. $this->reqUrl = "http://newopen.qa.imdada.cn";
  65. } else {
  66. $this->reqUrl = "https://newopen.imdada.cn";
  67. }
  68. }
  69. /**
  70. * 新增配送单接口
  71. */
  72. public function addOrder($order_info){
  73. $order_data = array();
  74. //供应商地址
  75. if($order_info['store_id'] > 0){
  76. $city_name = $order_info['store_data']['city'];
  77. $city_name = str_replace('市','',$city_name);
  78. $area_info = M('lionfish_comshop_imdada_area_code')->where( array('city_name' => $city_name) )->find();
  79. $this->city_code = $area_info['city_code'];
  80. }
  81. //门店编号
  82. $order_data['shop_no'] = $this->shop_no;
  83. //第三方订单ID
  84. $order_data['origin_id'] = $order_info['order_num_alias'];
  85. //订单所在城市的code
  86. $order_data['city_code'] = $this->city_code;
  87. //订单金额
  88. $order_data['cargo_price'] = $order_info['order_total'];
  89. //是否需要垫付 1:是 0:否 (垫付订单金额,非运费)
  90. $order_data['is_prepay'] = "0";
  91. //收货人姓名
  92. $order_data['receiver_name'] = $order_info['shipping_name'];
  93. //收货人地址
  94. $order_data['receiver_address'] = $order_info['shipping_address'];
  95. //收货人地址纬度
  96. $order_data['receiver_lat'] = $order_info['shipping_lat'];
  97. //收货人地址经度
  98. $order_data['receiver_lng'] = $order_info['shipping_lng'];
  99. //回调URL
  100. $order_data['callback'] = $this->notify_url;
  101. //收货人手机号
  102. $order_data['receiver_phone'] = $order_info['shipping_tel'];
  103. /**************非必填项*******************/
  104. //收货人座机号
  105. $order_data['receiver_tel'] = "";
  106. //小费
  107. $order_data['tips'] = "0";
  108. //订单备注
  109. $order_data['info'] = $order_info['note_content'];
  110. //订单商品类型:食品小吃-1,饮料-2,鲜花-3,文印票务-8,便利店-9,水果生鲜-13,同城电商-19, 医药-20,
  111. //蛋糕-21,酒品-24,小商品市场-25,服装-26,汽修零配-27,数码-28,小龙虾-29,火锅-51,其他-5
  112. $order_data['cargo_type'] = "19";
  113. //订单商品数量
  114. $order_data['cargo_num'] = $order_info['goods_count'];
  115. //订单重量(单位:Kg)
  116. $order_data['cargo_weight'] = round($order_info['goods_weight']/1000,2);
  117. //订单来源标示
  118. $order_data['origin_mark'] = "";
  119. //订单来源编号
  120. $order_data['origin_mark_no'] = "";
  121. //是否选择直拿直送(0:不需要;1:需要)
  122. $order_data['is_direct_delivery'] = "0";
  123. //订单商品明细
  124. $product_list = [];
  125. foreach($order_info['goods_list'] as $k=>$v){
  126. $goods_array = [];
  127. $goods_array['sku_name'] = $v['goods_name'];//商品名称
  128. $goods_array['src_product_no'] = $v['goods_id'];//商品编码
  129. $goods_array['count'] = $v['quantity'];//商品数量
  130. $product_list[] = $goods_array;
  131. }
  132. $order_data['product_list'] = $product_list;
  133. $body_data = json_encode($order_data);
  134. $reqParams = $this->bulidRequestParams($body_data);
  135. $resp = $this->getHttpRequestWithPost($this->reqUrl.$this->add_order_url,json_encode($reqParams));
  136. $result = $this->parseResponseData($resp);
  137. return $result;
  138. }
  139. /**
  140. * 重新推送配送单接口
  141. */
  142. public function reAddOrder($order_info){
  143. $order_data = array();
  144. //供应商地址
  145. if($order_info['store_id'] > 0){
  146. $city_name = $order_info['store_data']['city'];
  147. $city_name = str_replace('市','',$city_name);
  148. $area_info = M('lionfish_comshop_imdada_area_code')->where( array('city_name' => $city_name) )->find();
  149. $this->city_code = $area_info['city_code'];
  150. }
  151. //门店编号
  152. $order_data['shop_no'] = $this->shop_no;
  153. //第三方订单ID
  154. $order_data['origin_id'] = $order_info['order_num_alias'];
  155. //订单所在城市的code
  156. $order_data['city_code'] = $this->city_code;
  157. //订单金额
  158. $order_data['cargo_price'] = $order_info['order_total'];
  159. //是否需要垫付 1:是 0:否 (垫付订单金额,非运费)
  160. $order_data['is_prepay'] = "0";
  161. //收货人姓名
  162. $order_data['receiver_name'] = $order_info['shipping_name'];
  163. //收货人地址
  164. $order_data['receiver_address'] = $order_info['shipping_address'];
  165. //收货人地址纬度
  166. $order_data['receiver_lat'] = $order_info['shipping_lat'];
  167. //收货人地址经度
  168. $order_data['receiver_lng'] = $order_info['shipping_lng'];
  169. //回调URL
  170. $order_data['callback'] = $this->notify_url;
  171. //收货人手机号
  172. $order_data['receiver_phone'] = $order_info['shipping_tel'];
  173. /**************非必填项*******************/
  174. //收货人座机号
  175. $order_data['receiver_tel'] = "";
  176. //小费
  177. $order_data['tips'] = "0";
  178. //订单备注
  179. $order_data['info'] = $order_info['note_content'];
  180. //订单商品类型:食品小吃-1,饮料-2,鲜花-3,文印票务-8,便利店-9,水果生鲜-13,同城电商-19, 医药-20,
  181. //蛋糕-21,酒品-24,小商品市场-25,服装-26,汽修零配-27,数码-28,小龙虾-29,火锅-51,其他-5
  182. $order_data['cargo_type'] = "19";
  183. //订单商品数量
  184. $order_data['cargo_num'] = $order_info['goods_count'];
  185. //订单重量(单位:Kg)
  186. $order_data['cargo_weight'] = round($order_info['goods_weight']/1000,2);
  187. //订单来源标示
  188. $order_data['origin_mark'] = "";
  189. //订单来源编号
  190. $order_data['origin_mark_no'] = "";
  191. //是否选择直拿直送(0:不需要;1:需要)
  192. $order_data['is_direct_delivery'] = "0";
  193. //订单商品明细
  194. $product_list = [];
  195. foreach($order_info['goods_list'] as $k=>$v){
  196. $goods_array = [];
  197. $goods_array['sku_name'] = $v['goods_name'];//商品名称
  198. $goods_array['src_product_no'] = $v['goods_id'];//商品编码
  199. $goods_array['count'] = $v['quantity'];//商品数量
  200. $product_list[] = $goods_array;
  201. }
  202. $order_data['product_list'] = $product_list;
  203. $body_data = json_encode($order_data);
  204. $reqParams = $this->bulidRequestParams($body_data);
  205. $resp = $this->getHttpRequestWithPost($this->reqUrl.$this->re_add_order_url,json_encode($reqParams));
  206. $result = $this->parseResponseData($resp);
  207. return $result;
  208. }
  209. /**
  210. * 取消达达配送订单
  211. * @param $orderdistribution_info
  212. */
  213. public function cancelOrder($order_info,$cancel_reason_id,$cancel_reason){
  214. $order_data = array();
  215. //第三方订单ID
  216. $order_data['order_id'] = $order_info['order_num_alias'];
  217. //取消原因ID
  218. $order_data['cancel_reason_id'] = $cancel_reason_id;
  219. //取消原因
  220. $order_data['cancel_reason'] = $cancel_reason;
  221. $body_data = json_encode($order_data);
  222. $reqParams = $this->bulidRequestParams($body_data);
  223. $resp = $this->getHttpRequestWithPost($this->reqUrl.$this->cancel_order_url,json_encode($reqParams));
  224. $result = $this->parseResponseData($resp);
  225. return $result;
  226. }
  227. /**
  228. * 查询订单运费接口
  229. */
  230. public function queryDeliverFee($order_info){
  231. $order_data = array();
  232. //供应商地址
  233. if($order_info['store_id'] > 0){
  234. $city_name = $order_info['store_data']['city'];
  235. $city_name = str_replace('市','',$city_name);
  236. $area_info = M('lionfish_comshop_imdada_area_code')->where( array('city_name' => $city_name) )->find();
  237. $this->city_code = $area_info['city_code'];
  238. }
  239. //门店编号
  240. $order_data['shop_no'] = $this->shop_no;
  241. //第三方订单ID
  242. $order_data['origin_id'] = $order_info['order_num_alias'];
  243. //订单所在城市的code
  244. $order_data['city_code'] = $this->city_code;
  245. //订单金额
  246. $order_data['cargo_price'] = $order_info['order_total'];
  247. //是否需要垫付 1:是 0:否 (垫付订单金额,非运费)
  248. $order_data['is_prepay'] = "0";
  249. //收货人姓名
  250. $order_data['receiver_name'] = $order_info['shipping_name'];
  251. //收货人地址
  252. $order_data['receiver_address'] = $order_info['shipping_address'];
  253. //收货人地址纬度
  254. $order_data['receiver_lat'] = $order_info['shipping_lat'];
  255. //收货人地址经度
  256. $order_data['receiver_lng'] = $order_info['shipping_lng'];
  257. //回调URL
  258. $order_data['callback'] = $this->notify_url;
  259. //收货人手机号
  260. $order_data['receiver_phone'] = $order_info['shipping_tel'];
  261. /**************非必填项*******************/
  262. //收货人座机号
  263. $order_data['receiver_tel'] = "";
  264. //小费
  265. $order_data['tips'] = "0";
  266. //订单备注
  267. $order_data['info'] = $order_info['note_content'];
  268. //订单商品类型:食品小吃-1,饮料-2,鲜花-3,文印票务-8,便利店-9,水果生鲜-13,同城电商-19, 医药-20,
  269. //蛋糕-21,酒品-24,小商品市场-25,服装-26,汽修零配-27,数码-28,小龙虾-29,火锅-51,其他-5
  270. $order_data['cargo_type'] = "19";
  271. //订单商品数量
  272. $order_data['cargo_num'] = $order_info['goods_count'];
  273. //订单重量(单位:Kg)
  274. $order_data['cargo_weight'] = round($order_info['goods_weight']/1000,2);
  275. //订单来源标示
  276. $order_data['origin_mark'] = "";
  277. //订单来源编号
  278. $order_data['origin_mark_no'] = "";
  279. //是否选择直拿直送(0:不需要;1:需要)
  280. $order_data['is_direct_delivery'] = "0";
  281. //订单商品明细
  282. $product_list = [];
  283. foreach($order_info['goods_list'] as $k=>$v){
  284. $goods_array = [];
  285. $goods_array['sku_name'] = $v['goods_name'];//商品名称
  286. $goods_array['src_product_no'] = $v['goods_id'];//商品编码
  287. $goods_array['count'] = $v['quantity'];//商品数量
  288. $product_list[] = $goods_array;
  289. }
  290. $order_data['product_list'] = $product_list;
  291. $body_data = json_encode($order_data);
  292. $reqParams = $this->bulidRequestParams($body_data);
  293. $resp = $this->getHttpRequestWithPost($this->reqUrl.$this->query_delivery_url,json_encode($reqParams));
  294. $result = $this->parseResponseData($resp);
  295. return $result;
  296. }
  297. /**
  298. * 模拟取货
  299. */
  300. public function orderFetch($order_sn){
  301. $order_data = array();
  302. //第三方订单ID
  303. $order_data['order_id'] = $order_sn;
  304. $body_data = json_encode($order_data);
  305. $reqParams = $this->bulidRequestParams($body_data);
  306. $resp = $this->getHttpRequestWithPost($this->reqUrl."/api/order/fetch",json_encode($reqParams));
  307. echo $resp.'<br/>';
  308. $result = $this->parseResponseData($resp);
  309. print_r($result);
  310. }
  311. /**
  312. * 模拟完成订单
  313. */
  314. public function orderFinish($order_sn){
  315. $order_data = array();
  316. //第三方订单ID
  317. $order_data['order_id'] = $order_sn;
  318. $body_data = json_encode($order_data);
  319. $reqParams = $this->bulidRequestParams($body_data);
  320. $resp = $this->getHttpRequestWithPost($this->reqUrl."/api/order/finish",json_encode($reqParams));
  321. echo $resp.'<br/>';
  322. $result = $this->parseResponseData($resp);
  323. print_r($result);
  324. }
  325. /**
  326. * 模拟取消订单
  327. */
  328. public function orderCancel($order_sn){
  329. $order_data = array();
  330. //第三方订单ID
  331. $order_data['order_id'] = $order_sn;
  332. $body_data = json_encode($order_data);
  333. $reqParams = $this->bulidRequestParams($body_data);
  334. $resp = $this->getHttpRequestWithPost($this->reqUrl."/api/order/cancel",json_encode($reqParams));
  335. echo $resp.'<br/>';
  336. $result = $this->parseResponseData($resp);
  337. print_r($result);
  338. }
  339. /**
  340. * 模拟异常妥投物品返还中
  341. */
  342. public function orderAbnormal($order_sn){
  343. $order_data = array();
  344. //第三方订单ID
  345. $order_data['order_id'] = $order_sn;
  346. $body_data = json_encode($order_data);
  347. $reqParams = $this->bulidRequestParams($body_data);
  348. $resp = $this->getHttpRequestWithPost($this->reqUrl."/api/order/delivery/abnormal/back",json_encode($reqParams));
  349. echo $resp.'<br/>';
  350. $result = $this->parseResponseData($resp);
  351. print_r($result);
  352. }
  353. /**
  354. * 构造请求数据
  355. * data:业务参数,json字符串
  356. */
  357. public function bulidRequestParams($body_data){
  358. $requestParams = array();
  359. $requestParams['app_key'] = $this->app_key;
  360. $requestParams['body'] = $body_data;
  361. $requestParams['format'] = $this->format;
  362. $requestParams['v'] = $this->v;
  363. $requestParams['source_id'] = $this->source_id;
  364. $requestParams['timestamp'] = time();
  365. $requestParams['signature'] = $this->_sign($requestParams);
  366. return $requestParams;
  367. }
  368. /**
  369. * 签名生成signature
  370. */
  371. public function _sign($data){
  372. //1.升序排序
  373. ksort($data);
  374. //2.字符串拼接
  375. $args = "";
  376. foreach ($data as $key => $value) {
  377. $args.=$key.$value;
  378. }
  379. $args = $this->app_secret . $args . $this->app_secret;
  380. //3.MD5签名,转为大写
  381. $sign = strtoupper(md5($args));
  382. return $sign;
  383. }
  384. /**
  385. * 发送请求,POST
  386. * @param $url 指定URL完整路径地址
  387. * @param $data 请求的数据
  388. */
  389. public function getHttpRequestWithPost($url,$data){
  390. // json
  391. $headers = array(
  392. 'Content-Type: application/json',
  393. );
  394. $curl = curl_init($url);
  395. curl_setopt($curl, CURLOPT_URL, $url);
  396. curl_setopt($curl, CURLOPT_HEADER, false);
  397. curl_setopt($curl, CURLOPT_POST, true);
  398. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
  399. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
  400. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  401. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  402. curl_setopt($curl, CURLOPT_TIMEOUT, 3);
  403. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  404. $resp = curl_exec($curl);
  405. //var_dump( curl_error($curl) );//如果在执行curl的过程中出现异常,可以打开此开关查看异常内容。
  406. $info = curl_getinfo($curl);
  407. curl_close($curl);
  408. if (isset($info['http_code']) && $info['http_code'] == 200) {
  409. return $resp;
  410. }else{
  411. return '';
  412. }
  413. }
  414. /**
  415. * 解析响应数据
  416. * @param $arr返回的数据
  417. * 响应数据格式:{"status":"success","result":{},"code":0,"msg":"成功"}
  418. */
  419. public function parseResponseData($arr){
  420. $result = array();
  421. if (empty($arr)) {
  422. $result['status'] = 0;
  423. $result['message'] = "接口请求超时或失败";
  424. $result['code'] = "-2";
  425. }else{
  426. $data = json_decode($arr, true);
  427. if($data['status'] == 'success' && $data['code'] == 0){//成功
  428. $result['status'] = 1;
  429. $result['message'] = $data['msg'];
  430. $result['code'] = $data['code'];
  431. $result['result'] = $data['result'];
  432. }else{//失败
  433. $result['status'] = 0;
  434. $result['message'] = $data['msg'];
  435. $result['code'] = $data['code'];
  436. $result['result'] = $data['result'];
  437. }
  438. }
  439. return $result;
  440. }
  441. }
  442. ?>