hesabe payment gateway

0
75

<?php
class Payment extends Public_Controller
{

public function __construct()
{
parent::__construct();
$this->load->helper(array(‘file’,’cart/cart’));
$this->load->model(array(‘order/order_model’,’payment/payment_model’));
$this->load->library(array(‘Dmailer’));
}

public function index()
{
$this->cart->destroy();
$this->session->unset_userdata(“guest”);

if( $this->input->post()!=” )
{
$working_order_id = $this->session->userdata(‘working_order_id’);
$ordmaster = $this->order_model->get_order_master($working_order_id);

if($this->input->post(‘pay_method’) == “hesabe” )
{

$posturl= ‘https://www.hesabe.com/authpost’; // For Live Account

//$posturl= ‘http://demo.hesabe.com/paymentgateway/’; // For Test Account

$data =array();
$amount = ( ($ordmaster[‘total_amount’]) + ( $ordmaster[‘total_warranty_price’]) + ( $ordmaster[‘shipping_amount’]) + ( $ordmaster[‘vat_amount’]) – ( $ordmaster[‘coupon_discount_amount’]) );

$amount=number_format($amount,3,’.’,”);
$orderid = $ordmaster[‘order_id’];

$data[‘MerchantCode’] = “4123118”; // For Live
//$data[‘MerchantCode’] = “642616”; // For Test

$data[‘SuccessUrl’] = base_url(‘payment/order_success/hesabl/’.md5($orderid));
$data[‘FailureUrl’] = base_url(‘payment/order_cancle/hesabl/’.md5($orderid));;
$data[‘Amount’] = $amount;
$data[‘OrderId’] = $orderid;

$ch = curl_init($posturl);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array(“Accept: application/json”,’Content-Type: application/x-www-form-urlencoded;’));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$result=curl_exec($ch);

/*if (curl_error($ch))
{
$error_msg = curl_error($ch);
echo ‘<pre>’;
print_r($error_msg);
echo ‘</pre>’;

}*/
//trace($result);
$result=json_decode($result);
// exit;
if(isset($result->status) && $result->status===”success”){

$paymenturl=$result->data->paymenturl.$result->data->token;
if($paymenturl){
redirect($paymenturl);
}else{
redirect($data[‘FailureUrl’]);

}

}else{

redirect($data[‘FailureUrl’]);
}
}

// temp code to adjust stock
$ordId = $working_order_id;
$data = array(‘payment_status’=>’Paid’);
$where = “order_id = ‘$ordId’ “;
//$this->payment_model->safe_update(‘wl_order’,$data,$where,FALSE);
//echo_sql(); exit;

if($ordmaster[‘payment_status’]==”Paid”){
$orderproduct= $this->db->query(“select * from wl_orders_products where MD5(orders_id) = ‘”.$ordId.”‘ “)->result_array();
/*Minus quantity in tbl_product_stock*/
$get_order=”SELECT * FROM wl_orders_products WHERE orders_id='”.$ordId.”‘”;
$query_res=$this->db->query($get_order);
}
/*End Minus quantity in tbl_product_stock*/

$ordmaster = $this->order_model->get_order_master( $working_order_id );
$orddetail = $this->order_model->get_order_detail( $working_order_id);
if( is_array( $ordmaster ) && !empty( $ordmaster ) )
{
if($ordmaster[‘payment_method’]==”Cash On Delivery”)
{
//Send Invoice mail
ob_start();
$mail_subject =$this->admin_info->company_name.” Order Details”;
$from_email = $this->admin_info->admin_email;
$from_name = $this->admin_info->company_name;
$mail_to = $ordmaster[’email’];
$body = order_invoice_content($ordmaster,$orddetail,”yes”);
$msg = ob_get_contents();

$mail_conf = array(
‘subject’=>$this->admin_info->company_name.” Order Details”,
‘to_email’=>$mail_to,
‘from_email’=>$from_email,
‘from_name’=> $this->admin_info->company_name,
‘body_part’=>$msg);
$this->dmailer->mail_notify($mail_conf);

// End Invoice mail

// stock adjusted as client request
$data = array(‘payment_status’=>’Paid’);
$where = “order_id = ‘$ordId’ “;
$this->payment_model->safe_update(‘wl_order’,$data,$where,FALSE);

/*Minus quantity in tbl_product_stock*/
$get_order=”SELECT * FROM wl_orders_products WHERE orders_id='”.$ordId.”‘”;
$query_res=$this->db->query($get_order);
//trace($query_res);

$member_name = ucwords($ordmaster[“first_name”].” “.$ordmaster[“last_name”]);
$sms_message=”Hello “.$member_name.”, Your transaction has been successfully completed on webmarkindia.”;
// send message
$sms_conf[‘mobile_numbers’] = $ordmaster[‘mobile_number’];
$sms_conf[‘message_content’] = $sms_message;
send_sms($sms_conf);
}
}
redirect(‘payment/thanks’, ”);
}
}

