My favorites | Sign in
Project Logo
                
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
#!/usr/bin/env python

# 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


import urllib, md5, datetime

class PayPal:
""" #PayPal utility class"""
signature_values = {}
API_ENDPOINT = ""
PAYPAL_URL = ""

def __init__(self):
## Sandbox values
self.signature_values = {
'USER' : 'xxxx_xxxxxxxx_xx.xxxxxxxxxx.com', # Edit this to your API user name
'PWD' : 'xxxxxxxxxxxxxxx', # Edit this to your API password
'SIGNATURE' : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', # edit this to your API signature
'VERSION' : '3.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=' # Sandbox URL, not production
self.signature = urllib.urlencode(self.signature_values) + "&"

# API METHODS
def SetExpressCheckout(self, amount):
params = {
'METHOD' : "SetExpressCheckout",
'NOSHIPPING' : 1,
'PAYMENTACTION' : 'Authorization',
'RETURNURL' : 'http://www.yoursite.com/returnurl', #edit this
'CANCELURL' : 'http://www.yoursite.com/cancelurl', #edit this
'AMT' : amount,
}

params_string = self.signature + urllib.urlencode(params)
response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
response_token = ""
for token in response.split('&'):
if token.find("TOKEN=") != -1:
response_token = token[ (token.find("TOKEN=")+6):]
return response_token

def GetExpressCheckoutDetails(self, token):
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_tokens = {}
for token in response.split('&'):
response_tokens[token.split("=")[0]] = token.split("=")[1]
return response_tokens

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
Show details Hide details

Change log

r4 by Andrew.Stiegmann on Jan 30, 2008   Diff
Pre-Alpha release v0.1
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 6914 bytes, 165 lines
Hosted by Google Code