<?php
    $ord_compra = $_GET["ord_compra"];
	$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"];

	$headers = array('Authorization:' . $token);
	
 	$ch = curl_init();        
    curl_setopt($ch, CURLOPT_URL, "https://gestionapr.cl/public/Webpay/anular_pago");
    curl_setopt($ch, CURLOPT_POSTFIELDS, "orden_compra=".$ord_compra);
    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") {
		echo $mensaje;
		// redireccionar a punto_blue o imprimir comprabante etc ...
	} else {
		echo $mensaje;
	}
?>