- Author:
- uswaretech
- Posted:
- November 11, 2008
- Language:
- Python
- Version:
- 1.0
- Score:
- 6 (after 6 ratings)
This is a snippet to use Paypal with python. This modifies Mike Atlas's geocept Paypal library. (Which was not working, for me at least) and adds recurring payments.
I would try to write an article explaining how to use this and update the snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | #!/usr/bin/env python
#Usage http://uswaretech.com/blog/2008/11/using-paypal-with-django/
# PayPal python NVP API wrapper class.
# This is a sample to help others get started on working
# with the PayPal NVP API in Python.
# This is not a complete reference! Be sure to understand
# what this class is doing before you try it on production servers!
# ...use at your own peril.
## see https://www.paypal.com/IntegrationCenter/ic_nvp.html
## and
## https://www.paypal.com/en_US/ebook/PP_NVPAPI_DeveloperGuide/index.html
## for more information.
# by Mike Atlas / LowSingle.com / MassWrestling.com, September 2007
# No License Expressed. Feel free to distribute, modify,
# and use in any open or closed source project without credit to the author
# Example usage: ===============
# paypal = PayPal()
# pp_token = paypal.SetExpressCheckout(100)
# express_token = paypal.GetExpressCheckoutDetails(pp_token)
# url= paypal.PAYPAL_URL + express_token
# HttpResponseRedirect(url) ## django specific http redirect call for payment
#Modified by Shabda
import urllib, md5, datetime
from cgi import parse_qs
class PayPal:
""" #PayPal utility class"""
signature_values = {}
API_ENDPOINT = ""
PAYPAL_URL = ""
def __init__(self):
## Sandbox values
self.signature_values = {
'USER' : '', # Edit this to your API user name
'PWD' : '', # Edit this to your API password
'SIGNATURE' : '', # edit this to your API signature
'VERSION' : '53.0',
}
self.API_ENDPOINT = 'https://api-3t.sandbox.paypal.com/nvp' # Sandbox URL, not production
self.PAYPAL_URL = 'https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token='
self.signature = urllib.urlencode(self.signature_values) + "&"
# API METHODS
def SetExpressCheckout(self, amount, return_url, cancel_url, **kwargs):
params = {
'METHOD' : "SetExpressCheckout",
'NOSHIPPING' : 1,
'PAYMENTACTION' : 'Authorization',
'RETURNURL' : return_url,
'CANCELURL' : cancel_url,
'AMT' : amount,
}
params.update(kwargs)
params_string = self.signature + urllib.urlencode(params)
response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
response_dict = parse_qs(response)
response_token = response_dict['TOKEN'][0]
return response_token
def GetExpressCheckoutDetails(self, token, return_all = False):
params = {
'METHOD' : "GetExpressCheckoutDetails",
'RETURNURL' : 'http://www.yoursite.com/returnurl', #edit this
'CANCELURL' : 'http://www.yoursite.com/cancelurl', #edit this
'TOKEN' : token,
}
params_string = self.signature + urllib.urlencode(params)
response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
response_dict = parse_qs(response)
if return_all:
return response_dict
try:
response_token = response_dict['TOKEN'][0]
except KeyError:
response_token = response_dict
return response_token
def DoExpressCheckoutPayment(self, token, payer_id, amt):
params = {
'METHOD' : "DoExpressCheckoutPayment",
'PAYMENTACTION' : 'Sale',
'RETURNURL' : 'http://www.yoursite.com/returnurl', #edit this
'CANCELURL' : 'http://www.yoursite.com/cancelurl', #edit this
'TOKEN' : token,
'AMT' : amt,
'PAYERID' : payer_id,
}
params_string = self.signature + urllib.urlencode(params)
response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
response_tokens = {}
for token in response.split('&'):
response_tokens[token.split("=")[0]] = token.split("=")[1]
for key in response_tokens.keys():
response_tokens[key] = urllib.unquote(response_tokens[key])
return response_tokens
def GetTransactionDetails(self, tx_id):
params = {
'METHOD' : "GetTransactionDetails",
'TRANSACTIONID' : tx_id,
}
params_string = self.signature + urllib.urlencode(params)
response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
response_tokens = {}
for token in response.split('&'):
response_tokens[token.split("=")[0]] = token.split("=")[1]
for key in response_tokens.keys():
response_tokens[key] = urllib.unquote(response_tokens[key])
return response_tokens
def MassPay(self, email, amt, note, email_subject):
unique_id = str(md5.new(str(datetime.datetime.now())).hexdigest())
params = {
'METHOD' : "MassPay",
'RECEIVERTYPE' : "EmailAddress",
'L_AMT0' : amt,
'CURRENCYCODE' : 'USD',
'L_EMAIL0' : email,
'L_UNIQUE0' : unique_id,
'L_NOTE0' : note,
'EMAILSUBJECT': email_subject,
}
params_string = self.signature + urllib.urlencode(params)
response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
response_tokens = {}
for token in response.split('&'):
response_tokens[token.split("=")[0]] = token.split("=")[1]
for key in response_tokens.keys():
response_tokens[key] = urllib.unquote(response_tokens[key])
response_tokens['unique_id'] = unique_id
return response_tokens
def DoDirectPayment(self, amt, ipaddress, acct, expdate, cvv2, firstname, lastname, cctype, street, city, state, zipcode):
params = {
'METHOD' : "DoDirectPayment",
'PAYMENTACTION' : 'Sale',
'AMT' : amt,
'IPADDRESS' : ipaddress,
'ACCT': acct,
'EXPDATE' : expdate,
'CVV2' : cvv2,
'FIRSTNAME' : firstname,
'LASTNAME': lastname,
'CREDITCARDTYPE': cctype,
'STREET': street,
'CITY': city,
'STATE': state,
'ZIP':zipcode,
'COUNTRY' : 'United States',
'COUNTRYCODE': 'US',
'RETURNURL' : 'http://www.yoursite.com/returnurl', #edit this
'CANCELURL' : 'http://www.yoursite.com/cancelurl', #edit this
'L_DESC0' : "Desc: ",
'L_NAME0' : "Name: ",
}
params_string = self.signature + urllib.urlencode(params)
response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
response_tokens = {}
for token in response.split('&'):
response_tokens[token.split("=")[0]] = token.split("=")[1]
for key in response_tokens.keys():
response_tokens[key] = urllib.unquote(response_tokens[key])
return response_tokens
def CreateRecurringPaymentsProfile(self, token, startdate, desc, period, freq, amt):
params = {
'METHOD': 'CreateRecurringPaymentsProfile',
'PROFILESTARTDATE': startdate,
'DESC':desc,
'BILLINGPERIOD':period,
'BILLINGFREQUENCY':freq,
'AMT':amt,
'TOKEN':token,
'CURRENCYCODE':'USD',
}
params_string = self.signature + urllib.urlencode(params)
response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
response_dict = parse_qs(response)
return response_dict
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 9 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
- Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
- Help text hyperlinks by sa2812 1 year, 5 months ago
Comments
Great! Glad this is still out in the wild...
#
Please login first before commenting.