how to integrate paypal with codeigniter?

Viewed 29030

I'm trying to integrate PayPal payment with my codeigniter project. My project is about a dating site, in which two different users interact with each other. So I'm selling plans, on the based of which users will be provided access to send messages to to other for the that period of time. For that i have developed a very simple page containing radio buttons to choose from different plans. These are the following plans

  • one month ($2)
  • Two month ($4)
  • Three month ($6)
  • Four month ($8)
  • Five month ($10)

and so on

  • Six month ($12)

So, user can choose anyone plan from the above and can then proceed further to pay the amount on PayPal. This page will be available to user who have accounts on our site. so we can transfer all the details of current user to PayPal, such as -- name,email,etc..

I have tried reading many articles on how to integrate paypal but none of them helped me. As I'm new to codeigniter.

I have also created Paypal and sandbox account on paypal. I downloaded php-toolkit from sourceforge.net and used it independently, then it runs properly. but when i try to implement in codeigniter, it does not take anywhere.

Please help me !!

EDIT :

<?php 

    //Configuration File
    include_once APPPATH.'../php_paypal/includes/config.inc.php'; 

    //Global Configuration File
    include_once APPPATH.'../php_paypal/includes/global_config.inc.php';

?> 
<?php echo form_open('https://www.sandbox.paypal.com/cgi-bin/webscr');?>   

 <input type="hidden" name="amount" value="9.95">
 <input type="hidden" name="item_name" value="Test Payment">
</form>

This is what i'm simply trying to create. Earlier i tried to use "process.php" in the form action, but i was getting URL error in codeigniter. So i thought why not use this way. I know there are function responsible which are sending values to paypal. But through this action method. I'm being redirected to simple paypal page.

3 Answers
$orderide = $this->checkout_model->user_order($order_table);
$business='paypal@business.example.com';            
$cancel_url="http://".$_SERVER['SERVER_NAME']."/checkout/cancel?orderide=".$orderide;
$success_url="http://".$_SERVER['SERVER_NAME']."/checkout/success?order_id=".$orderide;
$data['payment_url']="https://www.sandbox.paypal.com/cgi-bin/webscr?business=$business&cmd=_xclick&item_name=$title&item_number=$product_id&amount=$totalamount&currency_code=EUR&return=$success_url&cancel_return=$cancel_url";              

redirect($data['payment_url']);

In return success URL you can able to get the user id and payment amount

Step 1: Download the CodeIgniter PayPal library from here: https://github.com/nrshoukhin/codeigniter-paypal-library.

Step 2: Drag and drop the entire "application" folder from the library to your codeigniter project. Make sure it's not conflict with your existing files.

Step 3: Load the library from your controller construtor:-

public function __construct(){
    parent::__construct();
    $this->load->library("paypal");
}

Step 4: Open the file from "application/config/paypal.php". And provide your client ID, client secret and currency.

Step 5: Create a controller method like below:-

public function subscribe(){

    if ( !empty($_POST["plan_name"]) && !empty($_POST["plan_description"]) ) {

        $this->paypal->set_api_context();

        $this->paypal->set_plan( $_POST["plan_name"], $_POST["plan_description"], "INFINITE" );

        $definition = "Regular Payments";
        $type       = "REGULAR";
        $frequency  = "MONTH";
        $frequncy_interval = '1';
        $cycles = 0;
        $price = "49";

        $this->paypal->set_billing_plan_definition( $definition, $type, $frequency, $frequncy_interval, $cycles, $price );

        $returnurl = base_url()."payment/success";
        $cancelurl = base_url()."payment/cancel";

        $this->paypal->set_merchant_preferences( $returnurl, $cancelurl );

        $line1 = "Street - 1, Sector - 1";
        $city  = "Dhaka";
        $state = "Dhaka";
        $postalcode = "12345";
        $country = "AU";

        $this->paypal->set_shipping_address( $line1, $city, $state, $postalcode, $country );

        $agreement_name = "Payment Agreement Name";
        $agreement_description = "Payment Agreement Description";

        $this->paypal->create_and_activate_billing_plan( $agreement_name, $agreement_description );

    }

}

Here, set the frequency as "MONTH" and set the frequency_interval according to your desired recurring period.

Related