<?php
/*
 * ##################################################################
 * Created on: Jul 28, 2005
 * File name:  paypal.php 
 * ##################################################################
 * This code is a sole property of netenberg.com 
 * ##################################################################
 */
$name   = "PayPal";
$paypal = array (
            array ("Email"      , "paypal_email"), 
            array ("Currency"   , "paypal_currency", "USD", "GBP", "EUR", "CAD", "JPY", "AUD"), 
            array ("Active"     , "active", "No", "Yes"), 
            array ("Title"      , "title")
            );
$send_method = "POST";
$pay         = new paypal($demo_mode);
/*
 * Class to do all paypal
 * paypal Version 
 */
class paypal
{
	/*
	 * Constructor
	 */
	function paypal($demo_mode= 0)
	{
		$this->demo_mode = $demo_mode;
		$this->pay_url   = "https://www.paypal.com/cgi-bin/webscr";
		if ($this->demo_mode)
        {
			$this->pay_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
        }
	}
	/*
	 * Function to send variables
	 */
	function sendVariables($path_url, $pp_vals)
	{
		$this->_POST1                = array ();
		$this->_POST1['cmd']         = "_ext-enter";
		$this->_POST1['redirect_cmd']= "_xclick";
		$this->_POST1['business']    = $pp_vals['paypal_email'];
		$this->_POST1['currency_code']= $pp_vals['paypal_currency'];
		$this->_POST1['amount']      = number_format($_POST['gross_amount'],2);
        $this->_POST1['item_name']   = $_POST['friendly_desc'];
        if(empty($this->_POST1['item_name']))
        {
            $this->_POST1['item_name'] = $_POST['desc'];
        }
        
		$this->_POST1['item_number'] = time().rand(0, 1000);
		if (isset ($_POST['force_inv_no']))
        {
			$this->_POST1['item_number'] = $_POST['force_inv_no'];
        }
            
		$this->_POST1['notify_url']   = $path_url."/ipn.php";
		$this->_POST1['return']       = $path_url."/OK.php";
		$this->_POST1['cancel_return']= $path_url."/NOK.php";
        
		$this->_POST1['no_shipping']= 1;
		$this->_POST1['no_note']    = 1;
		$this->_POST1['rm']         = 2;
		$array_name                 = array ();
		$array_name                 = explode(' ', $_POST['name'], 2);
		$this->_POST1['first_name']= $array_name[0];
		$this->_POST1['last_name'] = $array_name[1];
		$this->_POST1['address1']  = $_POST['address'];
		$this->_POST1['city']      = $_POST['city'];
		$this->_POST1['state']     = $_POST['state'];
		$this->_POST1['zip']       = $_POST['zip'];
        $this->_POST1['country']   = $_POST['country'];
	}
	/*
	 * Function to post back
	 */
	function postBack()
	{
		$POSTString = "cmd=_notify-validate";
		foreach ($_POST as $key => $val)
		{
			if ($key != "cmd")
            {
				$POSTString .= "&".$key."=".urlencode($val);
            }
		}
		$Connection = curl_init();
		curl_setopt($Connection, CURLOPT_URL, $this->pay_url);
		curl_setopt($Connection, CURLOPT_POST, 1);
		curl_setopt($Connection, CURLOPT_POSTFIELDS, $POSTString);
		curl_setopt($Connection, CURLOPT_RETURNTRANSFER, 1);
		$Response   = curl_exec($Connection);
		curl_close($Connection);
		if (eregi("VERIFIED", $Response))
        {
			return 1;
        }
		else
        {
			return 0;
        }
	}
	/*
	 * IPN=Internet Payment Notifier
	 */
	function ipn(& $BL)
	{
		$this->item_number    = $_POST['item_number'];
		$this->transaction_id = $_POST['txn_id'];
		$this->payment_status = $_POST['payment_status'];
		if ($this->postBack() && !empty ($this->item_number) && !empty ($this->transaction_id) && $this->payment_status=="Completed")
		{
			$BL->processTransaction($this->item_number, $this->transaction_id);
			return true;
		}
		return false;
	}
}
?>