| 1 |
import logging |
| 2 |
import requests |
| 3 |
|
| 4 |
_logger = logging.getLogger(__name__) |
| 5 |
|
| 6 |
|
| 7 |
REQUEST_TIMEOUT = 10 |
| 8 |
MERCADO_PAGO_API_ENDPOINT = 'https://api.mercadopago.com' |
| 9 |
|
| 10 |
|
| 11 |
class MercadoPagoPosRequest: |
| 12 |
def __init__(self, mp_bearer_token): |
| 13 |
self.mercado_pago_bearer_token = mp_bearer_token |
| 14 |
|
| 15 |
def call_mercado_pago(self, method, endpoint, payload): |
| 16 |
""" Make a request to Mercado Pago POS API. |
| 17 |
|
| 18 |
:param method: "GET", "POST", ... |
| 19 |
:param endpoint: The endpoint to be reached by the request. |
| 20 |
:param payload: The payload of the request. |
| 21 |
:return The JSON-formatted content of the response. |
| 22 |
|
| 23 |
Note: The platform id below is not secret, and is just used to |
| 24 |
quantify the amount of Odoo users on Mercado's backend. |
| 25 |
""" |
| 26 |
endpoint = MERCADO_PAGO_API_ENDPOINT + endpoint |
| 27 |
header = { |
| 28 |
'Authorization': f"Bearer {self.mercado_pago_bearer_token}", |
| 29 |
'X-platform-id': "dev_cdf1cfac242111ef9fdebe8d845d0987" |
| 30 |
} |
| 31 |
try: |
| 32 |
response = requests.request(method, endpoint, headers=header, json=payload, timeout=REQUEST_TIMEOUT) |
| 33 |
return response.json() |
| 34 |
except requests.exceptions.RequestException as error: |
| 35 |
_logger.warning("Cannot connect with Mercado Pago POS. Error: %s", error) |
| 36 |
return {'errorMessage': str(error)} |
| 37 |
except ValueError as error: |
| 38 |
_logger.warning("Cannot decode response json. Error: %s", error) |
| 39 |
return {'errorMessage': f"Cannot decode Mercado Pago POS response. Error: {error}"} |