<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Payment extends CI_Controller 
{
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('Index_model');
	}
	public function index()
	{  
		if(empty($this->session->userdata('cart')))
		{
			redirect(base_url());
		}
		else
		{
			$this->load->view("front/payment");
		}
	}

	public function confirm()
	{  
		$this->session->sess_destroy();
		$this->load->view("front/confirm");
	}

	public function payment_order()
	{  
		$cardsplit = str_split($this->input->post('card_number'));
		$ssl_customer_code = $cardsplit[12].$cardsplit[13].$cardsplit[14].$cardsplit[15];

		$ssl_invoice_number = $this->input->post('billing_contact').time();
		$ssl_transaction_type = "ccsale";
		$ssl_cvv2cvc2_indicator = 1;
		$country = "USA";

		$fields = array(
			"ssl_merchant_id" => "500693",
			"ssl_user_id" => "webpage",
			"ssl_pin" => "MX0MD3",
			"ssl_transaction_type" => urlencode($ssl_transaction_type),
			"ssl_show_form" => "false",
				   "ssl_cvv2cvc2_indicator" => urlencode($ssl_cvv2cvc2_indicator), //0=Bypassed; 1=present; 2=Illegible; 9=Not Present.
				   "ssl_salestax" => "0", 

				   "ssl_result_format" => "html",
				   "ssl_test_mode" => "false",
				   "ssl_receipt_apprvl_method" => "redg", // sends to page with long data string url
				   //"ssl_receipt_apprvl_method" => "link", // just posts receipt with link at bottom
				   "ssl_receipt_link_url" => "http://abc.mydataboxx.com/order-confirm",
				   "ssl_error_url" => "http://abc.mydataboxx.com/order-confirm",
				   
				   //submitted details
				   "ssl_invoice_number" => urlencode($ssl_invoice_number),
				   "ssl_customer_code" => urlencode($ssl_customer_code),
				   "ssl_first_name" => urlencode($this->input->post('company_name')),
				   "ssl_last_name" => urlencode($this->input->post('billing_contact')),
				   "ssl_avs_address" => urlencode($this->input->post('street_adrs')),
				   "ssl_address2" => urlencode($this->input->post('customer_id')),
				   "ssl_avs_zip" => urlencode($this->input->post('post_code')),
				   "ssl_state" => urlencode($this->input->post('state')),
				   "ssl_city" => urlencode($this->input->post('city_name')),
				   "ssl_country" => urlencode($country),
				   "ssl_phone" => urlencode($this->input->post('phone_no')),
				   "ssl_email" => urlencode($this->input->post('emai_id')),
				   "ssl_card_number" => urlencode($this->input->post('card_number')),
				   "ssl_exp_date" => urlencode($this->input->post('exp_month').$this->input->post('exp_year')),
				   "ssl_cvv2cvc2" => urlencode($this->input->post('cvv_code')),
				   "ssl_amount" => urlencode($this->session->userdata('granttotal'))

				);

		$url = "https://api.demo.convergepay.com/VirtualMerchantDemo/process.do";

				//initialize the post string variable 
		$fields_string = '';
				//build the post string
		foreach($fields as $key=>$value) { $fields_string .=$key.'='.$value.'&'; }
		rtrim($fields_string, "&");

				//open curl session 
		$ch = curl_init();
				//begin seting curl options
				//set URL
				curl_setopt($ch, CURLOPT_URL, $url); //set method
				curl_setopt($ch, CURLOPT_POST, 1);
				//set post data string
				curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
				//these two options are frequently necessary to avoid SSL errors with PHP
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
				curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

				//perform the curl post and store the result 
				$result = curl_exec($ch);
				//close the curl session 
				curl_close($ch);
				//a nice message to prevent people from seeing a blank screen 
				echo "Processing, please wait...";

			//print_r($result);				

				$masterdata = array('reg_date'=>date("Y-m-d"),
					'reg_total'=>$this->session->userdata('granttotal'),
					'reg_class_count'=>count($this->session->userdata('cart')),
					'payment_type'=>$this->input->post('payment_type'),
					'card_holder_name'=>$this->input->post('card_holder_name'),
					'card_number'=>$this->input->post('card_number'),
					'card_type'=>$this->input->post('card_type'),
					'exp_month'=>$this->input->post('exp_month'),
					'exp_year'=>$this->input->post('exp_year'),
					'cvv_code'=>$this->input->post('cvv_code'),
					'company_name'=>$this->input->post('company_name'),
					'billing_contact'=>$this->input->post('billing_contact'),
					'customer_id'=>$this->input->post('customer_id'),
					'street_adrs'=>$this->input->post('street_adrs'),
					'city_name'=>$this->input->post('city_name'),
					'state'=>$this->input->post('state'),
					'post_code'=>$this->input->post('post_code'),
					'phone_no'=>$this->input->post('phone_no'),
					'fax_no'=>$this->input->post('fax_no'),
					'adtnal_instrction'=>$this->input->post('adtnal_instrction'),
					'details'=>$this->input->post('details'),
					'email_id'=>$this->input->post('emai_id'),
					'status'=>'unpaid');

				$regid = $this->Index_model->regmaster($masterdata);
				if($regid)
				{
					$attotal = 0;
					$cart = $this->session->userdata('cart');/*Cart Session*/
					foreach ($cart as $cartcount => $form) 
					{
						$mcount = $this->session->userdata('mcount'.$form);
						$mpluscount = $this->session->userdata('mpluscount'.$form);
						$ncount = $this->session->userdata('ncount'.$form);
						$npluscount = $this->session->userdata('npluscount'.$form);
						$scount = $this->session->userdata('scount'.$form);
						$attcount = ($mcount + $mpluscount + $ncount + $npluscount + $scount); /*Attendee Count*/
						$classdata = array('reg_id'=>$regid,'form_id'=>$form,
							'att_count'=>$attcount,
							'regclass_total_price'=>$this->session->userdata('classtotal'.$form));
						$classform = $this->Index_model->returnclass($form);
						$remainseats = $classform->remain_seats-$attcount;
						$this->Index_model->remainclass($remainseats,$form);
						$regclassid = $this->Index_model->classdata($classdata);
						$classform = $this->session->userdata('classform'.$form);
						foreach ($classform as $formcount => $data) 
						{
							$mprice = $data['class_price_m'];
							$mplusprice = $data['class_price_m2'];
							$nprice = $data['class_price_n'];
							$nplusprice = $data['class_price_n2'];
							$sprice = $data['class_price_s'];
						}

						if($regclassid)
						{
							$attmcount = 0;
							$attmpcount = 0;
							$attncount = 0;
							$attnpcount = 0;
							$attscount = 0;

							$member = $this->session->userdata('member'.$form);
							if (!empty($member) || isset($member['M'])) 
							{
								foreach ($member['M'] as $key => $val)
								{
									$attendedata = array(
										'reg_id'=>$regid,
										'regclass_id'=>$regclassid,
										'reg_m_type'=>'M',
										'att_f_name'=>isset($val['first_name'])? $val['first_name'] : '',
										'att_l_name'=>isset($val['last_name'])? $val['last_name'] : '',
										'att_email'=>isset($val['email_id'])? $val['email_id'] : '',
										'att_number'=>isset($val['phone_no'])? $val['phone_no'] : '',
										'att_ssn'=>isset($val['ssn'])? $val['ssn'] : '',
										'att_price'=>$mprice );
									$data = $this->Index_model->attendedata($attendedata);
									$attmcount+=1;
								}
							}

							$memplus = $this->session->userdata('memplus'.$form);
							if (!empty($memplus) || isset($memplus['M2'])) 
							{
								foreach ($memplus['M2'] as $key => $val)
								{
									$attendedata = array(
										'reg_id'=>$regid,
										'regclass_id'=>$regclassid,
										'reg_m_type'=>'M2',
										'att_f_name'=>isset($val['first_name'])? $val['first_name'] : '',
										'att_l_name'=>isset($val['last_name'])? $val['last_name'] : '',
										'att_email'=>isset($val['email_id'])? $val['email_id'] : '',
										'att_number'=>isset($val['phone_no'])? $val['phone_no'] : '',
										'att_ssn'=>isset($val['ssn'])? $val['ssn'] : '',
										'att_price'=>$mplusprice );
									$data = $this->Index_model->attendedata($attendedata);
									$attmpcount+=1;
								}
							}

							$nonmember = $this->session->userdata('nonmember'.$form);
							if (!empty($nonmember) || isset($nonmember['N'])) 
							{
								foreach ($nonmember['N'] as $key => $val)
								{
									$attendedata = array(
										'reg_id'=>$regid,
										'regclass_id'=>$regclassid,
										'reg_m_type'=>'N',
										'att_f_name'=>isset($val['first_name'])? $val['first_name'] : '',
										'att_l_name'=>isset($val['last_name'])? $val['last_name'] : '',
										'att_email'=>isset($val['email_id'])? $val['email_id'] : '',
										'att_number'=>isset($val['phone_no'])? $val['phone_no'] : '',
										'att_ssn'=>isset($val['ssn'])? $val['ssn'] : '',
										'att_price'=>$nprice );
									$data = $this->Index_model->attendedata($attendedata);
									$attncount+=1;
								}
							}

							$nonplus = $this->session->userdata('nonplus'.$form);
							if (!empty($nonplus) || isset($nonplus['N2'])) 
							{
								foreach ($nonplus['N2'] as $key => $val)
								{
									$attendedata = array(
										'reg_id'=>$regid,
										'regclass_id'=>$regclassid,
										'reg_m_type'=>'N2',
										'att_f_name'=>isset($val['first_name'])? $val['first_name'] : '',
										'att_l_name'=>isset($val['last_name'])? $val['last_name'] : '',
										'att_email'=>isset($val['email_id'])? $val['email_id'] : '',
										'att_number'=>isset($val['phone_no'])? $val['phone_no'] : '',
										'att_ssn'=>isset($val['ssn'])? $val['ssn'] : '',
										'att_price'=>$nplusprice );
									$data = $this->Index_model->attendedata($attendedata);
									$attnpcount+=1;
								}
							}

							$semember = $this->session->userdata('semember'.$form);
							if (!empty($semember) || isset($semember['S'])) 
							{
								foreach ($semember['S'] as $key => $val)
								{
									$attendedata = array(
										'reg_id'=>$regid,
										'regclass_id'=>$regclassid,
										'reg_m_type'=>'S',
										'att_f_name'=>isset($val['first_name'])? $val['first_name'] : '',
										'att_l_name'=>isset($val['last_name'])? $val['last_name'] : '',
										'att_email'=>isset($val['email_id'])? $val['email_id'] : '',
										'att_number'=>isset($val['phone_no'])? $val['phone_no'] : '',
										'att_ssn'=>isset($val['ssn'])? $val['ssn'] : '',
										'att_price'=>$sprice );
									$data = $this->Index_model->attendedata($attendedata);
									$attscount+=1;
								}
							}
						}
						$attotal+= $attmcount + $attmpcount + $attncount + $attnpcount + $attscount; /*Attendee Total Count*/
					}
					$this->Index_model->attotal($attotal,$regid);
					$this->session->set_flashdata('success_msg','Your Order Has Been Completed Successfully.');
					redirect('order-confirm');
				}
				else
				{
					$this->session->set_flashdata('error_msg','Something Wrong Happen..! Register Again.');
					redirect('order-confirm');
				}
			}

		}
		?>