<?php
//error_reporting(E_ALL);
$host = 'localhost'; 
$username = 'restaura_taste';
include('../custom/db1.php');
$password = 'Daughters2'; 
include('mysql_connect.php');

$query = "select payflow_user, payflow_password, payflow_partner, payflow_test_mode from storePrefs";
$rt=mysql_query($query);          // query executed 
//echo mysql_error(); 
while($nt=mysql_fetch_array($rt)){
	$payflow_user = $nt[payflow_user];
	$payflow_password = $nt[payflow_password];
	$payflow_partner = $nt[payflow_partner];
	$payflow_test_mode = $nt[payflow_test_mode];
}
if($payflow_test_mode=="0"){
	$live_mode = 'TRUE';
} else {
	$live_mode = 'FALSE';
}
//echo "user: " . $payflow_user . " <br>";
//echo "pwd: " . $payflow_password . " <br>";
//echo "partner: " . $payflow_partner . " <br>";
//echo "test: " . $payflow_test_mode . " <br>";

if($live_mode=='TRUE'){
	define('_PAYPAL_LIVE', TRUE); #toggle to TRUE to activate live charges
} else {
	define('_PAYPAL_LIVE', FALSE); #toggle to FALSE to activate test mode
}
/*
if ( _PAYPAL_LIVE ) {
	define('_API_ENDPOINT', 'https://payflowpro.paypal.com');
	define('_PAYPAL_URL', 'https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=');
} else {
	define('_API_ENDPOINT', 'https://pilot-payflowpro.paypal.com');
	define('_PAYPAL_URL', 'https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=');
}
*/
define('_API_ENDPOINT', 'https://pilot-payflowpro.paypal.com');
define('_PAYPAL_URL', 'https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=');
		
define('_API_USER', $payflow_user);
define('_API_PASSWORD', $payflow_password);
define('_API_VENDOR', $payflow_user);
define('_API_PARTNER', $payflow_partner);
define('_PROXY_HOST', '127.0.0.1');
define('_PROXY_PORT', '808');
define('_PROXY_LIVE', FALSE);


function paypal_DirectPayment($params = NULL) {



  #Construct the parameter string that describes the credit card payment
    $nvpStr = "&TENDER=C";

  switch ($params['paymentType']){
    case 'Sale':
      $nvpStr .= "&TRXTYPE=S";
      break;
    case 'Authorization':
      $nvpStr .= "&TRXTYPE=A";
      break;
    default:
      $nvpStr .= "&TRXTYPE=A";
      break;
  }

    # Other information
    //$nvpStr .= '&ACCT='.$params['ccNumber'];
  //$nvpStr .= '&CVV2='.$params['ccCvv2'];
  //$nvpStr .= '&EXPDATE='.$params['ccExpiration'];
  //$nvpStr .= '&ACCTTYPE='.$params['ccType'];
  $nvpStr .= '&AMT='.$params['AMT'];
  $nvpStr .= '&CURRENCY='.$params['currencyCode'];
  $nvpStr .="&ORIGID=".$params['ORIGID'];
    //$nvpStr .= '&COMMENT1='.$params['comment1'];
  //$nvpStr .= '&COMMENT2='.$params['comment2'];
    //$nvpStr .= '&FIRSTNAME='.$params['firstName'];
  //$nvpStr .= '&LASTNAME='.$params['lastName'];
  //$nvpStr .= '&STREET='.$params['street'];
  //$nvpStr .= '&CITY='.$params['city'];
  //$nvpStr .= '&STATE='.$params['state'];
  //$nvpStr .= '&ZIP='.$params['zip'];
  //$nvpStr .= '&COUNTRY='.$params['countryCode'];
    //$nvpStr .= '&CLIENTIP='.$_SERVER['HTTP_X_FORWARDED_FOR'];
  //$nvpStr .= '&ORDERDESC='.$params['description'];


    // Transaction results (especially values for declines and error conditions) returned by each PayPal-supported
    // processor vary in detail level and in format. The Payflow Verbosity parameter enables you to control the kind
    // and level of information you want returned.
    // By default, Verbosity is set to LOW. A LOW setting causes PayPal to normalize the transaction result values.
    // Normalizing the values limits them to a standardized set of values and simplifies the process of integrating
    // the Payflow SDK.
    // By setting Verbosity to MEDIUM, you can view the processor.s raw response values. This setting is more .verbose.
    // than the LOW setting in that it returns more detailed, processor-specific information.
    // Review the chapter in the Developer's Guides regarding VERBOSITY and the INQUIRY function for more details.
    // Set the transaction verbosity to MEDIUM.
    $nvpStr .= '&VERBOSITY=High';

    # The $unique_id field is storing our unique id that we'll use in the request id header.
    $uniqueID = date('ymd-H').rand(1000, 9999);
    /*'-------------------------------------------------------------------------------------------
     ' Make the call to Payflow to finalize payment
     ' If an error occured, show the resulting errors
     '-------------------------------------------------------------------------------------------
     */
    $resArray = paypal_hashCall($nvpStr, $uniqueID);
    return $resArray;
}



