CI PayPal NVP Library


Introduction

This library has been provided by Leon Barrett (@leonbarrett) to provide a simple and clean way to interact with the PayPal NVP API. It builds upon concepts from the Code Igniter PayPal Library (v. 0.1) by http://aroussi.com/ci/paypal_lib.

This library assumes that you have some prior knowledge of the PayPal API. More information can be found on the PayPal API reference pages.

It should be noted that this library isn't the magic answer to your PayPal solution, you will need to be comfortable with PHP, CI and the PayPal API!

This package consists of the following files

Please note the library requires CI 1.7.2 and higher

Features

This library makes it easy to call the PayPal API via the NVP method and get a response back as an array. It also allows you to receive IPN notifications so that they can be processed.

Constructing an API call is as simple as passing some variables into an array and calling a function passing in the API method.

Functions

The API has the following functions:

$this->paypal_api_lib->add_nvp(name, value);

$this->paypal_api_lib->send_api_call(api_function);

$this->paypal_lib->validate_ipn();

paypal_errors();

Setting up

In order to use the library you will need to generate your API credentials. These can be found within the Profile section of your PayPal account.

Once you have your API credentials, these should be added to the paypallib_config.php file. Once these are in place, you are good to go.

Calling the API

Calling the API is straightforward. Simply pass in the parameters that the API call requires and then call the API. It is best practice to use an IF statement. This way you can handle the fact that the API response wasn't successful (i.e the card failed validation due to an incorrect card number).

Example call to get the balance of the current account

if($this->paypal_api_lib->send_api_call('GetBalance')){
   echo($this->paypal_api_lib->nvp_data['L_AMT0']);
}

Getting a response from the API

As soon as any API call is made the return values are stored in an array (nvp_data[]) so that they can be used elsewhere. The return values are stored as name value pairs. Based on the example above the balance in printed out by calling the name from the array.

Receiving an IPN

You should have already set up your IPN URL within PayPal to point to a controller and method within your application. For example http://www.test.com/controller_name/controller_function_name. For example you could use http://www.test.com/paypal/ipn.

Within your IPN function, add the following check, to ensure that the IPN is valid:

if ($this->paypal_lib->validate_ipn()) { // Do something with the data print_r($this->paypal_lib->ipn_data); }

Errors

Sometimes the API will return errors. These can be for numerous reasons - there were missing parameters in the API call or the customers card failed validation. All errors can be outputted using the paypal_errors function.

This works in pretty much the same way as the CI Validation errors, and will print out all of the errors returned from PayPal

paypal_errors();

Logging

The library has functions to log activity with PayPal. When turned on, this will log every IPN (wether validated or not) and every API response.

Logging can be turned on from within the Config file and all logs are stored in the Application > logs directory.

Download

You can also download the latest stable build here:

https://github.com/leonbarrett/CodeIgniter-PayPal-NVP-API

More

Creative Commons License
CodeIgniter PayPal NVP API Library by Leon Barrett is licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License.
Based on a work at aroussi.com.