| |
41 |
42 | - ISO8583
43 |
44 |
45 |
46 |
47 |
48 | class ISO8583 |
49 |
50 | |
51 | Main Class to work with ISO8583 packages.
52 | Used to create, change, send, receive, parse or work with ISO8593 Package version 1993.
53 | It's 100% Python :)
54 | Enjoy it!
55 | Thanks to: Vulcanno IT Solutions <http://www.vulcanno.com.br>
56 | Licence: GPL Version 3
57 | More information: http://code.google.com/p/iso8583py/
58 |
59 | Example:
60 | from ISO8583.ISO8583 import ISO8583
61 | from ISO8583.ISOErrors import *
62 |
63 | iso = ISO8583()
64 | try:
65 | iso.setMTI('0800')
66 | iso.setBit(2,2)
67 | iso.setBit(4,4)
68 | iso.setBit(12,12)
69 | iso.setBit(21,21)
70 | iso.setBit(17,17)
71 | iso.setBit(49,986)
72 | iso.setBit(99,99)
73 | except ValueToLarge, e:
74 | print ('Value too large :( %s' % e)
75 | except InvalidMTI, i:
76 | print ('This MTI is wrong :( %s' % i)
77 |
78 | print ('The Message Type Indication is = %s' %iso.getMTI())
79 |
80 | print ('The Bitmap is = %s' %iso.getBitmap())
81 | iso.showIsoBits();
82 | print ('This is the ISO8583 complete package %s' % iso.getRawIso())
83 | print ('This is the ISO8583 complete package to sent over the TCPIP network %s' % iso.getNetworkISO()) |
84 | |
85 | Methods defined here:
86 | - __cmp__(self, obj2)
- Method that compare two objects in "==", "!=" and other things
87 | Example:
88 | p1 = ISO8583()
89 | p1.setMTI('0800')
90 | p1.setBit(2,2)
91 | p1.setBit(4,4)
92 | p1.setBit(12,12)
93 | p1.setBit(17,17)
94 | p1.setBit(99,99)
95 |
96 | #get the rawIso and save in the iso variable
97 | iso = p1.getRawIso()
98 |
99 | p2 = ISO8583()
100 | p2.setIsoContent(iso)
101 |
102 | print 'Is equivalent?'
103 | if p1 == p1:
104 | print ('Yes :)')
105 | else:
106 | print ('Noooooooooo :(')
107 |
108 | @param: obj2 -> object that will be compared
109 | @return: <0 if is not equal, 0 if is equal
110 |
111 | - __init__(self, iso='', debug=False)
- Default Constructor of ISO8583 Package.
112 | It inicialize a "brand new" ISO8583 package
113 | Example: To Enable debug you can use:
114 | pack = ISO8583(debug=True)
115 | @param: iso a String that represents the ASCII of the package. The same that you need to pass to setIsoContent() method.
116 | @param: debug (True or False) default False -> Used to print some debug infos. Only use if want that messages!
117 |
118 | - getBit(self, bit)
- Return the value of the bit
119 | @param: bit -> the number of the bit that you want the value
120 | @raise: BitInexistent Exception, BitNotSet Exception
121 |
122 | - getBitLimit(self, bit)
- Method that return the bit limit (Max size)
123 | @param: bit -> Bit that will be searched and whose limit will be returned
124 | @return: int that indicate the limit of the bit
125 |
126 | - getBitType(self, bit)
- Method that return the bit Type
127 | @param: bit -> Bit that will be searched and whose type will be returned
128 | @return: str that represents the type of the bit
129 |
130 | - getBitValueType(self, bit)
- Method that return the bit value type
131 | @param: bit -> Bit that will be searched and whose value type will be returned
132 | @return: str that indicate the valuye type of the bit
133 |
134 | - getBitmap(self)
- Method that return the ASCII Bitmap of the package
135 | @return: str -> with the ASCII Bitmap
136 |
137 | - getBitsAndValues(self)
- Method that return an array of bits, values, types etc.
138 | Each array value is a dictionary with: {'bit':X ,'type': Y, 'value': Z} Where:
139 | bit: is the bit number
140 | type: is the bit type
141 | value: is the bit value inside this object
142 | so the Generic array returned is: [ (...),{'bit':X,'type': Y, 'value': Z}, (...)]
143 |
144 | Example:
145 | p1 = ISO8583()
146 | p1.setMTI('0800')
147 | p1.setBit(2,2)
148 | p1.setBit(4,4)
149 | p1.setBit(12,12)
150 | p1.setBit(17,17)
151 | p1.setBit(99,99)
152 |
153 | v1 = p1.getBitsAndValues()
154 | for v in v1:
155 | print ('Bit %s of type %s with value = %s' % (v['bit'],v['type'],v['value']))
156 |
157 | @return: array of values.
158 |
159 | - getLargeBitName(self, bit)
- Method that return the large bit name
160 | @param: bit -> Bit that will be searched and whose name will be returned
161 | @return: str that represents the name of the bit
162 |
163 | - getMTI(self)
- Method that return the MTI of the package
164 | @return: str -> with the MTI
165 |
166 | - getNetworkISO(self, bigEndian=True)
- Method that return ISO8583 ASCII package with the size in the beginning
167 | By default, it return the package with size represented with big-endian.
168 | Is the same that:
169 | import struct
170 | (...)
171 | iso = ISO8583()
172 | iso.setBit(3,'300000')
173 | (...)
174 | ascii = iso.getRawIso()
175 | # Example: big-endian
176 | # To little-endian, replace '!h' with '<h'
177 | netIso = struct.pack('!h',len(iso))
178 | netIso += ascii
179 | # Example: big-endian
180 | # To little-endian, replace 'iso.getNetworkISO()' with 'iso.getNetworkISO(False)'
181 | print ('This <%s> the same that <%s>' % (iso.getNetworkISO(),netIso))
182 |
183 | @param: bigEndian (True|False) -> if you want that the size be represented in this way.
184 | @return: size + ASCII ISO8583 package ready to go to the network!
185 | @raise: InvalidMTI Exception
186 |
187 | - getRawIso(self)
- Method that return ISO8583 ASCII complete representation
188 | Example:
189 | iso = ISO8583()
190 | iso.setMTI('0800')
191 | iso.setBit(2,2)
192 | iso.setBit(4,4)
193 | iso.setBit(12,12)
194 | iso.setBit(17,17)
195 | iso.setBit(99,99)
196 | str = iso.getRawIso()
197 | print ('This is the ASCII package %s' % str)
198 | output (print) -> This is the ASCII package 0800d010800000000000000000002000000001200000000000400001200170299
199 |
200 | @return: str with complete ASCII ISO8583
201 | @raise: InvalidMTI Exception
202 |
203 | - getValuesArray(self)
- Method that return an internal array of the package
204 | @return: array -> with all bits, presents or not in the bitmap
205 |
206 | - redefineBit(self, bit, smallStr, largeStr, bitType, size, valueType)
- Method that redefine a bit structure in global scope!
207 | Can be used to personalize ISO8583 structure to another specification (ISO8583 1987 for example!)
208 | Hint: If you have a lot of "ValueToLarge Exception" maybe the especification that you are using is different of mine. So you will need to use this method :)
209 | @param: bit -> bit to be redefined
210 | @param: smallStr -> a small String representantion of the bit, used to build "user friendly prints", example "2" for bit 2
211 | @param: largeStr -> a large String representantion of the bit, used to build "user friendly prints" and to be used to inform the "main use of the bit",
212 | example "Primary account number (PAN)" for bit 2
213 | @param: bitType -> type the bit, used to build the values, example "LL" for bit 2. Need to be one of (B, N, AN, ANS, LL, LLL)
214 | @param: size -> limit size the bit, used to build/complete the values, example "19" for bit 2.
215 | @param: valueType -> value type the bit, used to "validate" the values, example "n" for bit 2. This mean that in bit 2 we need to have only numeric values.
216 | Need to be one of (a, an, n, ansb, b)
217 | @raise: BitInexistent Exception, InvalidValueType Exception
218 |
219 | - setBit(self, bit, value)
- Method used to set a bit with a value.
220 | It's one of the most important method to use when using this library
221 | @param: bit -> bit number that want to be setted
222 | @param: value -> the value of the bit
223 | @return: True/False default True -> To be used in the future!
224 | @raise: BitInexistent Exception, ValueToLarge Exception
225 |
226 | - setIsoContent(self, iso)
- Method that receive a complete ISO8583 string (ASCII) understand it and remove the bits values
227 | Example:
228 | iso = '0210B238000102C080040000000000000002100000000000001700010814465469421614465701081100301000000N399915444303500019991544986020 Value not allowed009000095492'
229 | i2 = ISO8583()
230 | # in this case, we need to redefine a bit because default bit 42 is LL and in this especification is "N"
231 | # the rest remain, so we use "get" :)
232 | i2.redefineBit(42, '42', i2.getLargeBitName(42), 'N', i2.getBitLimit(42), i2.getBitValueType(42) )
233 | i2.setIsoContent(iso2)
234 | print 'Bitmap = %s' %i2.getBitmap()
235 | print 'MTI = %s' %i2.getMTI()
236 |
237 | print 'This ISO has bits:'
238 | v3 = i2.getBitsAndValues()
239 | for v in v3:
240 | print ('Bit %s of type %s with value = %s' % (v['bit'],v['type'],v['value']))
241 |
242 | @param: str -> complete ISO8583 string
243 | @raise: InvalidIso8583 Exception
244 |
245 | - setMTI(self, type)
- Method that set Transation Type (MTI)
246 | In fact, is an alias to "setTransationType" method
247 | @param: type -> MTI to be setted
248 |
249 | - setNetworkISO(self, iso, bigEndian=True)
- Method that receive sie + ASCII ISO8583 package and transfor it in the ISO8583 object.
250 | By default, it recieve the package with size represented with big-endian.
251 | Is the same that:
252 | import struct
253 | (...)
254 | iso = ISO8583()
255 | iso.setBit(3,'300000')
256 | (...)
257 | # Example: big-endian
258 | # To little-endian, replace 'iso.getNetworkISO()' with 'iso.getNetworkISO(False)'
259 | netIso = iso.getNetworkISO()
260 | newIso = ISO8583()
261 | # Example: big-endian
262 | # To little-endian, replace 'newIso.setNetworkISO()' with 'newIso.setNetworkISO(False)'
263 | newIso.setNetworkISO(netIso)
264 | #Is the same that:
265 | #size = netIso[0:2]
266 | ## To little-endian, replace '!h' with '<h'
267 | #size = struct.unpack('!h',size )
268 | #newIso.setIsoContent(netIso[2:size])
269 | arr = newIso.getBitsAndValues()
270 | for v in arr:
271 | print ('Bit %s Type %s Value = %s' % (v['bit'],v['type'],v['value']))
272 |
273 | @param: iso -> str that represents size + ASCII ISO8583 package
274 | @param: bigEndian (True|False) -> Codification of the size.
275 | @raise: InvalidIso8583 Exception
276 |
277 | - setTransationType(self, type)
- Method that set Transation Type (MTI)
278 | @param: type -> MTI to be setted
279 | @raise: ValueToLarge Exception
280 |
281 | - showBitmap(self)
- Method that print the bitmap in ASCII form
282 | Hint: Try to use getBitmap method and format your own print :)
283 |
284 | - showBitsFromBitmapStr(self, bitmap)
- Method that receive a bitmap str, process it, and print a array with bits this bitmap string represents.
285 | Usualy is used to debug things.
286 | @param: bitmap -> bitmap str to be analized and translated to "bits"
287 |
288 | - showIsoBits(self)
- Method that show in detail a list of bits , values and types inside the object
289 | Example: output to
290 | (...)
291 | iso.setBit(2,2)
292 | iso.setBit(4,4)
293 | (...)
294 | iso.showIsoBits()
295 | (...)
296 | Bit[2] of type LL has limit 19 = 012
297 | Bit[4] of type N has limit 12 = 000000000004
298 | (...)
299 |
300 | - showRawIso(self)
- Method that print ISO8583 ASCII complete representation
301 | Example:
302 | iso = ISO8583()
303 | iso.setMTI('0800')
304 | iso.setBit(2,2)
305 | iso.setBit(4,4)
306 | iso.setBit(12,12)
307 | iso.setBit(17,17)
308 | iso.setBit(99,99)
309 | iso.showRawIso()
310 | output (print) -> 0800d010800000000000000000002000000001200000000000400001200170299
311 | Hint: Try to use getRawIso method and format your own print :)
312 |
313 | | |