Recurring PayPal Payment in CodeIgniter with subscribe button
- create function in controller to call view file and pass below details in subscription form
<?php
$data['loggedInUserID'] = $this->session->userId;
$data['paypalURL'] = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
$data['paypalID'] = 'jayesh.borse@gmail.com'; //your paypal business email id $data['successURL'] = base_url($data['clang'].'/paypal/success');
$data['cancelURL'] = base_url($data['clang'].'/paypal/cancel');
$data['notifyURL'] = base_url($data['clang'].'/paypal/ipn_paypal');
$data['selected_plan'] = $selected_plan;
$this->load->view('includes/header',$data);
$this->load->view('validation_msg');
$this->load->view('myaccount_sidebar',$data);
$this->load->view('acc_type',$data);
$this->load->view('includes/footer',$data);
?>
- below is the view form for subscription form onclick of button will redirect to payment module.
<form action="<?php echo $paypalURL; ?>" method="post">
<!-- identify your business so that you can collect the payments -->
<input type="hidden" name="business" value="<?php echo $paypalID; ?>">
<!-- specify a subscriptions button. -->
<input type="hidden" name="cmd" value="_xclick-subscriptions">
<!-- specify details about the subscription that buyers will purchase -->
<input type="hidden" name="item_name" value="<?php echo $itemName; ?>">
<input type="hidden" name="item_number" value="<?php echo $itemNumber; ?>">
<input type="hidden" name="currency_code" value="EUR">
<input type="hidden" name="src" value="1">
<input type="hidden" name="a3" id="paypalAmt" value="<?php echo $price; ?>">
<input type="hidden" name="p3" id="paypalValid" value="1">
<input type="hidden" name="t3" value="M">
<!-- custom variable user ID -->
<input type="hidden" name="custom" value="<?php echo $loggedInUserID; ?>">
<!-- specify urls -->
<input type="hidden" name="cancel_return" value="<?php echo $cancelURL; ?>">
<input type="hidden" name="return" value="<?php echo $successURL; ?>">
<input type="hidden" name="notify_url" value="<?php echo $notifyURL; ?>">
<!-- display the payment button -->
<input class="paypal_button btn btn-success" type="submit" value="select plan">
</form>
- after click on button action for new controller in paypal.php
<?php
/* success redirect url */
function success(){
error_reporting(0);
ini_set('display_errors', 0);
//get the transaction data
$userId = $this->session->userId;
$paypalInfo = $this->input->get();
$txn_id = $paypalInfo['tx'];
$amount = $paypalInfo['amt'];
$currency_code = $paypalInfo['cc'];
$custom = $paypalInfo['cm'];
$item_number = $paypalInfo['item_number'];
$payment_status = $paypalInfo['st'];
$payment_rawData = json_encode($paypalInfo);
$clang = $this->uri->segment(1);
if(!isset($clang)){
$clang = "en";
}
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$date = strtotime($currentDate . $currentTime);
$plan = $this->user_model->getPlanInfo($item_number);
$plan_duration = $plan[0]->st_plan_duration;
$plan_name = $plan[0]->st_plan_name;
$max_video = $plan[0]->st_max_videos;
$max_img = $plan[0]->st_max_pics;
//Insert tansaction data into the database
$payment_info = array('payment_userId'=>$userId,'payment_planId'=>$item_number,'payment_txnId'=>$txn_id,'payment_amount'=>$amount,'payment_currency'=>$currency_code,'payment_planType'=>'0','payment_paidBy'=>'0','payment_status'=> $payment_status,'payment_rawData'=>$payment_rawData,'createdDate'=>date('Y-m-d H:i:s'),'updatedDate'=>date('Y-m-d H:i:s'));
$insert = $this->user_model->insertTransaction($payment_info);
redirect('user/payment_history');
}
/* cancel redirect url */
function cancel(){
$this->lang->load('label');
$data['clang']= $this->session->set_lang;
$this->load->view('paypal/cancel',$data);
}
/* recurring payment */
function ipn_paypal(){
if ($_SERVER['REQUEST_METHOD'] != "POST") die ("No Post Variables");
$req = 'cmd=_notify-validate';
// Read the post from PayPal
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
$paypalURL = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
// Now Post all of that back to PayPal's server using curl, and validate everything with PayPal
// We will use CURL instead of PHP for this for a more universally operable script (fsockopen has issues on some environments)
//$url = "https://www.sandbox.paypal.com/cgi-bin/webscr"; //USE SANDBOX ACCOUNT TO TEST WITH
//$url = "https://www.paypal.com/cgi-bin/webscr"; //LIVE ACCOUNT
$curl_result=$curl_err='';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$paypalURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($req)));
curl_setopt($ch, CURLOPT_HEADER , 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$curl_result = @curl_exec($ch);
$curl_err = curl_error($ch);
curl_close($ch);
$req = str_replace("&", "\n", $req); // Make it a nice list in case we want to email it to ourselves for reporting
// Check that the result verifies with PayPal
if (strpos($curl_result, "VERIFIED") !== false) {
$req .= "\n\nPaypal Verified OK";
} else {
$req .= "\n\nData NOT verified from Paypal!";
//mail("email@gmail.com", "IPN interaction not verified", "$req", "From: email@gmail.com" );
exit();
}
if (array_key_exists("txn_id", $_POST)) {
$txn_id = $_POST['txn_id'];
$result = $this->user_model->checktxn_idExist($txn_id);
$payment_rawData = json_encode($_POST);
if($result){
$status = "\n\n=====Data was already saved!=====\n";
$update_data = array('payment_subscr_id'=>$_POST['subscr_id'],'payment_rawData'=>$payment_rawData,'updatedDate'=>date('Y-m-d H:i:s'));
$this->user_model->paymentUpdate($update_data,$txn_id);
}else{
$item_number = $_POST['item_number'];
$txn_id = $_POST['txn_id'];
$amount = $_POST['mc_gross'];
$currency_code = $_POST['mc_currency'];
$custom = $_POST['custom'];
$payment_status = $_POST['payment_status'];
//Insert tansaction data into the database
$payment_info = array('payment_userId'=>$custom,'payment_planId'=>$item_number,'payment_txnId'=>$txn_id,'payment_amount'=>$amount,'payment_currency'=>$currency_code,'payment_planType'=>'0','payment_paidBy'=>'0','payment_status'=> $payment_status,'payment_invoice'=>'upload/invoice/'.$filename,'payment_plan_exdate'=>$exp_date,'payment_rawData'=>$payment_rawData,'createdDate'=>date('Y-m-d H:i:s'),'updatedDate'=>date('Y-m-d H:i:s'));
$this->user_model->insertTransaction($payment_info);
}
}
}
?>
- user_model.php
<?php
function checktxn_idExist($txn_id)
{
$this->db->select("payment_txnId");
$this->db->from("tbl_payment");
$this->db->where("payment_txnId", $txn_id);
$query = $this->db->get();
return $query->result();
}
public function paymentUpdate($update_data,$txn_id)
{
$this->db->where('payment_txnId', $txn_id);
$this->db->update('tbl_payment', $update_data);
return $this->db->affected_rows();
}
function insertTransaction($payment_info){
$this->db->insert('tbl_payment', $payment_info);
$insert_id = $this->db->insert_id();
return $insert_id;
}
?>
Comments
Post a Comment