Google App Engine & Paypal Redirect Payments

I gave a talk some time ago to the Google Users Group in Dublin about how to go about coding up the the integration of paypal redirect payments into a Google App Application with python, if anybody’s intersted the talk slides are here:


Hooking up GAE & Paypal.pdf


In summary its possible and not that difficult!


Google App Engine & Paypal Talk Slides

I very much enjoyed the GTUG Dublin meeting last night at Google HQ in Dublin. There were very interesting speakers and a nice pint was had afterwards in the schoolhouse! It was nice to meet everybody and have a good chin-wag.


I have uploaded the slides to my talk on integrating paypal and the google app engine, they can be downloaded from here.

Google App Engine & Paypal Talk

Hi, just a note to let you know that I will be giving a talk at the next Dublin Google Technology User Group (GTUG) meeting on how to go about hooking up Paypal payments to the Google App Engine (GAE). The meeting will be held next Tuesday the 28th at 6:30pm.


So if you feel up to being bored to within an inch of your life by some gory GAE & Paypal details then you now know where to go!


More details can be found on the Dublin GTUG page here:


I have uploaded the slides to my talk on integrating paypal and the google app engine, they can be downloaded from here




Paypal – Accept Credit Card Payments, ‘Paypal Account Optional’ not visible

Q: I want so setup my Paypal business account to accept credit card transactions and not just paypal payments, but my customers are not offered the ‘pay by credit card’ option and when I look for the “Paypal Account Optional” setting in Profile / Website Payments Preferences I can’t find it! What do I do?


A: Make sure that the email account associated with you paypal business account has been verified (also check that you bank account has been verified too.) if it is not verified the option to pay by credit card will not be available. Once everything is verified the Profile / Website Payments Preferences / Paypal Account Optional should be visible -make sure that this option is ‘On’ and your customers should now be given the option to pay by credit card!

Google App Engine – Paypal PDT Example

We have been slaving away in the cloud working on integrating paypal with a google app engine application for one our clients in Dublin. There really isn’t much information ‘out there’ on the subject of connecting to paypal from the GAE, so I have decided to write up some notes in the hope that they prove useful to others (and me when I forget all about it!)


I am going to start at the ‘end’ while the information is fresh in my head, when using ‘Paypal Payments Standard’ you can choose to get Paypal to pass you back transaction outcome information when it redirects back to your website after a transaction – they call this PDT. You can use this information to update the transaction/order status on your website (once you verify it).


Here is some more information on PDT:


With PDT enabled paypal adds some arguments to the return url, these arguments contain information about the transaction, however you shouldn’t really trust this info as the HTTP POST could have been spoofed by somebody else. So to check the transaction information you pull out the ‘tx’ argument and pass it back to paypal which will return a verification message that contains the transaction details.


So to verify a PDT transaction we must:


(a) Get the ‘txt’ argument from the paypal return request.
(b) Perform a HTTP POST back to paypal specifying ‘tx’
(c) Check the response from Paypal, check for ‘SUCCESS’, check ‘payment_status’ and possibly other transaction details.


In Python on the Google App Engine the python code could look like this:


[code lang=”python”]
import urllib
from google.appengine.api import urlfetch
import re
class paypal_pdt_handler(RequestHandler):
def get(self):
# Get the transaction id, tx=blahblahblah
trans_id = self.request.get(‘tx’)

# Confgure POST args
args = {}
# Specify the paypal command
args[‘cmd’] =’_notify-synch’
args[‘tx’] = trans_id
args[‘at’] = _identity_token

args = urllib.urlencode(args)

# Do a post back to validate
# the transaction data
status = urlfetch.fetch(url = _paypal_url,
method = urlfetch.POST,
payload = args).content
self.response.out.write(‘POST Failed’)

# Check for SUCCESS at the start of the response
if‘^SUCCESS’, status):
# Check other transaction details here like
# payment_status etc..
# Update order status


This is a slightly cut down example, in a real application you would need to do a bit more error handling etc. In this example the following variables need to be given values:


_paypal_url – The url to paypal, either sandbox or live.
_identity_token – Your “Identity Token”, see this post for info on how to find this in your paypal account:


NOTE: Another way to get information on transaction status is to handle Paypal IPN. I tend to use both methods as if you only use PDT and for some reason the user does not redirect back to your site, then your site won’t b able to check the transaction status whereas IPN messages will be delivered in all cases.

Paypal PDT, Fail Error: 4020

There are a few reasons that could cause paypal to send back:




When you attempt to verify PDT transaction data by posting a _notify-synch command.  However, in 9 times out of 10 the cause is due to the request containing an incorrect ‘at’ or authorisation token argument.


The ‘authorisation token’, or ‘Identity Token’ is a big long alphanumeric string, to find yours log into your paypal account, click on ‘Profile’, then click on ‘Website Payment Preferences’, scroll down to the ‘Payment Data Transfer’ section – you will see your ‘Identity Token’ at the end of this section.


This token must be included in your _notify-synch command as an argument named ‘at’.


Some more information on Paypal’s PTD can be found here: