Sfexpress.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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-08
  12. *
  13. */
  14. /*
  15. * 顺丰同城配送接口
  16. */
  17. namespace Lib\Localtown;
  18. class Sfexpress
  19. {
  20. //正式地址
  21. private $reqUrl = "https://commit-openic.sf-express.com";
  22. //顺丰同城开发者ID
  23. private $dev_id = "";
  24. //顺丰同城密钥
  25. private $dev_key = "";
  26. //顺丰同城店铺ID
  27. private $shop_id = "";
  28. /***********平台店铺信息************/
  29. //店铺名称
  30. private $shopname = "";
  31. //店铺电话
  32. private $shop_phone = "";
  33. //店铺地址
  34. private $shop_address = "";
  35. //店铺经度
  36. private $shop_lng = "";
  37. //店铺纬度
  38. private $shop_lat = "";
  39. //发单城市
  40. private $city_name = "";
  41. /**
  42. * 版本号
  43. */
  44. public $version = "17";
  45. //创建订单地址
  46. private $add_order_url = "/open/api/external/createorder?sign=";
  47. //取消订单地址
  48. private $cancel_order_url = "/open/api/external/cancelorder?sign=";
  49. private $query_delivery_url = "/open/api/external/precreateorder?sign=";
  50. private $notify_url = "";
  51. /**
  52. * 构造函数
  53. */
  54. public function __construct(){
  55. $localtown_sf_dev_id = D('Home/Front')->get_config_by_name('localtown_sf_dev_id');
  56. $localtown_sf_dev_key = D('Home/Front')->get_config_by_name('localtown_sf_dev_key');
  57. $localtown_sf_store_id = D('Home/Front')->get_config_by_name('localtown_sf_store_id');
  58. $localtown_shop_city_id = D('Home/Front')->get_config_by_name('localtown_shop_city_id');
  59. $localtown_shop_province_id = D('Home/Front')->get_config_by_name('localtown_shop_province_id');
  60. $localtown_shop_area_id = D('Home/Front')->get_config_by_name('localtown_shop_area_id');
  61. $localtown_shop_country_id = D('Home/Front')->get_config_by_name('localtown_shop_country_id');
  62. $localtown_shop_address = D('Home/Front')->get_config_by_name('localtown_shop_address');
  63. $localtown_shop_telephone = D('Home/Front')->get_config_by_name('localtown_shop_telephone');
  64. $localtown_shop_lon = D('Home/Front')->get_config_by_name('localtown_shop_lon');
  65. $localtown_shop_lat = D('Home/Front')->get_config_by_name('localtown_shop_lat');
  66. $this->dev_id = $localtown_sf_dev_id;
  67. $this->dev_key = $localtown_sf_dev_key;
  68. $this->shop_id = $localtown_sf_store_id;
  69. $this->city_name = $localtown_shop_city_id;
  70. $this->shop_phone = $localtown_shop_telephone;
  71. $this->shop_address = $localtown_shop_province_id.$localtown_shop_city_id.$localtown_shop_area_id.$localtown_shop_country_id.$localtown_shop_address;
  72. $this->shop_lng = $localtown_shop_lon;
  73. $this->shop_lat = $localtown_shop_lat;
  74. $shoname = D('Home/Front')->get_config_by_name('shoname');
  75. $this->shopname = $shoname;
  76. $shop_domain = D('Home/Front')->get_config_by_name('shop_domain');
  77. $this->notify_url = $shop_domain.'delivery_notify.php';
  78. }
  79. /**
  80. * 新增配送单接口
  81. */
  82. public function addOrder($order_info){
  83. $time = time();
  84. $order_data = array();
  85. //供应商地址
  86. if($order_info['store_id'] > 0){
  87. $this->shop_phone = $order_info['store_data']['shop_telephone'];
  88. $this->shop_address = $order_info['store_data']['address'];
  89. $this->shop_lng = $order_info['store_data']['shop_lon'];
  90. $this->shop_lat = $order_info['store_data']['shop_lat'];
  91. $this->city_name = $order_info['store_data']['city'];
  92. }
  93. //同城开发者ID
  94. $order_data['dev_id'] = $this->dev_id;
  95. //店铺ID
  96. $order_data['shop_id'] = $this->shop_id;
  97. //店铺ID类型 1:顺丰店铺ID;2:接入方店铺ID
  98. $order_data['shop_type'] = 1;
  99. //商家订单号 不允许重复
  100. $order_data['shop_order_id'] = $order_info['order_num_alias'];
  101. //订单接入来源 1:美团;2:饿了么;3:百度;4:口碑;其他请直接填写中文字符串值
  102. $order_data['order_source'] = mb_substr($this->shopname,0,12,'utf-8');
  103. //取货序号
  104. $order_data['order_sequence'] = "";
  105. //坐标类型 1:百度坐标,2:高德坐标
  106. $order_data['lbs_type'] = "1";
  107. //用户支付方式 1:已付款 0:货到付款
  108. $order_data['pay_type'] = "1";
  109. //代收金额 单位:分
  110. $order_data['receive_user_money'] = "0";
  111. //用户下单时间 秒级时间戳
  112. $order_data['order_time'] = $order_info['date_added'];
  113. //是否是预约单 0:非预约单;1:预约单
  114. $order_data['is_appoint'] = "0";
  115. //用户期望送达时间
  116. //$order_data['expect_time'] = "";
  117. //用户期望取件时间
  118. //$order_data['expect_pickup_time'] = "";
  119. //是否保价 0:非保价;1:保价
  120. $order_data['is_insured'] = "0";
  121. //是否是专人直送订单,0:否;1:是
  122. $order_data['is_person_direct'] = "0";
  123. //保价金额,单位:分
  124. $order_data['declared_value'] = "0";
  125. //订单小费,不传或者传0为不加小费
  126. $order_data['gratuity_fee'] = "0";
  127. //订单备注
  128. $order_data['remark'] = $order_info['note_content'];
  129. //物流流向 1:从门店取件送至用户;2:从用户取件送至门店
  130. $order_data['rider_pick_method'] = "1";
  131. //返回字段控制标志位(二进制)
  132. $order_data['return_flag'] = "511";
  133. //推单时间
  134. $order_data['push_time'] = $time;
  135. //版本号
  136. $order_data['version'] = $this->version;
  137. //收货人信息 begin
  138. $receive_array = [
  139. 'user_name' => $order_info['shipping_name'],//用户姓名
  140. 'user_phone' => $order_info['shipping_tel'],//用户电话
  141. 'user_address' => $order_info['shipping_address'],//用户地址
  142. 'user_lng' => $order_info['shipping_lng'],//用户经度
  143. 'user_lat' => $order_info['shipping_lat'],//用户纬度
  144. 'city_name' => $this->city_name,//发单城市
  145. ];
  146. $order_data['receive'] = $receive_array;
  147. //收货人信息 end
  148. //发货店铺信息
  149. $shop_array = [
  150. "shop_name" => $this->shopname,//店铺名称
  151. "shop_phone" => $this->shop_phone,//店铺电话
  152. "shop_address" => $this->shop_address,//店铺地址
  153. "shop_lng" => $this->shop_lng,//店铺经度
  154. "shop_lat" => $this->shop_lat,//店铺纬度
  155. ];
  156. $order_data['shop'] = $shop_array;
  157. //订单详情 begin
  158. $order_detail_array = array();
  159. $order_detail_array['total_price'] = $order_info['order_total']*100;//用户订单总金额(单位:分)
  160. //物品类型 1:快餐,2:送药,3:百货
  161. //,4:脏衣服收,5:干净衣服派,6:生鲜,7:保单,8:高端饮品,9:现场勘验,10:快递,12:文件
  162. //,13:蛋糕,14:鲜花,15:电子数码,16:服装鞋帽,17:汽车配件,18:珠宝,20:披萨,21:中餐
  163. //,22:水产,27:专人直送,32:中端饮品,33:便利店,34:面包糕点,35:火锅,36:证照,99:其他
  164. $order_detail_array['product_type'] = 1;
  165. $order_detail_array['user_money'] = $order_info['order_total']*100;//实收用户金额(单位:分)
  166. $order_detail_array['shop_money'] = $order_info['order_total']*100;//实付商户金额(单位:分)
  167. $order_detail_array['weight_gram'] = $order_info['goods_weight'];//物品重量(单位:克)
  168. $order_detail_array['volume_litre'] = 0;//物品体积(单位:升)
  169. $order_detail_array['delivery_money'] = 0;//商户收取的配送费(单位:分)
  170. $order_detail_array['product_num'] = $order_info['goods_count'];//物品个数
  171. $order_detail_array['product_type_num'] = $order_info['goods_type_count'];//物品种类个数
  172. $product_detail = [];
  173. foreach($order_info['goods_list'] as $k=>$v){
  174. $goods_array = [];
  175. $goods_array['product_id'] = $v['goods_id'];//物品ID
  176. $goods_array['product_name'] = $v['goods_name'];//物品名称
  177. $goods_array['product_num'] = $v['quantity'];//物品数量
  178. $product_detail[] = $goods_array;
  179. }
  180. $order_detail_array['product_detail'] = $product_detail;//物品详情
  181. $order_data['order_detail'] = $order_detail_array;
  182. //订单详情 end
  183. ksort($order_data);
  184. $body = json_encode($order_data);
  185. $sign = $this->bulidRequestParams($body);
  186. $resp = $this->curl_post($body,$this->reqUrl.$this->add_order_url.$sign);
  187. $result = $this->parseResponseData($resp);
  188. return $result;
  189. }
  190. //查询订单运费接口
  191. public function queryDeliverFee($order_info){
  192. $time = time();
  193. $order_data = array();
  194. //供应商地址
  195. if($order_info['store_id'] > 0){
  196. $this->shop_phone = $order_info['store_data']['shop_telephone'];
  197. $this->shop_address = $order_info['store_data']['address'];
  198. $this->shop_lng = $order_info['store_data']['shop_lon'];
  199. $this->shop_lat = $order_info['store_data']['shop_lat'];
  200. $this->city_name = $order_info['store_data']['city'];
  201. }
  202. //同城开发者ID
  203. $order_data['dev_id'] = $this->dev_id;
  204. //店铺ID
  205. $order_data['shop_id'] = $this->shop_id;
  206. //店铺ID类型 1:顺丰店铺ID;2:接入方店铺ID
  207. $order_data['shop_type'] = 1;
  208. //用户地址经度
  209. $order_data['user_lng'] = $order_info['shipping_lng'];
  210. //用户地址纬度
  211. $order_data['user_lat'] = $order_info['shipping_lat'];
  212. //用户详细地址
  213. $order_data['user_address'] = $order_info['shipping_address'];
  214. //发单城市
  215. $order_data['city_name'] = $this->city_name;
  216. //物品重量(单位:克)
  217. $order_data['weight'] = $order_info['goods_weight'];
  218. //物品类型
  219. $order_data['product_type'] = 1;
  220. //用户订单总金额(单位:分)
  221. $order_data['total_price'] = $order_info['order_total']*100;
  222. //是否是预约单
  223. $order_data['is_appoint'] = 0;
  224. /*//预约单类型
  225. $order_data['appoint_type'] = 0;
  226. //用户期望送达时间
  227. $order_data['expect_time'] = 0;
  228. //用户期望上门时间
  229. $order_data['expect_pickup_time'] = 0;*/
  230. //坐标类型,1:百度坐标,2:高德坐标
  231. $order_data['lbs_type'] = 1;
  232. //用户支付方式:1、已支付 0、货到付款
  233. $order_data['pay_type'] = 1;
  234. //代收金额
  235. $order_data['receive_user_money'] = 0;
  236. //是否保价,0:非保价;1:保价
  237. $order_data['is_insured'] = 0;
  238. //是否是专人直送订单,0:否;1:是
  239. $order_data['is_person_direct'] = 0;
  240. //配送交通工具,0:否;1:电动车;2:小轿车
  241. $order_data['vehicle'] = 0;
  242. //保价金额
  243. $order_data['declared_value'] = 0;
  244. //订单小费,不传或者传0为不加小费
  245. $order_data['gratuity_fee'] = 0;
  246. //物流流向
  247. $order_data['rider_pick_method'] = 1;
  248. //返回字段控制标志位(二进制)
  249. $order_data['return_flag'] = 511;
  250. //推单时间
  251. $order_data['push_time'] = time();
  252. //发货店铺信息
  253. $shop_array = [
  254. "shop_name" => $this->shopname,//店铺名称
  255. "shop_phone" => $this->shop_phone,//店铺电话
  256. "shop_address" => $this->shop_address,//店铺地址
  257. "shop_lng" => $this->shop_lng,//店铺经度
  258. "shop_lat" => $this->shop_lat,//店铺纬度
  259. ];
  260. $order_data['shop'] = $shop_array;
  261. //订单详情 end
  262. ksort($order_data);
  263. $body = json_encode($order_data);
  264. $sign = $this->bulidRequestParams($body);
  265. $resp = $this->curl_post($body,$this->reqUrl.$this->query_delivery_url.$sign);
  266. $result = $this->parseResponseData($resp);
  267. return $result;
  268. }
  269. public function cancelOrder($orderdistribution_info,$cancel_reason){
  270. $order_data = array();
  271. //api开发者ID
  272. $order_data['dev_id'] = $this->dev_id;
  273. //订单ID
  274. $order_data['order_id'] = $orderdistribution_info['third_order_id'];
  275. //查询订单ID类型 1、顺丰订单号 2、商家订单号
  276. $order_data['order_type'] = 1;
  277. //取消原因
  278. $order_data['cancel_reason'] = $cancel_reason;
  279. //取消时间;秒级时间戳
  280. $order_data['push_time'] = time();
  281. //店铺ID order_type=2时必传shop_id与shop_type
  282. $order_data['shop_id'] = 0;
  283. //店铺ID类型 1、顺丰店铺ID 2、接入方店铺ID
  284. $order_data['shop_type'] = 1;
  285. //订单详情 end
  286. ksort($order_data);
  287. $body = json_encode($order_data);
  288. $sign = $this->bulidRequestParams($body);
  289. $resp = $this->curl_post($body,$this->reqUrl.$this->cancel_order_url.$sign);
  290. $result = $this->parseResponseData($resp);
  291. return $result;
  292. }
  293. function bulidRequestParams($post_json){
  294. $signChar = $post_json . "&".$this->dev_id."&".$this->dev_key;
  295. $sign = base64_encode(MD5($signChar));
  296. return $sign;
  297. }
  298. function curl_post($jsonData, $url) {
  299. $ch = curl_init();
  300. // json
  301. $headers = array(
  302. 'Content-Type: application/json,charset=UTF-8',
  303. );
  304. curl_setopt($ch, CURLOPT_URL, $url);
  305. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  306. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
  307. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
  308. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  309. curl_setopt($ch, CURLOPT_HEADER, false);
  310. curl_setopt($ch, CURLOPT_POST, true);
  311. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
  312. $output = curl_exec($ch);
  313. curl_close($ch);
  314. return $output;
  315. }
  316. /**
  317. * 解析响应数据
  318. * @param $arr返回的数据
  319. * 响应数据格式:{"status":"success","result":{},"code":0,"msg":"成功"}
  320. */
  321. public function parseResponseData($arr){
  322. $result = array();
  323. if (empty($arr)) {
  324. $result['status'] = 0;
  325. $result['message'] = "接口请求超时或失败";
  326. $result['code'] = "-2";
  327. }else{
  328. $data = json_decode($arr, true);
  329. if($data['error_code'] == 0){//成功
  330. $result['status'] = 1;
  331. $result['message'] = $data['error_msg'];
  332. $result['code'] = $data['error_code'];
  333. $result['result'] = $data['result'];
  334. }else{//失败
  335. $result['status'] = 0;
  336. $result['message'] = $data['error_msg'];
  337. $result['code'] = $data['error_code'];
  338. $result['result'] = $data['error_data'];
  339. }
  340. }
  341. return $result;
  342. }
  343. }
  344. ?>