Track.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Track extends Start_Controller {
  3. private $sid;
  4. private $baseurl="https://api.sandbox.paypal.com";
  5. public function __construct(){
  6. parent::__construct();
  7. $this->load->library('session');
  8. $this->load->_model('Model_fullorder','fullorder');
  9. $this->load->_model('Model_fullordertt','fullordertt');
  10. $this->load->_model('Model_fullorder_smt','fullordersmt');
  11. $this->load->_model('Model_express','express');
  12. $this->load->_model('Model_shop','shop');
  13. $this->load->library('parser');
  14. // $this->load->library('dispute_collect');
  15. if(isset($_SESSION['api']))
  16. {
  17. $user = $this->user->get_api($_SESSION['api']);
  18. $fgshop = [];
  19. $user = explode('|',trim($user['shop'],'|'));
  20. foreach ($user as $value)
  21. {
  22. if($value<=6){
  23. $fgshop[]=$value;
  24. }
  25. }
  26. }
  27. $sid=$this->input->get('sid');
  28. if(!$sid||!in_array($sid,$fgshop)){
  29. $sid=$fgshop[0];
  30. }
  31. $this->sid=$sid;
  32. }
  33. //定义方法的调用规则 获取URI第二段值
  34. public function _remap($arg,$arg_array)
  35. {
  36. if($arg == 'detail')//添加
  37. {
  38. $this->_detail($arg_array);
  39. }else
  40. {
  41. $this->_index($arg_array);
  42. }
  43. }
  44. public function _index(){
  45. $start_time=strtotime(date('Y-m-d',time()-24*3600*2));
  46. $end_time=strtotime(date('Y-m-d',time()));
  47. $dlz = $this->fullorder->paypal($start_time,$end_time);
  48. $smt = $this->fullordersmt->paypal($start_time,$end_time);
  49. $data = array_merge($dlz,$smt);
  50. // $this->setTrack("tt",$data[112]);
  51. $this->transaction("tt",$data[112]["paypal"]);
  52. p($data);
  53. $shop=$this->shop->read($data[38]['shop']);
  54. // $this->creatOrder();
  55. // $shop = $this->shop->read(店铺ID)
  56. // $this->load->library('dispute_collect',['dp'=>$shop['shopname']]);
  57. $this->load->library('dispute_collect',['dp'=>'test']);
  58. $list=$this->dispute_collect->getDisputes();
  59. $this->data['list']=$list;
  60. $this->_Template('dispute_list',$this->data);
  61. }
  62. private function setTrack($dp,$order){
  63. $express=$this->express->read($order['express']);
  64. $iscode=$express['iscode'];
  65. $transaction_id=$order['paypal'];
  66. $tracking_number=$order['waybill'];
  67. if(!$iscode||!$transaction_id){
  68. return false;
  69. }
  70. $url = $this->baseurl.'/v1/shipping/trackers-batch';
  71. $header[] = 'Content-Type: application/json';
  72. $header[] = 'Authorization: Bearer '.$this->getToken($dp);
  73. $trackers=
  74. [
  75. [
  76. 'transaction_id'=>"1VV994313G159761B",
  77. 'tracking_number'=>"399475881888",
  78. 'status'=>'SHIPPED',
  79. 'carrier'=>$iscode,
  80. ]
  81. ];
  82. $param=['trackers'=>$trackers];
  83. $info = $this->_curl($param,$url,3000,'POST',"json",$header);
  84. $info = json_decode($info,true);
  85. p($info);
  86. }
  87. public function creatOrder(){
  88. $url = $this->baseurl.'/v2/checkout/orders';
  89. $header[] = 'Content-Type: application/json';
  90. $dp="aaa";
  91. $header[] = 'Authorization: Bearer '.$this->getToken($dp);
  92. $param=[
  93. 'intent'=>"CAPTURE",
  94. "purchase_units"=>[
  95. [
  96. "amount"=>[
  97. "currency_code"=>"USD",
  98. "value"=>"100.00",
  99. ]
  100. ]
  101. ],
  102. ];
  103. $info = $this->_curl($param,$url,3000,'POST',"json",$header);
  104. $info = json_decode($info,true);
  105. p($info);
  106. }
  107. private function transaction($dp,$transaction_id){
  108. $url = $this->baseurl.'/v1/reporting/transactions?';
  109. $header[] = 'Content-Type: application/json';
  110. $header[] = 'Authorization: Bearer '.$this->getToken($dp);
  111. $start_date=date("Y-m-d\TH:i:sO",time()-24*3600*30);
  112. $end_date=date("Y-m-d\TH:i:sO",time());
  113. $param=[
  114. 'transaction_id'=>$transaction_id,
  115. 'start_date'=>$start_date,
  116. 'end_date'=>$end_date,
  117. ];
  118. $data = http_build_query($param);
  119. $url=$url.$data;
  120. $info = $this->_curl([],$url,3000,'GET',"http_build_query",$header);
  121. $info = json_decode($info,true);
  122. return $info;
  123. }
  124. public function _detail(){
  125. $shop=$this->shop->find('`id` ='.$this->sid);
  126. $id=$this->input->get('id');
  127. $this->load->library('ci_dispute',['dp'=>$shop['shopname']]);
  128. $info=$this->ci_dispute->info($id);
  129. $this->data['info']=$info;
  130. $this->_Template('dispute_info',$this->data);
  131. }
  132. private function getToken($dp)
  133. {
  134. $access_arr=[];
  135. $url = $this->baseurl.'/v1/oauth2/token';
  136. $header[] = 'Content-Type: application/json';
  137. $header[] = 'Accept-Language: en_US';
  138. $header[] = 'Accept: */*';
  139. $data = array('grant_type' => 'client_credentials');
  140. $method = 'POST';
  141. $hf_path = $_SERVER["DOCUMENT_ROOT"] . '/data/hf_access_token.txt';
  142. $hf_access_token = '';
  143. if(file_exists($hf_path))
  144. {
  145. $file_read = fopen($hf_path, 'r');
  146. $access_str = fgets($file_read);
  147. fclose($file_read);
  148. $access_arr = unserialize($access_str);
  149. if(isset($access_arr[$dp])&&is_array($access_arr[$dp])){
  150. $dp_access_arr=$access_arr[$dp];
  151. if (time() < intval($dp_access_arr['expires_at']))
  152. {
  153. $hf_access_token = isset($dp_access_arr['access_token']) ? $dp_access_arr['access_token'] : '';
  154. }
  155. }
  156. }
  157. if(!$hf_access_token)
  158. {
  159. // $config=$this->getDpConfig();
  160. $config=[
  161. 'account'=>'paypal-facilitator@supernovahair.com',
  162. 'client_id'=>'Ae5ZECTwT-JY-GrHW2-XW234yJ4tYT-7RAt3s1mY8GtW1rX470Kr8weXkCH3GMaO-V7mnmnCTsxFvsiy',
  163. 'secret'=>'EOiZjIwRaiK3pvbJgMURKcGy6ULt5YCGLkqN7WngwG-r34brYrfVwS6ECI8cig7l8lOObvF-ukeZxB-3',
  164. ];
  165. $file = fopen($hf_path, 'w');
  166. $hf_account = $config['account'];
  167. $client_id = $config['client_id'];
  168. $secret_id = $config['secret'];
  169. $userpwd = $client_id . ':' . $secret_id;
  170. $info = $this->_curl($data,$url,3000,$method,true,$header,$userpwd);
  171. $result = json_decode($info,true);
  172. // $access_new = (time() + $result['expires_in']) . ':' . $result['access_token'];
  173. $access_arr[$dp]=[
  174. 'expires_at'=>time() + $result['expires_in'],
  175. 'access_token'=>$result['access_token'],
  176. ];
  177. fwrite($file, serialize($access_arr));
  178. fclose($file);
  179. $hf_access_token = $result['access_token'];
  180. }
  181. return $hf_access_token;
  182. }
  183. private function _curl($data,$url,$timeout=300,$httptype="POST",$date_type=false,$header=array(),$userpwd='')
  184. {
  185. if ($date_type == 'http_build_query')
  186. {
  187. $data = http_build_query($data);
  188. }
  189. else if ($date_type == 'json')
  190. {
  191. $data = json_encode($data);
  192. }
  193. $ch = curl_init();
  194. curl_setopt($ch, CURLOPT_URL, $url);
  195. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  196. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  197. curl_setopt($ch, CURLOPT_HEADER, false);
  198. switch ($httptype)
  199. {
  200. case "GET":
  201. curl_setopt($ch, CURLOPT_HTTPGET, true);
  202. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  203. break;
  204. case "POST":
  205. curl_setopt($ch, CURLOPT_POST, true);
  206. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  207. break;
  208. case "PUT":
  209. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  210. break;
  211. case "DELETE":
  212. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  213. break;
  214. }
  215. $isSecure = strpos($url, "https://");
  216. if ($isSecure === 0)
  217. {
  218. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  219. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  220. }
  221. if(!empty($header))
  222. {
  223. curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
  224. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  225. }
  226. if(!empty($userpwd))
  227. {
  228. curl_setopt($ch,CURLOPT_USERPWD,$userpwd);
  229. }
  230. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  231. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  232. $result = curl_exec($ch);
  233. curl_close($ch);
  234. return $result;
  235. }
  236. }