Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

datacash_requests.py 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import mock
  2. from decimal import Decimal as D
  3. from xml.dom.minidom import parseString
  4. from django.test import TestCase
  5. from oscar.apps.payment.datacash.utils import Gateway
  6. class TransactionMixin(object):
  7. def init_gateway(self, **kwargs):
  8. self.gateway = Gateway(client="DUMMY", password="123456", host="dummyhost.com", **kwargs)
  9. self.transport = mock.Mock()
  10. self.gateway.do_request = self.transport
  11. # Set a default success response
  12. response_xml = """<?xml version="1.0" encoding="UTF-8" ?>
  13. <Response>
  14. <CardTxn>
  15. <authcode>060642</authcode>
  16. <card_scheme>Switch</card_scheme>
  17. <country>United Kingdom</country>
  18. <issuer>HSBC</issuer>
  19. </CardTxn>
  20. <datacash_reference>3000000088888888</datacash_reference>
  21. <merchantreference>1000001</merchantreference>
  22. <mode>LIVE</mode>
  23. <reason>ACCEPTED</reason>
  24. <status>1</status>
  25. <time>1071567305</time>
  26. </Response>"""
  27. self.transport.return_value = response_xml
  28. def assertXmlTagValue(self, tag_name, value):
  29. request_xml = self.transport.call_args[0][0]
  30. doc = parseString(request_xml)
  31. try:
  32. tag = doc.getElementsByTagName(tag_name)[0]
  33. self.assertEquals(value, tag.firstChild.data)
  34. except IndexError:
  35. self.fail("Tag '%s' not found\n%s" % (tag_name, request_xml))
  36. def make_request(self, **kwargs):
  37. """
  38. Needs to be implemented by subclass.
  39. """
  40. pass
  41. def assertXmlTagAttributeValue(self, tag_name, attribute, value):
  42. request_xml = self.transport.call_args[0][0]
  43. doc = parseString(request_xml)
  44. try:
  45. tag = doc.getElementsByTagName(tag_name)[0]
  46. self.assertEquals(value, tag.attributes[attribute].value)
  47. except IndexError:
  48. self.fail("Tag '%s' not found\n%s" % (tag_name, request_xml))
  49. def test_request_includes_credentials(self):
  50. self.make_request()
  51. self.assertTrue(self.transport.called)
  52. self.assertXmlTagValue('client', 'DUMMY')
  53. self.assertXmlTagValue('password', '123456')
  54. class InitialTransactionMixin(TransactionMixin):
  55. def test_request_includes_merchant_reference(self):
  56. self.make_request()
  57. self.assertXmlTagValue('merchantreference', '123123')
  58. def test_request_includes_amount_and_currency(self):
  59. self.make_request()
  60. self.assertXmlTagValue('amount', '12.99')
  61. self.assertXmlTagAttributeValue('amount', 'currency', 'GBP')
  62. def test_request_can_include_authcode(self):
  63. self.make_request(auth_code='334455')
  64. self.assertXmlTagValue('authcode', '334455')
  65. def test_request_includes_card_details(self):
  66. self.make_request(start_date='01/10')
  67. self.assertXmlTagValue('pan', '1000010000000007')
  68. self.assertXmlTagValue('startdate', '01/10')
  69. self.assertXmlTagValue('expirydate', '01/12')
  70. def test_request_can_include_issue_number(self):
  71. self.make_request(issue_number='03')
  72. self.assertXmlTagValue('issuenumber', '03')
  73. class AuthTransactionTests(TestCase, InitialTransactionMixin):
  74. def setUp(self):
  75. self.init_gateway()
  76. def make_request(self, **kwargs):
  77. self.gateway.auth(card_number='1000010000000007',
  78. expiry_date='01/12',
  79. merchant_reference='123123',
  80. currency='GBP',
  81. amount=D('12.99'),
  82. **kwargs)
  83. def test_request_includes_method(self):
  84. self.make_request()
  85. self.assertXmlTagValue('method', 'auth')
  86. class PreTransactionTests(TestCase, InitialTransactionMixin):
  87. def setUp(self):
  88. self.init_gateway()
  89. def make_request(self, **kwargs):
  90. self.gateway.pre(card_number='1000010000000007',
  91. expiry_date='01/12',
  92. merchant_reference='123123',
  93. currency='GBP',
  94. amount=D('12.99'),
  95. **kwargs)
  96. def test_request_includes_method(self):
  97. self.make_request()
  98. self.assertXmlTagValue('method', 'pre')
  99. class RefundTransactionTests(TestCase, InitialTransactionMixin):
  100. def setUp(self):
  101. self.init_gateway()
  102. def make_request(self, **kwargs):
  103. self.gateway.refund(card_number='1000010000000007',
  104. expiry_date='01/12',
  105. merchant_reference='123123',
  106. currency='GBP',
  107. amount=D('12.99'),
  108. **kwargs)
  109. def test_request_includes_method(self):
  110. self.make_request()
  111. self.assertXmlTagValue('method', 'refund')
  112. class ErpTransactionTests(TestCase, InitialTransactionMixin):
  113. def setUp(self):
  114. self.init_gateway()
  115. def make_request(self, **kwargs):
  116. self.gateway.erp(card_number='1000010000000007',
  117. expiry_date='01/12',
  118. merchant_reference='123123',
  119. currency='GBP',
  120. amount=D('12.99'),
  121. **kwargs)
  122. def test_request_includes_method(self):
  123. self.make_request()
  124. self.assertXmlTagValue('method', 'erp')
  125. class CancelTransactionTests(TestCase, TransactionMixin):
  126. def setUp(self):
  127. self.init_gateway()
  128. def make_request(self, **kwargs):
  129. self.gateway.cancel(txn_reference='12312333444')
  130. def test_request_includes_method(self):
  131. self.make_request()
  132. self.assertXmlTagValue('method', 'cancel')
  133. def test_request_includes_reference(self):
  134. self.make_request()
  135. self.assertXmlTagValue('reference', '12312333444')
  136. class FulfilTransactionTests(TestCase, TransactionMixin):
  137. def setUp(self):
  138. self.init_gateway()
  139. def make_request(self, **kwargs):
  140. self.gateway.fulfil(currency='GBP',
  141. amount=D('12.99'),
  142. txn_reference='12312333444',
  143. auth_code='123444')
  144. def test_request_includes_method(self):
  145. self.make_request()
  146. self.assertXmlTagValue('method', 'fulfil')
  147. def test_request_includes_reference(self):
  148. self.make_request()
  149. self.assertXmlTagValue('reference', '12312333444')
  150. def test_request_includes_authcode(self):
  151. self.make_request()
  152. self.assertXmlTagValue('authcode', '123444')
  153. class TxnRefundTransactionTests(TestCase, TransactionMixin):
  154. def setUp(self):
  155. self.init_gateway()
  156. def make_request(self, **kwargs):
  157. self.gateway.txn_refund(currency='GBP',
  158. amount=D('12.99'),
  159. txn_reference='12312333444')
  160. def test_request_includes_method(self):
  161. self.make_request()
  162. self.assertXmlTagValue('method', 'txn_refund')
  163. def test_request_includes_reference(self):
  164. self.make_request()
  165. self.assertXmlTagValue('reference', '12312333444')
  166. class Av2CVsTests(TestCase, TransactionMixin):
  167. def setUp(self):
  168. self.init_gateway(cv2avs=True)
  169. def make_request(self, **kwargs):
  170. self.gateway.auth(card_number='1000010000000007',
  171. expiry_date='01/12',
  172. merchant_reference='123123',
  173. currency='GBP',
  174. amount=D('12.99'),
  175. ccv='234')
  176. def test_ccv_element_in_request(self):
  177. self.make_request()
  178. self.assertXmlTagValue('cv2', '234')