PayPal on Python

PayPal provides a REST-based interface to their Website Payments payment services, which they refer to as a "Name-Value Pair" interface, or NVP for short.

PayPal's interface is documented here as HTML or here as PDF.

I created a Python interface to this API for my internal use. I am no longer using it internally (since I don't currently require Website Payments), but I decided to release the code to the public.

API calls can be made using simple direct method calls, with parameters passed as keyword arguments. To aid development and testing, all parameters are validated locally before sending to the PayPal server.

The Interface

Usage is straightforward. Simply instantiate an object of the PayPal class, passing to the constructor your username, password, signature (provided when you register at PayPal), and optionally the API access URL (default is PayPal's standard URL for signature based authentication).

Then you can use this object to make API calls, as shown below. The response from PayPal is encoded as a python dict.

def testPayPal(ccNum):
       import paypal
       pp = paypal.PayPal(PAYPAL_TEST_USERNAME, PAYPAL_TEST_PASSWORD,
                          PAYPAL_TEST_SIGNATURE, PAYPAL_TEST_SIG_URL)
       resp = pp.DoDirectPayment(paymentaction='Sale',
                                 ipaddress='192.168.1.1',
                                 creditcardtype='Visa',
                                 acct='12345...',
                                 expdate=paypal.ShortDate(2018, 4),
                                 cvv2='111',
                                 salutation='Mr',
                                 firstname='Homer',
                                 middlename='J',
                                 lastname='Simpson',
                                 street='742 Evergreen Terrace.',
                                 city='Springfield',
                                 state='CA',
                                 countrycode='US',
                                 zip='94131',
                                 phonenum='123-456-7890',
                                 amt='100.00',
                                 currencycode='USD')
       logging.info('testPayPal: got response %s' % resp)

Additional definitions:

The following additional items are defined:

creditCardTypeFromNumber(numString)

Takes a credit card number as a string, and tries to determine what type of credit card it is. Returns a string: "Visa", "MasterCard", "Amex", or "Discover". If it cannot determine the card type, returns None.

class ShortDate

Represents a short date - just a year and month. Use it like:

x = ShortDate(2009, 11)

class PayPalException:

Raised when a parameter does not validate or an error is returned from PayPal.

Limitations

Payment amount validation currently only handles U.S. currency. To work around this, you can disable payment amount validation by setting the module-level variable paypal.SKIP_AMT_VALIDATION to True after importing the module but before calling the API function.

License

I am releasing PayPal on Python under the GNU General Public License (GPL), which means you can freely use it and distribute it (and even sell it), but if you make changes or improvements ("derivative works," as they call it) you must release the source code under the same license. You can accomplish this by sending me patches, which I will post here. If there is enough interest, I'll put the project on Sourceforge or Github.

Note that I had released a previous version under the less-permissive Affero Public License, but there were some concerns about what was a derivative work or not, so I have gone to the more standard GPL. Rest assured, you don't have to open source your entire server source code if you use this.

I believe this is a fair license for most users, but if you would like a version under a different license (a commercial one, for instance), feel free to use my contact form to get in touch.

Disclaimer

With a project of this sort, where money is involved, it is important to reiterate that this code is provided with absolutely no warranty. I tested the code and it worked for my needs on a live site with real transactions, but not every API call was used. Therefore, I recommend testing thoroughly before deploying on a site with real money involved.

Download

For now, I am hosting the file on my own server. If there is interest, I will put it up on Github or a similar site.

If there are any questions or feedback, please don't hesitate to use my contact form.