/**
 * This function will take NVPString and convert it to an Associative Array and it will decode the response.
 * It is usefull to search for a particular key and displaying arrays.
 * @nvpstr is NVPString.
 * @nvpArray is Associative Array.
 */
function paypal_deformatNVP($nvpStr) {
    $intial = 0;
    $nvpArray = array();
    while (strlen($nvpStr)) {

        # Postion of Key
        $keypos = strpos($nvpStr, '=');

        # Position of value
    $valuepos = strpos($nvpStr, '&') ? strpos($nvpStr, '&') : strlen($nvpStr);

        # Getting the Key and Value values and storing in a Associative Array
        $keyval = substr($nvpStr, $intial, $keypos);
        $valval = substr($nvpStr, $keypos + 1, $valuepos - $keypos - 1);

    # Decoding the respose
        $nvpArray[urldecode($keyval)] = urldecode($valval);
        $nvpStr = substr($nvpStr, $valuepos + 1, strlen($nvpStr));

    }
    return $nvpArray;
}



/**
 * Function to perform the API call to Payflow
 * @nvpStr is nvp string.
 * returns an associtive array containing the response from the server.
 */
function paypal_hashCall($nvpStr, $uniqueID) {

  # Set headers
    $headers[] = "Content-Type: text/namevalue";
    $headers[] = "Content-Length: ".strlen($nvpStr);

    # Set the server timeout value to 45, but notice below in the cURL section, the timeout
    # for cURL is set to 90 seconds.  Make sure the server timeout is less than the connection.
    $headers[] = "X-VPS-CLIENT-TIMEOUT: 45";
    $headers[] = "X-VPS-REQUEST-ID:".$uniqueID;

  # Set the host header
   /* if (_PAYPAL_LIVE) {
        $headers[] = "Host: payflowpro.paypal.com";
		$url = "payflowpro.paypal.com";
    } else {
		$headers[] = "Host: pilot-payflowpro.paypal.com";
		$url = "pilot-payflowpro.paypal.com";
    }
	*/
	$headers[] = "Host: pilot-payflowpro.paypal.com";
	$url = "pilot-payflowpro.paypal.com";
		
    # Set the curl parameters.
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, _API_ENDPOINT);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 90);
    curl_setopt($ch, CURLOPT_POST, 1);

    # Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php
    if ( _PROXY_LIVE ) {
    curl_setopt($ch, CURLOPT_PROXY, _PROXY_HOST.":"._PROXY_PORT);
    }

    # NVPRequest for submitting to server
    //$nvpreq = "USER="._API_USER.'&VENDOR='._API_VENDOR.'&PARTNER='._API_PARTNER.'&PWD='._API_PASSWORD.$nvpStr."&BUTTONSOURCE=".urlencode(_S_BN_CODE);
	$nvpreq = "USER="._API_USER.'&VENDOR='._API_VENDOR.'&PARTNER='._API_PARTNER.'&PWD='._API_PASSWORD.$nvpStr;
	echo "<br>NVPstring =".$nvpreq;
	
  # Set the nvpreq as POST FIELD to curl
    curl_setopt($ch,CURLOPT_POSTFIELDS, $nvpreq);

  # Get response from server
    $response = curl_exec($ch);

    # Convert NVPResponse to an Associative Array
    $nvpResArray = paypal_deformatNVP($response);
    $nvpReqArray = paypal_deformatNVP($nvpreq);

    if (curl_errno($ch)) {
        die( curl_errno($ch).curl_error($ch) );
    } else {
        curl_close($ch);
    }
    return $nvpResArray;
}



/**
 *
 * @return
 */
function paypal_generateCharacter() {
    $possible = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $char = substr($possible, mt_rand(0, strlen($possible) - 1), 1);
    return $char;
}

/**
 *
 * @return
 */
function paypal_generateGUID() {
    $GUID = generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter()."-";
    $GUID = $GUID.generateCharacter().generateCharacter().generateCharacter().generateCharacter()."-";
    $GUID = $GUID.generateCharacter().generateCharacter().generateCharacter().generateCharacter()."-";
    $GUID = $GUID.generateCharacter().generateCharacter().generateCharacter().generateCharacter()."-";
    $GUID = $GUID.generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter().generateCharacter();
    return $GUID;
}



?>

