-
Notifications
You must be signed in to change notification settings - Fork 1
/
MessageFactories.py
412 lines (338 loc) · 13.6 KB
/
MessageFactories.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
__author__ = 'kakarthikeyan'
import ISOValidator
import ISOBuilder
from ISO8583.ISO8583 import ISO8583
import utility
'''
This class the abstract factory class which will produce the request validator and
response generator
'''
class MessageFactory:
#function to generate a concrete factory based on message type
def __init__(self):
self.Message100Factory = MessageFactory0100()
self.Message200Factory = MessageFactory0200()
self.Message400Factory = MessageFactory0400()
self.Message420Factory = MessageFactory0420()
def getFactory(self, messageType):
if messageType is None:
raise "Invalid Message Type"
elif messageType == "0100":
return self.Message100Factory
elif messageType == "0200":
return self.Message200Factory
elif messageType == "0400":
return self.Message400Factory
elif messageType == "0420":
return self.Message420Factory
else:
raise NotImplementedError("Unimplemented Message Type")
# interface definitions
def getRequestValidator(self):
raise NotImplementedError("Call to Abstract Base class")
def getResponseGenerator(self):
raise NotImplementedError("Call to Abstract Base class")
'''
This class produces and stores factory class which will return objects for 0100 message
'''
class MessageFactory0100(MessageFactory):
def __init__(self):
self.validator = RequestValidatorFactory().createValidator("0100")
self.responseGenerator = ResponseGeneratorFactory().createResponseGenerator("0100")
#overloaded function
def getRequestValidator(self):
return self.validator
def getResponseGenerator(self):
return self.responseGenerator
'''
This class produces and stores factory class which will return objects for 0200 message
'''
class MessageFactory0200(MessageFactory):
def __init__(self):
self.validator = RequestValidatorFactory().createValidator("0200")
self.responseGenerator = ResponseGeneratorFactory().createResponseGenerator("0200")
#overloaded function
def getRequestValidator(self):
return self.validator
def getResponseGenerator(self):
return self.responseGenerator
'''
This class produces and stores factory class which will return objects for 0400 message
'''
class MessageFactory0400(MessageFactory):
def __init__(self):
self.validator = RequestValidatorFactory().createValidator("0400")
self.responseGenerator = ResponseGeneratorFactory().createResponseGenerator("0400")
#overloaded function
def getRequestValidator(self):
return self.validator
def getResponseGenerator(self):
return self.responseGenerator
'''
This class produces and stores factory class which will return objects for 0420 message
'''
class MessageFactory0420(MessageFactory):
def __init__(self):
self.validator = RequestValidatorFactory().createValidator("0420")
self.responseGenerator = ResponseGeneratorFactory().createResponseGenerator("0420")
#overloaded function
def getRequestValidator(self):
return self.validator
def getResponseGenerator(self):
return self.responseGenerator
'''
This class is the Abstract class for Request Validators
'''
class RequestValidatorFactory:
# function to create a concrete request Validator
def createValidator(self,messageType):
if messageType is None:
raise "Invalid message type"
elif messageType == "0100":
return RequestValidator0100()
elif messageType == "0200":
return RequestValidator0200()
elif messageType == "0400":
return RequestValidator0400()
elif messageType == "0420":
return RequestValidator0420()
else:
raise NotImplementedError("Unimplemented request validator")
# interface definitions
def validateMessage(self,requestMessage):
raise NotImplementedError("Call to Abstract base class")
'''
concrete class implementation for 0100 message
'''
class RequestValidator0100(RequestValidatorFactory):
def __init__(self):
#mandatory fields shd be present
self.FieldsToValidate = [2,3,4,7,11,12,13,18,19,22,25,32,37,41,42,43,49]
#overloaded function
def validateMessage(self,requestMessage):
if not isinstance(requestMessage, ISO8583):
print "Tried to validate a request which is not of ISO8583 type"
return False
print "0100 message : " + requestMessage.getNetworkISO()
return ISOValidator.validateBitMaps(requestMessage,self.FieldsToValidate)
'''
concrete class implementation for 0200 message
'''
class RequestValidator0200(RequestValidatorFactory):
def __init__(self):
#mandatory fields shd be present
self.FieldsToValidate = [2,3,4,7,11,12,13,18,19,22,25,32,37,41,42,43,49]
#overloaded function
def validateMessage(self,requestMessage):
if not isinstance(requestMessage, ISO8583):
print "Tried to validate a request which is not of ISO8583 type"
return False
print "0200 message : " + requestMessage.getNetworkISO()
return ISOValidator.validateBitMaps(requestMessage,self.FieldsToValidate)
'''
concrete class implementation for 0400 message
'''
class RequestValidator0400(RequestValidatorFactory):
def __init__(self):
#mandatory fields shd be present
self.FieldsToValidate = [2,3,4,7,11,12,13,18,19,22,25,32,37,41,42,43,49,63,90]
#overloaded function
def validateMessage(self,requestMessage):
if not isinstance(requestMessage, ISO8583):
print "Tried to validate a request which is not of ISO8583 type"
return False
print "0400 message : " + requestMessage.getNetworkISO()
return ISOValidator.validateBitMaps(requestMessage,self.FieldsToValidate)
'''
concrete class implementation for 0420 message
'''
class RequestValidator0420(RequestValidatorFactory):
def __init__(self):
#mandatory fields shd be present
self.FieldsToValidate = [2,3,4,7,11,12,13,18,19,22,25,32,37,41,42,43,49,63,90]
#overloaded function
def validateMessage(self,requestMessage):
if not isinstance(requestMessage, ISO8583):
print "Tried to validate a request which is not of ISO8583 type"
return False
print "0420 message : " + requestMessage.getNetworkISO()
return ISOValidator.validateBitMaps(requestMessage,self.FieldsToValidate)
'''
This class is the abstract class for response generators
'''
class ResponseGeneratorFactory:
# function to create a concrete response generator
def createResponseGenerator(self, messageType):
if messageType is None:
raise "Message type should be passed"
elif messageType == "0100":
return ResponseGenerator0100()
elif messageType == "0200":
return ResponseGenerator0200()
elif messageType == "0400":
return ResponseGenerator0400()
elif messageType == "0420":
return ResponseGenerator0420()
else:
raise NotImplementedError("Other response generators are not implemented")
# interfaces
def generateResponse(self, request, forceError = None):
raise NotImplementedError("Call to Abstract Base class")
'''
concrete class implementation for 0100 response
'''
class ResponseGenerator0100(ResponseGeneratorFactory):
def __init__(self):
#mandatory fields to build
self.FieldsToBuild = [2,3,4,7,11,12,13,14,15,18,19,32,37,38,39,49,60]
def getCustomISOObject(self):
temp = ISO8583()
# change the protocol - this is for customization
temp.redefineBit(18, '18', 'Merchant Category Code', 'N', 4, 'n')
temp.redefineBit(53,'53','Security related control information','LL',76,'n')
temp.redefineBit(60, '60', 'Additional Data', 'LLL', 999, 'ans')
temp.redefineBit(62, '62', 'Custom Data', 'ANS', 26, 'ans')
return temp
#overloaded function
def generateResponse(self, request, forceError = None):
if not isinstance(request,ISO8583):
request = self.getCustomISOObject()
tempList = list(self.FieldsToBuild)
# create a dummy response now
response = self.getCustomISOObject()
response.setMTI("0110")
# adding fields based on card
cardNumber = request.getBit(2)
cardType = utility.findCardType(cardNumber[2:])
if cardType == "V":
tempList.extend([44,62])
elif cardType == "M":
tempList.extend([44])
else:
pass
# adding EMV field only if it is set in field 55
try:
emvData = request.getBit(55)
if emvData:
tempList.append(55)
except:
pass
response = ISOBuilder.buildBitMaps(request,response,tempList,forceError)
print "110 message response : " + response.getNetworkISO()
return response
'''
concrete class implementation for 0200 response
'''
class ResponseGenerator0200(ResponseGeneratorFactory):
def __init__(self):
#mandatory fields to build
self.FieldsToBuild = [2,3,4,7,11,12,13,14,15,18,19,32,37,38,39,49,60]
def getCustomISOObject(self):
temp = ISO8583()
# change the protocol - this is for customization
temp.redefineBit(18, '18', 'Merchant Category Code', 'N', 4, 'n')
temp.redefineBit(53,'53','Security related control information','LL',76,'n')
temp.redefineBit(60, '60', 'Additional Data', 'LLL', 999, 'ans')
temp.redefineBit(62, '62', 'Custom Data', 'ANS', 26, 'ans')
return temp
#overloaded function
def generateResponse(self, request, forceError = None):
if not isinstance(request,ISO8583):
request = self.getCustomISOObject()
tempList = list(self.FieldsToBuild)
# create a dummy response now
response = self.getCustomISOObject()
response.setMTI("0210")
# adding fields based on card
cardNumber = request.getBit(2)
cardType = utility.findCardType(cardNumber[2:])
if cardType == "V":
#tempList.extend([44,62])
tempList.extend([44])
elif cardType == "M":
tempList.extend([44])
else:
pass
response = ISOBuilder.buildBitMaps(request,response,tempList,forceError)
print "210 message response : " + response.getNetworkISO()
return response
'''
concrete class implementation for 0400 response
'''
class ResponseGenerator0400(ResponseGeneratorFactory):
def __init__(self):
#mandatory fields to build
self.FieldsToBuild = [2,3,4,7,11,12,13,14,15,18,19,32,37,38,39,41,49]
def getCustomISOObject(self):
temp = ISO8583()
# change the protocol - this is for customization
temp.redefineBit(18, '18', 'Merchant Category Code', 'N', 4, 'n')
temp.redefineBit(53,'53','Security related control information','LL',76,'n')
temp.redefineBit(60, '60', 'Additional Data', 'LLL', 999, 'ans')
temp.redefineBit(62, '62', 'Custom Data', 'ANS', 26, 'ans')
return temp
#overloaded function
def generateResponse(self, request, forceError = None):
if not isinstance(request,ISO8583):
request = self.getCustomISOObject()
tempList = list(self.FieldsToBuild)
# create a dummy response now
response = self.getCustomISOObject()
response.setMTI("0410")
# adding fields based on card
cardNumber = request.getBit(2)
cardType = utility.findCardType(cardNumber[2:])
if cardType == "V":
tempList.extend([44,62])
else:
pass
# adding EMV field only if it is set in field 55
try:
emvData = request.getBit(55)
if emvData:
tempList.append(55)
except:
pass
response = ISOBuilder.buildBitMaps(request,response,tempList,forceError)
print "410 message response : " + response.getNetworkISO()
return response
'''
concrete class implementation for 0420 response
'''
class ResponseGenerator0420(ResponseGeneratorFactory):
def __init__(self):
#mandatory fields to build
self.FieldsToBuild = [2,3,4,7,11,12,13,14,15,18,19,32,37,38,39,41,49,60]
def getCustomISOObject(self):
temp = ISO8583()
# change the protocol - this is for customization
temp.redefineBit(18, '18', 'Merchant Category Code', 'N', 4, 'n')
temp.redefineBit(53,'53','Security related control information','LL',76,'n')
temp.redefineBit(60, '60', 'Additional Data', 'LLL', 999, 'ans')
temp.redefineBit(62, '62', 'Custom Data', 'ANS', 26, 'ans')
return temp
#overloaded function
def generateResponse(self, request, forceError = None):
if not isinstance(request,ISO8583):
request = self.getCustomISOObject()
tempList = list(self.FieldsToBuild)
# create a dummy response now
response = self.getCustomISOObject()
response.setMTI("0430")
# adding fields based on card
cardNumber = request.getBit(2)
cardType = utility.findCardType(cardNumber[2:])
if cardType == "V":
tempList.extend([44,62])
else:
pass
# adding EMV field only if it is set in field 55
try:
emvData = request.getBit(55)
if emvData:
tempList.append(55)
except:
pass
response = ISOBuilder.buildBitMaps(request,response,tempList,forceError)
print "430 message response : " + response.getNetworkISO()
return response