<?php
/**
 *
 * Copyright (C) Die Randgruppe GmbH
 *
 * http://www.randshop.com
 * http://www.dierandgruppe.com
 *
 * Unter der Lizenz von Die Randgruppe GmbH:
 * http://www.randshop.com/Lizenz
 *
 */

include_once('../../conf/config.inc.php');
include_once(DATEIPFAD . 'conf/db.inc.php');
include_once(DATEIPFAD . 'includes/functions.zahlungsschnittstellen.inc.php');
if(RECHNUNGSWESEN) {
    include_once(DATEIPFAD . "includes/functions.mod.bestellungen.inc.php");
} else {
    include_once(DATEIPFAD . "includes/functions.bestellungen.inc.php");
}

//ob_start();
//var_dump($_REQUEST);
//$dump = ob_get_clean();
//
//file_put_contents(DATEIPFAD . 'admin/data/logs/s2scb.log', $dump);
//
//ob_start();

$className = $_GET['paymentinterface'];

$classFile = DATEIPFAD . 'plugins/paymentInterfaces/' . $className . '/' . $className . '.cls.php';
if(!file_exists($classFile)) {
    exit("Error! Could not find Payment Interface Class " . $className);
}

include_once($classFile);

$reflect = new ReflectionClass($className);
$method = $reflect->getMethod('parseServerToServerCallback');
$zahlungsschnittstelle_bestell_id = $method->invoke(null, $_REQUEST);

//$zahlungsschnittstelle_bestell_id = {$className}::parseServerToServerCallback($_REQUEST);
$phpSessionID = GetPHPSessionIDFromZahlungsschnittstelleBestellID($className, $zahlungsschnittstelle_bestell_id);

if(!$phpSessionID) {
    exit("Error! Could not find Session.");
}

session_id($phpSessionID);
session_start();

if(!$_SESSION['paymentInterface']) {
    exit("Error! Payment Interface Object is missing from the session.");
}

$paymentInterface = $_SESSION['paymentInterface'];

$BestellObject = GetBestellenDetail(false, $_SESSION['sessionId']);
$WaehrungObject = GetWaehrungDetail();
$KundenObject = GetKundenDetail($_SESSION['mail'], '');

$order = createPaymentInterfaceOrderFromBestellObject($BestellObject, $WaehrungObject, $KundenObject);

$paymentResult = $paymentInterface->serverToServerCallback($order, $_REQUEST);

$ShopeinstellungObject = GetShopeinstellungDetail();
$ZahlungsartObject = GetZahlungsartDetail($BestellObject->zahlungsart_id);
$ZahlungsschnittstellenEinstellungen = GetEinstellungen('', 'zahlungsschnittstellen');

switch($paymentResult->result) {
    case PaymentResult::PAYMENT_COMPLETE:
        SavePaymentInterfaceFreiesFeld($BestellID, $paymentResult->freeField, $paymentResult->freeFieldName);
        SavePaymentInterfaceBestellParams($BestellID, $_SESSION['paymentInterfaceBestellParams']);
        $BestellID = $BestellObject->id;

        if (RECHNUNGSWESEN) {
            BestellStatusWechseln($BestellID, $ZahlungsartObject->bestellen_status_id, $ShopeinstellungObject, '');
        } else {
            BestellStatusWechseln($BestellID, BESTELLSTATUS_EINGEGANGEN, $ShopeinstellungObject, '');
        }
        break;
    case PaymentResult::PAYMENT_FAILED:
        // Bestellung bleibt in Unbestätigt
        break;
    case PaymentResult::PAYMENT_PENDING:
        SavePaymentInterfaceFreiesFeld($BestellID, $paymentResult->freeField, $paymentResult->freeFieldName);
        SavePaymentInterfaceBestellParams($BestellID, $_SESSION['paymentInterfaceBestellParams']);
        BestellStatusWechseln($BestellID, $ZahlungsschnittstellenEinstellungen->zahlungsschnittstellen->status_zahlung_ueberpruefung, $ShopeinstellungObject, '');
        break;
    default:
        exit('Internal Error! Invalid PaymentResult Code for Server to Server Callback.');
        break;
}

echo $paymentResult->serverToServerReply;

//$output = ob_get_clean();
//file_put_contents(DATEIPFAD . 'admin/data/logs/s2scboutput.log', $output);
echo $output;