<?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();

	}



	// MPI Integration 09/01/2023


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


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

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

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


			$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, '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);
	}
}