From 9a2ba941c58a7d623758ffd5335206d1cc727604 Mon Sep 17 00:00:00 2001 From: Jordi Ballester Alomar Date: Fri, 3 Jan 2025 20:14:15 +0100 Subject: [PATCH] [IMP] account_statement_import_online_gocardless: use a hashing strategy to produce the unique_transaction_id --- .../models/online_bank_statement_provider.py | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/account_statement_import_online_gocardless/models/online_bank_statement_provider.py b/account_statement_import_online_gocardless/models/online_bank_statement_provider.py index 11875253b..57f28aaf2 100644 --- a/account_statement_import_online_gocardless/models/online_bank_statement_provider.py +++ b/account_statement_import_online_gocardless/models/online_bank_statement_provider.py @@ -1,6 +1,7 @@ # Copyright 2022 ForgeFlow S.L. # Copyright 2023-2024 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import hashlib import json from datetime import datetime from uuid import uuid4 @@ -367,11 +368,7 @@ def _gocardless_obtain_statement_data(self, date_since, date_until): "date": current_date, "ref": partner_name or "/", "payment_ref": payment_ref, - "unique_import_id": ( - tr.get("entryReference") - or tr.get("transactionId") - or tr.get("internalTransactionId") - ), + "unique_import_id": self._gocardless_get_unique_import_id(tr), "amount": amount_currency, "account_number": account_number, "partner_name": partner_name, @@ -381,6 +378,25 @@ def _gocardless_obtain_statement_data(self, date_since, date_until): ) return res, {} + def _gocardless_prepare_unique_import_id_hasher(self, hasher, tr): + """Inherit this method if you need to add more fields + to produce a unique identifier for this transaction""" + transactionId = tr.get("transactionId", False) + entryReference = tr.get("entryReference", False) + internalTransactionId = tr.get("internalTransactionId", False) + if transactionId: + hasher.update(transactionId.encode("utf-8")) + if entryReference: + hasher.update(entryReference.encode("utf-8")) + if internalTransactionId: + hasher.update(internalTransactionId.encode("utf-8")) + return hasher + + def _gocardless_get_unique_import_id(self, tr): + hasher = hashlib.sha1() + hasher = self._gocardless_prepare_unique_import_id_hasher(hasher, tr) + return hasher.hexdigest() + def _gocardless_get_note(self, tr): """Override to get different notes.""" note_elements = [