Model_apitt.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. <?php
  2. class Model_apitt extends Lin_Model {
  3. function __construct(){
  4. parent::__construct();
  5. $this->load->_model("Model_logic_ding","logic_ding");
  6. }
  7. public function get_list($shop,$from,$to,$order_status,$cuy,$num,$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc,$zjsku)//获取订单列表
  8. {
  9. $url = 'https://open-api.tiktokglobalshop.com';
  10. $link = '/order/202309/orders/search';
  11. $time = time();
  12. $post['create_time_ge'] = $from;
  13. $post['create_time_lt'] = $to;
  14. $post['order_status'] = $order_status;
  15. $queryParams['page_size'] = 100;
  16. $queryParams['sort_order'] = 'ASC';
  17. $queryParams['sort_field'] = 'create_time';
  18. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  19. $queryParams['shop_id'] =$shop['codeid'];
  20. $queryParams['app_key'] =$shop['app_key'];
  21. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  22. $queryParams['timestamp'] =$time;
  23. $link .= '?'.http_build_query($queryParams);
  24. $sign = $this->sign($link,$shop['app_secret'],$post);
  25. $url .= $link.'&sign='.$sign;
  26. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  27. $res = $this->su_curl($post,$url,$headers);
  28. // if($shop['shopname'] == 'SuperNova Store3'){
  29. // $this->logic_ding->sendToDing($shop['shopname']."测试获取TT订单".$res);
  30. // }
  31. $this->logic_ding->sendToDing($shop['shopname']."测试获取TT订单".$res);
  32. $res = json_decode($res,true);
  33. $list = array();$page_token = '';$arr = array();
  34. if(isset($res['data']['orders']))
  35. {
  36. foreach ($res['data']['orders'] as $val)
  37. {
  38. $d = $this->fullordertt->get_orderinfo($val['id']);
  39. if(!$d)
  40. {
  41. $list[] = $val['id'];
  42. }
  43. }
  44. if($res['data']['total_count'] > 100)
  45. {
  46. $page_token = $res['data']['next_page_token'];
  47. $synum = $res['data']['total_count']-100;
  48. if($synum > 100)
  49. {
  50. $synum = ceil($synum/100);
  51. }
  52. else
  53. {
  54. $synum = 1;
  55. }
  56. for($i=0;$i<$synum;$i++)
  57. {
  58. if($page_token)
  59. {
  60. $net_date = $this->get_next_list($shop,$from,$to,$order_status,$page_token);
  61. $page_token = $net_date[1];
  62. foreach ($net_date['0'] as $val)
  63. {
  64. $d = $this->fullordertt->get_orderinfo($val['id']);
  65. if(!$d)
  66. {
  67. $list[] = $val['id'];
  68. }
  69. }
  70. }
  71. }
  72. }
  73. $cs = array();
  74. if(count($list) > 50)
  75. {
  76. $n = ceil(count($list)/50);$x = 0;
  77. for($i=0;$i<$n;$i++)
  78. {
  79. $k = ($i<1)?0:$i*50;
  80. $data = array_slice($list,$k,50);
  81. $d = $this->get_data($data,$shop);
  82. $cs[] = $d;
  83. if(isset($d['data']))
  84. {
  85. foreach ($d['data']['orders'] as $val)
  86. {
  87. $arr[$x] = $val;
  88. $x++;
  89. }
  90. }
  91. }
  92. }
  93. else
  94. {
  95. $d = $this->get_data($list,$shop);
  96. if(isset($d['data']))
  97. {
  98. foreach ($d['data']['orders'] as $val)
  99. {
  100. $arr[] = $val;
  101. }
  102. }
  103. }
  104. }
  105. $read = array();
  106. $gnum = array();
  107. foreach ($arr as $val)
  108. {
  109. $create_time = substr($val['create_time'],0,10)-$sjc;
  110. if(isset($gnum[date('ymd',$create_time)]))
  111. {
  112. $gnum[date('ymd',$create_time)] += 1;
  113. // if($shop['id'] == 44){
  114. // $this->logic_ding->sendToDing("测试获取TT订单-1-".date('ymd',$create_time)."-".$gnum[date('ymd',$create_time)]);
  115. // }
  116. }
  117. else
  118. {
  119. //$num = $this->fullordertt->find_count('gtime = "'.date('Ymd',$create_time).'" and shop = "'.$shop['id'].'"');
  120. //避免number 冲突的问题
  121. $info = $this->fullordertt->find('gtime = "'.date('Ymd',$create_time).'" and shop = "'.$shop['id'].'"',"*","id desc");
  122. if(empty($info)){
  123. $num = 0;
  124. }else{
  125. $tmp_number_arr = explode("-",$info['number']);
  126. //合并订单
  127. if(count($tmp_number_arr) == 5){
  128. $num = $this->fullordertt->find_count('gtime = "'.date('Ymd',$create_time).'" and shop = "'.$shop['id'].'"');
  129. }else{
  130. $num = array_pop($tmp_number_arr);
  131. $num = $num*1;
  132. }
  133. }
  134. $gnum[date('ymd',$create_time)] = $num+1;
  135. // if($shop['id'] == 44){
  136. // $this->logic_ding->sendToDing("测试获取TT订单-2-".date('ymd',$create_time)."-".$gnum[date('ymd',$create_time)]);
  137. // }
  138. }
  139. $read[] = $this->get_read($val,$time,$shop,$cuy,$gnum[date('ymd',$create_time)],$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc,$zjsku);
  140. }
  141. return array($read,$list);
  142. }
  143. public function get_next_list($shop,$from,$to,$order_status,$page_token='')//获取订单列表
  144. {
  145. $url = 'https://open-api.tiktokglobalshop.com';
  146. $link = '/order/202309/orders/search';
  147. $time = time();
  148. $post['create_time_ge'] = $from;
  149. $post['create_time_lt'] = $to;
  150. $post['order_status'] = $order_status;
  151. $queryParams['page_size'] = 100;
  152. $queryParams['sort_order'] = 'ASC';
  153. $queryParams['sort_field'] = 'create_time';
  154. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  155. $queryParams['shop_id'] =$shop['codeid'];
  156. $queryParams['app_key'] =$shop['app_key'];
  157. $queryParams['timestamp'] =$time;
  158. if($page_token)
  159. {
  160. $queryParams['page_token'] = $page_token;
  161. }
  162. $link .= '?'.http_build_query($queryParams);
  163. $sign = $this->sign($link,$shop['app_secret'],$post);
  164. $url .= $link.'&sign='.$sign;
  165. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  166. $res = $this->su_curl($post,$url,$headers);
  167. $res = json_decode($res,true);
  168. $orders = array();
  169. if(isset($res['data']['orders']))
  170. {
  171. $orders = $res['data']['orders'];
  172. }
  173. return array($orders,$res['data']['next_page_token']);
  174. }
  175. public function get_data($list,$shop)
  176. {
  177. if(empty($list)){
  178. return [];
  179. }
  180. $url = 'https://open-api.tiktokglobalshop.com';
  181. $link = '/order/202309/orders';
  182. $time = time();
  183. $ids = implode(",",$list);
  184. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time.'&ids='.$ids;
  185. $sign = $this->sign($link,$shop['app_secret'],'');
  186. $url .= $link.'&sign='.$sign;
  187. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  188. $res = $this->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  189. $res = json_decode($res,true);
  190. return $res;
  191. }
  192. public function get_je($d,$shop)//预估到账金额
  193. {
  194. $url = 'https://open-api.tiktokglobalshop.com';
  195. $link = '/finance/202309/orders/'.$d['orderinfo'].'/statement_transactions';
  196. $time = time();
  197. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time;
  198. $sign = $this->sign($link,$shop['app_secret'],'');
  199. $url .= $link.'&sign='.$sign;
  200. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  201. $res = $this->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  202. $res = json_decode($res,true);
  203. return $res;
  204. }
  205. public function get_read($data,$time,$shop,$cuy,$num,$dtc,$money,$int,$dtctitle,$pay,$typeclass,$ex,$sjc,$zjsku)
  206. {
  207. $purl = 'https://shop.tiktok.com/view/product/';
  208. $link = '';$product = '';$sku = '';$quantity = '';$price = "";$row_total ="";
  209. $lpsq = array();
  210. $extra_text = [];
  211. $line_items = [];
  212. $extra_text['sub_total'] = 0;
  213. if(isset($data['payment']) && isset($data['payment']['sub_total'])){
  214. $extra_text['sub_total'] = $data['payment']['sub_total'];
  215. }
  216. $extra_text['packages'] = $data['packages'];
  217. foreach ($data['line_items'] as $v)
  218. {
  219. $line_items[] = [
  220. "id"=>$v["id"],
  221. "package_id"=>empty($v['package_id'])?0:$v['package_id'],
  222. 'product_id'=>$v['product_id']
  223. ];
  224. if(isset($lpsq[$v['seller_sku']]))
  225. {
  226. $lpsq[$v['seller_sku']]['quantity'] += 1;
  227. $tmp_row_total = $v['sale_price'] ;
  228. if(empty($v['item_tax'])){
  229. }else{
  230. foreach($v['item_tax'] as $kk => $vv){
  231. if(isset($vv['tax_amount'])){
  232. $tmp_row_total = $tmp_row_total*1 + $vv['tax_amount']*1;
  233. }
  234. }
  235. }
  236. $lpsq[$v['seller_sku']]['row_total'] = $lpsq[$v['seller_sku']]['row_total']*1 + $tmp_row_total*1;
  237. }
  238. else
  239. {
  240. //$lpsq[$v['seller_sku']] = array('link'=>$purl.$v['product_id'],'product'=>$v['product_name'],'sku'=>$v['seller_sku'],'quantity'=>1);
  241. $tmp_row_total = $v['sale_price'] ;
  242. if(empty($v['item_tax'])){
  243. }else{
  244. foreach($v['item_tax'] as $kk => $vv){
  245. if(isset($vv['tax_amount'])){
  246. $tmp_row_total = $tmp_row_total*1 + $vv['tax_amount']*1;
  247. }
  248. }
  249. }
  250. $lpsq[$v['seller_sku']] = array('link'=>$purl.$v['product_id'],'product'=>$v['product_name'],'sku'=>$v['seller_sku'],'quantity'=>1,'price'=>$v['original_price'],"row_total"=>$tmp_row_total);
  251. }
  252. }
  253. $extra_text['line_items'] = $line_items;
  254. // $this->logic_ding->sendToDing($shop['shopname']."测试获取TT订单".json_encode($data));
  255. // $this->logic_ding->sendToDing($shop['shopname']."测试获取TT订单数据转化".json_encode($lpsq));
  256. foreach ($lpsq as $v)
  257. {
  258. $link .= $v['link'].',';
  259. $product .= $v['product'].',';
  260. $sku .= $v['sku'].',';
  261. $quantity .= $v['quantity'].';';
  262. $price .= $v['price'].',';
  263. $row_total .= $v['row_total'].',';
  264. }
  265. if($data['status'] == 'DELIVERED' || $data['status'] == 'IN_TRANSIT' || $data['status'] == 'COMPLETED')
  266. {
  267. $s = 216;
  268. }
  269. else if($data['status'] == 'AWAITING_SHIPMENT')
  270. {
  271. $s = 207;
  272. }
  273. else if($data['status'] == 'CANCELLED')
  274. {
  275. $s = 217;
  276. }
  277. else
  278. {
  279. $s = 283;
  280. }
  281. if(isset($zjsku[$shop['id']]))//判断是否找到店铺SKU
  282. {
  283. $sku = rtrim($sku,',').','.$zjsku[$shop['id']];
  284. }
  285. $post['extra_text'] = json_encode($extra_text,JSON_UNESCAPED_UNICODE);
  286. $post['user_id'] = $data['user_id'];
  287. $post['state'] = $s;
  288. $post['shop'] = $shop['id'];//店铺ID
  289. $post['user'] = $shop['shopuser'];//店铺负责人
  290. $post['product'] = rtrim($product,',');//产品名称
  291. $post['link'] = rtrim($link,',');//产品链接
  292. $post['issku'] = rtrim($sku,',');
  293. $post['quantity'] = rtrim($quantity,';');
  294. $post['clientremarks'] = $data['buyer_message'];//客户备注
  295. $post['paypal'] = '';//交易号
  296. $post['guarantee'] = '';//卖家保障
  297. $post['parameter'] = '';//属性
  298. $post['shippingmethod'] = $data['delivery_option_name'].' '.$data['payment']['shipping_fee'];//用户选择快递方式及运费金额
  299. $post['source'] = 2;//订单类型
  300. $post['type'] = 1;//发货仓库
  301. $post['capital'] = 3;//资金支付状况,全部付款
  302. $numbernew = $shop['shortname'].'-'.date('ymd',substr($data['create_time'],0,10)-$sjc);
  303. $numberwb = (substr(strval($num+1000),1,3));
  304. if($numbernew == 'TT-QQS-40807')
  305. {
  306. $numberwb = $numberwb+7;
  307. }
  308. //过完今天就删除
  309. $tmp_time = date("Y-m-d");
  310. // if($tmp_time == '20250114'){
  311. // $numberwb = $numberwb+7;
  312. // }
  313. $post['number'] = $shop['shortname'].'-'.date('ymd',substr($data['create_time'],0,10)-$sjc).'-'.$numberwb;//编号
  314. $post['orderinfo'] = $data['id'];//订单号
  315. $post['insurance'] = 0;//运输保险费用
  316. $address = $data['recipient_address']['address_detail'];
  317. if(isset($data['recipient_address']['district_info'][3])){
  318. $post['baddress'] = $address.','.$data['recipient_address']['district_info'][3]['address_name'].','.$data['recipient_address']['district_info'][1]['address_name'].','.$data['recipient_address']['postal_code'].','.$data['recipient_address']['region_code'].','.$data['recipient_address']['phone_number'];
  319. $post['saddress'] = $address.','.$data['recipient_address']['district_info'][3]['address_name'].','.$data['recipient_address']['district_info'][1]['address_name'].','.$data['recipient_address']['postal_code'].','.$data['recipient_address']['region_code'].','.$data['recipient_address']['phone_number'];
  320. }else{
  321. $post['baddress'] = $address.','.$data['recipient_address']['district_info'][2]['address_name'].','.$data['recipient_address']['district_info'][1]['address_name'].','.$data['recipient_address']['postal_code'].','.$data['recipient_address']['region_code'].','.$data['recipient_address']['phone_number'];
  322. $post['saddress'] = $address.','.$data['recipient_address']['district_info'][2]['address_name'].','.$data['recipient_address']['district_info'][1]['address_name'].','.$data['recipient_address']['postal_code'].','.$data['recipient_address']['region_code'].','.$data['recipient_address']['phone_number'];
  323. }
  324. $post['country'] = $cuy[$data['recipient_address']['region_code']];//此国家的ID
  325. $post['al'] = $data['recipient_address']['region_code'];//联邦名称
  326. $post['bname'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//名称
  327. $post['sname'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//名称
  328. $post['client'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//公司名称
  329. $post['name'] = preg_replace('/( | | |\s)/',' ',$data['recipient_address']['name']);//收件人名称
  330. $post['phone'] = preg_replace('/\D/s','',$data['recipient_address']['phone_number']);//收件人电话
  331. $post['email'] = preg_replace('/( | | |\s)+/','',$data['buyer_email']);//客户邮箱
  332. $post['zipcode'] = $data['recipient_address']['postal_code'];//收件人邮编
  333. $post['province'] = $data['recipient_address']['district_info'][1]['address_name'];//收件人省份
  334. if(isset($data['recipient_address']['district_info'][3])){
  335. $post['city'] = $data['recipient_address']['district_info'][3]['address_name'];//收件人城市
  336. }else{
  337. $post['city'] = $data['recipient_address']['district_info'][2]['address_name'];//收件人城市
  338. }
  339. $post['address'] = $address;//收件人地址
  340. $post['shouldmoney'] = $data['payment']['total_amount'];//应收金额
  341. $post['skje'] = $post['shouldmoney'];//收款金额
  342. $post['ismoney'] = $data['payment']['currency'].$post['shouldmoney'];//支付币种金额
  343. $yga = $post['shouldmoney'];//金额
  344. //现在用Fulfillment type 区分仓发和商家发
  345. if(strpos($data['fulfillment_type'],'SELLER')!== false){
  346. $post['cf'] = 0;
  347. }else{
  348. $post['cf'] = 1;
  349. }
  350. //$post['cf'] = ($data['shipping_type']=='SELLER')?0:1;//是否仓发TIKTOK平台仓发 SELLER商家发,ERP中1平台仓发,0商家发
  351. //但是还有 HOME_DELIVERY 如果店铺是 直接分到许昌仓
  352. if(in_array($post['shop'],[35,38,39,40,41,44,46,47,49,58])){
  353. $post['cf'] = 0;
  354. }
  355. $post['budget'] = 0;//预估到账金额
  356. /**
  357. if(isset($data['pay']) && $yga > 0)
  358. {
  359. $post['pay'] = $pay[$data['pay']]['id'];
  360. $ygc = $pay[$data['pay']]['estimaterate'];//预估到账公式
  361. $post['estimaterate'] = $pay[$data['pay']]['estimaterate'];
  362. $ifbudget = eval("return $yga*1.$ygc;");
  363. if($ifbudget > 0)
  364. {
  365. $post['budget'] = eval("return $yga*1.$ygc;");//预估到帐金额
  366. }
  367. }
  368. else if($yga > 0)
  369. {
  370. $post['pay'] = 0;
  371. $ygb = $shop['estimaterate'];//店铺默认到账公式
  372. $post['estimaterate'] = $shop['estimaterate'];
  373. $ifbudget = eval("return $yga*1.$ygb;");
  374. if($ifbudget > 0)
  375. {
  376. $post['budget'] = eval("return $yga*1.$ygb;");//预估到帐金额
  377. }
  378. }
  379. **/
  380. $post['overtime'] = $data['shipping_due_time']-$sjc;//超时时间
  381. $post['buytime'] = substr($data['paid_time'],0,10)-$sjc;//格式化付款时间
  382. $post['dtime'] = substr($data['create_time'],0,10)-$sjc;//订单时间
  383. $post['gtime'] = date('Ymd',substr($data['create_time'],0,10)-$sjc);//格式化订单时间
  384. $post['currencytitle'] = $data['payment']['currency'];//结算币种名称
  385. $post['currency'] = $typeclass[$data['payment']['currency']];//钱币ID
  386. $post['time'] = time();//同步时间
  387. $post['hl'] = '6.4';//汇率
  388. $post['delivery_option_id'] = $data['delivery_option_id'];
  389. if(isset($data['delivery_option_name']))
  390. {
  391. $post['express'] = (isset($ex[$data['delivery_option_name']]))?$ex[$data['delivery_option_name']]['id']:0;//快递方式
  392. }
  393. if(isset($data['line_items'][0]['tracking_number']))
  394. {
  395. $post['waybill'] = $data['line_items'][0]['tracking_number'];
  396. }
  397. //$this->logic_ding->sendToDing("测试获取TT订单编码".$post['number']);
  398. $matching = $this->api->matching($post['quantity'],$post['issku'],$dtc,$money,$int,$dtctitle,$shop,[
  399. 'price'=>trim($price,','),
  400. 'row_total'=>trim($row_total,','),
  401. ]);
  402. $post['shipremarks'] = $matching['title'];
  403. $post['cost'] = $matching['cost'];
  404. $post['purchase'] = $matching['purchase'];
  405. $post['fpdata'] = $matching['product'];
  406. $post['whlabel'] = $matching['whlabel'];
  407. $post['slpx'] = $matching['wcslpx'];
  408. return $post;
  409. }
  410. public function getExpressCompany($info,$shop){
  411. $url = 'https://open-api.tiktokglobalshop.com';
  412. $link = "/fulfillment/202309/orders/".trim($info['orderinfo'])."/shipping_services/query";
  413. $time = time();
  414. if(is_string($info['extra_text'])){
  415. $extra_text = json_decode($info['extra_text'],true);
  416. }else{
  417. $extra_text = $info['extra_text'];
  418. }
  419. $order_line_item_ids = [];
  420. foreach($extra_text['line_items'] as $v)
  421. {
  422. $order_line_item_ids[] = $v['id'];
  423. }
  424. $post = [
  425. "order_line_item_ids"=>$order_line_item_ids,
  426. "weight"=>[
  427. "value"=>"0.88",
  428. "unit"=>"POUND"
  429. ],
  430. "dimension"=>[
  431. "length"=> "5.91",
  432. "width"=> "3.93",
  433. "height"=> "1.97",
  434. "unit"=> "INCH"
  435. ]
  436. ];
  437. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  438. $queryParams['app_key'] =$shop['app_key'];
  439. $queryParams['timestamp'] =$time;
  440. $link .= '?'.http_build_query($queryParams);
  441. $sign = $this->sign($link,$shop['app_secret'],$post);
  442. $url .= $link.'&sign='.$sign;
  443. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  444. $res = $this->su_curl($post,$url,$headers);
  445. //$this->logic_ding->sendToDing($info['number']."获取TT快递服务信息CBT".$res);
  446. $res = json_decode($res,true);
  447. return $res;
  448. }
  449. //商家自定义发货 但是用的tiktok的渠道
  450. public function ShipByTiktok($pachage_id,$shop,$info){
  451. $url = 'https://open-api.tiktokglobalshop.com';
  452. $link = "/fulfillment/202309/packages/".$pachage_id."/ship";
  453. $time = time();
  454. $post = [
  455. "handover_method"=>"DROP_OFF"
  456. ];
  457. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  458. $queryParams['app_key'] =$shop['app_key'];
  459. $queryParams['timestamp'] =$time;
  460. $link .= '?'.http_build_query($queryParams);
  461. $sign = $this->sign($link,$shop['app_secret'],$post);
  462. $url .= $link.'&sign='.$sign;
  463. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  464. $res = $this->su_curl($post,$url,$headers);
  465. //$this->logic_ding->sendToDing($info['number']."TT的API的发货请求FBTXD".$res);
  466. $res = json_decode($res,true);
  467. return $res;
  468. }
  469. //创建运单
  470. public function createLabel($info,$shop){
  471. $url = 'https://open-api.tiktokglobalshop.com';
  472. $link = "/fulfillment/202309/packages";
  473. $time = time();
  474. if(is_string($info['extra_text'])){
  475. $extra_text = json_decode($info['extra_text'],true);
  476. //快递服务商
  477. $server_info = $extra_text['server_info'];
  478. $info['extra_text'] = $extra_text;
  479. }else{
  480. //快递服务商
  481. $server_info = $info['extra_text'];
  482. }
  483. //$this->logic_ding->sendToDing($info['number']."获取TT快递服务信息CBTXD【请求参数】".json_encode($info));
  484. if($info['express'] == 85){
  485. $ttsp_xz_name = $server_info['shipping_services'][$info['ttsp_xz']]['name'];
  486. if(strpos($ttsp_xz_name,'USPS')=== false){
  487. return ['code'=>10000,'msg'=>'该快递信息异常,请联系技术-USPS'];
  488. }
  489. }
  490. if($info['express'] == 87){
  491. $ttsp_xz_name = $server_info['shipping_services'][$info['ttsp_xz']]['name'];
  492. if(strpos($ttsp_xz_name,'FedEx')=== false){
  493. return ['code'=>10000,'msg'=>'该快递信息异常,请联系技术-FedEx'];
  494. }
  495. }
  496. $post = [
  497. 'order_id'=>$server_info['order_id'],
  498. "order_line_item_ids"=>$server_info['order_line_id'],
  499. "dimension"=>$server_info['dimension'],
  500. "shipping_service_id"=>$server_info['shipping_services'][$info['ttsp_xz']]['id'],
  501. "weight"=>$server_info['weight'],
  502. ];
  503. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  504. $queryParams['app_key'] =$shop['app_key'];
  505. $queryParams['timestamp'] =$time;
  506. $link .= '?'.http_build_query($queryParams);
  507. $sign = $this->sign($link,$shop['app_secret'],$post);
  508. $url .= $link.'&sign='.$sign;
  509. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  510. $res = $this->su_curl($post,$url,$headers);
  511. //$this->logic_ding->sendToDing($info['number']."获取TT快递服务信息CBTXD".$res."【请求参数】".json_encode($post));
  512. $res = json_decode($res,true);
  513. return $res;
  514. }
  515. //获取tiktok的来自于直播的订单
  516. //或者叫做获取联盟的单子
  517. public function getAffiliateOrders($shop){
  518. $url = "https://open-api.tiktokglobalshop.com";
  519. //$link = "/affiliate_partner/202411/orders/search";
  520. $link = "/affiliate_seller/202410/orders/search";
  521. $time = $end_time = time();
  522. $start_time = $end_time - 3600 * 24 *3;
  523. $post = [
  524. 'create_time_ge'=>$start_time,
  525. "create_time_lt"=>$end_time,
  526. ];
  527. $queryParams['shop_cipher'] =$shop['shop_cipher'];
  528. $queryParams['app_key'] =$shop['app_key'];
  529. $queryParams['timestamp'] =$time;
  530. $queryParams['page_size'] = 100;
  531. $link .= '?'.http_build_query($queryParams);
  532. $sign = $this->sign($link,$shop['app_secret'],$post);
  533. $url .= $link.'&sign='.$sign;
  534. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  535. $res = $this->su_curl($post,$url,$headers);
  536. echo "<pre>";
  537. var_dump($res);
  538. print_r($shop);
  539. //$this->logic_ding->sendToDing($info['number']."获取TT快递服务信息CBTXD".$res."【请求参数】".json_encode($post));
  540. $res = json_decode($res,true);
  541. }
  542. public function getPayInfo($order_id,$shop){
  543. $url = 'https://open-api.tiktokglobalshop.com';
  544. $link = '/order/202407/orders/'.$order_id.'/price_detail';
  545. $time = time();
  546. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time.'&ids='.$order_id;
  547. $sign = $this->sign($link,$shop['app_secret'],'');
  548. $url .= $link.'&sign='.$sign;
  549. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  550. $res = $this->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  551. $res = json_decode($res,true);
  552. return $res;
  553. }
  554. public function get_detail($order_id,$shop){
  555. $url = 'https://open-api.tiktokglobalshop.com';
  556. $link = '/order/202309/orders';
  557. $time = time();
  558. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time.'&ids='.$order_id;
  559. $sign = $this->sign($link,$shop['app_secret'],'');
  560. $url .= $link.'&sign='.$sign;
  561. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  562. $res = $this->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  563. $res = json_decode($res,true);
  564. return $res;
  565. }
  566. //下载快递面单
  567. public function downloadLabel($package_id,$shop,$info){
  568. $url = 'https://open-api.tiktokglobalshop.com';
  569. $link = '/fulfillment/202309/packages/'.$package_id.'/shipping_documents';
  570. $time = time();
  571. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time."&document_type=SHIPPING_LABEL";
  572. $sign = $this->sign($link,$shop['app_secret'],'');
  573. $url .= $link.'&sign='.$sign;
  574. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  575. $res = $this->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  576. //$this->logic_ding->sendToDing($info['number']."获取TT快递服务信息MD".$res);
  577. $res = json_decode($res,true);
  578. return $res;
  579. }
  580. //获取快递信息
  581. public function getWaybillInfo($package_id,$shop){
  582. $url = 'https://open-api.tiktokglobalshop.com';
  583. $link = '/fulfillment/202309/packages/'.$package_id;
  584. $time = time();
  585. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time."";
  586. $sign = $this->sign($link,$shop['app_secret'],'');
  587. $url .= $link.'&sign='.$sign;
  588. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  589. $res = $this->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  590. //$this->logic_ding->sendToDing($info['number']."获取TT快递服务信息MD".$res);
  591. $res = json_decode($res,true);
  592. return $res;
  593. }
  594. //获取直播单子
  595. public function getLiveOrders($shop){
  596. $url = 'https://open-api.tiktokglobalshop.com';
  597. $link = '/analytics/202508/shop_lives/performance';
  598. $time = time();
  599. $start_date_ge = date('Y-m-d', strtotime('-3 day'));
  600. $end_date_lt = date('Y-m-d', time());
  601. $link .= '?shop_cipher='.$shop['shop_cipher'].'&app_key='.$shop['app_key'].'&timestamp='.$time."&start_date_ge=".$start_date_ge."&end_date_lt=".$end_date_lt;
  602. $sign = $this->sign($link,$shop['app_secret'],'');
  603. $url .= $link.'&sign='.$sign;
  604. $headers = array('Content-Type: application/json','x-tts-access-token:'.$shop['token']);
  605. $res = $this->su_curl('',$url,$headers,$date_type='json',$timeout=300,$httptype="GET");
  606. //$this->logic_ding->sendToDing($info['number']."获取TT快递服务信息MD".$res);
  607. $res = json_decode($res,true);
  608. return $res;
  609. }
  610. public function sign($url,$appSecret,$body='')
  611. {
  612. // 解析URL获取查询参数
  613. $queryParams = array();
  614. parse_str(parse_url($url, PHP_URL_QUERY), $queryParams);
  615. // 排除'sign'和'access_token'
  616. unset($queryParams['sign'], $queryParams['access_token']);
  617. // 按字典顺序排序查询参数
  618. ksort($queryParams);
  619. // 拼接参数为{key}{value}格式
  620. $input = '';
  621. foreach ($queryParams as $key => $value)
  622. {
  623. $input .= $key . $value;
  624. }
  625. // 追加请求路径
  626. $path = parse_url($url, PHP_URL_PATH);
  627. $input = $path . $input;
  628. if ($body)
  629. {
  630. $input .= json_encode($body);
  631. }
  632. // 使用app_secret包裹生成的字符串
  633. $input = $appSecret . $input . $appSecret;
  634. // 生成签名
  635. $sign = hash_hmac('sha256', $input,$appSecret);
  636. return $sign;
  637. }
  638. public function su_curl($data,$url,$header=array(),$date_type='json',$timeout=300,$httptype="POST",$userpwd='')
  639. {
  640. if ($date_type == 'http_build_query')
  641. {
  642. $data = http_build_query($data);
  643. }
  644. else if ($date_type == 'json')
  645. {
  646. $data = json_encode($data);
  647. }
  648. $ch = curl_init();
  649. curl_setopt($ch, CURLOPT_URL, $url);
  650. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  651. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  652. curl_setopt($ch, CURLOPT_HEADER, false);
  653. switch ($httptype)
  654. {
  655. case "GET":
  656. curl_setopt($ch, CURLOPT_HTTPGET, true);
  657. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  658. break;
  659. case "POST":
  660. curl_setopt($ch, CURLOPT_POST, true);
  661. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  662. break;
  663. case "PUT":
  664. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  665. break;
  666. case "DELETE":
  667. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  668. break;
  669. }
  670. $isSecure = strpos($url, "https://");
  671. if ($isSecure === 0)
  672. {
  673. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  674. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  675. }
  676. if(!empty($header))
  677. {
  678. curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
  679. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  680. }
  681. if(!empty($userpwd))
  682. {
  683. curl_setopt($ch,CURLOPT_USERPWD,$userpwd);
  684. }
  685. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  686. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  687. $result = curl_exec($ch);
  688. curl_close($ch);
  689. return $result;
  690. }
  691. } //end class