From 8342b5905959cc08c0f2ecefbadd6dfada9742d1 Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Fri, 17 Dec 2021 12:17:46 +0100 Subject: [PATCH] [IMP] *_online_adyen base64 encode retrieved file --- .../models/online_bank_statement_provider.py | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/account_bank_statement_import_online_adyen/models/online_bank_statement_provider.py b/account_bank_statement_import_online_adyen/models/online_bank_statement_provider.py index 99f73aa9b9..04fbb1cc43 100644 --- a/account_bank_statement_import_online_adyen/models/online_bank_statement_provider.py +++ b/account_bank_statement_import_online_adyen/models/online_bank_statement_provider.py @@ -1,5 +1,6 @@ # Copyright 2021 Therp BV . # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import base64 import logging from html import escape @@ -40,9 +41,9 @@ def _pull(self, date_since, date_until): # noqa: C901 for provider in adyen_providers: is_scheduled = self.env.context.get("scheduled") try: - data_file, filename = self._adyen_get_settlement_details_file() + attachment_vals = self._get_attachment_vals() import_wizard = self.env["account.bank.statement.import"].create( - {"attachment_ids": [(0, 0, {"name": filename, "datas": data_file})]} + {"attachment_ids": [(0, 0, attachment_vals)]} ) import_wizard.with_context( {"account_bank_statement_import_adyen": True} @@ -67,6 +68,17 @@ def _pull(self, date_since, date_until): # noqa: C901 if is_scheduled: provider._schedule_next_run() + def _get_attachment_vals(self): + """Retrieve settlement details and convert to attachment vals.""" + content, filename = self._adyen_get_settlement_details_file() + encoded_content = base64.encodebytes(content) + # Make sure base64 encoded content contains multiple of 4 bytes. + byte_count = len(encoded_content) + byte_padding = b"=" * (byte_count % 4) + data_file = encoded_content + byte_padding + attachment_vals = {"name": filename, "datas": data_file} + return attachment_vals + def _adyen_get_settlement_details_file(self): """Retrieve daily generated settlement details file. @@ -86,6 +98,7 @@ def _adyen_get_settlement_details_file(self): response = requests.get(URL, auth=(self.username, self.password)) if response.status_code != 200: raise UserError(_("%s \n\n %s") % (response.status_code, response.text)) + _logger.debug(_("Headers returned by Adyen %s"), response.headers) # Check base64 decoding and padding of response.content. # Remember: response.text is unicode, response.content is in bytes. text_count = len(response.text) @@ -100,10 +113,7 @@ def _adyen_get_settlement_details_file(self): byte_count, response.content[:64], ) - # Make sure base64 encoded content contains multiple of 4 bytes. - byte_padding = b"=" * (byte_count % 4) - data_file = response.content + byte_padding - return data_file, filename + return response.content, filename def _schedule_next_run(self): """Set next run date and autoincrement batch number."""