<?php

if (!defined('INCLUDED_AMEMBER_CONFIG')) 
    die("Direct access to this location is not allowed");

/*
*
*
*     Author: Alex Scott
*      Email: alex@cgi-central.net
*        Web: http://www.cgi-central.net
*    FileName $RCSfile$
*    Release: 3.1.8PRO ($Revision: 3601 $)
*
* Please direct bug reports,suggestions or feedback to the cgi-central forums.
* http://www.cgi-central.net/forum/
*
*/






class payment_paypoint extends amember_payment {
    var $title       = "Paypoint";
    var $description = "Credit card payment";
    var $fixed_price = 0;
    var $recurring   = 1;

    function do_payment($payment_id, $member_id, $product_id,
            $price, $begin_date, $expire_date, &$vars){

        global $config;
        global $db;
        $product = $db->get_product($product_id);
		print_r($product);

        $vars = array(
            'MERCHKEY' => $this->config['intinstid'],
            'AMT' => sprintf("%.2f",$price),
            'CURR' => $this->config['currency'],
            'INVOICE'        => $payment_id,
	    'DESC' => $product['title'],
	    'RECURRING' => $product['is_recurring']
        );
	$vars['RECURRING_FIELDS']='';
	$product['trial1_days']=strtoupper($product['trial1_days']);
	$product['expire_days']=strtoupper($product['expire_days']);
	if($product['trial1_days'])
		$vars.="
		<input type=\"hidden\" name=\"fltSchAmount1\" value=\"$product[trial1_price]\">
		<input type=\"hidden\" name=\"strSchPeriod1\" value=\"$product[trial1_days]\">
		<input type=\"hidden\" name=\"fltSchAmount2\" value=\"$product[price]\">
		<input type=\"hidden\" name=\"strSchPeriod2\" value=\"$product[expire_days]\">
		";
	else
		$vars.="
		<input type=\"hidden\" name=\"fltSchAmount1\" value=\"$product[price]\">
		<input type=\"hidden\" name=\"strSchPeriod1\" value=\"$product[expire_days]\">
		";				
	if($product['rebill_times'])
		$vars.="
		<input type=\"hidden\" name=\"intCancelAfter\" value=\"$product[rebill_times]\">
		";
        if ($this->config['testing'])
		$vars.="
		<input type=\"hidden\" name=\"intTestMode\" value=\"1\">
		";
	$t = &new_smarty();
	$t->assign('vars', $vars);
	$t->display(dirname(__FILE__) . '/paypoint.html');
    }
    
    function log_debug($vars){
        global $db;
        $s = "PAYPOINT DEBUG:<br />\n";
        foreach ($vars as $k=>$v)
            $s .= "[$k] => '$v'<br />\n";
        $db->log_error($s);
    }
	function message($vars,$message="")
	{
		if($message)
		{
			echo $message;
		}
		else
		{
			echo "Your transaction has been $vars[fres].<br />
			Result code - $vars[res]<br />
			Financial Result Code - $vars[fres]<br />";
			if($vars['err'])
				echo "Error - $vars[err]<br />";
		}
		exit;
	}

    function process_thanks(&$vars){
		global $db;
        $this->log_debug($vars);
		if($vars['res']!="OK" && $vars['fres']!="APPROVED")
		{
			$this->message($vars);
		}
		else
		{
			$err = $db->finish_waiting_payment($vars['payment_id'],'4cs',$vars['tran'],'', $vars);
			if ($err) $this->message($vars,$err);
		}
    }
    function handle_postback(&$vars){
		global $db;
        $this->log_debug($vars);
	}
    function init(){
        parent::init();
    }
}

$pl = & instantiate_plugin('payment', 'paypoint');
?>
