KdApiEOrder.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. namespace Lib;
  3. class KdApiEOrder
  4. {
  5. //正式地址
  6. private $reqUrl = "http://api.kdniao.com/api/Eorderservice";
  7. private $eBusinessID = "";
  8. private $appKey = "";
  9. public function __construct() {
  10. $ebuss_info = M('lionfish_comshop_config')->where( array('name' => 'kdniao_id') )->find();
  11. $exappkey = M('lionfish_comshop_config')->where( array('name' => 'kdniao_api_key') )->find();
  12. $this->eBusinessID = $ebuss_info['value'];
  13. $this->appKey = $exappkey['value'];
  14. }
  15. /**
  16. * @param $order_id 订单编号
  17. * @param $express_no 快递编号
  18. * @param $config_data 配置信息
  19. * @return array
  20. */
  21. public function printOrder($order_id,$express_no,$config_data){
  22. $kd_result = array();
  23. $status = 0;
  24. $order_info = M('lionfish_comshop_order')->where( array('order_id' => $order_id) )->find();
  25. $express_info = M('lionfish_comshop_express')->where( array('simplecode' => $express_no) )->find();
  26. $kdn_info = M('lionfish_comshop_kdniao_list')->where( array('express_code' => $express_no) )->find();
  27. $customer_name = "";
  28. $customer_pwd = "";
  29. $month_code = "";
  30. $send_site = "";
  31. $send_staff = "";
  32. $template_size= "";
  33. $is_send_message = 0;
  34. $is_send_goods = 0;
  35. $sender_company = "";
  36. $sender_name = "";
  37. $sender_tel = "";
  38. $sender_mobile = "";
  39. $sender_postcode = "";
  40. $sender_address = "";
  41. $sender_province_name = "";
  42. $sender_city_name = "";
  43. $sender_area_name = "";
  44. if(!empty($kdn_info)){
  45. $customer_name = $kdn_info['customer_name'];
  46. $customer_pwd = $kdn_info['customer_pwd'];
  47. $month_code = $kdn_info['month_code'];
  48. $send_site = $kdn_info['send_site'];
  49. $send_staff = $kdn_info['send_staff'];
  50. $template_size= $kdn_info['template_size'];
  51. $is_send_message = $kdn_info['is_send_message'];
  52. $is_send_goods = $kdn_info['is_send_goods'];
  53. $sender_company = $kdn_info['sender_company'];
  54. $sender_name = $kdn_info['sender_name'];
  55. $sender_tel = $kdn_info['sender_tel'];
  56. $sender_mobile = $kdn_info['sender_mobile'];
  57. $sender_postcode = $kdn_info['sender_postcode'];
  58. $sender_address = $kdn_info['sender_address'];
  59. $sender_province_name = $kdn_info['sender_province_name'];
  60. $sender_city_name = $kdn_info['sender_city_name'];
  61. $sender_area_name = $kdn_info['sender_area_name'];
  62. }else{
  63. if(!empty($express_info)){
  64. $customer_name = $express_info['customer_name'];
  65. $customer_pwd = $express_info['customer_pwd'];
  66. }
  67. $sender_company = $config_data['kdn_sender_company'];
  68. $sender_name = $config_data['kdn_sender_name'];
  69. $sender_mobile = $config_data['kdn_sender_mobile'];
  70. $sender_postcode = $config_data['kdn_sender_postcode'];
  71. $sender_address = $config_data['kdn_sender_address'];
  72. $sender_province_name = $config_data['kdn_province_id'];
  73. $sender_city_name = $config_data['kdn_city_id'];
  74. $sender_area_name = $config_data['kdn_area_id'];
  75. //圆通快递 TemplateSize字段传值18001,拿到最新的二联单模板发货使用。
  76. //这是100*180的模板,打印机必须设置为100*180规格,有边距要设置为0
  77. if($express_no == 'YTO'){
  78. $template_size = "18001";
  79. }
  80. }
  81. $customername_list = array('SURE','KYSY','PJ','CND','JTSD','DNWL','SNWL','ZTO','YD','HTKY','YTO','YCWL','UC','ANE','DBLKY',
  82. 'ANEKY','JDKY','LB','HTKYKY','ZTOKY','CNEX','YDKY','SX');
  83. $customerpwd_list = array('PJ','CND','JTSD','DNWL','SNWL','ZTO','YD','HTKY','UC','ANEKY'
  84. ,'JDKY','LB','HTKYKY','ZTOKY','YDKY');
  85. $monthcode_list = array('KYSY','YTO');
  86. $sendsite_list = array('SURE','YCWL','ANE','ZTOKY','SX');
  87. $sendstaff_list = array('SURE','JDKY');
  88. $postcode_list = array('EMS','YZPY','YZBK');
  89. //判断客户账号
  90. if(in_array($express_no,$customername_list)){
  91. if(empty($customer_name)){
  92. $status = 2;
  93. }
  94. }
  95. //判断客户密码
  96. if(in_array($express_no,$customerpwd_list)){
  97. if(empty($customer_pwd)){
  98. $status = 2;
  99. }
  100. }
  101. //判断月结编码
  102. if(in_array($express_no,$monthcode_list)){
  103. if(empty($month_code)){
  104. $status = 2;
  105. }
  106. }
  107. //判断网点名称
  108. if(in_array($express_no,$sendsite_list)){
  109. if(empty($send_site)){
  110. $status = 2;
  111. }
  112. }
  113. //判断网点编码
  114. if(in_array($express_no,$sendstaff_list)){
  115. if(empty($send_staff)){
  116. $status = 2;
  117. }
  118. }
  119. //判断发件人信息
  120. if(empty($sender_name) || empty($sender_address) || empty($sender_province_name) || empty($sender_city_name)
  121. || empty($sender_area_name)){
  122. $status = 3;
  123. }
  124. if(empty($sender_mobile) && empty($sender_tel)){
  125. $status = 3;
  126. }
  127. if(in_array($express_no,$postcode_list)){
  128. if(empty($sender_postcode)){
  129. $status = 3;
  130. }
  131. }
  132. if($status == 0){
  133. if(!empty($order_info)){
  134. if($order_info['order_status_id'] == 1 && $order_info['delivery'] == 'express' && $order_info['is_kdn_print'] != 1){
  135. //构造电子面单提交信息
  136. $eorder = [];
  137. $eorder["ShipperCode"] = $express_no;
  138. $eorder["OrderCode"] = $order_info['order_num_alias'];
  139. //邮费支付方式:1-现付,2-到付,3-月结,4-第三方支付(仅SF支持)
  140. if(in_array($express_no,$monthcode_list)){
  141. if(!empty($month_code)){
  142. $eorder["PayType"] = 3;
  143. }else{
  144. $eorder["PayType"] = 1;
  145. }
  146. }else{
  147. $eorder["PayType"] = 1;
  148. }
  149. //快递类型:1-标准快件
  150. $eorder["ExpType"] = 1;
  151. //部分快递必须使用
  152. //客户账号
  153. $eorder["CustomerName"] = $customer_name;
  154. //客户密码
  155. $eorder["CustomerPwd"] = $customer_pwd;
  156. //网点名称
  157. $eorder["SendSite"] = $send_site;
  158. //网点编码
  159. $eorder["SendStaff"] = $send_staff;
  160. //月结编码
  161. $eorder["MonthCode"] = $month_code;
  162. //ERP系统、电商平台等系统或平台类型用户的会员ID或店铺账号等唯一性标识,用于区分其用户
  163. $eorder["MemberID"] = "";
  164. //发货仓编码
  165. $eorder["WareHouseID"] = "";
  166. //运输方式 1-陆运 2-空运 不填默认为1
  167. $eorder["TransType"] = 1;
  168. //快递单号(仅宅急送可用)
  169. $eorder["LogisticCode"] = "";
  170. //第三方订单号 (ShipperCode为JD且ExpType为1时必填)
  171. $eorder["LogisticCode"] = "";
  172. //是否要求签回单 1-要求 0-不要求
  173. $eorder["IsReturnSignBill"] = 0;
  174. //签回单操作要求(如:签名、盖章、身份证复印件等)
  175. $eorder["OperateRequire"] = "";
  176. //快递运费
  177. $eorder["Cost"] = "";
  178. //其他费用
  179. $eorder["OtherCost"] = "";
  180. //是否通知快递员上门揽件 0-通知 1-不通知 不填则默认为1
  181. $eorder["IsNotice"] = "";
  182. //返回电子面单模板:0-不需要;1-需要
  183. $eorder["IsReturnPrintTemplate"] = 1;
  184. //是否订阅短信:0-不需要;1-需要
  185. $eorder["IsSendMessage"] = $is_send_message;
  186. //包装类型(快运字段)默认为0; 0-纸 1-纤 2-木 3-托膜 4-木托 99-其他
  187. $eorder["PackingType"] = "";
  188. //送货方式(快运字段)默认为0; 0-自提 1-送货上门(不含上楼) 2-送货上楼
  189. $eorder["DeliveryMethod"] = "";
  190. $sender = [];
  191. $sender["Name"] = $sender_name;
  192. $sender["Tel"] = $sender_tel;
  193. $sender["Mobile"] = $sender_mobile;
  194. $sender["ProvinceName"] = $sender_province_name;
  195. $sender["CityName"] = $sender_city_name;
  196. $sender["ExpAreaName"] = $sender_area_name;
  197. $sender["Address"] = $sender_address;
  198. $sender["Company"] = $sender_company;
  199. $sender["PostCode"] = $sender_postcode;
  200. //收货人地址信息 begin
  201. $address_id = $order_info['address_id'];
  202. $address_info = M('lionfish_comshop_address')->where( "address_id=".$order_info['address_id'] )->find();
  203. $shipping_cityname = D('Seller/Area')->get_area_info($address_info['city_id']);
  204. $shipping_areaname = D('Seller/Area')->get_area_info($address_info['country_id']);
  205. $area_info = M('lionfish_comshop_area')->where( array('id' => $address_info['country_id'] ) )->find();
  206. $receiver = [];
  207. $receiver["Name"] = $order_info['shipping_name'];
  208. $receiver["Mobile"] = $order_info['shipping_tel'];
  209. $receiver["ProvinceName"] = $order_info['shipping_name'];
  210. $receiver["CityName"] = $shipping_cityname;
  211. $receiver["ExpAreaName"] = $shipping_areaname;
  212. $receiver["PostCode"] = $area_info['code'];
  213. $receiver["Address"] = $order_info['shipping_address'];
  214. //收货人地址信息 end
  215. //商品信息 begin
  216. $commodity = [];
  217. $goods_list = M('lionfish_comshop_order_goods')->where( array('order_id' => $order_id) )->select();
  218. $quantity_count = 0;
  219. $customArea = "";
  220. foreach($goods_list as $k=>$v){
  221. $commodityOne = [];
  222. $commodityOne["GoodsName"] = mb_substr($v['name'],0,16,'utf-8');
  223. $commodityOne["Goodsquantity"] = $v['quantity'];
  224. $quantity_count = $quantity_count + $v['quantity'];
  225. $customArea = $customArea . $v['name']." ".$v['quantity'].",";
  226. $commodity[] = $commodityOne;
  227. }
  228. //商品信息 end
  229. $eorder["Sender"] = $sender;
  230. $eorder["Receiver"] = $receiver;
  231. $eorder["Commodity"] = $commodity;
  232. $eorder["Quantity"] = $quantity_count;
  233. if($is_send_goods == 1){
  234. $eorder['CustomArea'] = $customArea;
  235. }
  236. //调用电子面单
  237. $jsonParam = json_encode($eorder, JSON_UNESCAPED_UNICODE);
  238. //$jsonParam = JSON($eorder);//兼容php5.2(含)以下
  239. //echo "电子面单接口提交内容:<br/>".$jsonParam;
  240. //error_log($jsonParam,3,'error.log');
  241. $jsonResult = $this->submitEOrder($jsonParam);
  242. //echo "<br/><br/>电子面单提交结果:<br/>".$jsonResult;
  243. //解析电子面单返回结果
  244. $result = json_decode($jsonResult, true);
  245. $kd_result['jsonResult'] = $jsonResult;
  246. if($result["ResultCode"] == "100" && $result['Success']) {
  247. //快递单号
  248. $shipping_no = $result["Order"]['LogisticCode'];
  249. //快递公司编码
  250. $shipperCode = $result["Order"]['ShipperCode'];
  251. //面单打印模板内容(html格式)
  252. $PrintTemplate = $result['PrintTemplate'];
  253. //打印结果
  254. $Reason = $result['Reason'];
  255. //用户ID
  256. $EBusinessID = $result['EBusinessID'];
  257. //订单编号
  258. $OrderCode = $result["Order"]['OrderCode'];
  259. //快递鸟编号
  260. $KDNOrderCode = $result["Order"]['KDNOrderCode'];
  261. //唯一标识
  262. $UniquerRequestNumber = $result["UniquerRequestNumber"];
  263. //打印结果编码
  264. $ResultCode = $result["ResultCode"];
  265. $kd_result['status'] = 0;
  266. //打印面单数据获取成功,更新订单打印状态,快递公司
  267. M('lionfish_comshop_order')->where( array('order_id' => $order_id) )->save(array('is_kdn_print'=>1,'shipping_no'=>$shipping_no));
  268. //保存打印结果信息
  269. $kdn_result_data = array();
  270. $kdn_result_data['order_id'] = $order_id;
  271. $kdn_result_data['order_code'] = $OrderCode;
  272. $kdn_result_data['e_business_id'] = $EBusinessID;
  273. $kdn_result_data['result_code'] = $ResultCode;
  274. $kdn_result_data['status'] = 1;
  275. $kdn_result_data['print_template'] = $PrintTemplate;
  276. $kdn_result_data['reason'] = $Reason;
  277. $kdn_result_data['uniquerRequestNumber'] = $UniquerRequestNumber;
  278. $kdn_result_data['kdnOrderCode'] = $KDNOrderCode;
  279. $kdn_info = M('lionfish_comshop_order_kdniao_info')->where( array('order_id' => $order_id,'status'=>0) )->find();
  280. if(empty($kdn_info)){
  281. M('lionfish_comshop_order_kdniao_info')->add($kdn_result_data);
  282. }else{//有失败记录更新
  283. M('lionfish_comshop_order_kdniao_info')->where( array('id' => $kdn_info['id']) )->save($kdn_result_data);
  284. }
  285. //更新订单快递信息
  286. $express_info = M('lionfish_comshop_express')->where( array('simplecode' => $express_no ) )->find();
  287. $time = time();
  288. $data = array(
  289. 'shipping_method' => $express_info['id'],
  290. 'shipping_no' => $shipping_no,
  291. 'dispatchname' => $express_info['name'],
  292. 'express_time' => $time
  293. );
  294. M('lionfish_comshop_order')->where( array('order_id' => $order_id) )->save( $data );
  295. }else {
  296. //打印失败 更新订单 打印状态
  297. M('lionfish_comshop_order')->where( array('order_id' => $order_id) )->save(array('is_kdn_print'=>2));
  298. $kd_result['status'] = 1;
  299. //快递公司编码
  300. $shipperCode = $express_no;
  301. //打印结果
  302. $Reason = $result['Reason'];
  303. //用户ID
  304. $EBusinessID = $result['EBusinessID'];
  305. //打印结果编码
  306. $ResultCode = $result["ResultCode"];
  307. //订单编号
  308. $OrderCode = $order_info['order_num_alias'];
  309. //唯一标识
  310. $UniquerRequestNumber = $result["UniquerRequestNumber"];
  311. //保存打印结果信息
  312. $kdn_result_data = array();
  313. $kdn_result_data['order_id'] = $order_id;
  314. $kdn_result_data['order_code'] = $OrderCode;
  315. $kdn_result_data['e_business_id'] = $EBusinessID;
  316. $kdn_result_data['result_code'] = $ResultCode;
  317. $kdn_result_data['status'] = 0;
  318. $kdn_result_data['print_template'] = "";
  319. $kdn_result_data['reason'] = $Reason;
  320. $kdn_result_data['uniquerRequestNumber'] = $UniquerRequestNumber;
  321. $kdn_info = M('lionfish_comshop_order_kdniao_info')->where( array('order_id' => $order_id,'status'=>0) )->find();
  322. if(empty($kdn_info)){
  323. M('lionfish_comshop_order_kdniao_info')->add($kdn_result_data);
  324. }else{//有失败记录更新
  325. M('lionfish_comshop_order_kdniao_info')->where( array('id' => $kdn_info['id']) )->save($kdn_result_data);
  326. }
  327. }
  328. $kd_result['code'] = $result["ResultCode"];
  329. $kd_result['message'] = $result["Reason"];
  330. }else{
  331. $kd_result['status'] = 1;
  332. $kd_result['message'] = "待发货未打印快递订单才能打印面单";
  333. }
  334. }else{
  335. $kd_result['status'] = 1;
  336. $kd_result['message'] = "订单不存在";
  337. }
  338. }else{
  339. if($status == 2){
  340. $kd_result['message'] = "快递信息未配置";
  341. }else if($status == 3){
  342. $kd_result['message'] = "发件人信息未配置";
  343. }
  344. $kd_result['status'] = $status;
  345. }
  346. $kd_result['order_id'] = $order_id;
  347. return $kd_result;
  348. }
  349. /**
  350. * Json方式 调用电子面单接口
  351. */
  352. function submitEOrder($requestData){
  353. $datas = array(
  354. 'EBusinessID' => $this->eBusinessID,
  355. 'RequestType' => '1007',
  356. 'RequestData' => urlencode($requestData) ,
  357. 'DataType' => '2',
  358. );
  359. $datas['DataSign'] = $this->encrypt($requestData, $this->appKey);
  360. $result = $this->sendPost($this->reqUrl, $datas);
  361. //根据公司业务处理返回的信息......
  362. return $result;
  363. }
  364. /**
  365. * post提交数据
  366. * @param string $url 请求Url
  367. * @param array $datas 提交的数据
  368. * @return url响应返回的html
  369. */
  370. function sendPost($url, $datas) {
  371. $temps = array();
  372. foreach ($datas as $key => $value) {
  373. $temps[] = sprintf('%s=%s', $key, $value);
  374. }
  375. $post_data = implode('&', $temps);
  376. $url_info = parse_url($url);
  377. if(empty($url_info['port']))
  378. {
  379. $url_info['port']=80;
  380. }
  381. $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
  382. $httpheader.= "Host:" . $url_info['host'] . "\r\n";
  383. $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
  384. $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
  385. $httpheader.= "Connection:close\r\n\r\n";
  386. $httpheader.= $post_data;
  387. $fd = fsockopen($url_info['host'], $url_info['port']);
  388. fwrite($fd, $httpheader);
  389. $gets = "";
  390. $headerFlag = true;
  391. while (!feof($fd)) {
  392. if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
  393. break;
  394. }
  395. }
  396. while (!feof($fd)) {
  397. $gets.= fread($fd, 128);
  398. }
  399. fclose($fd);
  400. return $gets;
  401. }
  402. /**
  403. * 电商Sign签名生成
  404. * @param data 内容
  405. * @param appkey Appkey
  406. * @return DataSign签名
  407. */
  408. function encrypt($data, $appkey) {
  409. return urlencode(base64_encode(md5($data.$appkey)));
  410. }
  411. /**************************************************************
  412. *
  413. * 使用特定function对数组中所有元素做处理
  414. * @param string &$array 要处理的字符串
  415. * @param string $function 要执行的函数
  416. * @return boolean $apply_to_keys_also 是否也应用到key上
  417. * @access public
  418. *
  419. *************************************************************/
  420. function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
  421. {
  422. static $recursive_counter = 0;
  423. if (++$recursive_counter > 1000) {
  424. die('possible deep recursion attack');
  425. }
  426. foreach ($array as $key => $value) {
  427. if (is_array($value)) {
  428. $this->arrayRecursive($array[$key], $function, $apply_to_keys_also);
  429. } else {
  430. $array[$key] = $function($value);
  431. }
  432. if ($apply_to_keys_also && is_string($key)) {
  433. $new_key = $function($key);
  434. if ($new_key != $key) {
  435. $array[$new_key] = $array[$key];
  436. unset($array[$key]);
  437. }
  438. }
  439. }
  440. $recursive_counter--;
  441. }
  442. /**************************************************************
  443. *
  444. * 将数组转换为JSON字符串(兼容中文)
  445. * @param array $array 要转换的数组
  446. * @return string 转换得到的json字符串
  447. * @access public
  448. *
  449. *************************************************************/
  450. public function JSON($array) {
  451. $this->arrayRecursive($array, 'urlencode', true);
  452. $json = json_encode($array);
  453. return urldecode($json);
  454. }
  455. }
  456. ?>