diff --git a/starkbank/iso8583/__init__.py b/starkbank/iso8583/__init__.py index 3c4c7e0..0af01c9 100644 --- a/starkbank/iso8583/__init__.py +++ b/starkbank/iso8583/__init__.py @@ -1,3 +1,8 @@ encoding = "cp500" -from starkbank.iso8583.message import parse, unparse \ No newline at end of file + +def getEncoding(): + return encoding + + +from starkbank.iso8583.message import parse, unparse diff --git a/starkbank/iso8583/message.py b/starkbank/iso8583/message.py index d58d20f..ac10b1a 100644 --- a/starkbank/iso8583/message.py +++ b/starkbank/iso8583/message.py @@ -1,7 +1,7 @@ from bisect import bisect_left as bisectLeft -from starkbank import iso8583 from .mastercard import mastercard from .utils.binary import Binary +from . import getEncoding def getVersion(MTI): @@ -12,7 +12,7 @@ def getVersion(MTI): try: return versionMap[MTI[0]] except KeyError: - raise ValueError("Expected '0' or '1' in MTI[0] (Actual {MTI}), please check message iso8583.encoding".format(MTI=MTI)) + raise ValueError("Expected '0' or '1' in MTI[0] (Actual: {MTI}), please check iso8583.encoding".format(MTI=MTI)) def parse(message, template=mastercard): @@ -46,7 +46,7 @@ def parseElement(message, elementId, template): rule = template[elementId] size = rule["limit"] if rule["type"]: - parseSize = int(message[:rule["type"]].decode(iso8583.encoding) or 0) + parseSize = int(message[:rule["type"]].decode(getEncoding()) or 0) size = min(size, parseSize) if parseSize > size: raise ValueError( @@ -105,7 +105,7 @@ def unparseElement(json, elementId, template): size = rule["limit"] unparsed = rule["unparser"](data)[:size] if rule["type"]: - return str(len(unparsed)).zfill(rule["type"]).encode(iso8583.encoding) + unparsed + return str(len(unparsed)).zfill(rule["type"]).encode(getEncoding()) + unparsed if len(unparsed) != size: raise ValueError("{elementId}: Expected length {lenExpected}, got {lenActual}".format( elementId=elementId, diff --git a/starkbank/iso8583/utils/parser.py b/starkbank/iso8583/utils/parser.py index f4e770a..7f999b1 100644 --- a/starkbank/iso8583/utils/parser.py +++ b/starkbank/iso8583/utils/parser.py @@ -1,15 +1,15 @@ from base64 import b64encode, b64decode from copy import deepcopy from binascii import hexlify, unhexlify -from starkbank import iso8583 +from .. import getEncoding def parseString(text): - return text.decode(iso8583.encoding) + return text.decode(getEncoding()) def unparseString(text): - return text.encode(iso8583.encoding) + return text.encode(getEncoding()) def parseBin(text): @@ -34,31 +34,31 @@ def unparseBytesToBin(text, length=64): def parseSubelements(text): json = { - "SE00": text[0:1].decode(iso8583.encoding) + "SE00": text[0:1].decode(getEncoding()) } text = text[1:] while text: - key, length, text = text[0:2].decode(iso8583.encoding), int(text[2:4].decode(iso8583.encoding)), text[4:] - value, text = text[0:length].decode(iso8583.encoding), text[length:] + key, length, text = text[0:2].decode(getEncoding()), int(text[2:4].decode(getEncoding())), text[4:] + value, text = text[0:length].decode(getEncoding()), text[length:] json["SE" + key.zfill(2)] = value return json def unparseSubelements(text): json = deepcopy(text) - string = json.pop("SE00").encode(iso8583.encoding) + string = json.pop("SE00").encode(getEncoding()) for key, value in sorted(json.items()): key = key.replace("SE", "") length = len(value) - string += key.encode(iso8583.encoding) + str(length).zfill(2).encode(iso8583.encoding) + value.encode(iso8583.encoding) + string += key.encode(getEncoding()) + str(length).zfill(2).encode(getEncoding()) + value.encode(getEncoding()) return string def parsePds(text): json = {} while text: - key, length, text = text[0:4].decode(iso8583.encoding), int(text[4:7].decode(iso8583.encoding)), text[7:] - value, text = text[0:length].decode(iso8583.encoding), text[length:] + key, length, text = text[0:4].decode(getEncoding()), int(text[4:7].decode(getEncoding())), text[7:] + value, text = text[0:length].decode(getEncoding()), text[length:] json["PDS" + key.zfill(4)] = value return json @@ -69,5 +69,5 @@ def unparsePds(text): for key, value in sorted(json.items()): key = key.replace("PDS", "") length = str(len(value)).zfill(3) - byteString += key.encode(iso8583.encoding) + length.encode(iso8583.encoding) + value.encode(iso8583.encoding) + byteString += key.encode(getEncoding()) + length.encode(getEncoding()) + value.encode(getEncoding()) return byteString