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

class MPI_Payments extends MY_Controller
{

	public function __construct()
	{
		parent::__construct();
		$currency = '';

		$this->$currency =  GeneralSettings();
	}


	public function Paynow()
	{

		$token = $this->input->post('token');
		$totolAmt = $this->input->post('totolAmt');
		$name = $this->input->post('name');
		$address = $this->input->post('address');
		$zipcode = $this->input->post('zipcode');
		$cart_type = @$this->input->post('cart_type');


		$roundedAmount = round($totolAmt, 2);
		$amountInCents = $roundedAmount * 100;
		// $amountInCents = 501;


		$invoiceNum = floor(microtime(true) * 1000);

		$postData = array(
			'token' => $token,
			'amount' => $amountInCents,
			'name' => $name,
			'address' => $address,
			'zipcode' => $zipcode,
			'clientId' => ApiBaseUrl()['clientID'],
			'InvoiceNum' => $invoiceNum
		);

		$getUserDetails = @getUserDetails()->data[0];
		$profileUpData = [];
		$profileUpData['zip'] = $zipcode;
		if (!empty($profileUpData) && @$getUserDetails->zip != $zipcode) {
			// var_dump($profileUpData);die();

			$ch = curl_init();
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

			curl_setopt($ch, CURLOPT_URL, ApiBaseUrl()['url'] . postUpdateProfileAPI()['url']);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_POST, 1);


			$post_json_data = [
				"_id" => $this->session->userdata('id'),
				"dataJson" => $profileUpData,
				"componentConfig" => [
					"moduleName" => "Contacts",
					"productID" => ApiBaseUrl()['productID'],
					"clientID" => ApiBaseUrl()['clientID'],
					"userName" => $this->session->userdata('refDataName')
				]
			];

			$payload = json_encode($post_json_data);

			// var_dump($this->input->post());

			curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);



			$token = $this->session->userdata('token');
			$headers = array();
			$headers[] = 'Authorization: Bearer ' . $token . '';
			$headers[] = 'Content-Type: application/json';
			curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

			$result = curl_exec($ch);
			$response = json_decode($result);

