<?php
/*
 * ##################################################################
 * Created on: Jul 28, 2005
 * File name:  nochex.php 
 * ##################################################################
 * This code is a sole property of netenberg.com 
 * ##################################################################
 */
$name       = "NOCHEX";
$nochex     = array (
            array ("Email"  , "nc_email"), 
            array ("Active" , "active", "No", "Yes"), 
            array ("Title"  , "title")
            );
$send_method= "GET";
$pay        = new nochex($demo_mode);
/*
 * Class to do all nochex
 * nochex Version 1.0
 */
class nochex
{
	/*
	 * Constructor
	 */
	function nochex($demo_mode= 0)
	{
		$this->demo_mode  = $demo_mode;
		$this->pay_url    = "https://www.nochex.com/nochex.dll/checkout";
	}
	/*
	 * Function to send variables
	 */
	function sendVariables($path_url, $pp_vals)
	{
		$this->_POST1                         = array ();
		$this->_POST1['email']                = $pp_vals['nc_email'];
		$this->_POST1['amount']               = number_format($_POST['gross_amount'],2);
		$this->_POST1['email_address_sender'] = $_POST['email'];
		$this->_POST1['description']          = $_POST['desc'];
		$this->_POST1['responderurl']         = $path_url."/ipn.php";
        $this->_POST1['returnurl']            = $path_url."/OK.php";
		$this->_POST1['item_number']          = time().rand(0, 1000);
		if (isset ($_POST['force_inv_no']))
        {
			$this->_POST1['item_number'] = $_POST['force_inv_no'];
        }
		$this->_POST1['ordernumber']          = $this->_POST1['item_number'];
	}
	/*
	 * Function to post back
	 */
	function postBack()
	{
		foreach ($_POST as $key => $value)
		{
			$value= urlencode(stripslashes($value));
			$req .= "&$key=$value";
		}
		$errno   = "";
		$errstr  = "";
		$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
		$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$header .= "Content-Length: ".strlen($req)."\r\n\r\n";
		$fp      = fsockopen('https://www.nochex.com/nochex.dll/apc/apc', 80, $errno, $errstr, 30);
		if (!$fp)
		{
			// HTTP ERROR
		}
		else
		{
			fputs($fp, $header.$req);
			while (!feof($fp))
			{
				$res = fgets($fp, 1024);
				if (strcmp($res, "AUTHORISED") == 0)
                {
					return true;
                }
				else
                {
					if (strcmp($res, "DECLINED") == 0)
                    {
						return false;
                    }
                }
			}
			fclose($fp);
		}
	}
	/*
	 * IPN=Internet Payment Notifier
	 */
	function ipn(& $BL)
	{
		$this->item_number    = $_POST['ordernumber'];
		$this->transaction_id = $_POST['transaction_id'];
        $this->payment_status = "OK";
		if (!empty ($this->item_number) && $this->postBack())
		{
			$BL->processTransaction($this->item_number, $this->transaction_id);
			return true;
		}
		return false;
	}
}
?>