Collection of PHP classes for interfacing with the PayPal NVP API
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

PayPal NVP Transactions for PHP 5.2+

Created and Copyright 2011 by Jarvis Badgley, chiper at chipersoft dot com.

After spending an entire day of searching and hair pulling trying to find an NVP class that actually worked I finally wrote this based upon PayPal's own PHP example code. The core of the library is the PayPalNVP class which all individual functions are subclassed from. Each NVP Method exists as an individual class with properties and tests specific to its own arguments.

The entire NVP command-set is not yet supported, as I had specific commands that I needed for my project. Further commands will be added as time permits or other contributors provide.

Current methods supported:

  • GetBalance
  • DoDirectPayment


Command objects are named in the format of PayPal_MethodName. The developer's PayPal API credentials are passed in the constructor. If the credentials are not provided, the class assumes sandbox mode and will use the sandbox account defined in the PayPalNVP class constants. Each command object has its own set of variables required for execution that must be set before calling the Send function.

$pp = new PayPal_DoDirectPayment(); //no credentials, run as sandbox
$pp->Amount         = 56;           //can be integer, float or string. Gets passed through number_format before being send to paypal
$pp->CardType       = 'Visa';       //currently supports visa, mastercard, amex and discover.  See PayPalCodes::CardTypeCheck()
$pp->CardNumber     = '4012888888881881'; //paypal visa test code
$pp->CardExpiration = '2018-11';
$pp->CardCVV2       = '555';
$pp->FirstName      = 'Jarvis';
$pp->LastName       = 'Badgley';
$pp->Address        = '12345 Example St';
$pp->City           = 'San Diego';
$pp->State          = 'CA'; //must be two character state code.  See PayPalCodes::$states
$pp->Zip            = '92128';

If the transaction was successful, Send() will return true, $pp->Success variable will be set to true, and the response from PayPal will be stored as an array in the $pp->Response variable.

$pp = new PayPal_GetBalance('API Username', 'API Password', 'API Signature');
if ($pp->Send()) echo "You have ${$pp->Balance} in your account.";

Adding Methods

The PayPalNVP class is an implementation of ArrayAccess. The data array it wraps contains the named pairs for the NVP request arguments. While the arguments can be accessed directly from outside the object via $this['ARGUMENT'], it is recommended that all arguments should have their own public variables and be processed internally by the overloaded Send function. All arguments are urlencoded when parent::Send() is called and do not need to be pre-encoded.

The PayPal Method name is defined via the $METHOD variable and should be defined in the declaration so that it is present when the constructor is called. If __construct() is overloaded you must pass the credentials to the parent constructor.

Take a look at the PayPal_DoDirectPayment class for an example of the process.

PayPal NVP Transactions is released under an MIT license.