<?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, webURL 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];
	$webaddress= $nt[webURL];	//to use in comment field
}
if($payflow_test_mode=="0"){
	$live_mode = 'TRUE';
} else {
	$live_mode = 'FALSE';
}

$ref_id=$_POST['ref_code'];
 if($live_mode=='TRUE'){
	//define('_PAYPAL_LIVE', TRUE); #toggle to TRUE to activate live charges
	$endpoint = "https://payflowpro.paypal.com";
} else {
	//define('_PAYPAL_LIVE', FALSE); #toggle to FALSE to activate test mode
	$endpoint = "https://pilot-payflowpro.paypal.com";
}


//echo "Endpoint:<br/>";
//echo $endpoint . "<br/>";

$cred_str = "PARTNER=" . "$payflow_partner"

   . "&VENDOR=" . "$payflow_user"

   . "&USER=" . "$payflow_user"

   . "&PWD=" . "$payflow_password";

$nvp_str = "&TRXTYPE=S"

   . "&VERBOSITY=High"

   . "&TENDER=C"

   . "&AMT=$tipamount"

   . "&ORIGID=$ref_id"

   . "&CURRENCY=USD"
   
   . "&COMMENT1=$webaddress"
   
   . "&COMMENT2=Customer approved tip";

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;
} 

//echo "<br/>Request:<br/>";

//echo $nvp_str . "<br/>";

  

//combine the two strings and make the API Call

$req_str = $cred_str . $nvp_str;

 

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $endpoint);

curl_setopt($ch, CURLOPT_VERBOSE, 1);

 

// turn off server and peer verification(TrustManager Concept).

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, 1);

 

// set the NVP $req_str as POST FIELD to curl

curl_setopt($ch, CURLOPT_POSTFIELDS, $req_str);

 

$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;
	
//echo "<br/>Response:<br/>";
//echo $nvpResArray['RESPMSG'];

if($nvpResArray['RESPMSG'] == 'Approved'){
	echo "<center><font color='red'>Transaction completed successfully.<br>";
	echo "Reference#: ". $nvpResArray['PNREF'] ."<br></font></center>";
	$approved=1;
	$cc_ref_code = $nvpResArray['PNREF'];
} else {
	echo "<center><font color='red'>Card Declined. Please confirm that you have your information entered correctly and try again.<br>";
	echo "Error: ". $nvpResArray['RESPMSG'] ."<br></font></center>";
	$approved=0;
}
?>