Model_paypal.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. class Model_Paypal extends Lin_Model {
  3. function __construct(){
  4. parent::__construct();
  5. }
  6. public function data($paypal,$shop)
  7. {
  8. $ToState = array('alabama'=>'AL','alaska'=>'AK','arizona'=>'AZ','arkansas'=>'AR','california'=>'CA','colorado'=>'CO','connecticut'=>'CT','delaware'=>'DE','florida'=>'FL','georgia'=>'GA','hawaii'=>'HI','idaho'=>'ID','illinois'=>'IL','indiana'=>'IN','iowa'=>'IA','kansas'=>'KS','kentucky'=>'KY','louisiana'=>'LA','maine'=>'ME','maryland'=>'MD','massachusetts'=>'MA','michigan'=>'MI','minnesota'=>'MN','mississippi'=>'MS','missouri'=>'MO','montana'=>'MT','nebraska'=>'NE','nevada'=>'NV','new hampshire'=>'NH','new jersey'=>'NJ','new mexico'=>'NM','new york'=>'NY','north carolina'=>'NC','north dakota'=>'ND','ohio'=>'OH','oklahoma'=>'OK','oregon'=>'OR','pennsylvania'=>'PA','rhode island'=>'RI','south carolina'=>'SC','south dakota'=>'SD','tennessee'=>'TN','texas'=>'TX','utah'=>'UT','vermont'=>'VT','virginia'=>'VA','washington'=>'WA','west virginia'=>'WV','wisconsin'=>'WI','wyoming'=>'WY','district of columbia'=>'DC','virgin islands'=>'VI','guam'=>'GU');
  9. $ToState = array_flip($ToState);
  10. $url = 'https://api-m.paypal.com/v1/reporting/transactions?start_date='.date('Y-m-d',time()-26*24*3600).'T00:00:00-0700&end_date='.date('Y-m-d',time()+24*3600).'T23:59:59-0700&transaction_id='.$paypal.'&fields=all&page_size=100&page=1';
  11. $token = $this->token($shop);
  12. $header[] = "Content-Type: application/json";
  13. //$header[] = "Authorization: Basic ".base64_encode($name.":".$pass);
  14. $header[] = "Authorization: Bearer ".$token;
  15. $ch = curl_init();
  16. curl_setopt($ch, CURLOPT_URL, $url);
  17. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  18. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  19. curl_setopt($ch, CURLOPT_HTTPGET, true);
  20. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  21. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  22. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  23. $res = curl_exec($ch);
  24. curl_close($ch);
  25. $res = json_decode($res,true);//str_replace('|','&#124;',$res['transaction_details'][0]['shipping_info']['address']['line2'])
  26. $f = '';
  27. if(isset($res['transaction_details'][0]['shipping_info']['address']['country_code']))
  28. {
  29. $address2 = (isset($res['transaction_details'][0]['shipping_info']['address']['line2']))?','.str_replace('|','&#124;',$res['transaction_details'][0]['shipping_info']['address']['line2']):'';
  30. $line = trim(str_replace('|','&#124;',$res['transaction_details'][0]['shipping_info']['address']['line1']),' ').trim($address2,' ').',';
  31. if(isset($res['transaction_details'][0]['shipping_info']['address']['state']))
  32. {
  33. $state = $res['transaction_details'][0]['shipping_info']['address']['state'];
  34. if(isset($ToState[$state]))
  35. {
  36. $address = $line.$res['transaction_details'][0]['shipping_info']['address']['city'].','.ucfirst($ToState[$state]).','.$res['transaction_details'][0]['shipping_info']['address']['postal_code'].','.$res['transaction_details'][0]['shipping_info']['address']['country_code'];
  37. }
  38. else
  39. {
  40. $address = $line.$res['transaction_details'][0]['shipping_info']['address']['city'].','.$state.','.$res['transaction_details'][0]['shipping_info']['address']['postal_code'].','.$res['transaction_details'][0]['shipping_info']['address']['country_code'];
  41. }
  42. }
  43. else
  44. {
  45. $address = $line.$res['transaction_details'][0]['shipping_info']['address']['city'].','.$res['transaction_details'][0]['shipping_info']['address']['postal_code'].','.$res['transaction_details'][0]['shipping_info']['address']['country_code'];
  46. }
  47. $x = array('name'=>str_replace(array(',','|'),array('','&#124;'),$res['transaction_details'][0]['payer_info']['payer_name']['alternate_full_name']),'email'=>$res['transaction_details'][0]['payer_info']['email_address'],'address'=>$address.','.$res['transaction_details'][0]['payer_info']['phone_number']['national_number']);//$res['transaction_details'][0]['payer_info']['phone_number']['country_code'] 区号,去掉 'name'=>str_replace(',','',$res['transaction_details'][0]['shipping_info']['name']) shipping name
  48. $f = 'paypal'.'|'.json_encode($x);
  49. }
  50. return $f;
  51. }
  52. public function token($shop)
  53. {
  54. $url = 'https://api-m.paypal.com/v1/oauth2/token';
  55. $header[] = 'Content-Type: application/json';
  56. $header[] = 'Accept-Language: en_US';
  57. $header[] = 'Accept: */*';
  58. $data = array('grant_type' => 'client_credentials');//请求头内容
  59. $method = 'POST';
  60. $hf_path = $_SERVER["DOCUMENT_ROOT"] . '/data/paypal/'.$shop['brandname'].'.txt';
  61. $hf_access_token = '';
  62. if(file_exists($hf_path))
  63. {
  64. $file_read = fopen($hf_path, 'r');
  65. $access_str = fgets($file_read);
  66. fclose($file_read);
  67. $access_arr = explode(':', $access_str);
  68. if (time() < intval($access_arr[0]))
  69. {
  70. $hf_access_token = isset($access_arr[1]) ? $access_arr[1] : '';
  71. }
  72. }
  73. if(!$hf_access_token)
  74. {
  75. $file = fopen($hf_path, 'w');
  76. $client_id = $shop['paypalname'];
  77. $secret_id = $shop['paypalpass'];
  78. $userpwd = $client_id . ':' . $secret_id;
  79. $info = $this->_curl($data,$url,3000,$method,true,$header,$userpwd);
  80. $result = json_decode($info,true);
  81. $access_new = (time() + $result['expires_in']) . ':' . $result['access_token'];
  82. fwrite($file, $access_new);
  83. fclose($file);
  84. $hf_access_token = $result['access_token'];
  85. }
  86. return $hf_access_token;
  87. }
  88. public function _curl($data,$url,$timeout=300,$httptype="POST",$date_type=false,$header=array(),$userpwd='')
  89. {
  90. if ($date_type == 'http_build_query')
  91. {
  92. $data = http_build_query($data);
  93. }
  94. else if ($date_type == 'json')
  95. {
  96. $data = json_encode($data);
  97. }
  98. $ch = curl_init();
  99. curl_setopt($ch, CURLOPT_URL, $url);
  100. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  101. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  102. curl_setopt($ch, CURLOPT_HEADER, false);
  103. switch ($httptype)
  104. {
  105. case "GET":
  106. curl_setopt($ch, CURLOPT_HTTPGET, true);
  107. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  108. break;
  109. case "POST":
  110. curl_setopt($ch, CURLOPT_POST, true);
  111. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  112. break;
  113. case "PUT":
  114. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  115. break;
  116. case "DELETE":
  117. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  118. break;
  119. }
  120. $isSecure = strpos($url, "https://");
  121. if ($isSecure === 0)
  122. {
  123. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  124. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  125. }
  126. if(!empty($header))
  127. {
  128. curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
  129. curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
  130. }
  131. if(!empty($userpwd))
  132. {
  133. curl_setopt($ch,CURLOPT_USERPWD,$userpwd);
  134. }
  135. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  136. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  137. $result = curl_exec($ch);
  138. curl_close($ch);
  139. return $result;
  140. }
  141. } //end class