Processing payments using Paypal, python SDK

1,353 views
Skip to first unread message

Robert Avram

unread,
Jan 2, 2014, 4:52:23 PM1/2/14
to google-a...@googlegroups.com
I'm trying to figure out how to process payments using paypal python sdk and google app engine.
the following code seems to work, but I can't figure out, based on the SDK, how to handle the exceptions (wrong credit card number etc).
I would like the customer to be able to see the error. I would appreciate any input! thanks!!!


class Buy(generichandler.MainHandler):

    def get(self):

       api = paypalrestsdk.set_config(

         mode="sandbox",

         client_id="myclientid",

         client_secret="secret")

       api.get_token()

       paymentDic = {"intent": "sale",

                     "payer": {

                               "payment_method": "credit_card",

                               "funding_instruments": [{

                                                        "credit_card": {

                                                                       "type": "visa",

                                                                       "number": "4417119669820331",

                                                                       "expire_month": "11",

                                                                       "expire_year": "2018",

                                                                       "cvv2": "874",

                                                                       "first_name": "Joe",

                                                                       "last_name": "Shopper",

                                                                       "billing_address": {

                                                                                             "line1": "52 N Main ST",

                                                                                             "city": "Johnstown",

                                                                                             "state": "OH",

                                                                                             "postal_code": "43210",

                                                                                             "country_code": "US" }}}]},

                     "transactions": [{

                                       "amount": {

                                                 "total": "1.47",

                                                 "currency": "USD",

                                                 "details":{

                                                           "subtotal": "1.41",

                                                           "tax": "0.03",

                                                           "shipping": "0.03"}},

                                       "description": "This is the payment transaction description." }]}

       payment = paypalrestsdk.Payment(paymentDic)

       payment.create()

Enter code here...


Vinny P

unread,
Jan 3, 2014, 8:47:52 PM1/3/14
to google-a...@googlegroups.com
On Thu, Jan 2, 2014 at 3:52 PM, Robert Avram <robert...@gmail.com> wrote:
I'm trying to figure out how to process payments using paypal python sdk and google app engine.
the following code seems to work, but I can't figure out, based on the SDK, how to handle the exceptions (wrong credit card number etc).
I would like the customer to be able to see the error. I would appreciate any input! thanks!
 

You can do this (at the bottom of your code):

if payment.create():
  # Preapproval successful, redirect the customer to the approval URL.
else:
  # Payment failed
  self.response.write(payment.error)

Payment.error holds the reason why the preapproval failed, but you may want to do some post-processing to clean up the error text and present a more user-friendly response.
 
 
-----------------
-Vinny P
Technology & Media Advisor
Chicago, IL

App Engine Code Samples: http://www.learntogoogleit.com

Doug Anderson

unread,
Jan 4, 2014, 2:51:13 PM1/4/14
to google-a...@googlegroups.com
My advice would be to use Stripe.com for processing payments.  You'll be amazed at how easy it is to get started AND customers don't have to leave your site!
(I'm not associated with Stripe in any way... just using it with App Engine and can attest to its ease of use)

I may re-add PayPal at some point (in addition to Stripe) but it's a pita to deal with the user leaving your site, processing the ipn notifications, and then HOPING the user hits the button to return to your site when he's finished on PayPal.  With PayPal there's no guarantee the user will return to your site (although it's fairly likely).  I've also noticed that the initial redirect to PayPal can take a while (for the page to load).  Just my experience... I'm sure there are others that really like it.

Jamie N.

unread,
Jan 5, 2014, 3:15:16 PM1/5/14
to google-a...@googlegroups.com
+1. I've been using Stripe in my GAE app for the past year and it's been a terrific experience. The API is clean and intuitive, and you can give your users a simple flow (either with the nice views that Stripe provides, or with your own custom UI). Unless you have to use Paypal, I'd start with Stripe.

Kaan Soral

unread,
Jan 6, 2014, 2:41:42 PM1/6/14
to google-a...@googlegroups.com
If you are inside US you probably didn't notice this, which is good for you, however there is a HUGE barrier around payment services

It's extremely hard to accept payments, you have to be a company at least and procedures after that are blurry

For example you can only use stripe from US/UK/Ireland - as far as I remember

Doug Anderson

unread,
Jan 6, 2014, 3:16:56 PM1/6/14
to google-a...@googlegroups.com
Stripe is working hard to expand their international support (add Canada to your list along with 8 other countries currently in beta).  But I would agree that PayPal has the best international/currency support.  With PayPal I can be located in the U.S. and accept payments in a variety of currencies (as long as I enable my account to allow it). But to my knowledge with PayPal you either have to transfer customers to the PayPal site for a portion of the processing OR deal with credit card numbers hitting your server and thus having to deal with PCI compliance.  I don't even know if PCI compliance is possible with App Engine so that may just leave option #1 (transfering customers to PayPal for a portion of the processing... a perfectly viable option... just less than ideal imo)

Kaan Soral

