|
|
@@ -13,78 +13,98 @@ class Gateway(object):
|
|
13
|
13
|
def do_request(self, request_xml):
|
|
14
|
14
|
pass
|
|
15
|
15
|
|
|
16
|
|
- def create_xml_doc(self):
|
|
17
|
|
-
|
|
18
|
|
-
|
|
19
|
|
- return doc
|
|
20
|
|
-
|
|
21
|
|
- def auth(self, card_number, expiry_date, amount, currency, merchant_reference, start_date=None):
|
|
|
16
|
+ def _initial_transaction(self, method_name, **kwargs):
|
|
|
17
|
+ """
|
|
|
18
|
+ Builds the XML for a 'initial' transaction
|
|
|
19
|
+ """
|
|
22
|
20
|
doc = Document()
|
|
23
|
|
- req = doc.createElement('Request')
|
|
24
|
|
- doc.appendChild(req)
|
|
|
21
|
+ req = self._create_element(doc, doc, 'Request')
|
|
25
|
22
|
|
|
26
|
|
- auth = doc.createElement('Authentication')
|
|
27
|
|
- req.appendChild(auth)
|
|
28
|
|
-
|
|
29
|
|
- client = doc.createElement('client')
|
|
30
|
|
- auth.appendChild(client)
|
|
31
|
|
- client_text = doc.createTextNode(self._client)
|
|
32
|
|
- client.appendChild(client_text)
|
|
33
|
|
-
|
|
34
|
|
- pw = doc.createElement('password')
|
|
35
|
|
- auth.appendChild(pw)
|
|
36
|
|
- pw_text = doc.createTextNode(self._password)
|
|
37
|
|
- pw.appendChild(pw_text)
|
|
|
23
|
+ # Authentication
|
|
|
24
|
+ auth = self._create_element(doc, req, 'Authentication')
|
|
|
25
|
+ self._create_element(doc, auth, 'client', self._client)
|
|
|
26
|
+ self._create_element(doc, auth, 'password', self._password)
|
|
38
|
27
|
|
|
39
|
|
- txn = doc.createElement('Transaction')
|
|
40
|
|
- req.appendChild(txn)
|
|
41
|
|
- card_txn = doc.createElement('CardTxn')
|
|
42
|
|
- txn.appendChild(card_txn)
|
|
43
|
|
- card = doc.createElement('Card')
|
|
44
|
|
- card_txn.appendChild(card)
|
|
45
|
|
-
|
|
46
|
|
- pan = doc.createElement('pan')
|
|
47
|
|
- card.appendChild(pan)
|
|
48
|
|
- pan_text = doc.createTextNode(card_number)
|
|
49
|
|
- pan.appendChild(pan_text)
|
|
50
|
|
-
|
|
51
|
|
- expiry = doc.createElement('expirydate')
|
|
52
|
|
- card.appendChild(expiry)
|
|
53
|
|
- expiry_text = doc.createTextNode(expiry_date)
|
|
54
|
|
- expiry.appendChild(expiry_text)
|
|
55
|
|
-
|
|
56
|
|
- if start_date:
|
|
57
|
|
- start = doc.createElement('startdate')
|
|
58
|
|
- card.appendChild(start)
|
|
59
|
|
- start_text = doc.createTextNode(start_date)
|
|
60
|
|
- start.appendChild(start_text)
|
|
61
|
|
-
|
|
62
|
|
- txn_details = doc.createElement('TxnDetails')
|
|
63
|
|
- txn.appendChild(txn_details)
|
|
64
|
|
-
|
|
65
|
|
- merchant = doc.createElement('merchantreference')
|
|
66
|
|
- txn_details.appendChild(merchant)
|
|
67
|
|
- merchant_text = doc.createTextNode(merchant_reference)
|
|
68
|
|
- merchant.appendChild(merchant_text)
|
|
69
|
|
-
|
|
70
|
|
- amount_ele = doc.createElement('amount')
|
|
71
|
|
- txn_details.appendChild(amount_ele)
|
|
72
|
|
- amount_text = doc.createTextNode(str(amount))
|
|
73
|
|
- amount_ele.appendChild(amount_text)
|
|
74
|
|
- amount_ele.setAttribute('currency', currency)
|
|
|
28
|
+ # Transaction
|
|
|
29
|
+ txn = self._create_element(doc, req, 'Transaction')
|
|
75
|
30
|
|
|
76
|
|
- method = doc.createElement('method')
|
|
77
|
|
- card_txn.appendChild(method)
|
|
78
|
|
- method_text = doc.createTextNode('auth')
|
|
79
|
|
- method.appendChild(method_text)
|
|
|
31
|
+ # CardTxn
|
|
|
32
|
+ if 'card_number' in kwargs:
|
|
|
33
|
+ card_txn = self._create_element(doc, txn, 'CardTxn')
|
|
|
34
|
+ self._create_element(doc, card_txn, 'method', method_name)
|
|
|
35
|
+
|
|
|
36
|
+ card = self._create_element(doc, card_txn, 'Card')
|
|
|
37
|
+ self._create_element(doc, card, 'pan', kwargs['card_number'])
|
|
|
38
|
+ self._create_element(doc, card, 'expirydate', kwargs['expiry_date'])
|
|
|
39
|
+
|
|
|
40
|
+ if 'start_date' in kwargs:
|
|
|
41
|
+ self._create_element(doc, card, 'startdate', kwargs['start_date'])
|
|
|
42
|
+
|
|
|
43
|
+ if 'issue_number' in kwargs:
|
|
|
44
|
+ self._create_element(doc, card, 'issuenumber', kwargs['issue_number'])
|
|
|
45
|
+
|
|
|
46
|
+ if 'auth_code' in kwargs:
|
|
|
47
|
+ self._create_element(doc, card, 'authcode', kwargs['auth_code'])
|
|
|
48
|
+
|
|
|
49
|
+ # HistoricTxn
|
|
|
50
|
+ if 'txn_reference' in kwargs:
|
|
|
51
|
+ historic_txn = self._create_element(doc, txn, 'HistoricTxn')
|
|
|
52
|
+ self._create_element(doc, historic_txn, 'reference', kwargs['txn_reference'])
|
|
|
53
|
+ self._create_element(doc, historic_txn, 'method', method_name)
|
|
|
54
|
+ if 'auth_code' in kwargs:
|
|
|
55
|
+ self._create_element(doc, historic_txn, 'authcode', kwargs['auth_code'])
|
|
|
56
|
+
|
|
|
57
|
+ # TxnDetails
|
|
|
58
|
+ if 'amount' in kwargs:
|
|
|
59
|
+ txn_details = self._create_element(doc, txn, 'TxnDetails')
|
|
|
60
|
+ if 'merchant_reference' in kwargs:
|
|
|
61
|
+ self._create_element(doc, txn_details, 'merchantreference', kwargs['merchant_reference'])
|
|
|
62
|
+ self._create_element(doc, txn_details, 'amount', str(kwargs['amount']), {'currency': kwargs['currency']})
|
|
80
|
63
|
|
|
81
|
64
|
self.do_request(doc.toxml())
|
|
82
|
65
|
|
|
83
|
|
- def pre(self, request):
|
|
84
|
|
- pass
|
|
|
66
|
+ def _create_element(self, doc, parent, tag, value=None, attributes=None):
|
|
|
67
|
+ ele = doc.createElement(tag)
|
|
|
68
|
+ parent.appendChild(ele)
|
|
|
69
|
+ if value:
|
|
|
70
|
+ text = doc.createTextNode(value)
|
|
|
71
|
+ ele.appendChild(text)
|
|
|
72
|
+ if attributes:
|
|
|
73
|
+ [ele.setAttribute(k, v) for k,v in attributes.items()]
|
|
|
74
|
+ return ele
|
|
85
|
75
|
|
|
86
|
|
- def refund(self, request):
|
|
87
|
|
- pass
|
|
|
76
|
+ def auth(self, **kwargs):
|
|
|
77
|
+ """
|
|
|
78
|
+ Performs an 'auth' request, which is to debit the money immediately
|
|
|
79
|
+ as a one-off transaction.
|
|
|
80
|
+
|
|
|
81
|
+ Note that currency should be ISO 4217 Alphabetic format.
|
|
|
82
|
+ """
|
|
|
83
|
+ return self._initial_transaction('auth', **kwargs)
|
|
|
84
|
+
|
|
|
85
|
+ def pre(self, **kwargs):
|
|
|
86
|
+ """
|
|
|
87
|
+ Performs an 'pre' request, which is to ring-fence the requested money
|
|
|
88
|
+ so it can be fulfilled at a later time.
|
|
|
89
|
+ """
|
|
|
90
|
+ return self._initial_transaction('pre', **kwargs)
|
|
|
91
|
+
|
|
|
92
|
+ def refund(self, **kwargs):
|
|
|
93
|
+ return self._initial_transaction('refund', **kwargs)
|
|
|
94
|
+
|
|
|
95
|
+ def erp(self, **kwargs):
|
|
|
96
|
+ return self._initial_transaction('erp', **kwargs)
|
|
|
97
|
+
|
|
|
98
|
+ # "Historic" transaction types
|
|
|
99
|
+
|
|
|
100
|
+ def cancel(self, txn_reference):
|
|
|
101
|
+ return self._initial_transaction('cancel', txn_reference=txn_reference)
|
|
|
102
|
+
|
|
|
103
|
+ def fulfil(self, **kwargs):
|
|
|
104
|
+ return self._initial_transaction('fulfil', **kwargs)
|
|
|
105
|
+
|
|
|
106
|
+ def txn_refund(self, **kwargs):
|
|
|
107
|
+ return self._initial_transaction('txn_refund', **kwargs)
|
|
88
|
108
|
|
|
89
|
109
|
|
|
90
|
110
|
class Adapter(object):
|