Paypal Integration Using Codeigniter – Phase:1

Standard

Paypal is one papular e-payment gateway and there are different types of API or way to integrate Paypal in your site. Most of us implement the PayPal package Website Payment Standard on our website. It is the process where user payment handled on PayPal website instead of seller website. I implement a process where user buy different products and all selected product show in my wesite cart also in PayPal Cart (multiproducts show on PayPal) and pay on paypal website. Then user redirect to my website with some post data necessary for database entry (back office process for order tracking) in my site. On the phase I will discuss, how to make a codeigniter controller for send request to paypal website. This phase also include paypal form creation, send request to paypal, show data on paypal cart, and all prerequisite need to integrate the gateway.

First of all we need to make a form that send data to paypal website. There are different discussion about that how make a form, and all are the same discussions. Everybody omit some essential issues as below :

  1. Need to create PayPal Sandbox account by developer (It need for create environment of dummy transaction)
  2. Forget to configure some essential value for successful test transaction (Discuss in future)
  3. Need to discuss how to send multiproduct list to paypal (Show Paypal Cart on Paypal page)
  4. How codeigniter create PayPal Request Form

(i) First we need to create paypal test account known as sandbox account on paypal. Go to here and create a test/developer account. Then create demo Sellers and Buyer account for live experience of dummy transaction. You can get different tutorial how to do that on Google. After create all account make a note of your buyer and seller email and password by notepad that you just created on paypal. In future you will need to set the seller/marcent email on paypal request form and you will enter the buyer email when redirect to PayPal site for pay. When you finish, go to the next step.

(ii) Now we will create a form for send request to paypal website. For minimize our development task we implement a well known PayPal library download from here . Place all files to specific location. Load the PayPal library on your request controller that create the PayPal Form. I have write the following code on my controller to make a PayPal Form by a view page.

