File: /home/barbeatleanalyti/public_html/mbaris.beatleanalytics.com/application/controllers/Razorpay.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Razorpay extends CI_Controller {
public function index() {
$this->checkout();
}
public function checkout() {
//$this->load->model("UserModel");
$proid = base64_decode($this->uri->segment(3));
$userdata = $this->db->Select("*")->from("baris_userlogin")->where("userId",$proid)->get();
$data['userdata'] = $userdata->result();
$data['title'] = 'Checkout payment | Beatleanalytics';
$data['callback_url'] = 'https://mbaris.beatleanalytics.com/index.php/razorpay/callback';
$data['surl'] = 'https://mbaris.beatleanalytics.com/index.php/razorpay/success';
$data['furl'] = 'https://mbaris.beatleanalytics.com/index.php/razorpay/failed';
$data['currency_code'] = 'INR';
$this->load->view('razorpay/checkout_new', $data);
}
public function checkout_old() {
// echo $this->db->last_query(); die;
$data['userdata'] = $udata1;
$data['title'] = 'Checkout payment | Infovistar';
$data['callback_url'] = 'https://mbaris.beatleanalytics.com/index.php/barisAdmin/directlogin/'.$udata1[0]->db_userLoginName;
$data['surl'] = 'https://mbaris.beatleanalytics.com/index.php/barisAdmin/directlogin/'.$udata1[0]->db_userLoginName;
$data['furl'] = 'https://mbaris.beatleanalytics.com/index.php/barisAdmin/directlogin/'.$udata1[0]->db_userLoginName;
$data['currency_code'] = 'INR';
$this->load->view('razorpay/checkout_new', $data);
}
// initialized cURL Request
private function curl_handler($payment_id, $amount) {
$url = 'https://api.razorpay.com/v1/payments/'.$payment_id.'/capture';
$key_id = "rzp_live_rh6BGAvjmBxaX5";
$key_secret = "RjA2L5ZZX3hN2Hwrd7ZH9ZR0";
$fields_string = "amount=$amount";
//cURL Request
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $key_id.':'.$key_secret);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
return $ch;
}
// callback method
public function callback() {
//print_r($this->input->post());
if (!empty($this->input->post('razorpay_payment_id')) && !empty($this->input->post('merchant_order_id'))) {
$razorpay_payment_id = $this->input->post('razorpay_payment_id');
$merchant_order_id = $this->input->post('merchant_order_id');
$userid = $this->input->post('userid');
$userdata1 = $this->db->select("*")->from("baris_userlogin")->where("userId",$userid)->get();
$udata1 = $userdata1->result();
if (!empty($this->input->post('razorpay_payment_id')))
{
$userdata = $this->db->select("*")->from("baris_userlogin")->where("userId",$userid)->get();
$udata = $userdata->result();
$insertarr = array (
"user_id" => $udata[0]->userId,
"transaction_id"=> $this->input->post('razorpay_payment_id'),
"order_id " => $this->input->post('merchant_order_id'),
"amount" => $udata[0]->renewal_amount,
"gst" => $udata[0]->renewal_gst_amount,
"total_amount" => $udata[0]->renewal_total_amount,
"created_date" => date('Y-m-d H:i:s')
);
// echo '<pre>'; print_r($insertarr); die;
$queryinsert = $this->db->insert("baris_paymet",$insertarr);
if ($queryinsert){
$d = date('Y-m-d',strtotime($udata1[0]->db_valid));
$enddate=date('Y-m-d H:i:s', strtotime('+1 year', strtotime($d)) );
$updatearr = array("db_valid_from"=>$udata1[0]->db_valid,"db_valid"=>$enddate);
// echo '<pre>'; print_r($updatearr); die;
$udpatebarisdate = $this->db->where("userId",$userid)->update("baris_userlogin",$updatearr);
}
else {
redirect('https://mbaris.beatleanalytics.com/index.php/razorpay/checkout/'.base64_encode($userid));
}
}
$this->session->set_flashdata('razorpay_payment_id', $this->input->post('razorpay_payment_id'));
$this->session->set_flashdata('merchant_order_id', $this->input->post('merchant_order_id'));
$currency_code = 'INR';
$amount = $this->input->post('merchant_total');
$success = false;
$error = '';
try {
$ch = $this->curl_handler($razorpay_payment_id, $amount);
//execute post
$result = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($result === false) {
$success = false;
$error = 'Curl error: '.curl_error($ch);
} else {
$response_array = json_decode($result, true);
//Check success response
if ($http_status === 200 and isset($response_array['error']) === false) {
$success = true;
} else {
$success = false;
if (!empty($response_array['error']['code'])) {
$error = $response_array['error']['code'].':'.$response_array['error']['description'];
} else {
$error = 'RAZORPAY_ERROR:Invalid Response <br/>'.$result;
}
}
}
//close curl connection
curl_close($ch);
} catch (Exception $e) {
$success = false;
$error = 'Request to Razorpay Failed';
}
if ($success === true) {
if(!empty($this->session->userdata('ci_subscription_keys'))) {
$this->session->unset_userdata('ci_subscription_keys');
}
else{
redirect($this->input->post('merchant_surl_id'));
}
/* if (!$order_info['order_status_id']) {
redirect($this->input->post('merchant_surl_id'));
} else {
redirect($this->input->post('merchant_surl_id'));
}*/
} else {
redirect($this->input->post('merchant_furl_id'));
}
} else {
echo 'An error occured. Contact site administrator, please!';
}
}
public function success() {
/*$data['title'] = 'Razorpay Success | TechArise';
echo "<h4>Your transaction is successful</h4>";
echo "<br/>";
echo "Transaction ID: ".$this->session->flashdata('razorpay_payment_id');
echo "<br/>";
echo "Order ID: ".$this->session->flashdata('merchant_order_id');*/
$data = array(
"transactionid"=>$this->session->flashdata('razorpay_payment_id'),
"order_id"=>$this->session->flashdata('merchant_order_id')
);
//$userid = base64_decode($this->uri->segment(3));
// redirect('https://mbaris.beatleanalytics.com/index.php/razorpay/checkout/'.base64_encode($userid));
$this->load->view('razorpay/thankspage',$data);
}
public function failed() {
$userid = base64_decode($this->uri->segment(3));
redirect('https://mbaris.beatleanalytics.com/index.php/razorpay/checkout/'.base64_encode($userid));
$data['title'] = 'Beatleanalytics Failed ';
echo "<h4>Your transaction got Failed</h4>";
echo "<br/>";
echo "Transaction ID: ".$this->session->flashdata('razorpay_payment_id');
echo "<br/>";
echo "Order ID: ".$this->session->flashdata('merchant_order_id');
$userid = base64_decode($this->uri->segment(3));
header('Location:https://mbaris.beatleanalytics.com/index.php/razorpay/checkout/'.base64_encode($userid));
//redirect(PAYMENT_URL.'checkout');
/*$proid = base64_decode($this->uri->segment(3));*/
}
}