Jump to Page:   1 · 2  |  Next Page
Simple PHP NVP integration   [ Edited ]
Options    Options  
Boanerges
Ace Developer
Posts: 939
Registered: 02-05-2008


Boanerges

Message 1 of 16

Viewed 17,357 times


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

 

7
Kudos!
06-23-2008 09:03 PM  
Re: Simple PHP NVP integration
Options    Options  
mattalexx
Regular Contributor
Posts: 34
Registered: 05-08-2007


mattalexx

Message 2 of 16

Viewed 14,275 times


Oh my God, this is so EASY! I've been banging my head on PayPal's exceedingly complex SOAP interface for a while now (I've written two applications of it). I can't believe this. A hundred lines of code!

 

Good going, PayPal. Good going, Boanerges.

Kudos!
12-04-2008 09:55 PM  
Re: Simple PHP NVP integration
Options    Options  
mat0cal
Newbie
Posts: 1
Registered: 07-15-2009


mat0cal

Message 3 of 16

Viewed 7,660 times


 

Hi

Thanks so much for that code but I am still receiving an error [L_LONGMESSAGE0] => Security header is not valid. Do you have any idea why that may be?

 

Many many thanks

Kudos!
07-15-2009 10:04 AM  
Re: Simple PHP NVP integration   [ Edited ]
Options    Options  
PP_MTS_Haack Moderator
Moderator
Posts: 3672
Registered: 10-25-2006


PP_MTS_Haack

Message 4 of 16

Viewed 7,652 times


Hi mat0cal,

 

The 10002 - Security header is not valid response can be caused by one or more of the following:

 

1. The credentials you are using are incorrect (i.e. bad password, username, signature/certificate)

2. You are pointing to the wrong endpoint for the API credentials generated.  If using Sandbox credentials you need to use a sandbox endpoint.  Likewise, If using Live Creds, you need to use a live endpoint. API endpoints:

 

Live Cert:
   api.paypal.com/2.0/
   api-aa.paypal.com/2.0/

Live Signature:
   api-3t.paypal.com/
   api-aa-3t.paypal.com/

Live NVP:
   https://api-3t.paypal.com/nvp
   https://api-aa-3t.paypal.com/nvp
 

Sandbox Cert:
   api.sandbox.paypal.com/2.0/
   api-aa.sandbox.paypal.com/2.0/

Sandbox Signature:
   api-3t.sandbox.paypal.com/2.0/
   api-aa-3t.sandbox.paypal.com/2.0/

Sandbox NVP:
     https://api.sandbox.paypal.com/nvp

 

3. There is a variable called Subject that is sometimes used in third-party API integrations (SO if not present ignore this step).  If this variable is present and set to anything other than The primary confirmed email on the account receiving the payment it could potential cause this issue or if the account it is set to does not have Pro activated on the account.

Message Edited by PP_MTS_Haack on 07-15-2009 01:29 PM
Kudos!
07-15-2009 11:29 AM  
Re: Simple PHP NVP integration
Options    Options  
gecko31
Newbie
Posts: 1
Registered: 07-30-2009


gecko31

Message 5 of 16

Viewed 7,059 times


it possible to use this code to generate a paypal sale url

 

I have tried this:

 

$p->addvalue('business', 'MY_EMAIL');
    $p->addvalue('return', 'http://www.domain.com/paypal/return/');
    $p->addvalue('cancel_return', 'http://www.domain.com/paypal/cancel/');
    $p->addvalue('notify_url', 'http://www.domain.com/paypal/ipn/');
    $p->addvalue('item_name', 'Signup');
    $p->addvalue('amount', '25.00');
    $p->addvalue('lc', 'NL');
    $p->addvalue('item_number', '100');

 

but it gives me this error:

 

$ => Array (5)
(
|    ['ACK'] = String(7) "Failure"
|    ['L_ERRORCODE0'] = String(5) "81002"
|    ['L_SHORTMESSAGE0'] = String(18) "Unspecified Method"
|    ['L_LONGMESSAGE0'] = String(33) "Method Specified is not Supported"
|    ['L_SEVERITYCODE0'] = String(5) "Error"
)

 

 

 

Kudos!
07-30-2009 05:55 AM  
Re: Simple PHP NVP integration
Options    Options  
Boanerges
Ace Developer
Posts: 939
Registered: 02-05-2008


Boanerges

Message 6 of 16

Viewed 7,057 times


Are you trying to do a SetExpressCheckout call? It looks like you're trying to pass Payments Basic fields to SEC. You have to include METHOD in any calls you make.

$pay->addvalue('METHOD', 'SetExpressCheckout'); $pay->addvalue('VERSION', '57.0'); $pay->addvalue('CURRENCYCODE', 'USD'); $pay->addvalue('MAXAMT', "10,000.00"); $pay->addvalue('RETURNURL', 'http://www.domain.com/paypal/return/'); $pay->addvalue('CANCELURL', "http://www.domain.com/paypal/cancel/"); $pay->addvalue('AMT', '25.00');

 


 

 

A SEC call looks more like this

 

 


 

Kudos!
07-30-2009 06:03 AM  
Re: Simple PHP NVP integration
Options    Options  
Citizen
Regular Visitor
Posts: 10
Registered: 02-18-2008


Citizen

Message 7 of 16

Viewed 6,678 times


Hello,

 

FYI, I ran into one problem:

 

Protocol https not supported or disabled in libcurl

Which I solved by installing curlssl. I had both curl and openssl, but not curlssl and got that error in case anyone else is having it.

 

But I did have one error that I could not solve yet:

 

error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure

Not sure what to do here. Googling it is giving me a lot of results but none that fix it.

 

Kudos!
08-05-2009 08:39 AM  
Re: Simple PHP NVP integration
Options    Options  
Citizen
Regular Visitor
Posts: 10
Registered: 02-18-2008


Citizen

Message 8 of 16

Viewed 6,675 times


Just kidding....

 

handshake error solved by switching (in your paypal class code):

 

from:

 

var $live_url='https://api.paypal.com/nvp';

 

to:

 

$live_url='https://api-aa-3t.paypal.com/nvp';

Kudos!
08-05-2009 08:46 AM  
Re: Simple PHP NVP integration
Options    Options  
camrat
Visitor
Posts: 2
Registered: 12-09-2008


camrat

Message 9 of 16

Viewed 6,395 times


This is pretty sweet.  I got in plugged in and working with a cart solution I am working on, Im new to the whole class Object stuff in php.  Has there been any work done for capturing errors. I tried, but I am retarted I guess.  I would like to show the errors to the users so they could fix the problems and then re-submit.  I will keep trying put a push in the right direction would be helpful.

 

Thanks

Kudos!
08-09-2009 01:20 AM  
Re: Simple PHP NVP integration
Options    Options  
camrat
Visitor
Posts: 2
Registered: 12-09-2008


camrat

Message 10 of 16

Viewed 6,327 times


Ok, so what I want to do and am still trying to do is figure out if the transaction was a success or not.  If it is I would like to re-direct to another page within my cart showing the details for the purchase.  If not then show what happened.  Some help is greatly appreciated.
Kudos!
08-09-2009 09:00 PM  
Jump to Page:   1 · 2  |  Next Page
Copyright © PayPal. All Rights Reserved. By using this site, you agree to be bound by PayPal's Legal Disclaimer, User Agreement, and Privacy Policy. This site is subject to change without notice. All other products and company names are trademarks of their respective corporations.