Subdomain Posts
Python | 13 hours ago
Python | 14 hours ago
Python | 14 hours ago
Python | 14 hours ago
Python | 21 hours ago
Python | 21 hours ago
Python | 1 day ago
Python | 1 day ago
Python | 1 day ago
Python | 2 days ago
Recent Posts
PAWN | 6 sec ago
Java | 24 sec ago
PHP | 48 sec ago
Java | 1 min ago
Java 5 | 1 min ago
None | 3 min ago
Bash | 3 min ago
Python | 4 min ago
None | 4 min ago
None | 4 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Michael Atlas on the 13th of Sep 2007 08:07:51 PM Download | Raw | Embed | Report
  1. #!/usr/bin/env python
  2.  
  3. # PayPal python NVP API wrapper class.
  4. # This is a sample to help others get started on working
  5. # with the PayPal NVP API in Python.
  6. # This is not a complete reference! Be sure to understand
  7. # what this class is doing before you try it on production servers!
  8. # ...use at your own peril.
  9.  
  10. # by Mike Atlas / LowSingle.com / MassWrestling.com, September 2007
  11. # No License Expressed. Feel free to distribute, modify,
  12. #  and use in any open or closed source project without credit to the author
  13.  
  14. # Example usage: ===============
  15. #       paypal = PayPal()
  16. #       pp_token = paypal.SetExpressCheckout(100)
  17. #       express_token = paypal.GetExpressCheckoutDetails(pp_token)
  18. #       url= paypal.PAYPAL_URL + express_token
  19. #       HttpResponseRedirect(url) ## django specific http redirect call for payment
  20.  
  21.  
  22. import urllib, md5, datetime
  23.  
  24. class PayPal:
  25.         """ #PayPal utility class"""
  26.         signature_values = {}
  27.         API_ENDPOINT = ""
  28.         PAYPAL_URL = ""
  29.        
  30.         def __init__(self):
  31.                 ## Sandbox values
  32.                 self.signature_values = {
  33.                 'USER' : 'xxxx_xxxxxxxx_xx.xxxxxxxxxx.com', # Edit this to your API user name
  34.                 'PWD' : 'xxxxxxxxxxxxxxx', # Edit this to your API password
  35.                 'SIGNATURE' : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', # edit this to your API signature
  36.                 'VERSION' : '3.0',
  37.                 }
  38.                 self.API_ENDPOINT = 'https://api-3t.sandbox.paypal.com/nvp' # Sandbox URL, not production
  39.                 self.PAYPAL_URL = 'https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=' # Sandbox URL, not production
  40.                 self.signature = urllib.urlencode(self.signature_values) + "&"
  41.  
  42.         # API METHODS
  43.         def SetExpressCheckout(self, amount):
  44.                 params = {
  45.                         'METHOD' : "SetExpressCheckout",
  46.                         'NOSHIPPING' : 1,
  47.                         'PAYMENTACTION' : 'Authorization',
  48.                         'RETURNURL' : 'http://www.yoursite.com/returnurl', #edit this
  49.                         'CANCELURL' : 'http://www.yoursite.com/cancelurl', #edit this
  50.                         'AMT' : amount,
  51.                 }
  52.  
  53.                 params_string = self.signature + urllib.urlencode(params)
  54.                 response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
  55.                 response_token = ""
  56.                 for token in response.split('&'):
  57.                         if token.find("TOKEN=") != -1:
  58.                                 response_token = token[ (token.find("TOKEN=")+6):]
  59.                 return response_token
  60.        
  61.         def GetExpressCheckoutDetails(self, token):
  62.                 params = {
  63.                         'METHOD' : "GetExpressCheckoutDetails",
  64.                         'RETURNURL' : 'http://www.yoursite.com/returnurl', #edit this
  65.                         'CANCELURL' : 'http://www.yoursite.com/cancelurl', #edit this
  66.                         'TOKEN' : token,
  67.                 }
  68.                 params_string = self.signature + urllib.urlencode(params)
  69.                 response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
  70.                 response_tokens = {}
  71.                 for token in response.split('&'):
  72.                         response_tokens[token.split("=")[0]] = token.split("=")[1]
  73.                 return response_tokens
  74.        
  75.         def DoExpressCheckoutPayment(self, token, payer_id, amt):
  76.                 params = {
  77.                         'METHOD' : "DoExpressCheckoutPayment",
  78.                         'PAYMENTACTION' : 'Sale',
  79.                         'RETURNURL' : 'http://www.yoursite.com/returnurl', #edit this
  80.                         'CANCELURL' : 'http://www.yoursite.com/cancelurl', #edit this
  81.                         'TOKEN' : token,
  82.                         'AMT' : amt,
  83.                         'PAYERID' : payer_id,
  84.                 }
  85.                 params_string = self.signature + urllib.urlencode(params)
  86.                 response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
  87.                 response_tokens = {}
  88.                 for token in response.split('&'):
  89.                         response_tokens[token.split("=")[0]] = token.split("=")[1]
  90.                 for key in response_tokens.keys():
  91.                                 response_tokens[key] = urllib.unquote(response_tokens[key])
  92.                 return response_tokens
  93.                
  94.         def GetTransactionDetails(self, tx_id):
  95.                 params = {
  96.                         'METHOD' : "GetTransactionDetails",
  97.                         'TRANSACTIONID' : tx_id,
  98.                 }
  99.                 params_string = self.signature + urllib.urlencode(params)
  100.                 response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
  101.                 response_tokens = {}
  102.                 for token in response.split('&'):
  103.                         response_tokens[token.split("=")[0]] = token.split("=")[1]
  104.                 for key in response_tokens.keys():
  105.                                 response_tokens[key] = urllib.unquote(response_tokens[key])
  106.                 return response_tokens
  107.                                
  108.         def MassPay(self, email, amt, note, email_subject):
  109.                 unique_id = str(md5.new(str(datetime.datetime.now())).hexdigest())
  110.                 params = {
  111.                         'METHOD' : "MassPay",
  112.                         'RECEIVERTYPE' : "EmailAddress",
  113.                         'L_AMT0' : amt,
  114.                         'CURRENCYCODE' : 'USD',
  115.                         'L_EMAIL0' : email,
  116.                         'L_UNIQUE0' : unique_id,
  117.                         'L_NOTE0' : note,
  118.                         'EMAILSUBJECT': email_subject,
  119.                 }
  120.                 params_string = self.signature + urllib.urlencode(params)
  121.                 response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
  122.                 response_tokens = {}
  123.                 for token in response.split('&'):
  124.                         response_tokens[token.split("=")[0]] = token.split("=")[1]
  125.                 for key in response_tokens.keys():
  126.                                 response_tokens[key] = urllib.unquote(response_tokens[key])
  127.                 response_tokens['unique_id'] = unique_id
  128.                 return response_tokens
  129.                                
  130.         def DoDirectPayment(self, amt, ipaddress, acct, expdate, cvv2, firstname, lastname, cctype, street, city, state, zipcode):
  131.                 params = {
  132.                         'METHOD' : "DoDirectPayment",
  133.                         'PAYMENTACTION' : 'Sale',
  134.                         'AMT' : amt,
  135.                         'IPADDRESS' : ipaddress,
  136.                         'ACCT': acct,
  137.                         'EXPDATE' : expdate,
  138.                         'CVV2' : cvv2,
  139.                         'FIRSTNAME' : firstname,
  140.                         'LASTNAME': lastname,
  141.                         'CREDITCARDTYPE': cctype,
  142.                         'STREET': street,
  143.                         'CITY': city,
  144.                         'STATE': state,
  145.                         'ZIP':zipcode,
  146.                         'COUNTRY' : 'United States',
  147.                         'COUNTRYCODE': 'US',
  148.                         'RETURNURL' : 'http://www.yoursite.com/returnurl', #edit this
  149.                         'CANCELURL' : 'http://www.yoursite.com/cancelurl', #edit this
  150.                         'L_DESC0' : "Desc: ",
  151.                         'L_NAME0' : "Name: ",
  152.                 }
  153.                 params_string = self.signature + urllib.urlencode(params)
  154.                 response = urllib.urlopen(self.API_ENDPOINT, params_string).read()
  155.                 response_tokens = {}
  156.                 for token in response.split('&'):
  157.                         response_tokens[token.split("=")[0]] = token.split("=")[1]
  158.                 for key in response_tokens.keys():
  159.                         response_tokens[key] = urllib.unquote(response_tokens[key])
  160.                 return response_tokens
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: