| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 | <?phpclass Model_Paypal extends Lin_Model {function __construct(){		parent::__construct();}	public function data($paypal,$shop)    {		$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');		$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
 |