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>
124 lines
4.7 KiB
Python
124 lines
4.7 KiB
Python
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
import json
|
|
import logging
|
|
import pprint
|
|
|
|
from odoo import _, api, fields, models
|
|
from odoo.exceptions import UserError, ValidationError
|
|
from odoo.fields import Command
|
|
|
|
from odoo.addons.payment_authorize.models.authorize_request import AuthorizeAPI
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class PaymentProvider(models.Model):
|
|
_inherit = 'payment.provider'
|
|
|
|
code = fields.Selection(
|
|
selection_add=[('authorize', 'Authorize.Net')], ondelete={'authorize': 'set default'})
|
|
authorize_login = fields.Char(
|
|
string="API Login ID", help="The ID solely used to identify the account with Authorize.Net",
|
|
required_if_provider='authorize')
|
|
authorize_transaction_key = fields.Char(
|
|
string="API Transaction Key", required_if_provider='authorize', groups='base.group_system')
|
|
authorize_signature_key = fields.Char(
|
|
string="API Signature Key", required_if_provider='authorize', groups='base.group_system')
|
|
authorize_client_key = fields.Char(
|
|
string="API Client Key",
|
|
help="The public client key. To generate directly from Odoo or from Authorize.Net backend.")
|
|
|
|
# === CONSTRAINT METHODS ===#
|
|
|
|
# Authorize.Net supports only one currency: "One gateway account is required for each currency"
|
|
# See https://community.developer.authorize.net/t5/The-Authorize-Net-Developer-Blog/Authorize-Net-UK-Europe-Update/ba-p/35957
|
|
@api.constrains('available_currency_ids', 'state')
|
|
def _limit_available_currency_ids(self):
|
|
for provider in self.filtered(lambda p: p.code == 'authorize'):
|
|
if len(provider.available_currency_ids) > 1 and provider.state != 'disabled':
|
|
raise ValidationError(
|
|
_("Only one currency can be selected by Authorize.Net account.")
|
|
)
|
|
|
|
#=== COMPUTE METHODS ===#
|
|
|
|
def _compute_feature_support_fields(self):
|
|
""" Override of `payment` to enable additional features. """
|
|
super()._compute_feature_support_fields()
|
|
self.filtered(lambda p: p.code == 'authorize').update({
|
|
'support_manual_capture': 'full_only',
|
|
'support_refund': 'full_only',
|
|
'support_tokenization': True,
|
|
})
|
|
|
|
# === ACTION METHODS ===#
|
|
|
|
def action_update_merchant_details(self):
|
|
""" Fetch the merchant details to update the client key and the account currency. """
|
|
self.ensure_one()
|
|
|
|
if self.state == 'disabled':
|
|
raise UserError(_("This action cannot be performed while the provider is disabled."))
|
|
|
|
authorize_API = AuthorizeAPI(self)
|
|
|
|
# Validate the API Login ID and Transaction Key
|
|
res_content = authorize_API.test_authenticate()
|
|
_logger.info("test_authenticate request response:\n%s", pprint.pformat(res_content))
|
|
if res_content.get('err_msg'):
|
|
raise UserError(_("Failed to authenticate.\n%s", res_content['err_msg']))
|
|
|
|
# Update the merchant details
|
|
res_content = authorize_API.merchant_details()
|
|
_logger.info("merchant_details request response:\n%s", pprint.pformat(res_content))
|
|
if res_content.get('err_msg'):
|
|
raise UserError(_("Could not fetch merchant details:\n%s", res_content['err_msg']))
|
|
|
|
currency = self.env['res.currency'].search([('name', 'in', res_content.get('currencies'))])
|
|
self.available_currency_ids = [Command.set(currency.ids)]
|
|
self.authorize_client_key = res_content.get('publicClientKey')
|
|
|
|
# === BUSINESS METHODS ===#
|
|
|
|
def _get_validation_amount(self):
|
|
""" Override of payment to return the amount for Authorize.Net validation operations.
|
|
|
|
:return: The validation amount
|
|
:rtype: float
|
|
"""
|
|
res = super()._get_validation_amount()
|
|
if self.code != 'authorize':
|
|
return res
|
|
|
|
return 0.01
|
|
|
|
def _get_validation_currency(self):
|
|
""" Override of payment to return the currency for Authorize.Net validation operations.
|
|
|
|
:return: The validation currency
|
|
:rtype: recordset of `res.currency`
|
|
"""
|
|
res = super()._get_validation_currency()
|
|
if self.code != 'authorize':
|
|
return res
|
|
|
|
return self.available_currency_ids[0]
|
|
|
|
def _authorize_get_inline_form_values(self):
|
|
""" Return a serialized JSON of the required values to render the inline form.
|
|
|
|
Note: `self.ensure_one()`
|
|
|
|
:return: The JSON serial of the required values to render the inline form.
|
|
:rtype: str
|
|
"""
|
|
self.ensure_one()
|
|
|
|
inline_form_values = {
|
|
'state': self.state,
|
|
'login_id': self.authorize_login,
|
|
'client_key': self.authorize_client_key,
|
|
}
|
|
return json.dumps(inline_form_values)
|