<?php

$NETCAT_FOLDER = realpath(dirname(__FILE__) . "/../../../") . "/";
require_once($NETCAT_FOLDER . "vars.inc.php");
require($INCLUDE_FOLDER . "index.php");
require_once("function.inc.php");

/** @var nc_input $input */
$input = nc_core('input');

$invoice_id = $input->fetch_post('invoice_id');
$payment_system_name = $input->fetch_post('payment_system');

try {
    $invoice = new nc_payment_invoice($invoice_id);

    if ($invoice->get('status') === nc_payment_invoice::STATUS_SUCCESS) {
        die(NETCAT_MODULE_PAYMENT_ERROR_ALREADY_PAID);
    }

    if ($invoice->get('customer_id')) {
        if (!$AUTH_USER_ID) {
            die(NETCAT_MODULE_PAYMENT_ERROR_AUTH_REQUIRED);
        }

        if ($AUTH_USER_ID != $invoice->get('customer_id')) {
            die(NETCAT_MODULE_PAYMENT_ERROR_WRONG_USER);
        }
    }

    $payment_system = nc_payment_factory::create($payment_system_name);
    $params = array();

    foreach ((array)$input->fetch_post() as $key => $value) {
        if (strpos($key, 'param_') === 0) {
            $key = substr($key, strlen('param_'));
            $params[$key] = $value;
        }
    }

    $payment_system->set_request_parameters($params);
    $payment_system->process_payment_request($invoice);
}
catch (Exception $e) {
    echo $e->getMessage();

    if (isset($perm) && is_object($perm) && $perm->isCatalogueAdmin($catalogue) && isset($payment_system)) {
        echo "<ul><li>" . join("</li>\n<li>", $payment_system->get_errors()) . "</li></ul>";
        $payment_system_id = $payment_system->get_id();
        echo "<p><a href='" . nc_core('ADMIN_PATH') . "#module.payment($catalogue,$payment_system_id)' target='_blank'>",
             NETCAT_MODULE_PAYMENT_SETTINGS_LINK,
             "</a></p>";
    }
}