Skip to content

Commit 9f516c4

Browse files
committed
[FIX] pos_mercado_pago: Use unique identifier for payment intents.
For Mercado Pago to recommend odoo to customers they needed two changes: 1) Send a unique external reference to identify payments 2) Send the platform-id header to identify how many people use the integration. This PR addresses both: 1) Utilize the pos_reference field (uid) to identify records 2) Use a system parameter to store the platform id special value we needed to send for identification. task-4075847 closes odoo#177845 X-original-commit: e87d682 Signed-off-by: Andrew Gavgavian (andg) <[email protected]>
1 parent 0197a45 commit 9f516c4

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

addons/pos_mercado_pago/controllers/main.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ def notification(self):
4646

4747
# If and only if this webhook is related with a payment intend (see payment_mercado_pago.js)
4848
# then the field data['additional_info']['external_reference'] contains a string
49-
# formated like "XXX_YYY" where "XXX" is the session_id and "YYY" is the payment_method_id
49+
# formated like "XXX_YYY_ZZZ" where "XXX" is the session_id, "YYY" is the payment_method_id,
50+
# and ZZZ is the pos_reference/uid for customer identification (Format ZZZZ-ZZZZ-ZZZZ)
5051
external_reference = data.get('additional_info', {}).get('external_reference')
5152

52-
if not external_reference or not re.fullmatch(r'\d+_\d+', external_reference):
53+
if not external_reference or not re.fullmatch(r'\d+_\d+[_\d-]*', external_reference):
5354
_logger.debug('POST message received with no or malformed "external_reference" key')
5455
return http.Response(status=400)
5556

56-
session_id, payment_method_id = external_reference.split('_')
57+
session_id, payment_method_id, _ = external_reference.split('_')
5758

5859
pos_session_sudo = request.env['pos.session'].sudo().browse(int(session_id))
5960
if not pos_session_sudo or pos_session_sudo.state != 'opened':

addons/pos_mercado_pago/models/mercado_pago_pos_request.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ def call_mercado_pago(self, method, endpoint, payload):
1919
:param endpoint: The endpoint to be reached by the request.
2020
:param payload: The payload of the request.
2121
: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.
2225
"""
2326
endpoint = MERCADO_PAGO_API_ENDPOINT + endpoint
24-
header = {'Authorization': f"Bearer {self.mercado_pago_bearer_token}"}
27+
header = {
28+
'Authorization': f"Bearer {self.mercado_pago_bearer_token}",
29+
'X-platform-id': "dev_cdf1cfac242111ef9fdebe8d845d0987"
30+
}
2531
try:
2632
response = requests.request(method, endpoint, headers=header, json=payload, timeout=REQUEST_TIMEOUT)
2733
return response.json()

addons/pos_mercado_pago/static/src/app/payment_mercado_pago.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import { AlertDialog } from "@web/core/confirmation_dialog/confirmation_dialog";
55

66
export class PaymentMercadoPago extends PaymentInterface {
77
async create_payment_intent() {
8-
const line = this.pos.get_order().selected_paymentline;
8+
const order = this.pos.get_order();
9+
const line = order.selected_paymentline;
910
// Build informations for creating a payment intend on Mercado Pago.
1011
// Data in "external_reference" are send back with the webhook notification
1112
const infos = {
1213
amount: parseInt(line.amount * 100, 10),
1314
additional_info: {
14-
external_reference: `${this.pos.config.current_session_id.id}_${line.payment_method.id}`,
15+
external_reference: `${this.pos.config.current_session_id.id}_${line.payment_method.id}_${order.uid}`,
1516
print_on_terminal: true,
1617
},
1718
};

0 commit comments

Comments
 (0)