Skip to content

Commit

Permalink
Support byte values when converting field to iso8583 - DE55 (issue #15)
Browse files Browse the repository at this point in the history
  • Loading branch information
adelosa committed Nov 18, 2023
1 parent b51a7bb commit 80a04c0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
14 changes: 10 additions & 4 deletions cardutil/iso8583.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,22 @@ def _dict_to_iso8583(message, bit_config, encoding=DEFAULT_ENCODING, hex_bitmap=

def _field_to_iso8583(bit_config, field_value, encoding=DEFAULT_ENCODING):

output = ''
output = b''
LOGGER.debug(f'bit_config={bit_config}, field_value={field_value}, encoding={encoding}')
field_value = _pytype_to_string(field_value, bit_config)
field_length = bit_config.get('field_length')
length_size = _get_field_length(bit_config) # size of length for llvar and lllvar fields

if length_size > 0:
field_length = len(field_value)
output += format(field_length, '0' + str(length_size))
output += format(field_value[:field_length], '<' + str(field_length))
return output.encode(encoding)
output += format(field_length, '0' + str(length_size)).encode(encoding)

if isinstance(field_value, bytes):
output += field_value[:field_length]
else:
output += format(field_value[:field_length], '<' + str(field_length)).encode(encoding)

return output


def _iso8583_to_field(bit, bit_config, message_data, encoding=DEFAULT_ENCODING):
Expand Down
5 changes: 5 additions & 0 deletions docs/source/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ unit test

$ pytest

If you want to run with debug messages displayed::

$ pytest --log-cli-level=10


coverage
--------

Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[pytest]
log_cli = true

[bumpversion]
current_version = 0.6.3
tag = True
Expand Down
2 changes: 2 additions & 0 deletions tests/cli/test_mci_ipm_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def test_ipm_to_csv_generate_exception(self):
"""
Actually run using real files, and exception generated
Triggered through negative RDW on second record -- invalid record length
KNOWN ISSUE: when pytest run with logging, this test will fail.
TODO Fix so this test works with pytest debug on
:return:
"""
in_ipm_data = (b'\x00\x00\x00\x1a0100\x80\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Expand Down
12 changes: 12 additions & 0 deletions tests/test_iso8583.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ def test_field_to_iso8583(self):
'field_length': 12, "field_date_format": "%y%m%d%H%M%S"},
datetime.datetime(2014, 1, 2, 15, 16, 10)))

def test_field_to_iso8583_binary_field(self):
"""
https://github.com/adelosa/cardutil/issues/15
Ensure function can support processing of byte values representing binary fields like DE55
"""
bit_config = {
'field_name': 'binary field', 'field_type': 'LLLVAR', 'field_length': 255}
field_value = b'\x01\x01\x41\x9f\x01\x02\x12\x34'
encoding = 'latin_1'
field_output = _field_to_iso8583(bit_config, field_value, encoding)
self.assertEqual(b'008\x01\x01A\x9f\x01\x02\x124', field_output)

def test_iso8583_to_dict(self):
expected_dict = {'MTI': '1144', 'DE2': '4444555544445555', 'DE3': '111111', 'DE4': 9999,
'DE12': datetime.datetime(2015, 8, 15, 17, 15, 0),
Expand Down

0 comments on commit 80a04c0

Please sign in to comment.