🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

php Paypal Auto-Return

posted in noaktree leaves
Published April 07, 2006
Advertisement
One of things that I've been working on is integrating my shopping cart with Paypal's Standard Checkout . It's fairly simple to build a form and send the customer on his or her way to the paypal checkout. But I want the user and the checkout data to be sent back to my site after the payment is finalized. Simple enough to get them back there. I log on to paypal and go to My Account > Profile > Website Payment Preferences. There I enable auto-return and also something called Payment Data Transfer. The return address isn't that important as it can be set in the original form as a hidden input.

Payment Data Transfer (PDT) will post a token associated with the transaction, and you will need to send this token back to PayPal in order to obtain the payment details associated with the transaction. It will send this token as a $_GET value in the URL. You will need to copy the Identity Token produced by paypal on the preferences page to use in your script.

So now the customer's browser is returned to your site after a successful payment. The paypal token can be found in $_GET["tx"]. I'm using CURL to POST three values back to paypal. The first is the token it just sent me (tx), the second is my personal identity token (at), and the third is a _notify-synch value telling paypal to synch (cmd). The code looks like this.

if(isset($_GET["tx"])){	// Your id token	$at = "[insert token here]";	// Setup the POST url (using sandbox for testing)	$URL="https://www.sandbox.paypal.com/cgi-bin/webscr"; 	// Ready the curl for POST	$ch = curl_init();	curl_setopt($ch, CURLOPT_URL, $URL); 	curl_setopt($ch, CURLOPT_POST, 1);	// Set the POST values to send tx, the id token, and cmd	curl_setopt($ch, CURLOPT_POSTFIELDS, "tx=".$_GET["tx"] . "&at=".$at." &cmd=_notify-synch");	// Begin the output buffer and POST the values to paypal	ob_start();			curl_exec ($ch);	curl_close ($ch);	// Get the contents of the buffer and clean up		$PDT = ob_get_contents();		ob_end_clean();	// ...}

Keep in mind that CURL will output the buffer to the page if this section of code is not within the body tags of HTML. So now we hopefully have all of the transaction values in one magnificent string. Next I needed to parse out the values so I wrote a function to do this.

// Parse the paypal string into an array with keys function parse_paypal_string_to_array($pp_string){	// Explode the cuurent string into parts divided by a line return	// These parts are variable assignments ex. item_name_1=Item+Name	$variable_parts = explode("\n", $pp_string);		// Check to see if paypal returned FAILED 	if($variable_parts[0] == "FAILED")		return "FAILED";				foreach ($variable_parts as $index => $val)	{		// Check that the variable isn't undefined		if($val{strlen($val)-1} != '=')		{	// Get the key and value			list($varkey, $varval) = explode("=", $val);			// Set to final array			$final_array[$varkey] = $varval;		}	}	return $final_array;}

This returns an array with appropriate keys and values. Now I can access returned data like so...

$pp_array = parse_paypal_string_to_array($PDT);
echo($pp_array["payer_email"]);

Paypal says that if the POST fails you should try a second time in case of a network problem. Using paypal's sandbox is a great way to test this stuff out.
Previous Entry Dead Tree
Next Entry My Desktop Idol
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement