Files
odoo_source/addons/payment_aps/models/payment_transaction.py
T
7e012dd544 [IMP] payment, *: replace providers with payment methods in payment forms
Before this commit, the payment providers (e.g., Stripe, Adyen...)
available for payment were displayed on the payment forms. The customer
had to select one to process their payment. After that, the customer had
to select their preferred payment method (e.g., Credit Card,
Bancontact...) from a list of payment methods supported by the selected
provider over which the website administrator had close to no control.
This was making the payment forms confusing because the payment methods
were displayed sometimes more than once, if at all, in a non-controlled
order, and behind the selection of a payment provider that customers
should not have to deal with.

As the payment method was selected in an iframe or directly on the
provider's website, the information on the selection payment method was
not available in Odoo. This posed many problems, among which were the
impossibility of assessing whether a specific feature (e.g.,
tokenization, refunds, manual capture...) was available, not being able
to easily identify payment tokens through the payment method logo,
listing available payment methods on the website, sorting and
fine-grained configuration of the available payment method, subpar
payment method-specific display on the payment form (e.g., PayPal that
requires displaying a "Pay with PayPal" button), etc.

In this commit, the payment providers are thus replaced by the payment
methods on the payment forms. All contextually available (depending on
the country, currency, requested feature...) payment methods are
displayed one after the other on a single-level list and in the order
configured by the website administrator. Each payment method is
"powered by" (i.e., linked) to a single payment provider: the first one,
by model order, to support it. This allows, for example, offering the
PayPal payment method through Mollie, which charges low processing fees,
while also offering Klarna through Stripe, which supports more payment
methods but charges higher processing fees.

While doing so, the two different payment forms, "Checkout" and
"Manage", are also merged together in a new, configurable case-by-case,
payment form that is entirely redesigned to offer a better user
experience.

After payment, the information on the selected payment method is saved
on the transaction and eventual payment record and updated with the
information received from the provider.

task-2882677

closes odoo/odoo#120446

Related: odoo/upgrade#5103
Related: odoo/documentation#5717
Related: odoo/enterprise#40666
Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
Co-authored-by: Anita (anko) <anko@odoo.com>
Co-authored-by: Brieuc-brd <brd@odoo.com>
Co-authored-by: Valeriya (vchu) <vchu@odoo.com>
2023-09-22 13:32:53 +00:00

148 lines
6.3 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 import utils as payment_utils
from odoo.addons.payment_aps import utils as aps_utils
from odoo.addons.payment_aps.const import PAYMENT_STATUS_MAPPING
from odoo.addons.payment_aps.controllers.main import APSController
_logger = logging.getLogger(__name__)
class PaymentTransaction(models.Model):
_inherit = 'payment.transaction'
@api.model
def _compute_reference(self, provider_code, prefix=None, separator='-', **kwargs):
""" Override of `payment` to ensure that APS' requirements for references are satisfied.
APS' requirements for transaction are as follows:
- References can only be made of alphanumeric characters and/or '-' and '_'.
The prefix is generated with 'tx' as default. This prevents the prefix from being
generated based on document names that may contain non-allowed characters
(eg: INV/2020/...).
:param str provider_code: The code of the provider handling the transaction.
:param str prefix: The custom prefix used to compute the full reference.
:param str separator: The custom separator used to separate the prefix from the suffix.
:return: The unique reference for the transaction.
:rtype: str
"""
if provider_code == 'aps':
prefix = payment_utils.singularize_reference_prefix()
return super()._compute_reference(provider_code, prefix=prefix, separator=separator, **kwargs)
def _get_specific_rendering_values(self, processing_values):
""" Override of `payment` to return APS-specific processing values.
Note: self.ensure_one() from `_get_processing_values`
:param dict processing_values: The generic processing values of the transaction.
:return: The dict of provider-specific processing values.
:rtype: dict
"""
res = super()._get_specific_rendering_values(processing_values)
if self.provider_code != 'aps':
return res
converted_amount = payment_utils.to_minor_currency_units(self.amount, self.currency_id)
base_url = self.provider_id.get_base_url()
payment_option = aps_utils.get_payment_option(self.payment_method_id.code)
rendering_values = {
'command': 'PURCHASE',
'access_code': self.provider_id.aps_access_code,
'merchant_identifier': self.provider_id.aps_merchant_identifier,
'merchant_reference': self.reference,
'amount': str(converted_amount),
'currency': self.currency_id.name,
'language': self.partner_lang[:2],
'customer_email': self.partner_id.email_normalized,
'return_url': urls.url_join(base_url, APSController._return_url),
}
if payment_option: # Not included if the payment method is 'card'.
rendering_values['payment_option'] = payment_option
rendering_values.update({
'signature': self.provider_id._aps_calculate_signature(
rendering_values, incoming=False
),
'api_url': self.provider_id._aps_get_api_url(),
})
return rendering_values
def _get_tx_from_notification_data(self, provider_code, notification_data):
""" Override of `payment` to find the transaction based on APS data.
:param str provider_code: The code of the provider that handled the transaction.
:param dict notification_data: The notification data sent by the provider.
:return: The transaction if found.
:rtype: recordset of `payment.transaction`
:raise ValidationError: If inconsistent data are received.
:raise ValidationError: If the data match no transaction.
"""
tx = super()._get_tx_from_notification_data(provider_code, notification_data)
if provider_code != 'aps' or len(tx) == 1:
return tx
reference = notification_data.get('merchant_reference')
if not reference:
raise ValidationError(
"APS: " + _("Received data with missing reference %(ref)s.", ref=reference)
)
tx = self.search([('reference', '=', reference), ('provider_code', '=', 'aps')])
if not tx:
raise ValidationError(
"APS: " + _("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 APS data.
Note: self.ensure_one()
:param dict notification_data: The notification data sent by the provider.
:return: None
:raise ValidationError: If inconsistent data are received.
"""
super()._process_notification_data(notification_data)
if self.provider_code != 'aps':
return
# Update the provider reference.
self.provider_reference = notification_data.get('fort_id')
# Update the payment method.
payment_option = notification_data.get('payment_option', '')
payment_method = self.env['payment.method']._get_from_code(payment_option.lower())
self.payment_method_id = payment_method or self.payment_method_id
# Update the payment state.
status = notification_data.get('status')
if not status:
raise ValidationError("APS: " + _("Received data with missing payment state."))
if status in PAYMENT_STATUS_MAPPING['pending']:
self._set_pending()
elif status in PAYMENT_STATUS_MAPPING['done']:
self._set_done()
else: # Classify unsupported payment state as `error` tx state.
status_description = notification_data.get('response_message')
_logger.info(
"Received data with invalid payment status (%(status)s) and reason '%(reason)s' "
"for transaction with reference %(ref)s",
{'status': status, 'reason': status_description, 'ref': self.reference},
)
self._set_error("APS: " + _(
"Received invalid transaction status %(status)s and reason '%(reason)s'.",
status=status, reason=status_description
))