-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExtensionMethods.cs
220 lines (191 loc) · 8.44 KB
/
ExtensionMethods.cs
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
using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Text;
namespace ISO_8583_MessageSandbox
{
/// <summary>
/// Extension class for convert int binary, hex and Ascii
/// </summary>
public static class ExtensionMethods
{
private static readonly StringDictionary ResponseCodes;
static ExtensionMethods()
{
ResponseCodes = new StringDictionary
{
{"00", "Approved"},
{"03", "Invalid Service Provider Id"},
{"10", "Delay in processing the recharge"},
{"12", "Invalid Recharge (e.g. Recharge attempt without activation, Business rule violation)"},
{"13", "Invalid Recharge Denomination"},
{"15", "Invalid Financial Institution Id"},
{"22", "System Related Problem. Transaction Failed"},
{"26", "Duplicate recharge attempted"},
{"42", "Invalid MSISDN"},
{"91", "back-end system is either unavailable or did not respond in time."}
};
}
/// <summary>
/// Converts Binary string to Hex string.
/// </summary>
/// <param name="binary">The binary string.</param>
/// <returns>Hex string</returns>
public static string BinaryToHexString(this string binary)
{
var result = new StringBuilder((binary.Length / 8) + 1);
var mod4Len = binary.Length % 8;
if (mod4Len != 0)
{
// pad to length multiple of 8
binary = binary.PadLeft(((binary.Length / 8) + 1) * 8, '0');
}
for (var i = 0; i < binary.Length; i += 8)
{
var eightBits = binary.Substring(i, 8);
result.AppendFormat("{0:X2}", Convert.ToByte(eightBits, 2));
}
return result.ToString();
}
/// <summary>
/// Converts the byte array to hex.
/// </summary>
/// <param name="ba">The byte array.</param>
/// <returns>Hex value</returns>
private static string ByteArrayToHex(this byte[] ba)
{
var hex = BitConverter.ToString(ba);
return hex.Replace("-", string.Empty);
}
/// <summary>
/// Converts the Hex string to binary.
/// </summary>
/// <param name="hex">The hex string.</param>
/// <returns>Binary representation</returns>
private static string HexStringToBinary(this string hex)
{
var binaryval = String.Join(String.Empty, hex.Select(c => Convert.ToString(Convert.ToInt64(c.ToString(CultureInfo.InvariantCulture), 16), 2).PadLeft(4, '0')));
return binaryval;
}
/// <summary>
/// Converts Hex to ASCII.
/// </summary>
/// <param name="hex">The hex string.</param>
/// <returns>The Ascii representation</returns>
public static string HexToAscii(this string hex)
{
var sb = new StringBuilder();
for (var i = 0; i <= hex.Length - 2; i += 2)
{
sb.Append(Convert.ToString(Convert.ToChar(int.Parse(hex.Substring(i, 2), NumberStyles.HexNumber))));
}
return sb.ToString();
}
/// <summary>
/// Converts ASCII text to Hex String.
/// </summary>
/// <param name="ascii">The ASCII string.</param>
/// <returns>THe Hex representation</returns>
public static string AsciiToHex(this string ascii)
{
var hex = string.Join(string.Empty, ascii.Select(c => ((int)c).ToString("X")).ToArray());
return hex;
}
public static byte[] HexToByteArray(this string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
public static string With2DigitLengthIndicator(this string value)
{
return string.Concat(value.Length.ToString().PadLeft(2, '0'), value);
}
public static string With3DigitLengthIndicator(this string value)
{
return string.Concat(value.Length.ToString().PadLeft(3, '0'), value);
}
/// <summary>
/// Decodes the message received from socket.
/// </summary>
/// <param name="message">The message.</param>
public static string DecodeMessage(this byte[] message)
{
try
{
// =================================================================================
// Note: For comments on creating the message structure please refer to Echo Message
// =================================================================================
Log.WriteLine("=======================================");
Log.WriteLine("response");
Log.WriteLine("=======================================");
// declare message structure byte arrays
var messageHeader = new byte[2];
var mti = new byte[4];
var primaryBitmap = new byte[8];
var secondaryBitmap = new byte[8];
var secondaryBitmapAvailable = false;
var msg = new byte[message.Length - 2];
byte[] elements;
byte[] messageBitmap;
// populate the message structure objects
Buffer.BlockCopy(message, 0, messageHeader, 0, 2);
Buffer.BlockCopy(message, 2, msg, 0, message.Length - 2);
Buffer.BlockCopy(message, 2, mti, 0, 4);
Buffer.BlockCopy(message, 6, primaryBitmap, 0, 8);
var primaryBitmapBinary = primaryBitmap.ByteArrayToHex()
.HexStringToBinary();
// determine if secondary bitmap is available
// copy primary bitmap and secondary bitmap into one byte array
if (primaryBitmapBinary.Substring(0, 1) == "1")
{
secondaryBitmapAvailable = true;
Buffer.BlockCopy(message, 14, secondaryBitmap, 0, 8);
}
if (secondaryBitmapAvailable)
{
messageBitmap = new byte[16];
Buffer.BlockCopy(message, 6, messageBitmap, 0, 16);
elements = new byte[message.Length - 22];
Buffer.BlockCopy(message, 22, elements, 0, message.Length - 22);
}
else
{
messageBitmap = new byte[8];
Buffer.BlockCopy(message, 6, messageBitmap, 0, 8);
elements = new byte[message.Length - 14];
Buffer.BlockCopy(message, 14, elements, 0, message.Length - 14);
}
Log.WriteLine($"MTI = {Encoding.Default.GetString(mti)}");
// create the data elements object and parses the elements and bitmap
var dataElements = new DataElements();
var dataElement = dataElements.Parse(elements, messageBitmap);
// write all populated data elements to log
for (var i = 0; i < dataElement.Length; i++)
if (dataElement[i] != null)
Log.WriteLine($"DE #{i.ToString(CultureInfo.InvariantCulture).PadLeft(3, ' ')} = {dataElement[i]}");
Log.WriteLine($"DE #127.3 = {dataElements.Field127_3}");
Log.WriteLine($"DE #127.6 = {dataElements.Field127_6}");
Log.WriteLine($"DE #127.9 = {dataElements.Field127_9}");
// response code (data elements 39)
var responseMessage = string.Empty;
if (dataElement[39] != null)
{
// get the response code description
responseMessage = ResponseCodes[dataElement[39]];
//if (dataElement[39] == "00")
// _authRequestApproved = true;
}
Log.WriteLine($"responseMessage = {responseMessage}");
return responseMessage;
}
catch (Exception ex)
{
Log.WriteLine(ex.ToString());
return ex.ToString();
}
}
}
}