| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
|
| 3 |
import pprint |
| 4 |
|
| 5 |
from odoo import http |
| 6 |
from odoo.exceptions import ValidationError |
| 7 |
from odoo.http import request |
| 8 |
|
| 9 |
from odoo.addons.payment import utils as payment_utils |
| 10 |
from odoo.addons.payment.logging import get_payment_logger |
| 11 |
|
| 12 |
from odoo.addons.payment_mercado_pago import const |
| 13 |
|
| 14 |
|
| 15 |
_logger = get_payment_logger(__name__) |
| 16 |
|
| 17 |
|
| 18 |
class MercadoPagoPaymentController(http.Controller): |
| 19 |
|
| 20 |
@http.route('/payment/mercado_pago/payments', type='jsonrpc', auth='public') |
| 21 |
def mercado_pago_payment( |
| 22 |
self, reference, transaction_amount, token, installments, payment_method_brand, issuer_id |
| 23 |
): |
| 24 |
"""Make a payment request and process the payment data. |
| 25 |
|
| 26 |
:param str reference: The reference of the transaction |
| 27 |
:param int transaction_amount: The amount of the transaction in minor units of the currency |
| 28 |
:param int token: The transaction token of card received from Mercado Pago. |
| 29 |
:param int installments: |
| 30 |
:param int payment_method_brand: The payment method of the transaction. |
| 31 |
:param int issuer_id: The issuer id of the card received from Mercado Pago. |
| 32 |
:rtype: None |
| 33 |
""" |
| 34 |
tx_sudo = request.env['payment.transaction'].sudo()._search_by_reference( |
| 35 |
'mercado_pago', {'external_reference': reference} |
| 36 |
) |
| 37 |
payload = tx_sudo._mercado_pago_prepare_payment_request_payload() |
| 38 |
payload.update({ |
| 39 |
'transaction_amount': float(transaction_amount), |
| 40 |
'token': token, |
| 41 |
'installments': installments, |
| 42 |
'payment_method_id': payment_method_brand, |
| 43 |
'issuer_id': issuer_id, |
| 44 |
}) |
| 45 |
response_content = tx_sudo._send_api_request( |
| 46 |
'POST', |
| 47 |
endpoint='/v1/payments', |
| 48 |
json=payload, |
| 49 |
idempotency_key=payment_utils.generate_idempotency_key(tx_sudo, scope='direct_payment'), |
| 50 |
) |
| 51 |
tx_sudo._process( |
| 52 |
'mercado_pago', dict(response_content, merchantReference=reference, token=token) |
| 53 |
) |
| 54 |
|
| 55 |
@http.route(const.PAYMENT_RETURN_ROUTE, type='http', methods=['GET'], auth='public') |
| 56 |
def mercado_pago_return_from_checkout(self, **data): |
| 57 |
"""Process the payment data sent by Mercado Pago after redirection from checkout. |
| 58 |
|
| 59 |
:param dict data: The payment data. |
| 60 |
""" |
| 61 |
_logger.info("Handling redirection from Mercado Pago with data:\n%s", pprint.pformat(data)) |
| 62 |
if data.get('payment_id') != 'null': |
| 63 |
self._verify_and_process(data) |
| 64 |
else: # The customer cancelled the payment by clicking on the return button. |
| 65 |
pass # Don't try to process this case because the payment id was not provided. |
| 66 |
|
| 67 |
# Redirect the user to the status page. |
| 68 |
return request.redirect('/payment/status') |
| 69 |
|
| 70 |
@http.route( |
| 71 |
f'{const.WEBHOOK_ROUTE}/<reference>', type='http', auth='public', methods=['POST'], |
| 72 |
csrf=False |
| 73 |
) |
| 74 |
def mercado_pago_webhook(self, reference, **_kwargs): |
| 75 |
""" Process the payment data sent by Mercado Pago to the webhook. |
| 76 |
|
| 77 |
:param str reference: The transaction reference embedded in the webhook URL. |
| 78 |
:param dict _kwargs: The extra query parameters. |
| 79 |
:return: An empty string to acknowledge the notification. |
| 80 |
:rtype: str |
| 81 |
""" |
| 82 |
data = request.get_json_data() |
| 83 |
_logger.info("Notification received from Mercado Pago with data:\n%s", pprint.pformat(data)) |
| 84 |
|
| 85 |
# Mercado Pago sends two types of asynchronous notifications: webhook notifications and |
| 86 |
# IPNs which are very similar to webhook notifications but are sent later and contain less |
| 87 |
# information. Therefore, we filter the notifications we receive based on the 'action' |
| 88 |
# (type of event) key as it is not populated for IPNs, and we don't want to process the |
| 89 |
# other types of events. |
| 90 |
if data.get('action') in ('payment.created', 'payment.updated'): |
| 91 |
self._verify_and_process( |
| 92 |
{'external_reference': reference, 'payment_id': data.get('data', {}).get('id')} |
| 93 |
) # Use 'external_reference' as the reference key like in the redirect data. |
| 94 |
return '' # Acknowledge the notification. |
| 95 |
|
| 96 |
@staticmethod |
| 97 |
def _verify_and_process(data): |
| 98 |
"""Verify and process the payment data sent by Mercado Pago. |
| 99 |
|
| 100 |
:param dict data: The payment data. |
| 101 |
:return: None |
| 102 |
""" |
| 103 |
tx_sudo = request.env['payment.transaction'].sudo()._search_by_reference( |
| 104 |
'mercado_pago', data |
| 105 |
) |
| 106 |
if not tx_sudo: |
| 107 |
return |
| 108 |
|
| 109 |
try: |
| 110 |
verified_data = tx_sudo._send_api_request( |
| 111 |
'GET', f'/v1/payments/{data.get("payment_id")}' |
| 112 |
) |
| 113 |
except ValidationError: |
| 114 |
_logger.error("Unable to verify the payment data") |
| 115 |
else: |
| 116 |
tx_sudo._process('mercado_pago', verified_data) |