Codeigniter | Paypal Integration

08 Jun

By:Nithin K Varrier

Here is a simple tutorial for integrating Paypal with your Codeigniter project. For easiest integration of Paypal you need to download paypal lib.

Extract the downloaded zip file. Copy Paypal_Lib.php from paypal_lib->application->libraries and paste this file to your application->libraries. One more file you need to copy. That is paypallib_config.php from paypal_lib->application->config and paste this file to your application->config.

Then you create a control paypal.php. It should be like this:

class Paypal extends CI_Controller{

  function __construct(){
      $this->load->library( 'paypal_lib' );


   function index(){

Here you created a control named paypal. And you included paupal_lib library. Then in index method you called payment view.

Then create a payment.php in your view folder. It should be like this:

<?php echo anchor (base_url()."index.php/paypal/pay",
 'Paypal', array('id'=>'paypal')); ?>

Here you created a Hyperlink paypal and it call method ‘pay’ in your paypal control. So you need to create ‘pay’ method in your paypal control, like this:

function pay(){

  $item_name = 'Product Name';

  $price = '50';

  $item_id = 'Product Id';  // Optional

  $quantity = 1;

  $this->paypal_lib->add_field('business',’’); // Business Email

  $this->paypal_lib->add_field('return',base_url().'returnpage'); // Return URL

  $this->paypal_lib->add_field('cancel_return', base_url().'cancel'); // Cancel URL

  $this->paypal_lib->add_field('notify_url', base_url().'ipn'); // <-- IPN url

  $this->paypal_lib->add_field('custom', $item_id);  // Optional

  $this->paypal_lib->add_field('item_name', $item_name); // Product Name

  $this->paypal_lib->add_field('quantity', $quantity); // Quantity

  $this->paypal_lib->add_field('amount', $price); // Price



In the above method ‘pay’ you gave all information about your product like name, price, quantity, etc. Here you gave a return URL and it called the method ‘returnpage’. So you need to create ‘returnpage’ method in your paypal controle, like:

function returnpage(){


     echo "Success !!";


     echo "Error !!";



Then create a ‘cancel’ method for cancel URL, like:

function cancel(){



Finally You need to create a method ‘ipn’ for IPN-URL, like:

function ipn(){

   $to    = 'Your Email';

   if ($this->paypal_lib->validate_ipn()) {

      $body  = 'An instant payment notification was successfully received from ';

      $body .= $this->paypal_lib->ipn_data['payer_email'] . 
       ' on '.date('m/d/Y') . ' at ' . date('g:i A') . "\n\n";

      $body .= " Details:\n";

      foreach ($this->paypal_lib->ipn_data as $key=>$value)
            $body .= "\n$key: $value";

      // load email lib and email results




     $this->email->subject('Received Payment');





Then call paypal control and see How its work…………..Thank you

Referenced by:


Posted by on June 8, 2012 in CodeIgniter, PHP


9 responses to “Codeigniter | Paypal Integration

  1. Nithin K Varrier

    June 9, 2012 at 1:10 PM

    Thanks sochinda

  2. Anooj

    March 16, 2013 at 1:12 PM

    Hi sochinda,
    how can i implement recurring payment using this code.
    thanks in advance.

  3. Dixon Cherian

    October 21, 2013 at 6:33 PM

    I have done according to you.
    It shows an error.
    “Error detected
    Your shopping cart is empty”

  4. Firoz Bhuyian

    May 31, 2014 at 1:48 PM

    I need your kind help and highly waiting for. Could you send me paypal lib and all codeigniter file, please. Can i use this paypal system in PHP codeigniter ? Which could be a great help for me now. Looking forward.

    Thanking you in Advance.

    • shanu

      November 19, 2014 at 6:50 PM

      Thank you for your code……….
      I followed all the steps as above …….but i am getting error

      Fatal error: Cannot redeclare class Paypal in D:\Program files\xmapp\htdocs\defence\test\ci\application\libraries\Paypal_lib.php on line 73

  5. sunil

    July 7, 2014 at 12:45 PM

    Hello sir I need this lib file, please send me on my id.

  6. Max

    August 21, 2014 at 1:13 PM

    is anyone provide me paypal lib files please.
    Please send me on my email id

  7. rose

    August 22, 2014 at 1:27 PM

  8. Asha

    November 21, 2014 at 1:56 PM

    I can’t download the paypal library in Codeigniter | Paypal Integration


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: