Jump to content

Display Paypal Balance on Homepage


trappedatuf

Recommended Posts

  • Replies 116
  • Created
  • Last Reply

Top Posters In This Topic

Thought I'd drop the recent paypal transaction data into the bottom of the home page.

It would appear that the functionality is there to list recent paypal transactions.

However, not being a coder, I'm not sure how to call the function transSearch.

 

Anybody done this from the code above?

Link to comment
Share on other sites

Hey this is a great module however I could not get it to work.

 

I inserted the user/pass/signature

 

then pasted the code in the TPL but the balance shows $0.00.

 

I have a Business PayPal account, will this only work with a Paypal Website Payments Pro account?

 

cheers

 

(sorry this has been resolved, I had to change the Primary currency on my paypal account, thanks for the great module)

Link to comment
Share on other sites

Thought I'd drop the recent paypal transaction data into the bottom of the home page.

It would appear that the functionality is there to list recent paypal transactions.

However, not being a coder, I'm not sure how to call the function transSearch.

 

Anybody done this from the code above?

 

I did this, it's actually pretty simple. I also modified the code to fit the WHMCS layout.

 

Here's the new paypalBalance.php file:

<?php
/****************************************************
CallerService.php

This file uses the constants.php to get parameters needed 
to make an API call and calls the server.if you want use your
own credentials, you have to change the constants.php

Called by TransactionDetails.php, ReviewOrder.php, 
DoDirectPaymentReceipt.php and DoExpressCheckoutPayment.php.

****************************************************/

/**
 * hash_call: Function to perform the API call to PayPal using API signature
 * @methodName is name of API  method.
 * @nvpStr is nvp string.
 * returns an associtive array containing the response from the server.
*/

// FOR WHMCS WE WANT TO CALL THE CHECK_BALANCE FUNCTION SO IT GETS DISPLAYED!
require_once 'paypalConstants.php';
$API_UserName=API_USERNAME;
$API_Password=API_PASSWORD;
$API_Signature=API_SIGNATURE;
$API_Endpoint =API_ENDPOINT;
$version=VERSION;

//session_start();

//declaring of global variables
//global $API_UserName, $API_Password, $API_Signature, $API_Endpoint, $version, $nvp_Header;

//$sslcertpath = "C:/Apache/htdocs/cert_key_pemlive.txt";
$SOAPrequest = <<< End_Of_Quote
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<RequesterCredentials xmlns="urn:ebay:api:PayPalAPI" soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="1">
<ebl:Credentials xmlns:ebl="urn:ebay:apis:eBLBaseComponents">
<ebl:Username>$API_UserName</ebl:Username>
<ebl:Password>$API_Password</ebl:Password>
<ebl:Signature>$API_Signature</ebl:Signature>
</ebl:Credentials>
</RequesterCredentials>
</soapenv:Header>
<soapenv:Body>
<GetBalanceReq xmlns="urn:ebay:api:PayPalAPI">
<GetBalanceRequest>
<Version xmlns="urn:ebay:apis:eBLBaseComponents">2.3</Version>
</GetBalanceRequest>
</GetBalanceReq>
</soapenv:Body>
</soapenv:Envelope>
End_Of_Quote;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api-3t.paypal.com/2.0/");
//curl_setopt($ch, CURLOPT_SSLCERT, $sslcertpath);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $SOAPrequest);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // Without this then $response will just be a true or false, this causes $response to be the actual returned XML code which is what we want
$response = curl_exec ($ch);

preg_match('/([0-9]*\.[0-9]*)(<\/Balance>)/i', $response, $matches); // This pulls out the balance from the response: <Balance xsi:type="cc:BasicAmountType" currencyID="USD">16457.62</Balance>
$data['PAYPALBALANCE'] = "$".number_format($matches[0], 2);


function hash_call($methodName,$nvpStr)
{

//declaring of global variables
//global $API_Endpoint,$version,$API_UserName,$API_Password,$API_Signature,$nvp_Header;

require_once 'paypalConstants.php';
$API_UserName=API_USERNAME;
$API_Password=API_PASSWORD;
$API_Signature=API_SIGNATURE;
$API_Endpoint =API_ENDPOINT;
$version=VERSION;
session_start();

//setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$API_Endpoint);
curl_setopt($ch, CURLOPT_VERBOSE, 1);

//turning off the 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);
   //if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
  //Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php 
if(USE_PROXY)
curl_setopt ($ch, CURLOPT_PROXY, PROXY_HOST.":".PROXY_PORT); 

//NVPRequest for submitting to server
$nvpreq="METHOD=".urlencode($methodName)."&VERSION=".urlencode($version)."&PWD=".urlencode($API_Password)."&USER=".urlencode($API_UserName)."&SIGNATURE=".urlencode($API_Signature).$nvpStr;

//setting the nvpreq as POST FIELD to curl
curl_setopt($ch,CURLOPT_POSTFIELDS,$nvpreq);

//getting response from server
$response = curl_exec($ch);

//convrting NVPResponse to an Associative Array
$nvpResArray=deformatNVP($response);
$nvpReqArray=deformatNVP($nvpreq);
$_SESSION['nvpReqArray']=$nvpReqArray;

if (curl_errno($ch)) {
	// moving to display page to display curl errors
	  $_SESSION['curl_error_no']=curl_errno($ch) ;
	  $_SESSION['curl_error_msg']=curl_error($ch);
	  $location = "APIError.php";
	  header("Location: $location");
 } else {
	 //closing the curl
		curl_close($ch);
  }

return $nvpResArray;
}

