|
I'm not a huge fan of the Paypal SDKs. I'm more of a granular coder. I like having the moving parts in my control. To that end, I've written a simple method to integrate Paypal NVP into your PHP code. I make no warranties about this code, only that I've used it and used it well. Use at your own risk (it's light on error checking). It's open source so feel free to redistribute it. First, the class <?php class paypal { var $postdata=array(); var $response=array(); var $username; var $password; var $signature=NULL; var $certfile=NULL; var $proxy=NULL; var $sandbox; var $sandbox_url="https://api.sandbox.paypal.com/nvp"; var $live_url="https://api.paypal.com/nvp"; //initialize function __construct($user, $pw, $cert, $sandbox=false, $proxy=NULL) { $this->username=$user; $this->password=$pw; if(is_file($cert)) $this->certfile=$cert; else $this->signature=$cert; if($proxy) $this->proxy=$proxy; $this->sandbox=$sandbox; } //add values to the array function addvalue($key, $val, $limit=NULL) { $v=$val; if(is_numeric($limit)) $v=substr($v,0,$limit); $this->postdata[$key]=urlencode($v); } //clear the array for a new call function resetdata() { $this->postdata=array(); } function call_paypal($showurl=false) { $this->postdata['USER']=urlencode($this->username); $this->postdata['PWD']=urlencode($this->password); if($this->signature) $this->postdata['SIGNATURE']=urlencode($this->signature); if(!isset($this->postdata['VERSION'])) $this->postdata['VERSION']=urlencode(50.0);
$url=($this->sandbox) ? $this->sandbox_url : $this->live_url; $nvp=NULL; foreach($this->postdata as $k => $v): $nvp.="$k=$v&"; endforeach; if(!$nvp) return false; //strip out the last character, which is a & $nvp=substr($nvp, 0, -1); if($showurl) echo $nvp; //curl request $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if($this->certfile) curl_setopt($ch, CURLOPT_SSLCERT, $this->certfile); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); if($this->proxy) { curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); curl_setopt ($ch, CURLOPT_PROXY,$this->proxy); } curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $mydata = curl_exec ($ch); if(curl_error($ch)) { //$this->save_error(curl_error($ch)); return false; } curl_close ($ch); return $this->process_response($mydata); } function process_response($str) { $data=array(); $x=explode("&", $str); foreach($x as $val): $y=explode("=", $val); $data[$y[0]]=urldecode($y[1]); endforeach; return $data; } function save_error($msg) { //do something here, like save it to a database } } ?> The initialization looks like this $p=new paypal(string username, string password, string auth, [bool sandbox, [string proxy]]); username - your API username password - your API password auth - accepts either a path to your API certificate or an API signature sandbox - if set to true, uses a sandbox endpoint for all calls. Defaults to false (Live endpoint) proxy - if provided, will use the specified proxy server for the cURL request. Be certain to specify the complete proxy address (including port) Now we have out class instantiated and ready to accept data. Let's set up a call addvalue(string name, string value[, int length]);
EXAMPLE: $p->addvalue('METHOD', 'GetTransactionDetails'); $p->addvalue('VERSION', '50.0'); $p->addvalue('PROFILEID', $trans_id); addvalue will format your value and place it into the array to send to Paypal name - the variable you want to pass to Paypal value - the value of your variable length - if supplied, will cut your string down to the given length So now we've populated our class with data to call Paypal. So let's do it
array call_paypal([bool showurl])
$data=$p->call_paypal(); showurl - if set to true, will echo out your NVP string exactly as it is sent to Paypal call_paypal will either return an array of values or a boolean false if the call failed. The only other function of note is void resetdata(void)
$p->resetdata(); This resets the data in the array so you can use your existing class to make a new Paypal call (i.e. a DoDirectPayment and then a GetTransactionDetails). Happy coding! Message Edited by Boanerges on 06-23-2008 11:03 PM
|