summaryrefslogtreecommitdiff
path: root/postgresqleu/paypal/util.py
diff options
context:
space:
mode:
authorMagnus Hagander2016-01-18 21:35:42 +0000
committerMagnus Hagander2016-01-18 21:36:15 +0000
commit470186d25f22b64fdff26aa97686b450438a0d8a (patch)
tree17e13d762de1af244120a70a6f51887efa66d942 /postgresqleu/paypal/util.py
parenta1cbcf89512d19d8225a23c73b564b1d9fd41afb (diff)
Move paypal fetching script to be an admin command
Centralizes Paypal API calls, which we will need for some other things
Diffstat (limited to 'postgresqleu/paypal/util.py')
-rw-r--r--postgresqleu/paypal/util.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/postgresqleu/paypal/util.py b/postgresqleu/paypal/util.py
new file mode 100644
index 00000000..fd1685aa
--- /dev/null
+++ b/postgresqleu/paypal/util.py
@@ -0,0 +1,48 @@
+from django.conf import settings
+
+import urllib2
+from urllib import urlencode
+from urlparse import parse_qs
+import itertools
+
+class PaypalAPI(object):
+ def __init__(self):
+ self.accessparam = {
+ 'USER': settings.PAYPAL_API_USER,
+ 'PWD': settings.PAYPAL_API_PASSWORD,
+ 'SIGNATURE': settings.PAYPAL_API_SIGNATURE,
+ 'VERSION': 95,
+ }
+ if settings.PAYPAL_SANDBOX:
+ self.API_ENDPOINT = 'https://api-3t.sandbox.paypal.com/nvp'
+ else:
+ self.API_ENDPOINT = 'https://api-3t.paypal.com/nvp'
+
+
+ def _api_call(self, command, params):
+ params.update(self.accessparam)
+ params['METHOD'] = command
+ resp = urllib2.urlopen(self.API_ENDPOINT, urlencode(params)).read()
+ return parse_qs(resp)
+
+ def _dateformat(self, d):
+ return d.strftime("%Y-%m-%dT%H:%M:%S")
+
+ def get_transaction_list(self, firstdate):
+ r = self._api_call('TransactionSearch', {
+ 'STARTDATE': self._dateformat(firstdate),
+ 'STATUS': 'Success',
+ })
+ for i in itertools.count(1):
+ if not r.has_key('L_TRANSACTIONID{0}'.format(i)):
+ break
+
+ yield dict([(k,r.get('L_{0}{1}'.format(k, i),[''])[0])
+ for k in
+ ('TRANSACTIONID', 'TIMESTAMP', 'EMAIL', 'TYPE', 'AMT', 'FEEAMT', 'NAME')])
+
+
+ def get_transaction_details(self, transactionid):
+ return self._api_call('GetTransactionDetails', {
+ 'TRANSACTIONID': transactionid,
+ })