// Generate Paypal Request Form
// By the Function Create Payment Processing Form And Submit a Pending Order to our MySQL database
function create_paypal_form()
{
$header_data=array();
$this->load->model('orderprocessing_model');
$orderarr = array();
/*
* Get post data/order details from ckeckout request form to send
* PayPal and and to add database for order process start
*/
// Get all the products ID by comma(') saperated from Shopping Cart
$setpaypal_prodid = $this->input->post('getpaypal_prodid');
// Get all the products name by comma(') saperated from Shopping Cart
$setpaypal_prodname = $this->input->post('getpaypal_prodname');
// Get all the products quentities by comma(') saperated from Shopping Cart
$setpaypal_totlqunt = $this->input->post('getpaypal_totlqunt');
// Get he total price value from Shopping Cart
$setpaypal_totlprice = $this->input->post('getpaypal_totlprice');
// Get all the subtotal prices of products/items by comma(') saperated from Shopping Cart
$setpaypal_itemprice = $this->input->post('getpaypal_itemprice');
//Get splited products IDs from comma(') saperated products IDs
$paypal_id_array = explode('|',$setpaypal_prodid);
//Get splited products name from comma(') saperated products name
$paypal_prod_array = explode('|',$setpaypal_prodname);
//Get splited products quentities from comma(') saperated products quentities
$paypal_qunt_array = explode('|',$setpaypal_totlqunt);
//Get splited subtotal prices from comma(') saperatedsubtotal prices
$paypal_price_array = explode('|', $setpaypal_itemprice);
/*
* Initialize & Submit Order to DATABASE Before Pass to Paypal Payment Gateway
*/
$orderarr['set_order_date'] = time();
$orderarr['set_order_item_id'] = $setpaypal_prodid;
$orderarr['set_order_item_name'] = $setpaypal_prodname;
$orderarr['set_order_totl_price'] = $setpaypal_totlprice;
$orderarr['set_order_item_qunt'] = $setpaypal_totlqunt;
$orderarr['set_item_summery_id'] = $setpaypal_summid;
$orderarr['set_order_status'] = "Pending";
$orderarr['set_order_transaction_id'] = "";
$orderarr['set_order_transaction_status'] = "Not Start";
$orderarr['set_order_shipto_id'] = "";
$orderarr['set_order_member_id'] = $this->session->userdata('member_id');
$orderarr['set_order_member_email'] = $this->session->userdata('member_userid_email');
$orderarr['set_delete_flag'] = '0';
$this->orderprocessing_model->user_order_submit($orderarr);
/*
Configure Basic Setup for Payment Request Form
*/
$result_paypal_business_accemail = $this->orderprocessing_model->get_paypal_acc('Paypal');
// "seller_email" is a table fieldname, on that table we store all paypal marcent information
$set_paypal_business_accemail = $result_paypal_business_accemail[0]->seller_email;
// If we want to show user cart also on PayPal site then we use this
// In that case we send multiple products to PayPal site
$this->paypal_lib->add_field('cmd', '_cart');
// Use value 1 for this
$this->paypal_lib->add_field('upload', '1');
// This is marcent/seller business email, we use our sandbox business email that create early
$this->paypal_lib->add_field('business', $paypal_business_acc );
// This is success page URL, in our case(MVC) we set there a controller URI
//here paypal is a controller & success is a method
$this->paypal_lib->add_field('return', site_url('paypal/success'));
// This is cancle page URL, in our case(MVC) we set there a controller URI
//here paypal is a controller & cancle is a method
$this->paypal_lib->add_field('cancel_return', site_url('paypal/cancel'));
// This is paypal notification page URL, in our case(MVC) we set there a controller URI
//here paypal is a controller & ipn is a method
$this->paypal_lib->add_field('notify_url', site_url('paypal/ipn'));
// This is Currency Code, never send symbolic code like $, #
$this->paypal_lib->add_field('currency_code', 'USD');
// Configure Return method = POST by set value 2
$this->paypal_lib->add_field('rm','2');
// Set this if you want to send any custom value
$this->paypal_lib->add_field('custom', '1234567890');
/*
*Use this for SingleProduct/TotalPrice_IN_A_SingleRow Type Order Process by PayPal
*/
/*$this->paypal_lib->add_field('item_name', $paypal_prod_array[0]);
$this->paypal_lib->add_field('item_number', $paypal_id_array[0]);
$this->paypal_lib->add_field('amount', $setpaypal_totlprice);
$this->paypal_lib->add_field('add', '1');*/
/*
*Use for PaypalCartType/MultiProducts Order Process
*/
for($loop=0; $looppaypal_lib->add_field($item_name_set, $paypal_prod_array[$loop]) {
$this->paypal_lib->add_field($item_number_set, $paypal_id_array[$loop]);
$this->paypal_lib->add_field($item_amount_set, $paypal_price_array[$loop]);
$this->paypal_lib->add_field($item_quent_set, $paypal_qunt_array[$loop]);
$this->paypal_lib->add_field($item_ship_set, '1');
}
//Generate Paypal Submition Form With Hidden Field
//Based on Form Field Configuration on The Above
$header_data['paypal_form'] = $this->paypal_lib->paypal_form();
$header_data['paypal_prodid_array'] = $paypal_id_array;
$header_data['paypal_name_array'] = $paypal_prod_array;
$header_data['paypal_qunt_array'] = $paypal_qunt_array;
//header('Location: '.config_item('base_url').'index.php/index/admindashboard/homeflag');
/*
* Available CART POST DATA to Header and Session
*/
$country_list = $this->user_model->get_country_list();
$header_data['country_list'] = $country_list;
$header_data['setpaypal_prodid'] = $flash_sess['setpaypal_prodid'] = $setpaypal_prodid;
$header_data['setpaypal_prodname'] = $flash_sess['setpaypal_prodname'] = $setpaypal_prodname;
$header_data['setpaypal_totlprice'] = $flash_sess['setpaypal_totlprice'] = $setpaypal_totlprice;
$header_data['setpaypal_totlqunt'] = $flash_sess['setpaypal_totlqunt'] = $setpaypal_totlqunt;
$header_data['setpaypal_itemprice'] = $flash_sess['setpaypal_itemprice'] = $setpaypal_itemprice;
$header_data['setpaypal_summid'] = $flash_sess['setpaypal_summid'] = $setpaypal_summid;
$this->load->library('session');
$this->session->unset_userdata('setpaypal_prodid');
$this->session->unset_userdata('setpaypal_prodname');
$this->session->unset_userdata('setpaypal_totlprice');
$this->session->unset_userdata('setpaypal_totlqunt');
$this->session->unset_userdata('setpaypal_itemprice');
$this->session->unset_userdata('setpaypal_summid');
$this->session->set_userdata($flash_sess);
$output=$this->load->view('header_front',$header_data,true);
if($paypal_flag=="OFF") {
$output.=$this->load->view('shipping_billing_form',$header_data,true);
}
if($paypal_flag=="ON") {
$output.=$this->load->view('paypal_confirm_form',$header_data,true);
}
$output.=$this->load->view('footer_front',$header_data,true);
$this->output->set_output($output);
}

Do not follow the same on the above code. Your controller just like your requirements. My code just help you to guess what to do.