Created
August 6, 2010 09:11
-
-
Save jgumbley/511080 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib, md5, datetime | |
from cgi import parse_qs | |
class PayPal: | |
"""PayPal Utility Class | |
* blog url | |
* http://djangosnippets.org/snippets/1181/ | |
* https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_NVPAPIOverview | |
""" | |
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.return_url = "http://www.jgumbley.com/" | |
self.cancel_url = "http://www.jgumbley.com/" | |
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 | |
## Express Checkout: | |
# Create Transaction - (add currency) | |
def SetExpressCheckout(self, amount, **kwargs): | |
params = { | |
'METHOD' : "SetExpressCheckout", | |
'NOSHIPPING' : 1, | |
'PAYMENTACTION' : 'Authorization', | |
'RETURNURL' : self.return_url, | |
'CANCELURL' : self.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 | |
# Commit Express Checkout | |
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 | |
import webbrowser | |
if __name__ == "__main__": | |
pp = PayPal() | |
# setup an express checkout token | |
e_tk = pp.SetExpressCheckout(100) | |
print "Token Recieved: " + e_tk | |
url = pp.PAYPAL_URL + e_tk | |
print "URL constructed: " + url | |
webbrowser.open(url) | |
print "Enter Payer_ID from URL query string when back from PayPal." | |
pay_id = raw_input(">") | |
print "Reponse from PayPal:" | |
print pp.DoExpressCheckoutPayment(e_tk, pay_id, 100) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment