How to integrate paypal using ci-merchant in codeigniter

It’s easy to integrate paypal using ci-merchant in codeigniter. you can integrate
Payflow Pro
PayPal Express Checkout
PayPal Website Payments Pro

Integrate paypal using ci-merchant in codeigniter

and more payment gateway and methods using ci merchant easily. Just a quick step you need to follow and done. There is also a step by step guide included on official site. I am just used ci-merchant library for implementing paypal express checkout in codeigniter site, and i really like this simple and easy library. So i am sharing what code i have done to integrate paypal using ci-merchant in codeigniter. below i am sharing step by step integration of ci-merchant library for express checkout purchase method.

Purchase (Combined Authorize & Capture)

We are using purchase method to complete paypal express checkout transactions. let’s see how to integrate paypal using ci-merchant in codeigniter.

Steps integrate paypal using ci-merchant in codeigniter

1. Download ci-merchant library files form ci-merchant.
2. Extract the downloaded zip and upload files on same directory sturcture like config, libraries etc.
3. Now go to “application/config.php” open it edit mode and add below code. Then add your “sandbox/paypal” credentials in given config.

//paypal configuration
$config['api_username'] = 'YOUR_API_USERNAME';
$config['api_password'] = 'YOUR_API_PASSWORD';
$config['api_signature'] = 'YOUR_API_SIGNATURE';
$config['test_mode'] = TRUE; //keep false on production, true for test
$config['currency'] = 'USD';

Note :- Make $config[‘test_mode’] = FALSE; on production

4. Now go to “application/controllers/payment.php” (Make a controller with this name) and add below code.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Payment extends CI_Controller {
 
    /**
     * Class constructor.
     * Adding libraries for each call.
     */
    public function __construct() {
        parent::__construct();
        $this->load->library('merchant');
        $this->merchant->load('paypal_express');
    }
 
    /**
     * On controller load call function.
     * Initializing paypal settings and make purchase call.
     */
    public function index() {
        // initialize gateway
        $settings = $this->merchant->default_settings();
        $this->initialize_settings();
        // sending values to payment gateway.
        $params = array(
              'amount' => 10,
              'item' => 'myitem',
              'description' => 'Your_item_description',
              'currency' => $this->config->item('currency'),
              'return_url' => base_url() . 'payment/payment_return',
              'cancel_url' => base_url() . 'payment/cancel'
        );
        $response = $this->merchant->purchase($params);
    }
     
    /**
     * Handling return call.
     * Make final payment.
     */
    public function payment_return() {
        $this->initialize_settings();
        $params = array(
              'amount' => 10,
              'item' => 'myitem',
              'description' => 'Your_item_description',
              'currency' => $this->config->item('currency'),
              'cancel_url' => base_url() . 'payment/cancel'
        );
        $response = $this->merchant->purchase_return($params);
        // A complete transaction.
        if ($response->status() == Merchant_response::COMPLETE) {
          // data which is return by payment gateway to use.
          $token = $this->input->get('token');
          $payer_id = $this->input->get('PayerID');
          // Unique id for payment must save it for further payment queries.
          $gateway_reference = ($response->reference() ? $response->reference() : '');
          print_r($response);
          // Do your stuff here db insertion, email etc..
        }
        else{
           //Your payment has been failed redirect with message.
          $message = ($response->message() ? $response->message() :'');
          $this->session->set_flashdata('error','Error processing payment: ' . $message.' Please try again');
          redirect('payment/cancel');
        }
    }
    
   /**
     * Handling on payment cancel.
     */
    public function cancel(){
      $this->load->view('your_view_name');
    }
 
    /**
     * Paypal settings initialization.
     */
    public function initialize_settings(){
      $settings = array(
            'username' => $this->config->item('api_username'),
            'password' => $this->config->item('api_password'),
            'signature' => $this->config->item('api_signature'),
            'test_mode' => $this->config->item('test_mode')
      );
      $this->merchant->initialize($settings);
    }
}

Add or send custom data to paypal using ci-merchant :-

If you want to send more custom data like want to add more items details, shipping etc. Then try to follow Github – fix to allow write detailed list of products pp . new commit and replace your file “libraries/merchant/merchant_paypal_base.php” with this.
Also you can send your custom variable with url like below i am sending $order_id as a custom variable then i am getting with return url.
Now you can send details like below:-

$params = array(
   'amount' => 10,
   'currency' =>  'USD',
   'return_url' => base_url() . 'payment/payment_return/' . $order_id,
   'notify_url' => base_url() . 'payment/notify/' . $order_id,
   'items' => array(
            array( 
              'name'=>'',
              'desc'=>'',
              'amt'=>,
              'qty'=>
           )
        )
 );

Now you can get $order_id in your payment_return() method like below

public function payment_return($order_id) {
  echo $order_id;
  // order id get now do your stuff.
}