-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
152 lines (127 loc) · 5.15 KB
/
test.cpp
File metadata and controls
152 lines (127 loc) · 5.15 KB
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
#include <string>
#include <map>
#include <iostream>
#include <tinyxml2.h>
using namespace tinyxml2;
// Helper function to parse variable-length fields
std::string parseVariableField(const std::string& data, size_t& start, int maxLength) {
std::cout << "parse " << start<<std::endl;
int length = std::stoi(data.substr(start, 2)); // Assuming 2-digit length indicator
start += 2; // Move start past the length indicator
if (length > maxLength) {
throw std::runtime_error("Field length exceeds maximum allowed length.");
}
std::string field = data.substr(start, length);
start += length; // Update start position to the end of the current field
return field;
}
// Parses an ISO8583 response string into a map of fields.
std::map<int, std::string> parseISO8583(const std::string& response) {
std::map<int, std::string> fields;
size_t index = 0;
// Fixed-length fields
fields[0] = response.substr(index, 4);
index += 4;
// Variable-length fields
fields[2] = parseVariableField(response, index, 20); // Field 2 can be up to 20 characters long
fields[4] = response.substr(index, 12); // Fixed length
index += 12;
fields[10] = response.substr(index, 12); // Fixed length
index += 12;
// More variable-length fields
fields[11] = parseVariableField(response, index, 40);
fields[12] = parseVariableField(response, index, 40);
fields[13] = parseVariableField(response, index, 40);
fields[14] = parseVariableField(response, index, 256);
fields[15] = parseVariableField(response, index, 256);
fields[16] = parseVariableField(response, index, 256);
fields[22] = response.substr(index, 4);
index += 4;
fields[23] = parseVariableField(response, index, 12);
fields[26] = parseVariableField(response, index, 20);
fields[31] = parseVariableField(response, index, 40);
fields[32] = response.substr(index, 12);
index += 12;
fields[33] = response.substr(index, 40);
index += 40;
// More fields including very large variable fields
fields[35] = parseVariableField(response, index, 660);
fields[36] = parseVariableField(response, index, 128);
fields[49] = response.substr(index, 3); // Assuming 3 characters for demo, adjust as needed
index += 3;
fields[50] = response.substr(index, 1);
index += 1;
fields[55] = parseVariableField(response, index, 20);
fields[57] = parseVariableField(response, index, 128);
fields[59] = parseVariableField(response, index, 20);
fields[60] = response.substr(index, 1);
index += 1;
fields[61] = response.substr(index, 1);
index += 1;
fields[62] = response.substr(index, 1);
index += 1;
return fields;
}
// Parses an ISO8583 response string into a map of fields.
std::map<int, std::string> parseXmlISO8583(const std::string& response) {
std::cout << "Parse XML"<<std::endl;
std::map<int, std::string> fieldMap;
XMLDocument doc;
doc.Parse(response.c_str());
XMLElement* isomsg = doc.FirstChildElement("isomsg");
if (isomsg) {
XMLElement* field = isomsg->FirstChildElement("field");
while (field) {
const char* id = field->Attribute("id");
const char* value = field->Attribute("value");
const int idint=std::stoi(id);
fieldMap[idint] = value;
std::cout << "Parse "<<id<<" "<<value<<std::endl;
std::cout << "Field " << id << ": " << value << std::endl;
field = field->NextSiblingElement("field");
}
}
return fieldMap;
}
std::string a = R"xml(
<isomsg>
<field id="0" value="0610"/>
<field id="2" value="2454-3000-0002"/>
<field id="4" value="490.00"/>
<field id="10" value="10.00"/>
<field id="11" value="k_ecpay_billspayment"/>
<field id="12" value="2020123456"/>
<field id="14" value="e21fa6326ebbea193d658f5b8fbb6bb8a4bd143c"/>
<field id="15" value="fc52e179fb491c85044daab53d8f07c10fbacef3"/>
<field id="16" value="k_ecpay_billspayment"/>
<field id="22" value="1"/>
<field id="23" value="0297"/>
<field id="26" value="54.88.172.141"/>
<field id="31" value=""/>
<field id="32" value="SUCCESS"/>
<field id="33" value="SUCCESS"/>
<field id="35" value="Cullinan Test ecpay"/>
<field id="36" value="2020123456; 2500000050060: k_ecpay_billspayment"/>
<field id="49"><![CDATA[{"itemCode":"2500000050893","account":"2020123456","qty":"1","contact":"test"}]]></field>
<field id="50" value="Y"/>
<field id="55" value="1.2.0.0"/>
<field id="57" value="56"/>
<field id="59" value="SALES"/>
<field id="60" value="null"/>
<field id="61" value="null"/>
</isomsg>
)xml";
int main(){
//parse and check for token expiry
std::string isoMessage=a;
try {
auto parsedFields = parseXmlISO8583(isoMessage);
for (const auto& field : parsedFields) {
std::cout << "Field " << field.first << ": " << field.second << std::endl;
}
} catch (const std::exception& e) {
std::cerr << "Error parsing ISO8583 message: " << e.what() <<"\n"<<isoMessage<< std::endl;
return 1;
}
return 0;
}