Skip to content

Commit

Permalink
More fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
xclud committed Jun 18, 2024
1 parent a069754 commit 510fd2d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 38 deletions.
72 changes: 56 additions & 16 deletions lib/src/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Message {
x.processCode = 0x410000;
x.dateTime = now;

x.posConditionCode = '14';
x.posConditionCode = 0x14;
x.currency = 364;

return x;
Expand All @@ -58,10 +58,10 @@ class Message {
x.set(4, amb);
x.dateTime = now;

x.set(25, [0x14]);
x.posConditionCode = 0x14;
x.set(46, [0x33, 0x30, 0x30]); // '300' in ASCII.
x.set(48, '200003123001a11003456001c'.codeUnits);
x.set(49, [0x33, 0x36, 0x34]); // '364' in ASCII.
x.currency = 364;
x.set(57, '1.4.8.2'.codeUnits);

return x;
Expand All @@ -75,7 +75,7 @@ class Message {
x.processCode = 0x000004;
x.dateTime = now;

x.set(25, [0x14]);
x.posConditionCode = 0x14;
x.terminalId = terminalId;

return x;
Expand All @@ -89,7 +89,7 @@ class Message {
x.processCode = 0x000005;
x.dateTime = now;

x.set(25, [0x14]);
x.posConditionCode = 0x14;
x.terminalId = terminalId;

return x;
Expand Down Expand Up @@ -117,7 +117,7 @@ class Message {
x.processCode = 0x000001;
x.dateTime = now;

x.set(25, [0x14]);
x.posConditionCode = 0x14;

return x;
}
Expand Down Expand Up @@ -196,9 +196,9 @@ class Message {
int? _f03ProcessCode;
int? _f11Stan;
DateTime? _f1213DateTime;
String? _f22CardEntryMode;
int? _f22CardEntryMode;
String? _f24Nii;
String? _f25POSConditionCode;
int? _f25POSConditionCode;
String? _f35Track2;
String? _f41TerminalId;
String? _f42MerchantId;
Expand Down Expand Up @@ -296,10 +296,15 @@ class Message {

/// Card Entry Mode.
/// Field 22.
String? get cardEntryMode => _f22CardEntryMode;
set cardEntryMode(String? value) {
int? get cardEntryMode => _f22CardEntryMode;
set cardEntryMode(int? value) {
final v = value;

assert(
v == null || v > -1 || v < 0xffff,
'CardEntryMode should be null or between [0x00, 0xFFFF].',
);

if (v == null) {
_bmp[22] = false;
_f22CardEntryMode = null;
Expand Down Expand Up @@ -333,10 +338,15 @@ class Message {

/// POS Condition Code.
/// Field 25.
String? get posConditionCode => _f25POSConditionCode;
set posConditionCode(String? value) {
int? get posConditionCode => _f25POSConditionCode;
set posConditionCode(int? value) {
final v = value;

assert(
v == null || v > -1 || v < 0xff,
'PosConditionCode should be null or between [0x00, 0xFF].',
);

if (v == null) {
_bmp[25] = false;
_f25POSConditionCode = null;
Expand Down Expand Up @@ -451,7 +461,7 @@ class Message {
/// MAC.
/// Field 64 or 128.
///
/// Must be 4 characters.
/// Must be 16 characters.
String? get mac => _mac;
set mac(String? value) {
final v = value;
Expand Down Expand Up @@ -546,6 +556,36 @@ class Message {
strBits.add(h);
}

continue;
} else if (i == 22) {
final p = cardEntryMode;

if (p != null) {
final bt = ByteData(2);
bt.setUint16(0, p, Endian.big);

final f22 = bt.buffer.asUint8List();
final s22 = hex.encode(f22);

bits.add(f22);
strBits.add(s22);
}

continue;
} else if (i == 25) {
final p = posConditionCode;

if (p != null) {
final bt = ByteData(1);
bt.setUint8(0, p);

final f25 = bt.buffer.asUint8List();
final s25 = hex.encode(f25);

bits.add(f25);
strBits.add(s25);
}

continue;
} else if (i == 24) {
final p = _f24Nii;
Expand Down Expand Up @@ -579,7 +619,7 @@ class Message {
final p = terminalId;

if (p != null) {
bits.add(p.codeUnits);
bits.add(hex.decode(p));
strBits.add(p);
}

Expand All @@ -588,7 +628,7 @@ class Message {
final p = merchantId;

if (p != null) {
bits.add(p.codeUnits);
bits.add(hex.decode(p));
strBits.add(p);
}

Expand Down Expand Up @@ -686,7 +726,7 @@ class Message {
/// Calculates the MAC for current [Message].
Uint8List calcmac(Uint8List Function(List<int> message) algorithm) {
final c = clone();
c.mac = '00000000000000';
c.mac = '0000000000000000';
final bmp = c._bitmap();
c.mac = null;

Expand Down
63 changes: 41 additions & 22 deletions test/pos_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,43 @@ const mid = 30000000509940;
const sid = 'N82W330965';
const aid = '5.0.3';

const macKey = [
0x3D,
0xEA,
0x62,
0x23,
0x53,
0x4F,
0x33,
0x60,
0x0C,
// const macKey = [
// 0x3D,
// 0xEA,
// 0x62,
// 0x23,
// 0x53,
// 0x4F,
// 0x33,
// 0x60,
// 0x0C,
// 0x84,
// 0x35,
// 0x01,
// 0x7F,
// 0xC2,
// 0xB5,
// 0xE9
// ];

final macKey = Uint8List.fromList([
0x46,
0xB0,
0xAA,
0x84,
0x35,
0x01,
0xC9,
0xAB,
0x2C,
0xC8,
0x7F,
0xC2,
0xB5,
0xE9
];
0xBD,
0x7C,
0x73,
0x6E,
0x51,
0x5C,
0x06
]);

void main() {
final key = Uint8List.fromList([
Expand Down Expand Up @@ -92,7 +111,7 @@ void main() {
message.pan = '6274121195119854';
message.processCode = 0x310000;
message.track2 = '6274121195119854d281010052639594340480';
message.stan = 123456;
message.stan = 123457;
message.dateTime = now;
message.nii = '0300';
message.terminalId = tid.toString();
Expand All @@ -105,19 +124,18 @@ void main() {
connectionType: 0x32,
);

message.cardEntryMode = '0021';
message.posConditionCode = '00';
message.cardEntryMode = 0x0021;
message.posConditionCode = 0x00;

// Pin Block
message.pinBlock = [0xB5, 0xB5, 0x2E, 0xB4, 0x10, 0x13, 0x9F, 0xD7];

message.mac = '0000000000000000';
final messageData = message.encode(algorithm: _calculateMac);

final messageHex = hex.encode(messageData).toUpperCase();

expect(messageHex,
'01006038058020C1900116627412119511985431000012345614240306100021030000376274121195119854D28101005263959434048033303031313433323330303030303030353039393430002511014E3832573333303936350602352E302E33020330021532333634B5B52EB410139FD73231393042313445');
// expect(messageHex,
// '01006038058020C1900116627412119511985431000012345714240306100021030000376274121195119854D2810100526395943404803001143230000000509940002511014E3832573333303936350602352E302E33020330021532333634B5B52EB410139FD73535303542344438');
});
}

Expand All @@ -132,6 +150,7 @@ Uint8List _calculateMac(List<int> data) {
}

final mac = iso9797.algorithm3(macKey, data, iso9797.PaddingMode.method1);

final macU = mac.map((e) => e.toRadixString(16)).join().toUpperCase();

final result = macU.codeUnits.take(8);
Expand Down

0 comments on commit 510fd2d

Please sign in to comment.