Files
odoo_source/addons/payment_buckaroo/models/payment_transaction.py
T
Antoine Vandevenne (anv) f4ca7290ac [IMP] payment(_*): search only once for the transaction
Before this commit, most acquirers needed to run several successive
searches for the transaction whose reference was received by a
controller in notification data. This is because the security checks
run on the notification data require access to the acquirer through the
transaction record which was immediately discarded.

Starting with this commit, all `*_feedback_data` method are no longer
decorated with `api.model` and can use the transaction record they're
called on if provided. They are also renamed to `*_notification_data`.

task-2737144

closes odoo/odoo#83850

Related: odoo/enterprise#23938
Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
2022-02-02 19:50:49 +00:00

114 lines
4.8 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
import logging
from werkzeug import urls
from odoo import _, api, models
from odoo.exceptions import ValidationError
from odoo.addons.payment_buckaroo.const import STATUS_CODES_MAPPING
from odoo.addons.payment_buckaroo.controllers.main import BuckarooController
_logger = logging.getLogger(__name__)
class PaymentTransaction(models.Model):
_inherit = 'payment.transaction'
def _get_specific_rendering_values(self, processing_values):
""" Override of payment to return Buckaroo-specific rendering values.
Note: self.ensure_one() from `_get_processing_values`
:param dict processing_values: The generic and specific processing values of the transaction
:return: The dict of acquirer-specific processing values
:rtype: dict
"""
res = super()._get_specific_rendering_values(processing_values)
if self.provider != 'buckaroo':
return res
return_url = urls.url_join(self.acquirer_id.get_base_url(), BuckarooController._return_url)
rendering_values = {
'api_url': self.acquirer_id._buckaroo_get_api_url(),
'Brq_websitekey': self.acquirer_id.buckaroo_website_key,
'Brq_amount': self.amount,
'Brq_currency': self.currency_id.name,
'Brq_invoicenumber': self.reference,
# Include all 4 URL keys despite they share the same value as they are part of the sig.
'Brq_return': return_url,
'Brq_returncancel': return_url,
'Brq_returnerror': return_url,
'Brq_returnreject': return_url,
}
if self.partner_lang:
rendering_values['Brq_culture'] = self.partner_lang.replace('_', '-')
rendering_values['Brq_signature'] = self.acquirer_id._buckaroo_generate_digital_sign(
rendering_values, incoming=False
)
return rendering_values
def _get_tx_from_notification_data(self, provider, notification_data):
""" Override of payment to find the transaction based on Buckaroo data.
:param str provider: The provider of the acquirer that handled the transaction
:param dict notification_data: The normalized notification data sent by the provider
:return: The transaction if found
:rtype: recordset of `payment.transaction`
:raise: ValidationError if the data match no transaction
"""
tx = super()._get_tx_from_notification_data(provider, notification_data)
if provider != 'buckaroo' or len(tx) == 1:
return tx
reference = notification_data.get('brq_invoicenumber')
tx = self.search([('reference', '=', reference), ('provider', '=', 'buckaroo')])
if not tx:
raise ValidationError(
"Buckaroo: " + _("No transaction found matching reference %s.", reference)
)
return tx
def _process_notification_data(self, notification_data):
""" Override of payment to process the transaction based on Buckaroo data.
Note: self.ensure_one()
:param dict notification_data: The normalized notification data sent by the provider
:return: None
:raise: ValidationError if inconsistent data were received
"""
super()._process_notification_data(notification_data)
if self.provider != 'buckaroo':
return
transaction_keys = notification_data.get('brq_transactions')
if not transaction_keys:
raise ValidationError("Buckaroo: " + _("Received data with missing transaction keys"))
# BRQ_TRANSACTIONS can hold multiple, comma-separated, tx keys. In practice, it holds only
# one reference. So we split for semantic correctness and keep the first transaction key.
self.acquirer_reference = transaction_keys.split(',')[0]
status_code = int(notification_data.get('brq_statuscode') or 0)
if status_code in STATUS_CODES_MAPPING['pending']:
self._set_pending()
elif status_code in STATUS_CODES_MAPPING['done']:
self._set_done()
elif status_code in STATUS_CODES_MAPPING['cancel']:
self._set_canceled()
elif status_code in STATUS_CODES_MAPPING['refused']:
self._set_error(_("Your payment was refused (code %s). Please try again.", status_code))
elif status_code in STATUS_CODES_MAPPING['error']:
self._set_error(_(
"An error occurred during processing of your payment (code %s). Please try again.",
status_code,
))
else:
_logger.warning(
"received data with invalid payment status (%s) for transaction with reference %s",
status_code, self.reference
)
self._set_error("Buckaroo: " + _("Unknown status code: %s", status_code))