Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 _initial_transaction(self, method_name, **kwargs):
  11. """
  12. Builds the XML for a 'initial' transaction
  13. """
  14. doc = Document()
  15. req = self._create_element(doc, doc, 'Request')
  16. # Authentication
  17. auth = self._create_element(doc, req, 'Authentication')
  18. self._create_element(doc, auth, 'client', self._client)
  19. self._create_element(doc, auth, 'password', self._password)
  20. # Transaction
  21. txn = self._create_element(doc, req, 'Transaction')
  22. # CardTxn
  23. if 'card_number' in kwargs:
  24. card_txn = self._create_element(doc, txn, 'CardTxn')
  25. self._create_element(doc, card_txn, 'method', method_name)
  26. card = self._create_element(doc, card_txn, 'Card')
  27. self._create_element(doc, card, 'pan', kwargs['card_number'])
  28. self._create_element(doc, card, 'expirydate', kwargs['expiry_date'])
  29. if 'start_date' in kwargs:
  30. self._create_element(doc, card, 'startdate', kwargs['start_date'])
  31. if 'issue_number' in kwargs:
  32. self._create_element(doc, card, 'issuenumber', kwargs['issue_number'])
  33. if 'auth_code' in kwargs:
  34. self._create_element(doc, card, 'authcode', kwargs['auth_code'])
  35. # HistoricTxn
  36. if 'txn_reference' in kwargs:
  37. historic_txn = self._create_element(doc, txn, 'HistoricTxn')
  38. self._create_element(doc, historic_txn, 'reference', kwargs['txn_reference'])
  39. self._create_element(doc, historic_txn, 'method', method_name)
  40. if 'auth_code' in kwargs:
  41. self._create_element(doc, historic_txn, 'authcode', kwargs['auth_code'])
  42. # TxnDetails
  43. if 'amount' in kwargs:
  44. txn_details = self._create_element(doc, txn, 'TxnDetails')
  45. if 'merchant_reference' in kwargs:
  46. self._create_element(doc, txn_details, 'merchantreference', kwargs['merchant_reference'])
  47. self._create_element(doc, txn_details, 'amount', str(kwargs['amount']), {'currency': kwargs['currency']})
  48. self.do_request(doc.toxml())
  49. def _create_element(self, doc, parent, tag, value=None, attributes=None):
  50. ele = doc.createElement(tag)
  51. parent.appendChild(ele)
  52. if value:
  53. text = doc.createTextNode(value)
  54. ele.appendChild(text)
  55. if attributes:
  56. [ele.setAttribute(k, v) for k,v in attributes.items()]
  57. return ele
  58. def auth(self, **kwargs):
  59. """
  60. Performs an 'auth' request, which is to debit the money immediately
  61. as a one-off transaction.
  62. Note that currency should be ISO 4217 Alphabetic format.
  63. """
  64. return self._initial_transaction('auth', **kwargs)
  65. def pre(self, **kwargs):
  66. """
  67. Performs an 'pre' request, which is to ring-fence the requested money
  68. so it can be fulfilled at a later time.
  69. """
  70. return self._initial_transaction('pre', **kwargs)
  71. def refund(self, **kwargs):
  72. return self._initial_transaction('refund', **kwargs)
  73. def erp(self, **kwargs):
  74. return self._initial_transaction('erp', **kwargs)
  75. # "Historic" transaction types
  76. def cancel(self, txn_reference):
  77. return self._initial_transaction('cancel', txn_reference=txn_reference)
  78. def fulfil(self, **kwargs):
  79. return self._initial_transaction('fulfil', **kwargs)
  80. def txn_refund(self, **kwargs):
  81. return self._initial_transaction('txn_refund', **kwargs)
  82. class Adapter(object):
  83. """
  84. Responsible for dealing with oscar objects
  85. """
  86. def __init__(self):
  87. self.gateway = Gateway(settings.OSCAR_DATACASH_CLIENT, settings.OSCAR_DATACASH_PASSWORD)
  88. def auth(self, order_number, amount, bankcard, billing_address):
  89. response = self.gateway.auth(pan=bankcard.pan,
  90. expiry_date=bankcard.expiry_date,
  91. merchant_reference=self.generate_merchant_reference(order_number))
  92. def generate_merchant_reference(self, order_number):
  93. return '%s_%s' % (order_number, datetime.datetime.now().microsecond)