/** 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 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 transSearch($startDateStr, $endDateStr, $transID) {
//session_unset(); // clearing the session before starting new API Call
//session_start();

/* Construct the request string that will be sent to PayPal.
  	The variable $nvpstr contains all the variables and is a
  	name value pair string with & as a delimiter */
$nvpStr;

$transactionID=urlencode($transID);

if(isset($startDateStr)) {
	$start_time = strtotime($startDateStr);
	$iso_start = date('Y-m-d\T00:00:00\Z',  $start_time);
	$nvpStr="&STARTDATE=$iso_start";
}

if(isset($endDateStr)&&$endDateStr!='') {
	$end_time = strtotime($endDateStr);
	$iso_end = date('Y-m-d\T24:00:00\Z', $end_time);
	$nvpStr.="&ENDDATE=$iso_end";    
}

if($transactionID!='') 
	$nvpStr=$nvpStr."&TRANSACTIONID=$transactionID";
	/* Make the API call to PayPal, using API signature.
  		The API response is stored in an associative array called $resArray */

	$resArray=hash_call("TransactionSearch",$nvpStr);

	/* Next, collect the API request in the associative array $reqArray
	as well to display back to the browser.
	Normally you wouldnt not need to do this, but its shown for testing */

	$reqArray=$_SESSION['nvpReqArray'];

	/* Display the API response back to the browser.
  		If the response from PayPal was a success, display the response parameters'
  		If the response was an error, display the errors received using APIError.php.
  		*/
	$ack = strtoupper($resArray["ACK"]);

	if($ack!="SUCCESS" && $ack!="SUCCESSWITHWARNING"){
		//$_SESSION['reshash']=$resArray;
		$location = "ppnvp/APIError.php";
		header("Location: $location");
	}

	?>
	<h1>Paypal Transactions Last 5 Days</h1>

	<p><table width=100% cellspacing=1 bgcolor="#cccccc">

	<?php //checking for Transaction ID in NVP response
	if(!isset($resArray["L_TRANSACTIONID0"])){
	?>
		<tr>
			<td colspan="6">
			No Transaction Selected
			</td>
		</tr>
	<?php 
	} else {
		$count=0;
		//counting num of  transaction IDs present in NVP response arrray.
		while (isset($resArray["L_TRANSACTIONID".$count])) 
			$count++; 
		?>	

			<tr bgcolor=#efefef style="text-align:center;font-weight:bold;">
				<td></td>
				<td>ID</td>
				<td>Time</td>
				<td>Status</td>
				<td>Payer Name</td>
				<td>Gross Amount</td>
				<td>Net Amount</td>
			</tr>

		<?php 

		$ID=0;
		while ($count>0) {
			$transactionID    = $resArray["L_TRANSACTIONID".$ID];
			$timeStamp = $resArray["L_TIMESTAMP".$ID];
			$tsarray = array("T","Z");
			$timeStamp = str_replace($tsarray, " ", $timeStamp);
			$payerName  = $resArray["L_NAME".$ID]; 
			$amount  = $resArray["L_AMT".$ID]; 
			$net  = $resArray["L_NETAMT".$ID]; 
			$status  = $resArray["L_STATUS".$ID]; 
			$count--; $ID++;

		?>
		<tr bgcolor="#ffffff">
	    	<td><?=$ID ?></td>
			<td><?=$transactionID ?></td>
			<td><?=$timeStamp?> <!--12/7/2005 9:57:58 AM--></td>
			<td><?=$status?></td>
			<td><?=$payerName?></td>
			<td>$<?=$amount?></td>
			<td>$<?=$net?></td>
		</tr>

		<?php
		}
	}

	?>
	</TABLE>
	<?

}

?>

 

Then in your homepage.tpl file just add:

 

{php}
$date = date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-5,date("Y")));
transSearch($date,'','');
{/php}

 

The paypalBalance.php file has to have been included already. I put the balance in the top area where it shows pending orders etc. and the transactions just above the to-do list. This code shows transactions from the last 5 days, I also have that coded as the h1 in the code I posted. Modify as needed.

Link to comment
Share on other sites

  • 1 month later...

Great mod, and good idea. Just curious why you chose to go with the SOAP method over the NVP ?

 

hightekhosting, there may be an extra value that has to be appended to the output to paypal which asks for AUD back instead of USD. I see a "ReturnAllCurrencies" value, but what it says is that by default PayPal will return the default currency.

 

Make sure AUD is the default currency on your account.

 

Cheers,

Link to comment
Share on other sites

  • 1 month later...
  • 2 weeks later...

This is pretty neat. Would be nice to list, say, the past 5-10 transactions new subscriptions (not just payments) instead of by days. When I first put 5 days up I had to scroll a long way to get to my main homepage modules (same for the past 24 hours). Time to investigate API search calls I can use. :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated