<?php

//require ROOTDIR . "/init.php ";
require ROOTDIR . "/includes/orderfunctions.php";

require ROOTDIR . "/includes/invoicefunctions.php";
require ROOTDIR . "/includes/processinvoices.php";
require ROOTDIR . "/includes/gatewayfunctions.php";
//require ROOTDIR . "/includes/fraudfunctions.php";
require ROOTDIR . "/includes/modulefunctions.php";
require ROOTDIR . "/includes/ccfunctions.php";
;
$orderid = $_POST['orderid'];
$invoiceid = $_POST['invoiceid'];
$userid = $_POST['clientid'];

$total = 0;
if ($invoiceid) {
	$result = select_query("tblinvoices", "id,total,paymentmethod,status", array("userid" => $userid, "id" => $invoiceid));
	$data = mysql_fetch_array($result);
	 

	$invoiceid = $data['id'];
	$total = $data['total'];
	$paymentmethod = $data['paymentmethod'];
	$status = $data['status'];
	if (!$invoiceid) {
		exit("Invalid Invoice ID");
	}

}

	$result = select_query("tblhosting", "tblhosting.id,tblproducts.servertype", array("tblhosting.orderid" => $orderid, "tblhosting.domainstatus" => "Pending", "tblproducts.autosetup" => "order"), "", "", "", "tblproducts ON tblproducts.id=tblhosting.packageid");
	while ($data = mysql_fetch_array($result)) {
		$id = $data['id'];
		$servertype = $data['servertype'];
		
		if (getNewClientAutoProvisionStatus($userid)) {
			logActivity("Running Module Create on Order");
			if (!isValidforPath($servertype)) {
				exit("Invalid Server Module Name");
			}
			include_once ROOTDIR . ("/modules/servers/" . $servertype . "/" . $servertype . ".php");
			$moduleresult = ServerCreateAccount($id);
			if ($moduleresult == "success") {
				sendMessage("defaultnewacc", $id);
			}
		}
	}
	
	

	loadGatewayModule($paymentmethod);

	if (($invoiceid && $status == "Unpaid") && function_exists($paymentmethod . "_orderformcheckout")) {
		$params = getGatewayVariables($paymentmethod, $invoiceid, $total);
		$captureresult = call_user_func($paymentmethod . "_orderformcheckout", $params);
		$gatewayname = get_query_val("tblpaymentgateways", "value", array("gateway" => $paymentmethod, "setting" => "name"));
		//logTransaction($gatewayname, $captureresult['rawdata'], ucfirst($captureresult['status']));
		if ($captureresult['status'] == "success") {
			addInvoicePayment($invoiceid, $captureresult['transid'], "", $captureresult['fee'], $paymentmethod);
			$_SESSION['orderdetails']['paymentcomplete'] = true;
			$status = "Paid";
		}
	}

	if ($invoiceid && $status == "Unpaid") {
		$gatewaytype = get_query_val("tblpaymentgateways", "value", array("gateway" => $paymentmethod, "setting" => "type"));
		if (!isValidforPath($paymentmethod)) {
			$apiresults['error'][] = "Invalid Payment Gateway Name";
		}

		$gatewaypath = ROOTDIR . "/modules/gateways/" . $paymentmethod . ".php";
		if (file_exists($gatewaypath)) {
			if ((!function_exists($paymentmethod . "_config") && !function_exists($paymentmethod . "_link")) && !function_exists($paymentmethod . "_capture")) {
				require_once $gatewaypath;
			}
		}
		if (($gatewaytype == "CC" || $gatewaytype == "OfflineCC") && ($CONFIG['AutoRedirectoInvoice'] == "on" || $CONFIG['AutoRedirectoInvoice'] == "gateway")) {
			if (function_exists($paymentmethod . "_nolocalcc")) {
			}
			else {
				$apiresults['redirect'] = 'creditcard';
			}
		}
		if ($CONFIG['AutoRedirectoInvoice'] == "on") {
			$apiresults['redirect'] = 'viewinvoice';
		}
		
		//$apiresults['config'] = $CONFIG['AutoRedirectoInvoice'];
		if ($CONFIG['AutoRedirectoInvoice'] == "gateway") {
			
			if (in_array($paymentmethod, array("mailin", "banktransfer"))) {
				$apiresults['redirect'] = 'viewinvoice';
			}else{
				$params = getGatewayVariables($paymentmethod, $invoiceid, $total);
				$paymentbutton = call_user_func($paymentmethod . "_link", $params);	
				$apiresults['paymentbutton'] = $paymentbutton;
			}
		}
	}