<?php
if(isset($_POST)){
	include "../../../init.php";
	include(dirname(__FILE__)."/serviceREST.php");

	function object2array($object) {
		return @json_decode(@json_encode($object),true); 
	}

	function retform($id){
		switch($id){
			case '1':
			return 'Cartao de credito';
			break;
	        case '2':
			return 'Boleto';
			break;
			case '3':
			return 'Debito online';
			break;
			default:
			return 'Nao identificado';
		}
	}

	function retmotivo($id){
		switch($id){
			case 'INTERNAL':
			return 'Recusado Pagseguro';
			break;
			case 'EXTERNAL':
			return 'Recusado Operadora';
			break;
			default:
			return 'Não reprovado';
		}
	}

	function ipnPsApi(){
		if(isset($_POST['notificationType']) && $_POST['notificationType']=='transaction'){
			$c = new clientREST();
			$dados['email'] = trim(GetModuleVariable("checkout_psapi","email"));
			$dados['token'] = trim(GetModuleVariable("checkout_psapi","api"));
			$response = $c->execRequest('https://ws.pagseguro.uol.com.br/v3/transactions/notifications/'.$_POST['notificationCode'].'','get',$dados);
			libxml_use_internal_errors(true);
			$xml = simplexml_load_string($response);
			$ret = object2array($xml);
			$pul ="<br>";
			if(isset($ret['code'])){
				$Transacao = preg_replace('/\D/', '', $ret['reference']);
				$status = $ret['status'];
				$transacaoID = $ret['code'];
				$quantparce = $ret['installmentCount'];
				$metodop = $ret['paymentMethod']['type'];
				$methform = retform($metodop);
				$Motivoret = $ret['cancellationSource'];
				$methMotivo = retmotivo($Motivoret);
				if($status==1){
					@UpdateOrderStatus($Transacao, GetModuleVariable("checkout_psapi","aguardando"));
					$msg = "AG.Pagamento | Forma: ".$methform." | Parcela(s): ".$quantparce."  ";
					$query = "UPDATE [|PREFIX|]orders SET ordpayproviderid = '".$transacaoID."', ordpaymentstatus = '".$msg."' where orderid = '".$Transacao."'";
					@$GLOBALS['ISC_CLASS_DB']->Query($query);
				}elseif($status==2){
					@UpdateOrderStatus($Transacao, GetModuleVariable("checkout_psapi","analise"));
					$msg = "Em analise | Forma: ".$methform." | Parcela(s): ".$quantparce."  ";
					$query = "UPDATE [|PREFIX|]orders SET ordpayproviderid = '".$transacaoID."', ordpaymentstatus = '".$msg."' where orderid = '".$Transacao."'";
					@$GLOBALS['ISC_CLASS_DB']->Query($query);
				}elseif($status==3){
					@UpdateOrderStatus($Transacao, GetModuleVariable("checkout_psapi","aprovado"));
					$msg = "Aprovado | Forma: ".$methform." | Parcela(s): ".$quantparce."  ";
					$query = "UPDATE [|PREFIX|]orders SET ordpayproviderid = '".$transacaoID."', ordpaymentstatus = '".$msg."' where orderid = '".$Transacao."'";
					@$GLOBALS['ISC_CLASS_DB']->Query($query);
				}elseif($status==4){
					@UpdateOrderStatus($Transacao, GetModuleVariable("checkout_psapi","disponivel"));
					$query = "UPDATE [|PREFIX|]orders SET ordpayproviderid = '".$transacaoID."', ordpaymentstatus = 'Disponivel' where orderid = '".$Transacao."'";
					@$GLOBALS['ISC_CLASS_DB']->Query($query);
				}elseif($status==5){
					@UpdateOrderStatus($Transacao, GetModuleVariable("checkout_psapi","disputa"));
					$query = "UPDATE [|PREFIX|]orders SET ordpayproviderid = '".$transacaoID."', ordpaymentstatus = 'Em disputa' where orderid = '".$Transacao."'";
					@$GLOBALS['ISC_CLASS_DB']->Query($query);
				}elseif($status==6){
					@UpdateOrderStatus($Transacao, GetModuleVariable("checkout_psapi","devolvida"));
					$query = "UPDATE [|PREFIX|]orders SET ordpayproviderid = '".$transacaoID."', ordpaymentstatus = 'Devolvida' where orderid = '".$Transacao."'";
					@$GLOBALS['ISC_CLASS_DB']->Query($query);
				}elseif($status==7){
					@UpdateOrderStatus($Transacao, GetModuleVariable("checkout_psapi","cancelada"));
					$msg = "Cancelado |Forma: ".$methform." |Parcela(s):".$quantparce." |".$methMotivo." ";
					$query = "UPDATE [|PREFIX|]orders SET ordpayproviderid = '".$transacaoID."', ordpaymentstatus = '".$msg."' where orderid = '".$Transacao."'";
					@$GLOBALS['ISC_CLASS_DB']->Query($query);
				}elseif($status==8){
					@UpdateOrderStatus($Transacao, GetModuleVariable("checkout_psapi","debitado"));
					$query = "UPDATE [|PREFIX|]orders SET ordpayproviderid = '".$transacaoID."', ordpaymentstatus = 'Chargeback debitado' where orderid = '".$Transacao."'";
					@$GLOBALS['ISC_CLASS_DB']->Query($query);
				}elseif($status==9){
					@UpdateOrderStatus($Transacao, GetModuleVariable("checkout_psapi","contestacao"));
					$query = "UPDATE [|PREFIX|]orders SET ordpayproviderid = '".$transacaoID."', ordpaymentstatus = 'Em contestacao' where orderid = '".$Transacao."'";
					@$GLOBALS['ISC_CLASS_DB']->Query($query);
				}
				@http_response_code(200);
			}
		}else{
			@http_response_code(404);
		}
	}
	ipnPsApi();
}
?>