<?
/* Netpay-Democode fuer XML-Interface
   Modul:	  backref, Auswertung der Netpay-Rueckgabedaten
   Author:        Ludwig Ertl  (ludwig.ertl@softwerker.at)
*/
include( 'includes/application_top.php');

function EPSHeader()
{
      return "<?xml version=\"1.0\" encoding=\"UTF-8\"  standalone=\"yes\"?>".
        "<epsp:EpsProtocolDetails ".
        "xmlns:epsp=\"http://www.stuzza.at/namespaces/eps/protocol/2011/11\" ".
        "xmlns:atrul=\"http://www.stuzza.at/namespaces/eps/austrianrules/2011/11\" ".
        "xmlns:epi=\"http://www.stuzza.at/namespaces/eps/epi/2011/11\" ".
        "xmlns:eps=\"http://www.stuzza.at/namespaces/eps/payment/2011/11\" ".
        "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ".
        "xsi:schemaLocation=\"http://www.stuzza.at/namespaces/eps/protocol/2011/11 ".
        "http://www.eps.or.at/eps/protocol/20111111/EPSProtocol-V24.xsd\" SessionLanguage=\"DE\">";
}

// PHP 5.2.2 bugfix
if (!isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = file_get_contents('php://input');

$xmltext = $GLOBALS["HTTP_RAW_POST_DATA"]; 	// String vom Server
if (!$xmltext)  // wenn raw nichts existiert, dann einen normalen Post einlesen
{
  while(list($key, $val) = each($HTTP_POST_VARS))
  {
   $key = stripslashes($key);
   $val = stripslashes($val);
   $key = rawurldecode($key);

 // bloederweise wird bei HTTP_POST_VARS aus <?xml version  xml_version weil Leerzeichen konvertiert werden => leerzeichen wiederherstellen
   if (($i=strpos($key, 'xml_',0))> 0) $key=str_replace('xml_', 'xml ', $key);
   $val = rawurldecode($val);
   $xmltext.= "$key=$val";
  }
}

$xmltext=utf8_decode($xmltext);

// Parser initialisieren zum Parsen, handler initialisieren
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "backref_startElement", "backref_endElement");
xml_set_character_data_handler($xml_parser, "backref_characterData");
$antwort=EPSHeader()."<epsp:ShopResponseDetails>";


// Parsen
if (!xml_parse($xml_parser, $xmltext)) {
  // TODO: Handle XML parse error if you want to.
  die ('Invalid XML stream');
}
if (isset($vitality)) $antwort=$xmltext; else $antwort.="</eps:ShopConfirmationDetails></epsp:ShopResponseDetails></epsp:EpsProtocolDetails>";

echo utf8_encode($antwort);
xml_parser_free($xml_parser);

// Parse-Funktionen
function backref_characterData($parser, $data) {
    global $currentTag, $antwort, $prefid;

    switch (strtoupper($currentTag)) {
	case "SESSIONID": $antwort.=$data; break;
	case "STATUSCODE":
            xtc_db_query("INSERT INTO payment_eps2 (eps2_TRID, eps2_RETURNSTATUS, eps2_DATE) VALUES ('".$_REQUEST['trid']."', '$data', NOW())");
	    $antwort.="<eps:StatusCode>$data</eps:StatusCode>$prefid";
	    break;
	case "PAYMENTREFERENCEIDENTIFIER":
	    $prefid.=$data;
	    break;
	default:
	    $GLOBALS[$currentTag]=$data;
    }
}

function backref_startElement($parser, $name, $attrs) { 
    global $currentTag, $vitality, $prefid, $antwort;

    list($prefix, $currentTag)=split(":", $name, 2);
    if (!$currentTag) $currentTag=$name;
    switch (strtoupper($currentTag)) {
	case "SESSIONID": $antwort.="<epsp:SessionId>"; break;
	case "VITALITYCHECKDETAILS": $vitality=1; break;
	case "PAYMENTREFERENCEIDENTIFIER": $prefid="<eps:PaymentReferenceIdentifier>"; break;
    }
}

function backref_endElement($parser, $name) { 
    global $prefid, $antwort, $currentTag;
    
    switch (strtoupper($currentTag)) {
	case "PAYMENTREFERENCEIDENTIFIER": $prefid.="</eps:PaymentReferenceIdentifier>"; break;
	case "SESSIONID": $antwort.="</epsp:SessionId><eps:ShopConfirmationDetails>";
    }
    $currentTag="";
}

?>