Jump to content
Sign in to follow this  
  • entries
    149
  • comments
    510
  • views
    95067

php Paypal Auto-Return

Sign in to follow this  
noaktree

233 views

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


0 Comments


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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!