-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquickpay.py
More file actions
159 lines (134 loc) · 4.92 KB
/
Copy pathquickpay.py
File metadata and controls
159 lines (134 loc) · 4.92 KB
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
"""
QuickPay - Interface to QuickPay payment gateway, http://www.quickpay.dk
Author: Niels Sandholt Busch, Spacergif Software 2008
"""
import urllib
import logging
import md5
from xml.dom import minidom
import string
import random
class QuickPayError(Exception):
"Indicates the operation failed"
class QuickPay(object):
"""
Implements Quickpay payment gateway. Use pretend = True for testing
"""
def __init__(self, merchant, secretkey, pretend=False):
self.merchant = merchant
self.secretkey = secretkey
self.pretend = pretend
self.url = 'https://secure.quickpay.dk/transaction.php'
def _do_post(self, data):
msgtypes = {'1100':'1110', '1220':'1230', '1420':'1430'}
try:
if self.pretend:
reply = '<?xml version="1.0" encoding="ISO-8859-1"?> \
<values msgtype="%s" pbsstat="000" qpstat="000" qpstatmsg="OK" transaction="%s" />' \
% (msgtypes[data['msgtype']], ''.join([random.choice(string.digits) for x in xrange(7)]))
doc = minidom.parseString(reply)
else:
url_obj = urllib.urlopen(self.url, urllib.urlencode(data))
doc = minidom.parse(url_obj)
elm = doc.documentElement
msgtype = elm.getAttribute('msgtype')
pbsstat = elm.getAttribute('pbsstat')
qpstat = elm.getAttribute('qpstat')
qpstatmsg = elm.getAttribute('qpstatmsg')
assert qpstat == pbsstat == '000'
assert msgtype == msgtypes[data['msgtype']]
except IOError, e:
raise QuickPayError, e
except AssertionError, e:
raise QuickPayError, qpstatmsg
return elm
def authorize(self,
cardnumber=None,
amount=None,
ordernum=None,
currency=None,
expirationdate=None,
cvd=None,
authtype=None,
reference=None,
transaction=None):
"""
Authorize
"""
msgtype = '1100'
posc = 'K00540K00130' if (authtype or '') == 'recurring' else 'K00500K00130'
data = {}
if authtype == 'recurring':
cardnumber = ''
expirationdate = ''
cvd = ''
data.update({'authtype':authtype, 'transaction':transaction})
if authtype == 'preauth':
amount = 100
data.update({'authtype':authtype, 'reference':reference})
ordernum = ''.join(['0' for i in range(4-len(ordernum))]) + ordernum
data.update({'msgtype':msgtype,
'cardnumber':cardnumber,
'amount':amount,
'expirationdate':expirationdate,
'posc':posc,
'ordernum':ordernum,
'currency':currency,
'cvd':cvd,
'merchant':self.merchant})
md_input = ''.join((msgtype,
cardnumber or '',
str(amount),
expirationdate or '',
posc,
str(ordernum),
currency,
str(cvd or ''),
self.merchant,
authtype or '',
reference or '',
transaction or '',
self.secretkey))
md5checkV2 = md5.new(md_input).hexdigest().upper()
data['md5checkV2'] = md5checkV2
elm = self._do_post(data)
transaction = elm.getAttribute('transaction')
return transaction
def capture(self, transaction, amount):
"""
Capture
"""
msgtype = '1220'
md_input = ''.join((msgtype,
str(amount),
self.merchant,
transaction,
self.secretkey))
md5check = md5.new(md_input).hexdigest().upper()
data = {'msgtype':msgtype,
'amount':amount,
'merchant':self.merchant,
'transaction':transaction,
'md5check':md5check}
elm = self._do_post(data)
def reversal(self, transaction):
"""
Reversal
"""
msgtype = '1420'
md_input = ''.join((msgtype,
self.merchant,
transaction,
self.secretkey))
md5check = md5.new(md_input).hexdigest().upper()
data = {'msgtype':msgtype,
'merchant':self.merchant,
'transaction':transaction,
'md5check':md5check}
self._do_post(data)
def credit(self):
pass
def status(self):
pass
def pbsstatus(self):
pass