public function order_success()
{
$post_data=$_REQUEST;

$PaymentId = $this->input->get_post(‘PaymentId’);

if(isset($post_data) && $post_data[‘Status’]==”1″){
$ordId = $this->uri->segment(4);
$payment_method = $this->uri->segment(3);

$data = array(‘payment_method’=>$payment_method,’payment_status’=>’Paid’,’payment_ref_number’=>$PaymentId);
$where = “md5(order_id) = ‘$ordId’ “;

$this->payment_model->safe_update(‘wl_order’,$data,$where,FALSE);

$ordmaster = $this->order_model->get_order_master( $this->session->userdata(‘working_order_id’) );
$orddetail = $this->order_model->get_order_detail( $this->session->userdata(‘working_order_id’));

$orderproduct= $this->db->query(“select * from wl_orders_products where md5(orders_id) = ‘”.$ordId.”‘ “)->result_array();
/*Minus quantity in tbl_product_stock*/
$get_order=”SELECT * FROM wl_orders_products WHERE md5(orders_id)='”.$ordId.”‘”;
$query_res=$this->db->query($get_order);

if( is_array( $ordmaster ) && !empty( $ordmaster ) )
{
/***** Send Invoice mail */
ob_start();
$mail_subject =$this->admin_info->company_name.” Order Details”;
$from_email = $this->admin_info->admin_email;
$from_name = $this->admin_info->company_name;
$mail_to = $ordmaster[’email’];
$body = order_invoice_content($ordmaster,$orddetail);
$msg = ob_get_contents();

$mail_conf = array(
‘subject’=>$this->admin_info->company_name.” Order Details”,
‘to_email’=>$mail_to,
‘from_email’=>$from_email,
‘from_name’=> $this->config->item(‘site_name’),
‘body_part’=>$msg);
$this->dmailer->mail_notify($mail_conf);

/******* End Invoice mail */

$member_name = ucwords($ordmaster[“first_name”].” “.$ordmaster[“last_name”]);
$sms_message=”Hello “.$member_name.”, Your transaction has been successfully completed on webmarkindia.”;
// send message
$sms_conf[‘mobile_numbers’] = $ordmaster[‘mobile_number’];
$sms_conf[‘message_content’] = $sms_message;
send_sms($sms_conf);
}
}

//$this->session->unset_userdata(array(‘working_order_id’ =>0));
$this->session->set_flashdata(‘msg’, $this->config->item(‘payment_success’));
redirect(‘payment/thanks/’, ”);
}

public function order_cancle()
{
$status=$this->input->get_post(‘Status’);

$PaymentId = $this->input->get_post(‘PaymentId’);

$ordId = $this->uri->segment(4);
$payment_method = $this->uri->segment(3);
$data = array(‘payment_method’=>$payment_method,’order_status’=>’Canceled’,’payment_ref_number’=>$PaymentId);
$where = “md5(order_id) = ‘$ordId’ “;
$this->payment_model->safe_update(‘wl_order’,$data,$where,FALSE);

$this->session->unset_userdata(array(‘working_order_id’ =>0));
$this->session->set_flashdata(‘msg’, $this->config->item(‘payment_failed’));
redirect(‘payment/thanks/’.$ordId, ”);

}

public function thanks()
{
if($this->session->userdata(‘working_order_id’)){

$ordId = $this->session->userdata(‘working_order_id’);
}else{
$ordId = $this->uri->segment(3);
}

//$ordId = $this->session->userdata(‘working_order_id’);
$this->load->model(array(‘order/order_model’));
$order_res = $this->order_model->get_order_master( $ordId );
$order_details_res = $this->order_model->get_order_detail($order_res[‘order_id’]);
$data[‘orddetail’] = $order_details_res;
$data[‘ordmaster’] = $order_res;

$this->load->view(‘payment/pay_thanks’,$data);
}

//Add money to wallet
public function wallet_payment()
{
$tid = $this->session->userdata(‘wallet_transaction_id’);
//payment gateway will be integrated here
redirect(‘payment/wallet_payment_success/’.$tid, ”);
}

public function wallet_payment_success()
{
$tid = $this->uri->segment(3);

$user_id = get_db_field_value(‘wl_wallet’,’user_id’,” AND id='”.$tid.”‘ “);

$data = array(‘status’=>’1’);
$where = “id = ‘$tid’ “;
$this->payment_model->safe_update(‘wl_wallet’,$data,$where,FALSE);
$this->session->set_userdata(array(‘msg_type’=>’success’));
$this->session->set_flashdata(‘success’, ‘You have added money to wallet successfully.’);

redirect(‘members/wallet/’, ”);

}

}
/* End of file member.php */
/* Location: .application/modules/products/controllers/cart.php */