Files
odoo_source/addons/payment_mercado_pago/models/payment_provider.py
T
Laurent Desausoi 7593c073d2 [IMP] core: use inert SQL based neutralization
Before this commit the neutralize system introduced in v16 was using ORM
methods in order to change appropriate records. Although flexible, this approach
could lead to call some methods with side effects while neutralizing
(eg: overloads of write).

This patch converts the neutralize system to a safer "inert" SQL based approach
by migrating the generic method _neutralize to SQL files exposed in the
data folder.

Task id: 2961687

closes odoo/odoo#102792

X-original-commit: e5dbded9bb363351feff7ca8a56c7f8a6860f492
Related: odoo/enterprise#32580
Signed-off-by: Fabien Meghazi <fme@odoo.com>
2022-10-09 22:04:00 +02:00

83 lines
3.2 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
import logging
import pprint
import requests
from werkzeug import urls
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.addons.payment_mercado_pago.const import SUPPORTED_CURRENCIES
_logger = logging.getLogger(__name__)
class Paymentprovider(models.Model):
_inherit = 'payment.provider'
code = fields.Selection(
selection_add=[('mercado_pago', "Mercado Pago")], ondelete={'mercado_pago': 'set default'}
)
mercado_pago_access_token = fields.Char(
string="Mercado Pago Access Token",
required_if_provider='mercado_pago',
groups='base.group_system',
)
# === BUSINESS METHODS === #
@api.model
def _get_compatible_providers(self, *args, currency_id=None, **kwargs):
""" Override of `payment` to unlist Mercado Pago providers for unsupported currencies. """
providers = super()._get_compatible_providers(*args, currency_id=currency_id, **kwargs)
currency = self.env['res.currency'].browse(currency_id).exists()
if currency and currency.name not in SUPPORTED_CURRENCIES:
providers = providers.filtered(lambda p: p.code != 'mercado_pago')
return providers
def _mercado_pago_make_request(self, endpoint, payload=None, method='POST'):
""" Make a request to Mercado Pago 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 = urls.url_join('https://api.mercadopago.com', endpoint)
headers = {'Authorization': f'Bearer {self.mercado_pago_access_token}'}
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),
)
response_content = response.json()
error_code = response_content.get('error')
error_message = response_content.get('message')
raise ValidationError("Mercado Pago: " + _(
"The communication with the API failed. Mercado Pago gave us the following "
"information: '%s' (code %s)", error_message, error_code
))
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
_logger.exception("Unable to reach endpoint at %s", url)
raise ValidationError(
"Mercado Pago: " + _("Could not establish the connection to the API.")
)
return response.json()