how to integrate paypal with codeigniter? how to integrate paypal with codeigniter? codeigniter codeigniter

how to integrate paypal with codeigniter?


Here are the steps you can follow.

Step1 Create IPN Form. make sure to pass IPN URL (notify URL) to paypal.

For Form variables, you can refer https://developer.paypal.com/webapps/developer/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/

<form name="paypalFrm" id="paypalFrm" action="https://www.paypal.com/cgi-bin/webscr" method="post">    <input type="hidden" name="cmd" value="_ext-enter">    <input type="hidden" name="redirect_cmd" value="_xclick-subscriptions">    <input type="hidden" name="return" value="<?php echo $return_url;?>">    <input type="hidden" name="cancel_return" value="<?php echo $cancel_return;?>">    <input type="hidden" name="notify_url" value="<?php echo $notify_url;?>">    <input type="hidden" name="custom" value="<?php echo $custom.",".$custom2;?>">    <input type="hidden" name="business" value="<?php echo $business_id;?>">    <input type="hidden" name="item_name" value="<?php echo $item_name;?>">    <input type="hidden" name="item_number" value="1">    <input type="hidden" name="no_note" value="1">    <input type="hidden" name="currency_code" value="USD">    <input type="hidden" name="a3" value="<?php echo $plan_amount;?>">      <input type="hidden" name="p3" value="1">      <input type="hidden" name="t3" value="M">       <input type="hidden" name="src" value="1">    <input type="hidden" name="sra" value="1">    <input type="hidden" name="srt" value="12">    <input type="hidden" name="first_name" value="<?php echo $txtname;?>">    <input type="hidden" name="lc" value="<?php echo $merchant_country;?>">    <input type="hidden" name="email" value="<?php echo $txtemail;?>"></form>

Step 2 Create IPN controller. For detailed understanding review https://developer.paypal.com/docs/classic/ipn/gs_IPN/

$req = 'cmd=_notify-validate';foreach ($_POST as $key => $value) { $_POST[$key] = mysql_real_escape_string($value); }         foreach ($_POST as $key => $value) {    $value = urlencode(stripslashes($value));    $req .= "&$key=$value";}$header = ''; $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";$header .= "Content-Type: application/x-www-form-urlencoded\r\n";$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";$fp = fsockopen('www.sandbox.paypal.com', 80, $errno, $errstr, 30);// assign posted variables to local variables$content['payment_status']      = $this->input->post('payment_status');$content['payment_amount']      = $this->input->post('mc_gross');$content['payment_currency']    = $this->input->post('mc_currency');$content['txn_id']              = $this->input->post('txn_id');$content['receiver_email']      = $this->input->post('receiver_email');$content['payer_email']         = $this->input->post('payer_email');    $custom                         = explode(",",$this->input->post('custom'));$content['payment_id']          = $custom[0];$content['type']                = $custom[1];$content['txn_type']            = $this->input->post('txn_type');        $content['paydate']             = date('Y-m-d H:i:s');if (!$fp){    // HTTP ERROR}else{    fputs ($fp, $header . $req);    if (!feof($fp))    {        $res = fgets ($fp, 1024);        if(strcasecmp($content['txn_type'], "subscr_payment") == 0)        {            //Action                    }        else if(strcasecmp($content['payment_status'], "Completed") == 0)        {            //Action                    }        else if(strcasecmp($content['txn_type'], "subscr_cancel") == 0)        {           //Action                    }    }    fclose ($fp);}


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