<?php
/*
 * ##################################################################
 * Created on: Jul 28, 2005
 * File name:  brpay.php 
 * ##################################################################
 * This code is a sole property of netenberg.com 
 * ##################################################################
 */
$name     = "BRpay";
$brpay = array (
                array ("Receiver Email" , "brpay_email_cobranca"), 
                array ("Active"         , "active", "No", "Yes"), 
                array ("Title"          , "title")
                );
$send_method = "POST";
$pay         = new brpay($demo_mode);
/*
 * Class to do all brpay
 * brpay Version 1.0
 */
class brpay
{
	/*
	 * Constructor
	 */
	function brpay($demo_mode= 0)
	{
		$this->demo_mode = $demo_mode;
		$this->pay_url   = "https://www.brpay.com.br/security/webpagamentos/webpagto.aspx";
	}
	/*
	 * Function to send variables
	 */
	function sendVariables($path_url, $pp_vals)
	{
		$this->_POST1 = array ();
		$this->_POST1['email_cobranca'] = $pp_vals['brpay_email_cobranca'];
        $this->_POST1['tipo']  = "CBR";
        $this->_POST1['moeda'] = "BRL";
        $this->_POST1['item_number']= time().rand(0, 1000);
        if (isset ($_POST['force_inv_no']))
        {
            $this->_POST1['item_number'] = $_POST['force_inv_no'];
        }
        $this->_POST1['item_id']    = $this->_POST1['item_number'];
		$this->_POST1['item_descr'] = $_POST['desc'];
        $this->_POST1['item_quant'] = 1;
		$this->_POST1['item_valor'] = number_format($_POST['gross_amount'],2);
        $this->_POST1['cliente_nome'] = $_POST['name'];
        $this->_POST1['cliente_cep']  = $_POST['zip'];
        $this->_POST1['cliente_email']= $_POST['email'];

		$this->_POST1['notifyurl']  = $path_url."/ipn.php";
        $this->_POST1['successurl'] = $path_url."/OK.php";
        $this->_POST1['cancelurl']  = $path_url."/NOK.php";

		$this->_POST1['payment_method'] = "brpay";
	}
	/*
	 * IPN=Internet Payment Notifier
	 */
	function ipn(& $BL)
	{
		$this->item_number    = $_POST['item_id'];
		$this->transaction_id = $_POST['batch'];
		$this->payment_status = $_POST['status'];
        
		if (!empty ($this->item_number) && isset($_POST['email_cobranca']))
		{
            if($this->payment_status == 0)
            {
                $_POST['skip_auto_creation'] = 1;
            }
			$BL->processTransaction($this->item_number, $this->transaction_id);
			return true;
		}
		return false;
	}
}
?>