<?php

if(isset($_REQUEST['payment_method']) && $_REQUEST['payment_method'] === 'postfinance') {

if (!defined("ROOT_PATH"))
{
    define("ROOT_PATH", dirname(__FILE__) . '/');
}
require ROOT_PATH . 'app/config/options.inc.php';

$baseUrl = PJ_INSTALL_URL."index.php?controller=pjFrontEnd&action=pjActionConfirm";

?>

<script>
    // Get transaction ID from localStorage
    const transactionId = localStorage.getItem('postfinance_transaction_id');
    const payment_method = 'postfinance';
    if (transactionId) {
        console.log(transactionId, 'transactionId');

               const redirectUrl = "<?php echo $baseUrl; ?>" + "&txn_id=" + transactionId + "&payment_method=" + payment_method;
      
        // Redirect to URL with transaction ID
        window.location.href = redirectUrl;
    }
</script>
<?php } ?>

<?php

if (!defined("ROOT_PATH"))
{
    define("ROOT_PATH", dirname(__FILE__) . '/');
}
require ROOT_PATH . 'app/config/options.inc.php';

$http_referer = isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])? $_SERVER['HTTP_REFERER']: gethostbyaddr($_SERVER['REMOTE_ADDR']);

$opts = array('http' => array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'follow_location' => 1,
    'content' => http_build_query($_REQUEST + array('pj_http_referer' => $http_referer))
));
$context = stream_context_create($opts);

$url = file_get_contents(PJ_INSTALL_URL."index.php?controller=pjFrontEnd&action=pjActionConfirm", false, $context);

if(!empty($url))
{
    if (strstr($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS'))
    {
        echo '<html><head><title></title><script type="text/javascript">window.location.href="'.$url.'";</script></head><body></body></html>';
    } else {
        header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']);
        header("Access-Control-Allow-Credentials: true");
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
        header("Access-Control-Allow-Headers: Origin, X-Requested-With");
        header('P3P: CP="ALL DSP COR CUR ADM TAI OUR IND COM NAV INT"');
        $install_url = parse_url(PJ_INSTALL_URL);
        if($install_url['scheme'] == 'https'){
            header('Set-Cookie: '.session_name().'='.session_id().'; SameSite=None; Secure');
        }
        header("Location: $url", true, 303);
    }
}
?>