You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import datetime
  2. from xml.dom.minidom import Document
  3. from django.conf import settings
  4. class Gateway(object):
  5. def __init__(self, client, password):
  6. self._client = client
  7. self._password = password
  8. def do_request(self, request_xml):
  9. pass
  10. def create_xml_doc(self):
  11. return doc
  12. def auth(self, card_number, expiry_date, amount, currency, merchant_reference, start_date=None):
  13. doc = Document()
  14. req = doc.createElement('Request')
  15. doc.appendChild(req)
  16. auth = doc.createElement('Authentication')
  17. req.appendChild(auth)
  18. client = doc.createElement('client')
  19. auth.appendChild(client)
  20. client_text = doc.createTextNode(self._client)
  21. client.appendChild(client_text)
  22. pw = doc.createElement('password')
  23. auth.appendChild(pw)
  24. pw_text = doc.createTextNode(self._password)
  25. pw.appendChild(pw_text)
  26. txn = doc.createElement('Transaction')
  27. req.appendChild(txn)
  28. card_txn = doc.createElement('CardTxn')
  29. txn.appendChild(card_txn)
  30. card = doc.createElement('Card')
  31. card_txn.appendChild(card)
  32. pan = doc.createElement('pan')
  33. card.appendChild(pan)
  34. pan_text = doc.createTextNode(card_number)
  35. pan.appendChild(pan_text)
  36. expiry = doc.createElement('expirydate')
  37. card.appendChild(expiry)
  38. expiry_text = doc.createTextNode(expiry_date)
  39. expiry.appendChild(expiry_text)
  40. if start_date:
  41. start = doc.createElement('startdate')
  42. card.appendChild(start)
  43. start_text = doc.createTextNode(start_date)
  44. start.appendChild(start_text)
  45. txn_details = doc.createElement('TxnDetails')
  46. txn.appendChild(txn_details)
  47. merchant = doc.createElement('merchantreference')
  48. txn_details.appendChild(merchant)
  49. merchant_text = doc.createTextNode(merchant_reference)
  50. merchant.appendChild(merchant_text)
  51. amount_ele = doc.createElement('amount')
  52. txn_details.appendChild(amount_ele)
  53. amount_text = doc.createTextNode(str(amount))
  54. amount_ele.appendChild(amount_text)
  55. amount_ele.setAttribute('currency', currency)
  56. method = doc.createElement('method')
  57. card_txn.appendChild(method)
  58. method_text = doc.createTextNode('auth')
  59. method.appendChild(method_text)
  60. self.do_request(doc.toxml())
  61. def pre(self, request):
  62. pass
  63. def refund(self, request):
  64. pass
  65. class Adapter(object):
  66. """
  67. Responsible for dealing with oscar objects
  68. """
  69. def __init__(self):
  70. self.gateway = Gateway(settings.OSCAR_DATACASH_CLIENT, settings.OSCAR_DATACASH_PASSWORD)
  71. def auth(self, order_number, amount, bankcard, billing_address):
  72. response = self.gateway.auth(pan=bankcard.pan,
  73. expiry_date=bankcard.expiry_date,
  74. merchant_reference=self.generate_merchant_reference(order_number))
  75. def generate_merchant_reference(self, order_number):
  76. return '%s_%s' % (order_number, datetime.datetime.now().microsecond)