| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
import logging |
| 4 |
import pprint |
| 5 |
from datetime import timedelta |
| 6 |
|
| 7 |
from werkzeug.exceptions import Forbidden |
| 8 |
|
| 9 |
from odoo import _, fields |
| 10 |
from odoo.exceptions import ValidationError |
| 11 |
from odoo.http import Controller, request, route |
| 12 |
|
| 13 |
from odoo.addons.payment_mercado_pago import const |
| 14 |
|
| 15 |
_logger = logging.getLogger(__name__) |
| 16 |
|
| 17 |
|
| 18 |
class MercadoPagoOnboardingController(Controller): |
| 19 |
|
| 20 |
@route(const.OAUTH_RETURN_ROUTE, type='http', auth='user', methods=['GET'], website=True) |
| 21 |
def mercado_pago_return_from_authorization(self, **data): |
| 22 |
"""Exchange the authorization code for an access token and redirect to the provider form. |
| 23 |
|
| 24 |
:param dict data: The authorization code received from Mercado Pago, in addition to the |
| 25 |
provided provider id and CSRF token that were sent back by the proxy. |
| 26 |
:raise Forbidden: If the received CSRF token cannot be verified. |
| 27 |
:raise ValidationError: If the provider id does not match any Mercado Pago provider. |
| 28 |
:return: Redirect to the payment provider form. |
| 29 |
""" |
| 30 |
_logger.info("Returning from authorization with data:\n%s", pprint.pformat(data)) |
| 31 |
|
| 32 |
# Retrieve the Mercado Pago data and Odoo metadata from the redirect data. |
| 33 |
provider_id = int(data['provider_id']) |
| 34 |
authorization_code = data.get('authorization_code') |
| 35 |
csrf_token = data.get('csrf_token') # Could be missing if authorization was cancelled. |
| 36 |
provider_sudo = request.env['payment.provider'].sudo().browse(provider_id).exists() |
| 37 |
if not provider_sudo or provider_sudo.code != 'mercado_pago': |
| 38 |
raise ValidationError(_("Could not find Mercado Pago provider %s", provider_sudo)) |
| 39 |
|
| 40 |
# Verify the CSRF token. |
| 41 |
if not request.validate_csrf(csrf_token): |
| 42 |
_logger.warning("CSRF token verification failed.") |
| 43 |
raise Forbidden() |
| 44 |
|
| 45 |
# Request and set the OAuth tokens on the provider. |
| 46 |
action = request.env.ref('payment.action_payment_provider') |
| 47 |
redirect_url = f'/odoo/action-{action.id}/{int(provider_sudo.id)}' |
| 48 |
if not authorization_code: # The user cancelled the authorization. |
| 49 |
return request.redirect(redirect_url) |
| 50 |
|
| 51 |
# Fetch an access token using the authorization token. |
| 52 |
proxy_payload = self.env['payment.provider']._prepare_json_rpc_payload( |
| 53 |
{ |
| 54 |
'authorization_code': authorization_code, |
| 55 |
'account_country_code': provider_sudo.mercado_pago_account_country_id.code.lower(), |
| 56 |
} |
| 57 |
) |
| 58 |
try: |
| 59 |
response_content = provider_sudo._send_api_request( |
| 60 |
'POST', '/get_access_token', json=proxy_payload, is_proxy_request=True, |
| 61 |
) |
| 62 |
except ValidationError as e: |
| 63 |
return request.render( |
| 64 |
'payment_mercado_pago.authorization_error', |
| 65 |
{'error_message': str(e), 'provider_url': redirect_url}, |
| 66 |
) |
| 67 |
# Backdate the access token expiry to refresh it before it expires, since the refresh token |
| 68 |
# would become unusable at that time (according to Mercado Pago's dev team). |
| 69 |
expires_in = ( |
| 70 |
fields.Datetime.now() |
| 71 |
+ timedelta(seconds=int(response_content['expires_in'])) |
| 72 |
- timedelta(days=31) |
| 73 |
) |
| 74 |
provider_sudo.write({ |
| 75 |
# Save the OAuth credentials. |
| 76 |
'mercado_pago_access_token': response_content['access_token'], |
| 77 |
'mercado_pago_refresh_token': response_content['refresh_token'], |
| 78 |
'mercado_pago_access_token_expiry': expires_in, |
| 79 |
'mercado_pago_public_key': response_content['public_key'], |
| 80 |
# Enable the provider. |
| 81 |
'state': 'enabled', |
| 82 |
'is_published': True, |
| 83 |
'allow_tokenization': True, |
| 84 |
}) |
| 85 |
|
| 86 |
return request.redirect(redirect_url) |