			if (curl_errno($ch)) {
				echo 'Error:' . curl_error($ch);
			}
			curl_close($ch);
		}

		// ====================================

		// $cart_data = json_decode($this->input->post('cart_data'), true);
		$cart_data = $this->input->post('cart_data');
		if (!is_array($cart_data)) {
			$cart_data = json_decode($this->input->post('cart_data'), true);
		}
		$eventDetails = new stdClass();
		$serviceDetails = new stdClass();
		if ($cart_type == 'serviceRequest') {
			$cartRequestData = new stdClass();
			// var_dump($cart_data);
			// var_dump($cart_data->serviceId);
			// var_dump($cart_data['serviceId']);die();




			$preferredLanguage = @getUserDetails()->data[0]->languagePreferenceName;
			$priestName = @$cart_data['priestName'];
			$isConfirmed = ($priestName != 'Any Priest') ? 'true' : 'false';
			$cartRequestData->memberId = $this->session->userdata('id');
			$cartRequestData->serviceId = @$cart_data['serviceId'];
			$cartRequestData->serviceRequestName = @$cart_data['fname'] . " " . @$cart_data['lname'];
			$cartRequestData->prsnPhone = @$cart_data['phone'] ? base64_encode($cart_data['phone']) : '';
			$cartRequestData->prsnEmail = @$cart_data['email'] ? base64_encode($cart_data['email']) : '';
			$cartRequestData->clientId = ApiBaseUrl()['clientID'];
			$cartRequestData->serviceDate = @$cart_data['serviceDate'];
			$cartRequestData->serviceTime = @$cart_data['serviceTime'];
			$cartRequestData->serviceLocationName = @$cart_data['preferredLocation'];
			$cartRequestData->serviceAddress = @$cart_data['serviceAddress'];
			$cartRequestData->serviceName = @$cart_data['serviceName'];
			$cartRequestData->serviceCategoryTypes = @$cart_data['preferredLocation'];
			$cartRequestData->serviceTypes = @$cart_data['serviceTypes'];
			$cartRequestData->serviceAmount = @$cart_data['serviceAmount'];
			$cartRequestData->oldServiceAmount = @$cart_data['oldServiceAmount'] ? $cart_data['oldServiceAmount'] : 0;
			$cartRequestData->discountedAmount = @$cart_data['discountedAmount'] ? $cart_data['discountedAmount'] : 0;
			$cartRequestData->couponCode = @$cart_data['couponCode'] ? $cart_data['couponCode'] : '';
			$cartRequestData->couponId = @$cart_data['couponId'] ? $cart_data['couponId'] : '';
			$cartRequestData->languagePreferenceName = @$preferredLanguage;
			$cartRequestData->requestForPriestName = @$cart_data['priestName'];
			$cartRequestData->priestEmail = @$cart_data['priestEmail'] ? base64_encode($cart_data['priestEmail']) : '';
			$cartRequestData->priestPhone = @$cart_data['priestPhone'] ? base64_encode($cart_data['priestPhone']) : '';
			$cartRequestData->requestedPriestId = @$cart_data['priestId'];
			$cartRequestData->isConfirmed = @$isConfirmed;
			$cartRequestData->foodType = "";
			$cartRequestData->generalDonation = 0;
			$cartRequestData->acharyasanbhavana = 0;
			$cartRequestData->package = "";
			$cartRequestData->extraFood = "";
			$cartRequestData->etShraadhamFor = "";
			$cartRequestData->ettithiDetail = "";
			$cartRequestData->source = "WEBSITE";
			$cartRequestData->notificationEmailName = "YES";
			$cartRequestData->transactionId = "";
			$cartRequestData->paymentStatus = "";
			$cartRequestData->paymentType = "CREDIT CARD";
			$cartRequestData->cartCreDate = date('m/d/Y');
			$cartRequestData->cartCreTime = date('h:i A');
			$cartRequestData->adults = "";
			$cartRequestData->children = "";
			$cartRequestData->bookingType = @$cart_data['formAction'];
			$cartRequestData->mpiInvNumber = $invoiceNum;
			$cartRequestData->cartType = "SERVICE REQUEST";


			$cart_data = [];
			$cart_data[] = $cartRequestData;
		} else {
			if ($cart_type == 'facility') {
				$cart_data = getFacilitySessionData();
				if (!empty($this->session->userdata('event_details'))) {
					$eventDetails = $this->session->userdata('event_details');
				}
				if (!empty($this->session->userdata('service_details'))) {
					$serviceDetails = $this->session->userdata('service_details');
				}
			} else {
				$cart_data = getCartSessiondata();
			}
		}
		// var_dump($cart_data);
		// die();
		$postData_addServiceCartTemp = array(
			'cardData' => $cart_data,
			'eventDetail' => $eventDetails,
			'serviceDetail' => $serviceDetails,
			'Paymenttype' => 'CREDIT CARD',

			'InvNumber' => $invoiceNum,
			'totalAmount' => $roundedAmount,

			'TerminalId' => 'WEBSITE',

			'clientId' => ApiBaseUrl()['clientID'],
			'productId' => ApiBaseUrl()['productID'],
			'paymentSource' => 'MPI_CARD_NOT_PRESENT',
			'Source' => 'WEBSITE',
			"cartCreDate" => date("m/d/Y"),
			"cartCreTime" => date("h:i A"),
			'remark' => 'WEBSITE~WEBSITE~' . $this->session->userdata('id'),
			'memberId' => $this->session->userdata('id'),
			'memberName' => $this->session->userdata('refDataName'),
			'prsnEmail' => $this->session->userdata('email'),
			'prsnPhone' => $this->session->userdata('phone')
		);


		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

		curl_setopt($ch, CURLOPT_URL, ApiBaseUrl()['url'] . 'api/bookingService/addServiceCartTemp');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData_addServiceCartTemp));
		// var_dump(json_encode($postData_addServiceCartTemp));
		$headers = array();
		$headers[] = 'Content-Type: application/json';
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

		curl_exec($ch);
		if (curl_errno($ch)) {
			echo 'Error:' . curl_error($ch);
		}
		curl_close($ch);


		// =====================================

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

		curl_setopt($ch, CURLOPT_URL, ApiBaseUrl()['url'] . 'api/vaaptech/mpiCardPayment'); //previous
		// curl_setopt($ch, CURLOPT_URL, 'http://192.168.1.12:3000/api/vaaptech/mpiCardPayment');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));

		$headers = array();
		$headers[] = 'Content-Type: application/json';
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

		$result = curl_exec($ch);
		if (curl_errno($ch)) {
			echo 'Error:' . curl_error($ch);
		}
		curl_close($ch);

		$response = json_decode($result, true);
		$response['mpiInvNumber'] = $invoiceNum;
		echo json_encode($response);
	}



	public function PaynowTest()
	{

		$token = $this->input->post('token');
		$totolAmt = $this->input->post('totolAmt');
		$name = $this->input->post('name');
		$address = $this->input->post('address');
		$zipcode = $this->input->post('zipcode');
		$cart_type = @$this->input->post('cart_type');


		$roundedAmount = round($totolAmt, 2);
		$amountInCents = $roundedAmount * 100;


		$invoiceNum = floor(microtime(true) * 1000);

		$postData = array(
			'token' => $token,
			// 'amount' => $amountInCents,
			'amount' => 500,
			'name' => $name,
			'address' => $address,
			'zipcode' => $zipcode,
			'clientId' => ApiBaseUrl()['clientID'],
			'InvoiceNum' => $invoiceNum
		);

		$getUserDetails = @getUserDetails()->data[0];
		$profileUpData = [];
		$profileUpData['zip'] = $zipcode;
		if (!empty($profileUpData) && @$getUserDetails->zip != $zipcode) {
			// var_dump($profileUpData);die();

			$ch = curl_init();
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

			curl_setopt($ch, CURLOPT_URL, ApiBaseUrl()['url'] . postUpdateProfileAPI()['url']);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_POST, 1);


			$post_json_data = [
				"_id" => $this->session->userdata('id'),
				"dataJson" => $profileUpData,
				"componentConfig" => [
					"moduleName" => "Contacts",
					"productID" => ApiBaseUrl()['productID'],
					"clientID" => ApiBaseUrl()['clientID'],
					"userName" => $this->session->userdata('refDataName')
				]
			];

			$payload = json_encode($post_json_data);

			// var_dump($this->input->post());

			curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);



			$token = $this->session->userdata('token');
			$headers = array();
			$headers[] = 'Authorization: Bearer ' . $token . '';
			$headers[] = 'Content-Type: application/json';
			curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

			$result = curl_exec($ch);
			$response = json_decode($result);

			if (curl_errno($ch)) {
				echo 'Error:' . curl_error($ch);
			}
			curl_close($ch);
		}

		// ====================================

		// $cart_data = json_decode($this->input->post('cart_data'), true);
		if (!is_array($this->input->post('cart_data'))) {
			$cart_data = json_decode($this->input->post('cart_data'), true);
		} else {
			$cart_data = $this->input->post('cart_data');
		}
		$eventDetails = new stdClass();
		$serviceDetails = new stdClass();
		if ($cart_type == 'serviceRequest') {
			$cartRequestData = new stdClass();
			// var_dump($cart_data);
			// var_dump($cart_data->serviceId);
			// var_dump($cart_data['serviceId']);die();




			$preferredLanguage = @getUserDetails()->data[0]->languagePreferenceName;
			$priestName = @$cart_data['priestName'];
			$isConfirmed = ($priestName != 'Any Priest') ? 'true' : 'false';
			$cartRequestData->memberId = $this->session->userdata('id');
			$cartRequestData->serviceId = @$cart_data['serviceId'];
			$cartRequestData->serviceRequestName = @$cart_data['fname'] . " " . @$cart_data['lname'];
			$cartRequestData->prsnPhone = @$cart_data['phone'] ? base64_encode($cart_data['phone']) : '';
			$cartRequestData->prsnEmail = @$cart_data['email'] ? base64_encode($cart_data['email']) : '';
			$cartRequestData->clientId = ApiBaseUrl()['clientID'];
			$cartRequestData->serviceDate = @$cart_data['serviceDate'];
			$cartRequestData->serviceTime = @$cart_data['serviceTime'];
			$cartRequestData->serviceLocationName = @$cart_data['preferredLocation'];
			$cartRequestData->serviceAddress = @$cart_data['serviceAddress'];
			$cartRequestData->serviceName = @$cart_data['serviceName'];
			$cartRequestData->serviceCategoryTypes = @$cart_data['preferredLocation'];
			$cartRequestData->serviceTypes = @$cart_data['serviceTypes'];
			$cartRequestData->serviceAmount = @$cart_data['serviceAmount'];
			$cartRequestData->oldServiceAmount = @$cart_data['oldServiceAmount'] ? $cart_data['oldServiceAmount'] : 0;
			$cartRequestData->discountedAmount = @$cart_data['discountedAmount'] ? $cart_data['discountedAmount'] : 0;
			$cartRequestData->couponCode = @$cart_data['couponCode'] ? $cart_data['couponCode'] : '';
			$cartRequestData->couponId = @$cart_data['couponId'] ? $cart_data['couponId'] : '';
			$cartRequestData->languagePreferenceName = @$preferredLanguage;
			$cartRequestData->requestForPriestName = @$cart_data['priestName'];
			$cartRequestData->priestEmail = @$cart_data['priestEmail'] ? base64_encode($cart_data['priestEmail']) : '';
			$cartRequestData->priestPhone = @$cart_data['priestPhone'] ? base64_encode($cart_data['priestPhone']) : '';
			$cartRequestData->requestedPriestId = @$cart_data['priestId'];
			$cartRequestData->isConfirmed = @$isConfirmed;
			$cartRequestData->foodType = "";
			$cartRequestData->generalDonation = 0;
			$cartRequestData->acharyasanbhavana = 0;
			$cartRequestData->package = "";
			$cartRequestData->extraFood = "";
			$cartRequestData->etShraadhamFor = "";
			$cartRequestData->ettithiDetail = "";
			$cartRequestData->source = "WEBSITE";
			$cartRequestData->notificationEmailName = "YES";
			$cartRequestData->transactionId = "";
			$cartRequestData->paymentStatus = "";
			$cartRequestData->paymentType = "CREDIT CARD";
			$cartRequestData->cartCreDate = date('m/d/Y');
			$cartRequestData->cartCreTime = date('h:i A');
			$cartRequestData->adults = "";
			$cartRequestData->children = "";
			$cartRequestData->bookingType = @$cart_data['formAction'];
			$cartRequestData->mpiInvNumber = $invoiceNum;
			$cartRequestData->cartType = "SERVICE REQUEST";


			$cart_data = [];
			$cart_data[] = $cartRequestData;
		} else {
			if ($cart_type == 'facility') {
				$cart_data = getFacilitySessionData();
				if (!empty($this->session->userdata('event_details'))) {
					$eventDetails = $this->session->userdata('event_details');
				}
				if (!empty($this->session->userdata('service_details'))) {
					$serviceDetails = $this->session->userdata('service_details');
				}
			} else {
				$cart_data = getCartSessiondata();
			}
		}
		// var_dump($cart_data);
		// die();
		$postData_addServiceCartTemp = array(
			'cardData' => $cart_data,
			'eventDetail' => $eventDetails,
			'serviceDetail' => $serviceDetails,
			'Paymenttype' => 'CREDIT CARD',

			'InvNumber' => $invoiceNum,
			'totalAmount' => $roundedAmount,

			'TerminalId' => 'WEBSITE',

			'clientId' => ApiBaseUrl()['clientID'],
			'productId' => ApiBaseUrl()['productID'],
			'paymentSource' => 'MPI_CARD_NOT_PRESENT',
			'Source' => 'WEBSITE',
			"cartCreDate" => date("m/d/Y"),
			"cartCreTime" => date("h:i A"),
			'remark' => 'WEBSITE~WEBSITE~' . $this->session->userdata('id'),
			'memberId' => $this->session->userdata('id'),
			'memberName' => $this->session->userdata('refDataName'),
			'prsnEmail' => $this->session->userdata('email'),
			'prsnPhone' => $this->session->userdata('phone')
		);
		// var_dump($postData_addServiceCartTemp);
		// var_dump(json_encode($postData_addServiceCartTemp));die();
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

		curl_setopt($ch, CURLOPT_URL, ApiBaseUrl()['url'] . 'api/bookingService/addServiceCartTemp');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData_addServiceCartTemp));
