summaryrefslogtreecommitdiff
path: root/postgresqleu/paypal/util.py
blob: d9c2e4c337aec5ccc029a1fdf70df9b24058122b (plain)
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
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()
		q = parse_qs(resp)
		if q['ACK'][0] != 'Success':
			raise Exception("API error from paypal: {0}/{1}".format(q['L_SHORTMESSAGE0'][0], q['L_LONGMESSAGE0'][0]))
		return q

	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,
		})