pos_payment_method.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # coding: utf-8
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import json
  4. import logging
  5. import pprint
  6. import random
  7. import requests
  8. import string
  9. from werkzeug.exceptions import Forbidden
  10. from odoo import fields, models, api, _
  11. from odoo.exceptions import ValidationError
  12. _logger = logging.getLogger(__name__)
  13. class PosPaymentMethod(models.Model):
  14. _inherit = 'pos.payment.method'
  15. def _get_payment_terminal_selection(self):
  16. return super(PosPaymentMethod, self)._get_payment_terminal_selection() + [('adyen', 'Adyen')]
  17. # Adyen
  18. adyen_api_key = fields.Char(string="Adyen API key", help='Used when connecting to Adyen: https://docs.adyen.com/user-management/how-to-get-the-api-key/#description', copy=False)
  19. adyen_terminal_identifier = fields.Char(help='[Terminal model]-[Serial number], for example: P400Plus-123456789', copy=False)
  20. adyen_test_mode = fields.Boolean(help='Run transactions in the test environment.')
  21. adyen_latest_response = fields.Char(copy=False, groups='base.group_erp_manager') # used to buffer the latest asynchronous notification from Adyen.
  22. adyen_latest_diagnosis = fields.Char(copy=False, groups='base.group_erp_manager') # used to determine if the terminal is still connected.
  23. @api.constrains('adyen_terminal_identifier')
  24. def _check_adyen_terminal_identifier(self):
  25. for payment_method in self:
  26. if not payment_method.adyen_terminal_identifier:
  27. continue
  28. # sudo() to search all companies
  29. existing_payment_method = self.sudo().search([('id', '!=', payment_method.id),
  30. ('adyen_terminal_identifier', '=', payment_method.adyen_terminal_identifier)],
  31. limit=1)
  32. if existing_payment_method:
  33. if existing_payment_method.company_id == payment_method.company_id:
  34. raise ValidationError(_('Terminal %s is already used on payment method %s.')
  35. % (payment_method.adyen_terminal_identifier, existing_payment_method.display_name))
  36. else:
  37. raise ValidationError(_('Terminal %s is already used in company %s on payment method %s.')
  38. % (payment_method.adyen_terminal_identifier,
  39. existing_payment_method.company_id.name,
  40. existing_payment_method.display_name))
  41. def _get_adyen_endpoints(self):
  42. return {
  43. 'terminal_request': 'https://terminal-api-%s.adyen.com/async',
  44. }
  45. def _is_write_forbidden(self, fields):
  46. whitelisted_fields = set(('adyen_latest_response', 'adyen_latest_diagnosis'))
  47. return super(PosPaymentMethod, self)._is_write_forbidden(fields - whitelisted_fields)
  48. def _adyen_diagnosis_request_data(self, pos_config_name):
  49. service_id = ''.join(random.choices(string.ascii_letters + string.digits, k=10))
  50. return {
  51. "SaleToPOIRequest": {
  52. "MessageHeader": {
  53. "ProtocolVersion": "3.0",
  54. "MessageClass": "Service",
  55. "MessageCategory": "Diagnosis",
  56. "MessageType": "Request",
  57. "ServiceID": service_id,
  58. "SaleID": pos_config_name,
  59. "POIID": self.adyen_terminal_identifier,
  60. },
  61. "DiagnosisRequest": {
  62. "HostDiagnosisFlag": False
  63. }
  64. }
  65. }
  66. def get_latest_adyen_status(self, pos_config_name):
  67. self.ensure_one()
  68. latest_response = self.sudo().adyen_latest_response
  69. latest_response = json.loads(latest_response) if latest_response else False
  70. return {
  71. 'latest_response': latest_response,
  72. }
  73. def proxy_adyen_request(self, data, operation=False):
  74. ''' Necessary because Adyen's endpoints don't have CORS enabled '''
  75. if data['SaleToPOIRequest']['MessageHeader']['MessageCategory'] == 'Payment': # Clear only if it is a payment request
  76. self.sudo().adyen_latest_response = '' # avoid handling old responses multiple times
  77. if not operation:
  78. operation = 'terminal_request'
  79. return self._proxy_adyen_request_direct(data, operation)
  80. def _proxy_adyen_request_direct(self, data, operation):
  81. self.ensure_one()
  82. TIMEOUT = 10
  83. _logger.info('request to adyen\n%s', pprint.pformat(data))
  84. environment = 'test' if self.adyen_test_mode else 'live'
  85. endpoint = self._get_adyen_endpoints()[operation] % environment
  86. headers = {
  87. 'x-api-key': self.adyen_api_key,
  88. }
  89. req = requests.post(endpoint, json=data, headers=headers, timeout=TIMEOUT)
  90. # Authentication error doesn't return JSON
  91. if req.status_code == 401:
  92. return {
  93. 'error': {
  94. 'status_code': req.status_code,
  95. 'message': req.text
  96. }
  97. }
  98. if req.text == 'ok':
  99. return True
  100. return req.json()