unread,
Jan 6, 2014, 3:26:46 PM1/6/14
to google-a...@googlegroups.com
I was set on Braintree last time I assessed the market, currently deciding on how to incorporate locally, Braintree seems to have the widest coverage for a simplistic/elegant solution similar to Stripe

I think Paypal is a major turn off from many aspects, I would use a layer similar to Paymentwall to handle all-else payments other than credit cards

Spreedly is also extremely promising, it's not a payment gateway but rather a PCI workaround that enables one to switch from one provider to another or use multiple providers etc

(Paypal/Braintree acquisition was alarming, I hope they don't mess braintree too :)

A beautiful reference for anyone who are interested: https://spreedly.com/gateways

Andreas Schmid

unread,
Jan 6, 2014, 3:47:02 PM1/6/14
to google-a...@googlegroups.com
paypal has a rest api and you don’t need to redirect the customers to paypal. but paypal is a pain to deal with in any case… and customer service is very bad.
i believe you need to be PCI compliant only if you store the credit card data, not if you just process the api call on server side.

another great service is www.braintreepayments.com which works all over the world.

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To post to this group, send email to google-a...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-appengine.
For more options, visit https://groups.google.com/groups/opt_out.

Kaan Soral

unread,
Jan 6, 2014, 5:24:55 PM1/6/14
to google-a...@googlegroups.com
should be that way but the page and request has to be https/secure as far as I remember

Robert Avram

unread,
Jan 7, 2014, 3:57:58 PM1/7/14
to google-a...@googlegroups.com
beautiful! Thanks! You're awesome. Do you know if the SDK is threadsafe though?

Robert Avram

unread,
Jan 7, 2014, 4:06:52 PM1/7/14
to google-a...@googlegroups.com
I checked out Stripe.com.. It seems pretty good. Paypal would still be my personal choice so far. It has a UI for the business to track their sales, etc also the customer that I'm developing for is a big fan of established names. Thanks for the input though , I'll definitely keep it in mind for future projects.

Doug Anderson

unread,
Jan 7, 2014, 6:23:39 PM1/7/14
to google-a...@googlegroups.com
The paypal rest api involves receiving credit cards on the server which means they are potentially in logs etc (that with App Engine you don't control).  This complicates PCI compliance:

Q: To whom does PCI apply?
A: PCI applies to ALL organizations or merchants, regardless of size or number of transactions, that accepts, transmits or stores any cardholder data. Said another way, if any customer of that organization ever pays the merchant directly using a credit card or debit card, then the PCI DSS requirements apply.

Q: What is defined as ‘cardholder data’?
A: Cardholder data is any personally identifiable data associated with a cardholder. This could be an account number, expiration date, name, address, social security number, etc. All personally identifiable information associated with the cardholder that is stored, processed, or transmitted is also considered cardholder data.


If someone does achieve PCI compliance with the PayPal rest api and App Engine please document the process so others can benefit from your experience.  I personally believe you're better off redirecting to PayPal and back than using PayPal rest.  Enough said on my part... good luck

Vinny P

unread,
Jan 7, 2014, 9:25:19 PM1/7/14
to google-a...@googlegroups.com
On Tue, Jan 7, 2014 at 2:57 PM, Robert Avram <robert...@gmail.com> wrote:
beautiful! Thanks! You're awesome. Do you know if the SDK is threadsafe though?

On Friday, January 3, 2014 5:47:52 PM UTC-8, Vinny P wrote:
You can do this (at the bottom of your code):
if payment.create():
  # Preapproval successful, redirect the customer to the approval URL.


No problem! Glad to hear it's fixed.

Unfortunately, the SDK is not thread safe. 

james maringa

unread,
Mar 18, 2014, 8:52:48 AM3/18/14
to google-a...@googlegroups.com
How did you add the sdk to the app engine project? It's been giving me trouble

Robert Avram

unread,
Mar 18, 2014, 5:18:57 PM3/18/14
to google-a...@googlegroups.com
Appengine allows you to add your own packages... 
I basically just downloaded the paypalrstsdk package (folder) from  https://github.com/paypal/rest-api-sdk-python and put it straight into my appengine project folder.

from there on all you need to do is say: 
"import paypalrestsdk" in whatever module you're using to handle your transactions.


since we're here though... does anyone know if the "PCI compliance" issue is a valid one using this SDK in appengine? I looked into it for a while but honestly I can't see an issue as long as you're transmitting the data straight to paypal and never saving the info.
Can anyone expand on this issue?

Vinny P

unread,
Mar 19, 2014, 1:06:54 AM3/19/14
to google-a...@googlegroups.com
You need to copy the SDK files into your application's folder. See the explanation here: http://stackoverflow.com/a/14851686 
 
 
-----------------
-Vinny P
Technology & Media Advisor
Chicago, IL

App Engine Code Samples: http://www.learntogoogleit.com
 
 


Reply all
Reply to author
Forward
Message has been deleted
0 new messages