Files
odoo_source/addons/account_payment/controllers/portal.py
T
Horacio Tellez c1aa010af7 [IMP] payment: filter out acquirers based on the amount
This commit adds the possibility to define a maximum payment amount that
a given acquirer can process. If the payment amount exceeds the value,
the acquirer is filtered out of the available acquirers listed on the
payment forms.

While we're at it, the field `country_ids` is renamed to
`available_country_ids` to better depict that it is not a property of
the acquirer, but a configuration option. It will also be coherent with
the field `available_currency_ids` that is expected to be added soon.

Task - 2162165

closes odoo/odoo#82411

Related: odoo/enterprise#24412
Related: odoo/upgrade#3703
Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
2022-07-26 13:24:57 +02:00

58 lines
2.8 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.http import request
from odoo.addons.account.controllers import portal
from odoo.addons.payment.controllers.portal import PaymentPortal
from odoo.addons.portal.controllers.portal import _build_url_w_params
class PortalAccount(portal.PortalAccount):
def _invoice_get_page_view_values(self, invoice, access_token, **kwargs):
values = super()._invoice_get_page_view_values(invoice, access_token, **kwargs)
logged_in = not request.env.user._is_public()
# We set partner_id to the partner id of the current user if logged in, otherwise we set it
# to the invoice partner id. We do this to ensure that payment tokens are assigned to the
# correct partner and to avoid linking tokens to the public user.
partner = request.env.user.partner_id if logged_in else invoice.partner_id
# Make sure that the partner's company matches the invoice's company.
invoice_company = invoice.company_id or request.env.company
PaymentPortal._ensure_matching_companies(partner, invoice_company)
acquirers_sudo = request.env['payment.acquirer'].sudo()._get_compatible_acquirers(
invoice_company.id, partner.id, invoice.amount_total, currency_id=invoice.currency_id.id
) # In sudo mode to read the fields of acquirers and partner (if not logged in)
tokens = request.env['payment.token'].search(
[('acquirer_id', 'in', acquirers_sudo.ids), ('partner_id', '=', partner.id)]
) # Tokens are cleared at the end if the user is not logged in
fees_by_acquirer = {
acq_sudo: acq_sudo._compute_fees(
invoice.amount_total, invoice.currency_id, invoice.partner_id.country_id
) for acq_sudo in acquirers_sudo.filtered('fees_active')
}
values.update({
'acquirers': acquirers_sudo,
'tokens': tokens,
'fees_by_acquirer': fees_by_acquirer,
'show_tokenize_input': PaymentPortal._compute_show_tokenize_input_mapping(
acquirers_sudo, logged_in=logged_in
),
'amount': invoice.amount_residual,
'currency': invoice.currency_id,
'partner_id': partner.id,
'access_token': access_token,
'transaction_route': f'/invoice/transaction/{invoice.id}/',
'landing_route': _build_url_w_params(invoice.access_url, {'access_token': access_token})
})
if not logged_in:
# Don't display payment tokens of the invoice partner if the user is not logged in, but
# inform that logging in will make them available.
values.update({
'existing_token': bool(tokens),
'tokens': request.env['payment.token'],
})
return values