Pārlūkot izejas kodu

Got basic XML generation working

master
David Winterbottom 14 gadus atpakaļ
vecāks
revīzija
78f3aa0db3

+ 96
- 2
oscar/apps/payment/datacash/utils.py Parādīt failu

1
+import datetime
2
+from xml.dom.minidom import Document
3
+
4
+from django.conf import settings
5
+
1
 
6
 
2
 class Gateway(object):
7
 class Gateway(object):
3
 
8
 
4
-    def pre_auth(self, request):
9
+    def __init__(self, client, password):
10
+        self._client = client
11
+        self._password = password
12
+
13
+    def do_request(self, request_xml):
5
         pass
14
         pass
6
 
15
 
7
-    def auth(self, request):
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):
22
+        doc = Document()
23
+        req = doc.createElement('Request')
24
+        doc.appendChild(req)
25
+        
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)
38
+            
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)
75
+        
76
+        method = doc.createElement('method')
77
+        card_txn.appendChild(method)
78
+        method_text = doc.createTextNode('auth')
79
+        method.appendChild(method_text)
80
+        
81
+        self.do_request(doc.toxml())
82
+
83
+    def pre(self, request):
8
         pass
84
         pass
9
 
85
 
10
     def refund(self, request):
86
     def refund(self, request):
11
         pass
87
         pass
12
 
88
 
13
 
89
 
90
+class Adapter(object):
91
+    """
92
+    Responsible for dealing with oscar objects
93
+    """
94
+    
95
+    def __init__(self):
96
+        self.gateway = Gateway(settings.OSCAR_DATACASH_CLIENT, settings.OSCAR_DATACASH_PASSWORD)
97
+    
98
+    def auth(self, order_number, amount, bankcard, billing_address):
99
+        response = self.gateway.auth(pan=bankcard.pan,
100
+                                     expiry_date=bankcard.expiry_date,
101
+                                     merchant_reference=self.generate_merchant_reference(order_number))
102
+        
103
+    def generate_merchant_reference(self, order_number):
104
+        return '%s_%s' % (order_number, datetime.datetime.now().microsecond)
105
+        
106
+        
107
+

+ 2
- 0
oscar/apps/payment/exceptions.py Parādīt failu

1
+class InvalidBankcardException(Exception):
2
+    pass

+ 3
- 0
oscar/apps/payment/tests/__init__.py Parādīt failu

1
+from oscar.apps.payment.tests.main import *
2
+from oscar.apps.payment.tests.datacash import *
3
+

+ 82
- 0
oscar/apps/payment/tests/datacash.py Parādīt failu

1
+import mock
2
+from decimal import Decimal as D
3
+from xml.dom.minidom import parseString
4
+
5
+from django.test import TestCase
6
+
7
+from oscar.apps.payment.datacash.utils import Gateway
8
+
9
+
10
+class AuthTransactionTests(TestCase):
11
+    
12
+    def setUp(self):
13
+        self.gateway = Gateway(client="DUMMY", password="123456")
14
+        self.transport = mock.Mock()
15
+        self.gateway.do_request = self.transport
16
+    
17
+    def assertXmlTagValue(self, tag_name, value):
18
+        request_xml = self.transport.call_args[0][0]
19
+        doc = parseString(request_xml)
20
+        try:
21
+            tag = doc.getElementsByTagName(tag_name)[0]
22
+            self.assertEquals(value, tag.firstChild.data)
23
+        except IndexError:
24
+            self.fail("Tag '%s' not found\n%s" % (tag_name, request_xml))
25
+            
26
+    def assertXmlTagAttributeValue(self, tag_name, attribute, value):
27
+        request_xml = self.transport.call_args[0][0]
28
+        doc = parseString(request_xml)
29
+        try:
30
+            tag = doc.getElementsByTagName(tag_name)[0]
31
+            self.assertEquals(value, tag.attributes[attribute].value)
32
+        except IndexError:
33
+            self.fail("Tag '%s' not found\n%s" % (tagName, request_xml))
34
+    
35
+    def make_auth_request(self):
36
+        self.gateway.auth(card_number='1000010000000007', 
37
+                          start_date='01/10',
38
+                          expiry_date='01/12',
39
+                          issue_number='03',
40
+                          merchant_reference='123123',
41
+                          currency='GBP',
42
+                          amount=D('12.99'))
43
+    
44
+    def test_auth_includes_credentials(self):
45
+        self.make_auth_request()
46
+        self.assertTrue(self.transport.called)
47
+        self.assertXmlTagValue('client', 'DUMMY')
48
+        self.assertXmlTagValue('password', '123456')
49
+        
50
+    def test_auth_includes_card_details(self):   
51
+        self.make_auth_request()
52
+        self.assertXmlTagValue('pan', '1000010000000007')
53
+        self.assertXmlTagValue('startdate', '01/10')
54
+        self.assertXmlTagValue('expirydate', '01/12')
55
+        
56
+    def test_auth_includes_merchant_reference(self):   
57
+        self.make_auth_request()
58
+        self.assertXmlTagValue('merchantreference', '123123')
59
+        
60
+    def test_auth_includes_amount_and_currency(self):   
61
+        self.make_auth_request()
62
+        self.assertXmlTagValue('amount', '12.99')
63
+        self.assertXmlTagAttributeValue('amount', 'currency', 'GBP')
64
+        
65
+    def test_auth_includes_method(self):
66
+        self.make_auth_request()    
67
+        self.assertXmlTagValue('method', 'auth')
68
+    
69
+
70
+class HistoricTransactionTests(TestCase):
71
+    
72
+    def setUp(self):
73
+        self.gateway = Gateway(client="DUMMY", password="123456")
74
+
75
+    def test_cancel(self):
76
+        self.gateway.auth
77
+        
78
+    def test_fulfil(self):
79
+        pass
80
+    
81
+    def test_txn_refund(self):
82
+        pass

oscar/apps/payment/tests.py → oscar/apps/payment/tests/main.py Parādīt failu

63
             
63
             
64
     def test_invalid_numbers_fail(self):
64
     def test_invalid_numbers_fail(self):
65
         for number in self.invalid_numbers:
65
         for number in self.invalid_numbers:
66
-            self.assertFalse(luhn(number))
66
+            self.assertFalse(luhn(number))
67
+    
68
+    

Notiek ielāde…
Atcelt
Saglabāt