[REV] pos_adyen: avoid concurrent updates

Revert commit e6372c3, the concurrent updates were solved by the change
in the order of the lines, not by the fact that functions were defined
as api.model
This commit is contained in:
Antoine Prieels
2020-09-29 04:25:21 +00:00
committed by Pierre Masereel
parent a2a0bfaa04
commit 57beab80fe
3 changed files with 15 additions and 33 deletions
+12 -26
View File
@@ -40,7 +40,7 @@ class PosPaymentMethod(models.Model):
whitelisted_fields = set(('adyen_latest_response', 'adyen_latest_diagnosis'))
return super(PosPaymentMethod, self)._is_write_forbidden(fields - whitelisted_fields)
def _adyen_diagnosis_request_data(self, pos_config_name, terminal_identifier):
def _adyen_diagnosis_request_data(self, pos_config_name):
service_id = ''.join(random.choices(string.ascii_letters + string.digits, k=10))
return {
"SaleToPOIRequest": {
@@ -51,7 +51,7 @@ class PosPaymentMethod(models.Model):
"MessageType": "Request",
"ServiceID": service_id,
"SaleID": pos_config_name,
"POIID": terminal_identifier,
"POIID": self.adyen_terminal_identifier,
},
"DiagnosisRequest": {
"HostDiagnosisFlag": False
@@ -59,41 +59,27 @@ class PosPaymentMethod(models.Model):
}
}
@api.model
def get_latest_adyen_status(self, payment_method_id, pos_config_name, terminal_identifier, test_mode, api_key):
'''See the description of proxy_adyen_request as to why this is an
@api.model function.
'''
def get_latest_adyen_status(self, pos_config_name):
self.ensure_one()
# Poll the status of the terminal if there's no new
# notification we received. This is done so we can quickly
# notify the user if the terminal is no longer reachable due
# to connectivity issues.
self.proxy_adyen_request(self._adyen_diagnosis_request_data(pos_config_name, terminal_identifier),
test_mode,
api_key)
self.proxy_adyen_request(self._adyen_diagnosis_request_data(pos_config_name))
payment_method = self.sudo().browse(payment_method_id)
latest_response = payment_method.adyen_latest_response
latest_response = self.sudo().adyen_latest_response
latest_response = json.loads(latest_response) if latest_response else False
payment_method.adyen_latest_response = '' # avoid handling old responses multiple times
self.adyen_latest_response = '' # avoid handling old responses multiple times
return {
'latest_response': latest_response,
'last_received_diagnosis_id': payment_method.adyen_latest_diagnosis,
'last_received_diagnosis_id': self.adyen_latest_diagnosis,
}
@api.model
def proxy_adyen_request(self, data, test_mode, api_key, test_endpoint=False, live_endpoint=False):
'''Necessary because Adyen's endpoints don't have CORS enabled. This is an
@api.model function to avoid concurrent update errors. Adyen's
async endpoint can still take well over a second to complete a
request. By using @api.model and passing in all data we need from
the POS we avoid locking the pos_payment_method table. This way we
avoid concurrent update errors when Adyen calls us back on
/pos_adyen/notification which will need to write on
pos.payment.method.
'''
def proxy_adyen_request(self, data, test_endpoint=False, live_endpoint=False):
''' Necessary because Adyen's endpoints don't have CORS enabled '''
self.ensure_one()
TIMEOUT = 10
if not test_endpoint:
@@ -107,7 +93,7 @@ class PosPaymentMethod(models.Model):
_logger.info('request to adyen\n%s', pprint.pformat(data))
headers = {
'x-api-key': api_key,
'x-api-key': self.adyen_api_key,
'Content-Type': 'application/json'
}
req = requests.post(endpoint, data=json.dumps(data), headers=headers, timeout=TIMEOUT)
+1 -1
View File
@@ -3,5 +3,5 @@ var models = require('point_of_sale.models');
var PaymentAdyen = require('pos_adyen.payment');
models.register_payment_method('adyen', PaymentAdyen);
models.load_fields('pos.payment.method', ['adyen_terminal_identifier', 'adyen_test_mode', 'adyen_api_key']);
models.load_fields('pos.payment.method', 'adyen_terminal_identifier');
});
@@ -48,7 +48,7 @@ var PaymentAdyen = PaymentInterface.extend({
return rpc.query({
model: 'pos.payment.method',
method: 'proxy_adyen_request',
args: [data, this.payment_method.adyen_test_mode, this.payment_method.adyen_api_key, test_endpoint, live_endpoint],
args: [[this.payment_method.id], data, test_endpoint, live_endpoint],
}, {
// When a payment terminal is disconnected it takes Adyen
// a while to return an error (~6s). So wait 10 seconds
@@ -174,11 +174,7 @@ var PaymentAdyen = PaymentInterface.extend({
return rpc.query({
model: 'pos.payment.method',
method: 'get_latest_adyen_status',
args: [this.payment_method.id,
this._adyen_get_sale_id(),
this.payment_method.adyen_terminal_identifier,
this.payment_method.adyen_test_mode,
this.payment_method.adyen_api_key],
args: [[this.payment_method.id], this._adyen_get_sale_id()],
}, {
timeout: 5000,
shadow: true,