Files
odoo_source/addons/payment_flutterwave/models/payment_provider.py
T
Horacio Tellez f7b8f07501 [IMP] payment: rename of acquirer to provider
Changing the name of model payment.acquirer to payment.provider
and everything that it touches. It is technically incorrect to
use the term "acquirer" for systems that only provide a service
of payment.
After this commit the model payment.acquirer and all related to
it will be renamed to payment.provider.

Task - 2842088

closes odoo/odoo#90899

Related: odoo/upgrade#3542
Related: odoo/documentation#1981
Related: odoo/enterprise#27131
Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
2022-09-09 13:38:08 +02:00

110 lines
4.0 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
import logging
import pprint
import requests
from werkzeug.urls import url_join
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.addons.payment_flutterwave.const import SUPPORTED_CURRENCIES
_logger = logging.getLogger(__name__)
class PaymentProvider(models.Model):
_inherit = 'payment.provider'
code = fields.Selection(
selection_add=[('flutterwave', "Flutterwave")], ondelete={'flutterwave': 'set default'}
)
flutterwave_public_key = fields.Char(
string="Flutterwave Public Key",
help="The key solely used to identify the account with Flutterwave.",
required_if_provider='flutterwave',
)
flutterwave_secret_key = fields.Char(
string="Flutterwave Secret Key",
required_if_provider='flutterwave',
groups='base.group_system',
)
flutterwave_webhook_secret = fields.Char(
string="Flutterwave Webhook Secret",
required_if_provider='flutterwave',
groups='base.group_system',
)
#=== 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 == 'flutterwave').update({
'support_tokenization': True,
})
# === BUSINESS METHODS ===#
@api.model
def _get_compatible_providers(self, *args, currency_id=None, is_validation=False, **kwargs):
""" Override of payment to filter out Flutterwave providers for unsupported currencies or
for validation operations. """
providers = super()._get_compatible_providers(
*args, currency_id=currency_id, is_validation=is_validation, **kwargs
)
currency = self.env['res.currency'].browse(currency_id).exists()
if (currency and currency.name not in SUPPORTED_CURRENCIES) or is_validation:
providers = providers.filtered(lambda p: p.code != 'flutterwave')
return providers
def _flutterwave_make_request(self, endpoint, payload=None, method='POST'):
""" Make a request to Flutterwave API at the specified endpoint.
Note: self.ensure_one()
:param str endpoint: The endpoint to be reached by the request.
:param dict payload: The payload of the request.
:param str method: The HTTP method of the request.
:return The JSON-formatted content of the response.
:rtype: dict
:raise ValidationError: If an HTTP error occurs.
"""
self.ensure_one()
url = url_join('https://api.flutterwave.com/v3/', endpoint)
headers = {'Authorization': f'Bearer {self.flutterwave_secret_key}'}
try:
if method == 'GET':
response = requests.get(url, params=payload, headers=headers, timeout=10)
else:
response = requests.post(url, json=payload, headers=headers, timeout=10)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
_logger.exception(
"Invalid API request at %s with data:\n%s", url, pprint.pformat(payload),
)
raise ValidationError("Flutterwave: " + _(
"The communication with the API failed. Flutterwave gave us the following "
"information: '%s'", response.json().get('message', '')
))
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
_logger.exception("Unable to reach endpoint at %s", url)
raise ValidationError(
"Flutterwave: " + _("Could not establish the connection to the API.")
)
return response.json()
def _neutralize(self):
super()._neutralize()
self._neutralize_fields('flutterwave', [
'flutterwave_public_key',
'flutterwave_secret_key',
'flutterwave_webhook_secret',
])