IT:AD:PayPal:HowTo:Setup Post-Processing Page and Logic
Summary
With Website Payments Standard the client will be redirected to complete their purchase on PayPal.
You'll want them to complete their purchase, and if they click the Return to Merchant page, they'll get back to your website (ie AutoReturn) where they'll be presented with a summary of their purchase. Whether they AutoReturn or Not, your server should receive a message to complete the sale, send out a pin, whatever.
To cut to the caseThe summary of it is that you will have to implement all 3 parts:
- AutoReturn
- Payment Data Transfer (PDT)
- Instant Payment Notification (IPN)
Process
AutoReturn
The simplest: after the user pays, automatically (unless if purchasing by CC he'll always be presented with an transacted complete page that says “Return to Merchant” button. Hated by all…but it's a legal thingie that PaPal resues to change) redirected to a specified page on your website on which you can display some confirmation text.
A default can be setup under Menu/Profile/SellingPreferences/Website Payment Preferences
Under Profile/Selling Preferences/Website Payment Preferences Turn Auto Return On and enter the Url Save.
Can be overridden with a custom one embedded as Html Variable within the Buy button.
Will get posted back the following variables:
tx- Transaction IDst- Payment statusamt- Payment amountcc- Currency code
Eg:
http://www.yourdomain.com/Thanks.aspx?tx=[TransactionID]&st=[PaymentStatus]&amt=[PaymentAmount]&cc=[currenccode]
Payment Data Transfer (PDT)
But the vars above are not enough to work with… You need to use PDT to perform a callback to the server, in order to get more info.
Under Profile/Selling Preferences/Website Payment Preferences
Turn Auto Return On and enter the Url (same as above). Turn PaymentDataTransfer On, and you'll get a PDT Identity Token. Save.
After following these steps, you should get a PDT Identity Token that is needed for querying PayPal.
If you don't copy-paste the token after clicking Save, know that you can always see it in your Website Payment Preferences:
You'll now get
http://www.yourdomain.com/Thanks.aspx?tx=[TransactionID].
which can then use to POST to:
https://www.paypal.com/cgi-bin/webscr?cmd=_notify-synch&tx=[TransactionID]&at=[PDTIdentityToken]
and you'll get back:
SUCCESS first_name=Firstname last_name=Lastname payment_status=Completed payer_email=firstname%40lastname.com payment_gross=50.00 mc_currency=USD custom=Custom+value+you+passed+with+your+HTML+form etc.
In Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
authToken = WebConfigurationManager.AppSettings["PDTToken"];
//read in txn token from querystring
txToken = Request.QueryString.Get("tx");
query = string.Format("cmd=_notify-synch&tx={0}&at={1}",
txToken, authToken);
// Create the request back
string url = WebConfigurationManager.AppSettings["PayPalSubmitUrl"];
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
// Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = query.Length;
// Write the request back IPN strings
StreamWriter stOut = new StreamWriter(req.GetRequestStream(),
System.Text.Encoding.ASCII);
stOut.Write(query);
stOut.Close();
// Do the request to PayPal and get the response
StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
strResponse = stIn.ReadToEnd();
stIn.Close();
// sanity check
Label2.Text = strResponse;
// If response was SUCCESS, parse response string and output details
if (strResponse.StartsWith("SUCCESS"))
{
PDTHolder pdt = PDTHolder.Parse(strResponse);
Label1.Text =
string.Format("Thank you {0} {1} [{2}] for your payment of {3} {4}!",
pdt.PayerFirstName, pdt.PayerLastName,
pdt.PayerEmail, pdt.GrossTotal, pdt.Currency);
}
else
{
Label1.Text = "Oooops, something went wrong...";
}
}
}
Instant Payment Notification (IPN)
One big shortcoming of the use of PDT above is that it if the user closes the browser (and remember that if the buyer is paying by CC, he'll be shown a “Return to Merchant” page on PayPal that he might close) your website will receive no notification of the sale, and not complete any post-processing logic.
That's why you are advised to combine the PDT with IPN for any serious integration with PayPal.