Skip to content

Commit

Permalink
[IMP] *_online_adyen base64 encode retrieved file
Browse files Browse the repository at this point in the history
  • Loading branch information
NL66278 committed Dec 17, 2021
1 parent b0b35f6 commit 8342b59
Showing 1 changed file with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2021 Therp BV <https://therp.nl>.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import base64
import logging
from html import escape

Expand Down Expand Up @@ -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}
Expand All @@ -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.
Expand All @@ -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)
Expand All @@ -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."""
Expand Down

0 comments on commit 8342b59

Please sign in to comment.