// var_dump(json_encode($postData_addServiceCartTemp));
		$headers = array();
		$headers[] = 'Content-Type: application/json';
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

		$result2 = curl_exec($ch);
		if (curl_errno($ch)) {
			echo 'Error:' . curl_error($ch);
		}
		curl_close($ch);
		$response2 = json_decode($result2, true);
		// var_dump($response2);die();

		// =====================================

		$ch1 = curl_init();
		curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);

		// curl_setopt($ch1, CURLOPT_URL, ApiBaseUrl()['url'].'/api/vaaptech/mpiCardPayment');
		curl_setopt($ch1, CURLOPT_URL, ApiBaseUrl()['url'] . 'api/vaaptech/mpiCardPaymentTest');
		// curl_setopt($ch1, CURLOPT_URL, 'http://192.168.1.12:3000/api/vaaptech/mpiCardPayment');
		curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch1, CURLOPT_POST, 1);
		curl_setopt($ch1, CURLOPT_POSTFIELDS, json_encode($postData));

		$headers = array();
		$headers[] = 'Content-Type: application/json';
		curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);

		$result = curl_exec($ch1);
		if (curl_errno($ch1)) {
			echo 'Error:' . curl_error($ch1);
		}
		curl_close($ch1);

		$response = json_decode($result, true);
		$response['mpiInvNumber'] = $invoiceNum;
		echo json_encode($response);
	}
}
