Codeigniter | Paypal Integration

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:

9 thoughts on “Codeigniter | Paypal Integration

  1. 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.

    1. 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

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