'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'); $ToState = array_flip($ToState); $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'; $token = $this->token($shop); $header[] = "Content-Type: application/json"; //$header[] = "Authorization: Basic ".base64_encode($name.":".$pass); $header[] = "Authorization: Bearer ".$token; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_HTTPGET, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $res = curl_exec($ch); curl_close($ch); $res = json_decode($res,true);//str_replace('|','|',$res['transaction_details'][0]['shipping_info']['address']['line2']) $f = ''; if(isset($res['transaction_details'][0]['shipping_info']['address']['country_code'])) { $address2 = (isset($res['transaction_details'][0]['shipping_info']['address']['line2']))?','.str_replace('|','|',$res['transaction_details'][0]['shipping_info']['address']['line2']):''; $line = trim(str_replace('|','|',$res['transaction_details'][0]['shipping_info']['address']['line1']),' ').trim($address2,' ').','; if(isset($res['transaction_details'][0]['shipping_info']['address']['state'])) { $state = $res['transaction_details'][0]['shipping_info']['address']['state']; if(isset($ToState[$state])) { $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']; } else { $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']; } } else { $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']; } $x = array('name'=>str_replace(array(',','|'),array('','|'),$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 $f = 'paypal'.'|'.json_encode($x); } return $f; } public function token($shop) { $url = 'https://api-m.paypal.com/v1/oauth2/token'; $header[] = 'Content-Type: application/json'; $header[] = 'Accept-Language: en_US'; $header[] = 'Accept: */*'; $data = array('grant_type' => 'client_credentials');//请求头内容 $method = 'POST'; $hf_path = $_SERVER["DOCUMENT_ROOT"] . '/data/paypal/'.$shop['brandname'].'.txt'; $hf_access_token = ''; if(file_exists($hf_path)) { $file_read = fopen($hf_path, 'r'); $access_str = fgets($file_read); fclose($file_read); $access_arr = explode(':', $access_str); if (time() < intval($access_arr[0])) { $hf_access_token = isset($access_arr[1]) ? $access_arr[1] : ''; } } if(!$hf_access_token) { $file = fopen($hf_path, 'w'); $client_id = $shop['paypalname']; $secret_id = $shop['paypalpass']; $userpwd = $client_id . ':' . $secret_id; $info = $this->_curl($data,$url,3000,$method,true,$header,$userpwd); $result = json_decode($info,true); $access_new = (time() + $result['expires_in']) . ':' . $result['access_token']; fwrite($file, $access_new); fclose($file); $hf_access_token = $result['access_token']; } return $hf_access_token; } public function _curl($data,$url,$timeout=300,$httptype="POST",$date_type=false,$header=array(),$userpwd='') { if ($date_type == 'http_build_query') { $data = http_build_query($data); } else if ($date_type == 'json') { $data = json_encode($data); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS,$data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); switch ($httptype) { case "GET": curl_setopt($ch, CURLOPT_HTTPGET, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); break; case "POST": curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); break; case "PUT": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); break; case "DELETE": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); break; } $isSecure = strpos($url, "https://"); if ($isSecure === 0) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); } if(!empty($header)) { curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION curl_setopt($ch,CURLOPT_HTTPHEADER,$header); } if(!empty($userpwd)) { curl_setopt($ch,CURLOPT_USERPWD,$userpwd); } curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $result = curl_exec($ch); curl_close($ch); return $result; } } //end class