Skip to content

Commit

Permalink
Data Element (DE) for field 48.
Browse files Browse the repository at this point in the history
  • Loading branch information
xclud committed Jun 18, 2024
1 parent fc4d2cf commit c4e7f8a
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 0.3.5

* Data Element (DE) for field 48.

## 0.3.5

* Add partial field support.

## 0.3.4
Expand Down
1 change: 1 addition & 0 deletions lib/pos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:convert/convert.dart';
import 'package:iso9797/iso9797.dart' as iso9797;

part 'src/bitmap.dart';
part 'src/data_element.dart';
part 'src/field.dart';
part 'src/fields.dart';
part 'src/message.dart';
Expand Down
75 changes: 75 additions & 0 deletions lib/src/data_element.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
part of '../pos.dart';

/// Data Element (DE) for field 48 used for Private Additional data in all original specifications from 1987, 1993 and 2003 years.
class DataElement {
/// Constructor.
const DataElement({
this.serialNumber,
//this.protocolVersion,
this.appVersion,
this.language,
this.connectionType,
});

// Version of the protocol specifications.
//final String? protocolVersion;

/// Serial number of a component.
final String? serialNumber;

/// App version.
final String? appVersion;

/// Connection type.
final int? connectionType;

/// Language
final int? language;

List<int> _encode() {
final buffer = <int>[];

final s = serialNumber;

if (s != null) {
final sub = [0x01, ...s.codeUnits];

buffer.addAll(_decimalAsHexBytes(sub.length, 2));
buffer.addAll(sub);
}

final v = appVersion;
if (v != null) {
final sub = [0x02, ...v.codeUnits];

buffer.addAll(_decimalAsHexBytes(sub.length, 2));
buffer.addAll(sub);
}

final l = language;
if (l != null) {
final sub = [0x03, l];
buffer.addAll(_decimalAsHexBytes(sub.length, 2));
buffer.addAll(sub);
}

final c = connectionType;
if (c != null) {
final sub = [0x15, c];

buffer.addAll(_decimalAsHexBytes(sub.length, 2));
buffer.addAll(sub);
}

buffer.insertAll(0, _decimalAsHexBytes(buffer.length, 4));

return buffer;
}

/// Converts this object into JSON.
Map<String, Object> toJson() {
final m = <String, Object>{};

return m;
}
}
138 changes: 129 additions & 9 deletions lib/src/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class Message {
}

/// Sets a data element with index.
@Deprecated('Please use the specific property for the field number')
void set(int field, List<int> value) {
_data[field] = Uint8List.fromList(value);
}
Expand Down Expand Up @@ -164,20 +165,14 @@ class Message {
return Uint8List.fromList(x);
}

/// Gets the pos device terminal id (field 41).
String? get terminalId {
final f = get(41);
if (f == null) return null;

return String.fromCharCodes(f);
}

/// Gets a data element for index.
@Deprecated('Please use the specific property for the field number')
List<int>? get(int field) {
return _data[field];
}

/// Unset a data element for index.
@Deprecated('Please use the specific property for the field number')
void unset(int field) {
_data.remove(field);
}
Expand All @@ -186,7 +181,8 @@ class Message {
Uint8List encode({Uint8List Function(List<int> message)? algorithm}) {
if (algorithm != null) {
final y = calcmac(algorithm);
set(64, y);

mac = hex.encode(y).toUpperCase();
}

final bdy = _body();
Expand All @@ -203,6 +199,10 @@ class Message {
DateTime? _f1213DateTime;
String? _f24Nii;
String? _f35Track2;
String? _f41TerminalId;
String? _f42MerchantId;
DataElement? _f48DataElement;
int? _f49Currency;

String? _mac;

Expand Down Expand Up @@ -336,6 +336,66 @@ class Message {
}
}

/// Terminal Id.
/// Field 41.
String? get terminalId => _f41TerminalId;
set terminalId(String? value) {
final v = value;

if (v == null) {
_bmp[41] = false;
_f41TerminalId = null;
} else {
_bmp[41] = true;
_f41TerminalId = value;
}
}

/// Merchant Id.
/// Field 42.
String? get merchantId => _f42MerchantId;
set merchantId(String? value) {
final v = value;

if (v == null) {
_bmp[42] = false;
_f42MerchantId = null;
} else {
_bmp[42] = true;
_f42MerchantId = value;
}
}

/// Currency.
/// Field 48.
DataElement? get dataElement => _f48DataElement;
set dataElement(DataElement? value) {
final v = value;

if (v == null) {
_bmp[48] = false;
_f48DataElement = null;
} else {
_bmp[48] = true;
_f48DataElement = value;
}
}

/// Currency.
/// Field 49.
int? get currency => _f49Currency;
set currency(int? value) {
final v = value;

if (v == null) {
_bmp[49] = false;
_f49Currency = null;
} else {
_bmp[49] = true;
_f49Currency = value;
}
}

/// MAC.
/// Field 64 or 128.
///
Expand Down Expand Up @@ -458,6 +518,46 @@ class Message {
strBits.add(s2);
}

continue;
} else if (i == 41) {
final p = terminalId;

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

continue;
} else if (i == 42) {
final p = merchantId;

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

continue;
} else if (i == 48) {
final p = dataElement;

if (p != null) {
final enc = p._encode();
bits.add(enc);
strBits.add(hex.encode(enc).toUpperCase());
}

continue;
} else if (i == 49) {
final p = currency;

if (p != null) {
final h = p.toString().codeUnits;
final hh = hex.encode(h);

bits.add(h);
strBits.add(hh);
}

continue;
} else if (i == 64) {
final p = mac;
Expand Down Expand Up @@ -549,7 +649,11 @@ class Message {
final mDateTime = dateTime;
final mNii = nii;
final mTrack2 = track2;
final mTerminalId = terminalId;
final mMerchantId = merchantId;
final mCurrency = currency;
final mMac = mac;
final mDataElement = dataElement;

if (mPan != null) {
map['PAN'] = mPan;
Expand All @@ -575,6 +679,22 @@ class Message {
map['Track2'] = mTrack2;
}

if (mTerminalId != null) {
map['TerminalId'] = mTerminalId;
}

if (mMerchantId != null) {
map['MerchantId'] = mMerchantId;
}

if (mDataElement != null) {
map['DataElement'] = mDataElement.toJson();
}

if (mCurrency != null) {
map['Currency'] = mCurrency;
}

for (var i = 1; i < 64; i++) {
final f = _data[i];

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: pos
description: Dart Implementation of the ISO-8583 banking protocol for Point of Sale (POS) Devices.
version: 0.3.5
version: 0.3.6
repository: https://github.com/xclud/dart_pos
homepage: https://pwa.ir

Expand Down
Loading

0 comments on commit c4e7f8a

Please sign in to comment.