<?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');
		// var_dump($this->input->post());die();

		$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
		);



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

		// $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);
		}
		$cart_data = getCartSessiondata();
		$postData_addServiceCartTemp = array(
			'cardData' => $cart_data,
			'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));

		$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);

		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');


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


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

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



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

		// $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');
		}
		$cart_data = getCartSessiondata();

		$postData_addServiceCartTemp = array(
			'cardData' => $cart_data,
			'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(json_encode($postData_addServiceCartTemp));
		$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));

		$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');
		curl_setopt($ch, CURLOPT_URL, ApiBaseUrl()['url'] . 'api/vaaptech/mpiCardPaymentTest');
		// 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);

		echo json_encode($response);
	}
}
