Model_fedexv1.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <?php
  2. class Model_Fedexv1 extends Lin_Model {
  3. function __construct(){
  4. parent::__construct();
  5. $this->load->_model('Model_aapeizhi','aapeizhi');
  6. $this->load->_model("Model_logic_ding",'logic_ding');
  7. }
  8. public $base_url = "https://apis.fedex.com.cn";
  9. public $f_config = [
  10. "account"=>"692362136",
  11. "key"=>"l7c6ae3d27cce34bdca31cf6e0ff4af125",
  12. 'secret' =>'475eb390bf334bda8b5f44f1c07b7c10',
  13. "client_id"=>"l7c6ae3d27cce34bdca31cf6e0ff4af125",
  14. "client_secret"=>"475eb390bf334bda8b5f44f1c07b7c10",
  15. //追物流轨迹的
  16. 'track_key'=>"l71bded23966b144df9fc01956f6dd655d",
  17. 'track_secret'=>"61730ddbae764dcba70f4737cdaaa6e7"
  18. ];
  19. public function getAccessToken(){
  20. $res = $this->setting->find("skey='fedex_v1_access_token'");
  21. if($res){
  22. if(time() > $res['expiretime']){
  23. $this->setting->query("DELETE FROM `crowd_setting` WHERE `skey`='fedex_v1_access_token'");
  24. return $this->makeAccessToken();
  25. }
  26. return $res['svalue'];
  27. }else{
  28. return $this->makeAccessToken();
  29. }
  30. }
  31. public function makeAccessToken(){
  32. $url = "/oauth/token";
  33. $headers = [
  34. "Content-Type:application/x-www-form-urlencoded",
  35. ];
  36. $params = [
  37. "grant_type"=>"client_credentials",
  38. "client_id"=> $this->f_config['client_id'],
  39. "client_secret"=> $this->f_config['client_secret'],
  40. ];
  41. $params=http_build_query($params);
  42. $res = $this->sendPost($url,$params,$headers);
  43. if(isset($res['access_token'])){
  44. $this->setting->insert([
  45. 'skey'=>"fedex_v1_access_token",
  46. 'svalue' => $res['access_token'],
  47. 'expiretime' => time() + 3600 -10
  48. ]);
  49. }
  50. return $res['access_token'];
  51. }
  52. public function getTrackToken(){
  53. $res = $this->setting->find("skey='fedex_v1_track_access_token'");
  54. if($res){
  55. if(time() > $res['expiretime']){
  56. $this->setting->query("DELETE FROM `crowd_setting` WHERE `skey`='fedex_v1_track_access_token'");
  57. return $this->makeTrackToken();
  58. }
  59. return $res['svalue'];
  60. }else{
  61. return $this->makeTrackToken();
  62. }
  63. }
  64. public function makeTrackToken(){
  65. $url = "/oauth/token";
  66. $headers = [
  67. "Content-Type:application/x-www-form-urlencoded",
  68. ];
  69. $params = [
  70. "grant_type"=>"client_credentials",
  71. "client_id"=> $this->f_config['track_key'],
  72. "client_secret"=> $this->f_config['track_secret'],
  73. ];
  74. $params=http_build_query($params);
  75. $res = $this->sendPost($url,$params,$headers);
  76. if(isset($res['access_token'])){
  77. $this->setting->insert([
  78. 'skey'=>"fedex_v1_track_access_token",
  79. 'svalue' => $res['access_token'],
  80. 'expiretime' => time() + 3600 -10
  81. ]);
  82. }
  83. return $res['access_token'];
  84. }
  85. private function setData($order_info){
  86. $customer_streets = [];
  87. $customer_streets[] = $order_info['address'];
  88. if(!empty($order_info['address2'])){
  89. $customer_streets[] = $order_info['address2'];
  90. }
  91. $state_codes = $this->aapeizhi->getFedexStateCode();
  92. $hgbm = 67041900;
  93. if(isset($order_info['hgbm']))
  94. {
  95. if($order_info['hgbm'] != '')
  96. {
  97. $hgbm = $order_info['hgbm'];
  98. }
  99. }
  100. $params = [
  101. "labelResponseOptions"=>"URL_ONLY",
  102. "accountNumber"=>[
  103. "value"=> (int)$this->f_config['account']
  104. ],
  105. "requestedShipment"=>[
  106. "shipper"=>[
  107. "contact"=>[
  108. "personName"=>"LONGYI",
  109. "phoneNumber"=>"18539006340",
  110. "companyName"=>"LONGYI",
  111. ],
  112. "address"=>[
  113. "streetLines"=> [
  114. "1403, Buliding C, Bodi Center",
  115. "NingWei Street, XiaoShan"
  116. ],
  117. "city"=>"Hangzhou",
  118. "stateOrProvinceCode"=>"",
  119. "postalCode"=>310000,//杭州邮编
  120. "countryCode"=>$order_info['warehouse']['lb']
  121. ],
  122. ],
  123. "recipients"=>[
  124. [
  125. "contact" => [
  126. "personName"=>$order_info['name'],
  127. "phoneNumber"=>$order_info['phone'],
  128. "companyName"=>$order_info['client']
  129. ],
  130. "address"=>[
  131. "streetLines" =>$customer_streets,
  132. "city"=>$order_info['city'],
  133. "stateOrProvinceCode"=>strlen($order_info['province'])==2?strtoupper($order_info['province']):$state_codes[strtolower($order_info['province'])],
  134. "postalCode"=>(string)$order_info['zipcode'],
  135. "countryCode"=>$order_info['lb'],
  136. ],
  137. ]
  138. ],
  139. "shipDatestamp"=>date("Y-m-d"),
  140. "serviceType"=>($order_info['js'] == 3)?"FEDEX_INTERNATIONAL_PRIORITY":"FEDEX_INTERNATIONAL_CONNECT_PLUS",
  141. "packagingType"=> "FEDEX_PAK",
  142. "pickupType"=>"DROPOFF_AT_FEDEX_LOCATION",
  143. "blockInsightVisibility"=>false,
  144. "shippingChargesPayment"=>[
  145. "paymentType"=>"SENDER"
  146. ],
  147. "shipmentSpecialServices"=>[
  148. "specialServiceTypes"=>[
  149. "ELECTRONIC_TRADE_DOCUMENTS",
  150. ],
  151. "etdDetail"=>[
  152. "requestedDocumentTypes"=>[
  153. "COMMERCIAL_INVOICE"
  154. ]
  155. ]
  156. ],
  157. "shippingDocumentSpecification"=>[
  158. "shippingDocumentTypes"=>[
  159. "COMMERCIAL_INVOICE"
  160. ],
  161. "commercialInvoiceDetail"=>[
  162. "documentFormat"=>[
  163. "docType"=>"PDF",
  164. "stockType"=>"PAPER_LETTER"
  165. ],
  166. "customerImageUsages"=>[
  167. [
  168. "id"=> "IMAGE_1",
  169. "type"=> "SIGNATURE",
  170. "providedImageType"=> "SIGNATURE"
  171. ],
  172. [
  173. "id"=> "IMAGE_2",
  174. "type"=> "LETTER_HEAD",
  175. "providedImageType"=> "LETTER_HEAD"
  176. ]
  177. ]
  178. ]
  179. ],
  180. "rateRequestType"=>['LIST'],
  181. "labelSpecification"=>[
  182. "imageType"=>"PDF",
  183. "labelStockType"=>"STOCK_4X6",
  184. "customerSpecifiedDetail"=>[
  185. "docTabContent"=>[
  186. "docTabContentType"=>"STANDARD"
  187. ]
  188. ]
  189. ],
  190. "customsClearanceDetail"=>[
  191. "dutiesPayment"=>[
  192. "paymentType"=>"RECIPIENT",
  193. ],
  194. "documentContent"=>"COMMODITY",
  195. "commodities"=>[
  196. [
  197. "numberOfPieces"=>$order_info['zjs'],
  198. "description"=>$order_info['sbpm'].'-'.$order_info['ts'].'PCS',
  199. "countryOfManufacture"=> "CN",
  200. 'harmonizedCode'=> $hgbm,//海关编码
  201. "quantity"=> $order_info['ts'],
  202. "quantityUnits"=> "cm",
  203. "unitPrice"=> [
  204. "amount"=> $order_info['dtsbjz'],
  205. "currency"=> "USD"
  206. ],
  207. "customsValue"=> [
  208. "amount"=>$order_info['zsbjz'],
  209. "currency"=> "USD"
  210. ],
  211. "weight"=> [
  212. "units"=> "KG",
  213. "value"=> 0.4
  214. ]
  215. ]
  216. ]
  217. ],
  218. "requestedPackageLineItems"=>[
  219. [
  220. "weight"=>[
  221. "units"=>"KG",
  222. "value"=>0.4
  223. ],
  224. "customerReferences"=>[
  225. "customerReferenceType"=>"CUSTOMER_REFERENCE",
  226. "value"=>$order_info['number']
  227. ]
  228. ],
  229. ]
  230. ]
  231. ];
  232. return $params;
  233. }
  234. //上传货物信息 获取运单 托运
  235. public function get_data($order_info){
  236. try{
  237. $url = "/ship/v1/shipments";
  238. $headers = [
  239. "x-customer-transaction-id"=>$order_info['number'],
  240. "Content-Type:application/json",
  241. "Authorization: Bearer ".$this->getAccessToken(),
  242. "x-locale" => "en_US",//这个是语种加国家
  243. "Accept: application/json",
  244. ];
  245. // echo "<pre>";
  246. // var_dump("Bearer ".$this->getAccessToken());
  247. $params = $this->setData($order_info);
  248. //var_dump($params);
  249. $params=json_encode($params);
  250. //var_dump($params);
  251. $res = $this->sendPost($url,$params,$headers);
  252. //var_dump(json_encode($res,JSON_UNESCAPED_UNICODE));
  253. //var_dump($res);
  254. //die;
  255. if(isset($res['code']) && $res['code'] == -1){
  256. return array('x'=>'0','Description'=>$res['msg']);
  257. }
  258. if(!isset($res["transactionId"])){
  259. return array('x'=>'0','Description'=>'请求异常');
  260. }
  261. if(isset($res["errors"])){
  262. return array('x'=>'0','Description'=>json_encode($res["errors"]));
  263. }
  264. $invoice_url = "";
  265. foreach($res['output']['transactionShipments'][0]['shipmentDocuments'] as $v){
  266. if($v['contentType'] == "COMMERCIAL_INVOICE"){
  267. $invoice_url = $v['url'];
  268. }
  269. }
  270. return
  271. [
  272. 'x'=>'1',
  273. 'waybill'=>$res['output']['transactionShipments'][0]['masterTrackingNumber'],
  274. 'label'=>$res['output']['transactionShipments'][0]['pieceResponses'][0]['packageDocuments'][0]['url'],
  275. 'invoice'=>$invoice_url,
  276. ];
  277. }catch(Exception $e){
  278. $error_msg = "error:[fedexv1]创建发货单出错".$e->getMessage()."|".$e->getLine()."|".$e->getFile()."|请求参数".json_encode($order_info,JSON_UNESCAPED_UNICODE);
  279. $this->logic_ding->sendToDing($error_msg);
  280. if(isset($res)){
  281. $error_msg = "error:[fedexv1]创建发货单出错".$e->getMessage()."|".$e->getLine()."|".$e->getFile()."|返回参数".json_encode($res,JSON_UNESCAPED_UNICODE);
  282. $this->logic_ding->sendToDing($error_msg);
  283. }
  284. }
  285. }
  286. //根据单号获取 快递信息
  287. public function get_logistics($number){
  288. $url = "/track/v1/trackingnumbers";
  289. $params = [
  290. "includeDetailedScans"=>false,
  291. "trackingInfo"=>[
  292. [
  293. "trackingNumberInfo"=>[
  294. "trackingNumber"=> $number['waybill']
  295. ]
  296. ]
  297. ]
  298. ];
  299. $headers = [
  300. "Content-Type:application/json",
  301. "Authorization: Bearer ".$this->getTrackToken(),
  302. "x-locale" => "en_US",//这个是语种加国家
  303. "Accept: application/json",
  304. ];
  305. $params=json_encode($params);
  306. var_dump(json_encode($headers));
  307. var_dump($params);
  308. var_dump($this->getTrackToken());
  309. $res = $this->sendPost($url,$params,$headers);
  310. var_dump(json_encode($res));
  311. echo "<pre>";
  312. var_dump($res);
  313. }
  314. public function sendPost($url,$params,$headers){
  315. // var_dump($bodyString);
  316. // 设置请求的URL
  317. $all_url = $this->base_url.$url;
  318. //var_dump($all_url);
  319. // 初始化cURL会话
  320. $ch = curl_init($all_url);
  321. // 设置cURL选项
  322. $options = array(
  323. CURLOPT_SSL_VERIFYPEER=>false,
  324. CURLOPT_SSL_VERIFYHOST=>false,
  325. CURLOPT_RETURNTRANSFER => true,
  326. CURLOPT_POST => true,
  327. CURLOPT_POSTFIELDS => $params,
  328. CURLOPT_HTTPHEADER => $headers,
  329. CURLOPT_ENCODING => "gzip,deflate"
  330. );
  331. // 应用这些选项到cURL会话
  332. curl_setopt_array($ch, $options);
  333. // 执行cURL会话并获取响应
  334. $response = curl_exec($ch);
  335. // 检查是否有错误发生
  336. if ($response === false) {
  337. $error = curl_error($ch);
  338. curl_close($ch);
  339. return [
  340. "code"=> -1,
  341. "msg"=> $error
  342. ];
  343. }
  344. // 关闭cURL会话
  345. curl_close($ch);
  346. // 输出响应内容
  347. return json_decode($response,true);
  348. }
  349. //上传数字签名 和 信头 接口在 文件上传api 主要就传一次 用postman传就行 不费事了 etd 必须先传
  350. public function upload_img(){
  351. $all_url = "https://documentapitest.prod.fedex.com/sandbox/documents/v1/lhsimages/upload";
  352. }
  353. public function send_sign(){
  354. $params = [
  355. "document"=>[
  356. "referenceId"=>"1234",
  357. "name"=>"sign.png",
  358. "contentType"=>"image/png",
  359. "meta"=>[
  360. "imageType"=>"SIGNATURE",
  361. "imageIndex"=>"IMAGE_1"
  362. ]
  363. ],
  364. "rules"=>[
  365. "workflowName"=> "LetterheadSignature"
  366. ]
  367. ];
  368. $body = [
  369. 'document'=>json_encode($params),
  370. 'attachment'=>new CURLFile("D:\wwwroot\cerp.wepolicy.cn\sign.png")
  371. ];
  372. $headers = [
  373. "x-customer-transaction-id"=>time(),
  374. "Content-Type:multipart/form-data",
  375. "Authorization: Bearer ".$this->getAccessToken(),
  376. "x-locale" => "en_US",//这个是语种加国家
  377. "Accept: application/json",
  378. ];
  379. $r = $this->send_img_post($body,$headers);
  380. echo "<pre>";
  381. var_dump($r);
  382. }
  383. public function send_xintou(){
  384. $params = [
  385. "document"=>[
  386. "referenceId"=>"1234",
  387. "name"=>"Logo.png",
  388. "contentType"=>"image/png",
  389. "meta"=>[
  390. "imageType"=>"LETTERHEAD",
  391. "imageIndex"=>"IMAGE_2"
  392. ]
  393. ],
  394. "rules"=>[
  395. "workflowName"=> "LetterheadSignature"
  396. ]
  397. ];
  398. $body = [
  399. 'document'=>json_encode($params),
  400. 'attachment'=>new CURLFile("D:\wwwroot\cerp.wepolicy.cn\Logo.png")
  401. ];
  402. $headers = [
  403. "x-customer-transaction-id"=>time(),
  404. "Content-Type:multipart/form-data",
  405. "Authorization: Bearer ".$this->getAccessToken(),
  406. "x-locale" => "en_US",//这个是语种加国家
  407. "Accept: application/json",
  408. ];
  409. $r = $this->send_img_post($body,$headers);
  410. echo "<pre>";
  411. var_dump($r);
  412. }
  413. public function send_img_post($body,$headers){
  414. $upload_img_url = "/documents/v1/lhsimages/upload";
  415. $all_url = "https://documentapi.prod.fedex.com/documents/v1/lhsimages/upload";
  416. var_dump($all_url);
  417. var_dump($body);
  418. // 初始化cURL会话
  419. $ch = curl_init($all_url);
  420. // 设置cURL选项
  421. $options = array(
  422. CURLOPT_SSL_VERIFYPEER=>false,
  423. CURLOPT_SSL_VERIFYHOST=>false,
  424. CURLOPT_RETURNTRANSFER => true,
  425. CURLOPT_POST => true,
  426. CURLOPT_POSTFIELDS => $body,
  427. CURLOPT_HTTPHEADER => $headers,
  428. CURLOPT_ENCODING => "gzip,deflate"
  429. );
  430. // 应用这些选项到cURL会话
  431. curl_setopt_array($ch, $options);
  432. // 执行cURL会话并获取响应
  433. $response = curl_exec($ch);
  434. // 检查是否有错误发生
  435. if ($response === false) {
  436. $error = curl_error($ch);
  437. curl_close($ch);
  438. return [
  439. "code"=> -1,
  440. "msg"=> $error
  441. ];
  442. }
  443. // 关闭cURL会话
  444. curl_close($ch);
  445. // 输出响应内容
  446. return json_decode($response,true);
  447. }
  448. }