Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

php Paypal Auto-Return

Sign in to follow this  


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.

// Your id token
$at = "[insert token here]";
// Setup the POST url (using sandbox for testing)
// 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
curl_exec ($ch);
curl_close ($ch);
// Get the contents of the buffer and clean up
$PDT = ob_get_contents();

// ...

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

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.
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!