<?php

	include_once("../../vendor/autoload.php");
	use Transbank\Webpay\WebpayPlus\MallTransaction;
	
	$mode="PRODUCCION";
	$commerceCode="597037376434";
	$apiKeySecret="af6c37f24926bb3f2c159e8029edfdec";
	
	if(!isset($_GET["token_ws"]))
		header('Location: https://medidorinteligente.cl/error-en-el-pago/');

	$token = $_GET["token_ws"];

    if($mode == "PRODUCCION"){
    	$response = (new MallTransaction)->configureForProduction($commerceCode, $apiKeySecret)->commit($token);
    }
    else{
    	$response = (new MallTransaction)->commit($token);
    }

	$err = 0;
	foreach($response->details as $detalle){
		if($detalle->status != "AUTHORIZED")
			$err++;
	}

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,"https://gestionapr.cl/public/Auth/create");
	curl_setopt($ch, CURLOPT_POST, TRUE);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "usuario=puntoblue&clave=pb380380$#");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$data_token = curl_exec ($ch);
	curl_close ($ch);
	$json = json_decode($data_token, true);
	$token = $json["token"];

	$datos = [];
	if($err == 0){
		foreach($response->details as $detalle){
			$datos = [
				"id_webpay" => $response->buyOrder,
				"vci" => $response->vci,
				"amount" => $detalle->amount,
				"status" => $detalle->status,
				"buy_order" => $detalle->buyOrder,
				"session_id" => $response->sessionId,
				"card_detail" => json_encode($response->cardDetail),
				"card_number" => $response->cardNumber,
				"accounting_date" => $response->accountingDate,
				"transaction_date" => $response->transactionDate,
				"autorization_code" => $detalle->authorizationCode,
				"payment_type_code" => $detalle->paymentTypeCode,
				"installments_amount" => $detalle->installmentsAmount,
				"installments_number" => $detalle->installmentsNumber,
				"response_code" => $detalle->responseCode,
				"balance" => $detalle->amount
			];		
		}
		$payload = json_encode($datos);
	}
	else{
		//foreach($response->details as $detalle){
			$ord_compra = $response->buyOrder;
		//}
		$payload = "orden_compra=".$ord_compra;
	}

	$headers = array('Authorization:' . $token);

 	$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://gestionapr.cl/public/Webpay/confirmar_pago");
	if(isset($ord_compra)) curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
    $result=curl_exec($ch);
    $json = json_decode($result, true);

	$i = 0;
	foreach ($json as $key => $value) {
		switch ($i) {
			case 0:
				$mensaje = $value;
				break;
			case 1:
				$estado = $value;
				break;
			case 2:
				$folio = $value;
				break;
		}

		$i++;
	}

	if ($estado == "exito") {
		if($err == 0){
			header('Location: https://medidorinteligente.cl/pago-exitoso/');
		}
		else{
			header('Location: https://medidorinteligente.cl/error-en-el-pago/');
		}
		//echo $mensaje;
		// redireccionar a punto_blue o imprimir comprabante etc ...
	} else {
		header('Location: https://medidorinteligente.cl/error-en-el-pago/');
		//echo $mensaje;
	}
?>