adelosa / cardutil

Payment cards tools including ISO8583 parser and Mastercard IPM files processing
MIT License
24 stars 4 forks source link

Error processing bit 55 - mci_ipm_encode #15

Closed elisandrom closed 7 months ago

elisandrom commented 7 months ago

Execution Parameters: mci_ipm_encode T112 -o T112_T --in-format 1014 --out-format 1014 --in-encoding cp500 --out-encoding latin_1 --debug

Error:

...
DEBUG:cardutil.iso8583:processing bit 49
DEBUG:cardutil.iso8583:986
DEBUG:cardutil.iso8583:processing bit 55
DEBUG:cardutil.iso8583:b'\x82\x02\x19\....' (removed the rest)
DEBUG:cardutil.mciipm:bytes_to_write=b'\x00\x00\x00\x00'
DEBUG:cardutil.mciipm:len->4<858
DEBUG:cardutil.mciipm:write b'\x00\x00\x00\x00'
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\Scripts\mci_ipm_encode.exe\__main__.py", line 7, in <module>
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\cardutil\cli\mci_ipm_encode.py", line 9, in cli_entry
    cli_run(**vars(cli_parser().parse_args()))
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\cardutil\cli\mci_ipm_encode.py", line 26, in cli_run
    mci_ipm_encode(in_file, out_file=out_file, **kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\cardutil\cli\mci_ipm_encode.py", line 61, in mci_ipm_encode
    writer.write_many(reader)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\cardutil\mciipm.py", line 587, in write_many
    self.write(record)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\cardutil\mciipm.py", line 576, in write
    record = iso8583.dumps(obj, encoding=self.encoding, iso_config=self.iso_config)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\cardutil\iso8583.py", line 108, in dumps
    output = _dict_to_iso8583(obj, iso_config, encoding, hex_bitmap)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\cardutil\iso8583.py", line 248, in _dict_to_iso8583
    output_data += _field_to_iso8583(
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\cardutil\iso8583.py", line 275, in _field_to_iso8583
    output += format(field_value[:field_length], '<' + str(field_length))
TypeError: unsupported format string passed to bytes.__format__

What can be adjusted? Thanks!

adelosa commented 7 months ago

I'll need enough information to recreate the issue. If possible provide desensitised data that causes the issue. If parsing DE55 is the issue, then I would need the full DE55 element.

elisandrom commented 7 months ago

I'll need enough information to recreate the issue. If possible provide desensitised data that causes the issue. If parsing DE55 is the issue, then I would need the full DE55 element.

I sent you the DE55 example in your email.

adelosa commented 7 months ago

I have added some additional debugging statements to the master branch. If you could rerun after installing from source and send me the debug messages, I'll be able to see what is causing the issue and work out next steps. pip install git+https://github.com/adelosa/cardutil

adelosa commented 7 months ago

Thanks for reporting the issue and providing the debug information. I have now been able to recreate the issue.

The issue is that the function that converts the data to the required ISO format did not expect to process a bytes based field. Generally all fields in the dict are not bytes... except DE55, the ICC field which is a binary field. I will be pushing an update so that it leaves the byte field as is rather than formatting it.

If you find fields missing in the output, it's probably because of the use of both PDS and DE fields in the conversion. See warning in https://adelosa.github.io/cardutil/cli.html#mci-csv-to-ipm. The encode function you are using first goes from iso to python dict which will contain both DE and PDS fields and then send the result to dict to iso which will be impacted by this constraint. You can strip PDS fields being output via a custom configuration (remove the field processors = PDS)

Will push a new commit shortly to master branch - you can test it to ensure it resolves your issue and let me know. I will then push out a new minor release of the tool.

adelosa commented 7 months ago

The CLI does not have a custom bit config option. I have updated the CLI to strip any PDS field processors to ensure you don't hit any issues with the encode. Let me know how you go.

elisandrom commented 7 months ago

Hello, I will only be able to test it on Tuesday. As soon as I have the results I will let you know. thank you very much!

elisandrom commented 7 months ago

Hello, I updated cardutil here to the latest version and using the same T112 file, it still gives the error, is it something I still need to change?

mci_ipm_encode T112_T -o T112_T_ASC --in-format 1014 --out-format 1014 --in-encoding cp500 --out-encoding latin_1 --debug

File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\cardutil\iso8583.py", line 275, in _field_to_iso8583
     output += format(field_value[:field_length], '<' + str(field_length))
TypeError: unsupported format string passed to bytes.__format__
adelosa commented 7 months ago

Did you install from source? I have not updated the release yet. Use 'pip install git+https://github.com/adelosa/cardutil'

elisandrom commented 7 months ago

Yes, I did it using that same command.

adelosa commented 7 months ago

The error indicates line 275 of iso8583.py The code in master is different than what is displayed in your error. If you already have the cardutil package installed make sure you use the --upgrade --force-reinstall options with pip.

elisandrom commented 7 months ago

Hi, now everything works using--in-format 1014 --out-format 1014 --in-encoding cp500 --out-encoding latin_1, thank you very much for your help.

elisandrom commented 7 months ago

The goal was to use: --in-format 1014 --out-format 1014 --in-encoding cp500 --out-encoding latin_1 this worked perfectly now.

I tested --in-format 1014 --out-format vbs --in-encoding cp500 --out-encoding latin_1 and it returns an error but in other places, if you want to look, you can see it in the same test file that I sent .

But if you wish, we can close this Issue, since the problem of DE55 has been resolved.

adelosa commented 7 months ago

Happy to have a look at that. If you can send me debug details that would be great (here or email). I'll push a release for this fix and raise a new issue for format conversion.

adelosa commented 7 months ago

Thanks for the test file. Was able to recreate the second issue and fixed it. Seems I had the input and output formats mixed up on the CLI command - so input was output and vice versa. Fixed and rerun - working now. Will push out a release shortly.

adelosa commented 7 months ago

Release 0.6.4 has fixes for both the issues identified.