Skip to content

Commit

Permalink
compatible with .net 5 and .net standard 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmyo committed Jun 15, 2022
1 parent 09aff51 commit acd005d
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 139 deletions.
81 changes: 42 additions & 39 deletions DataUtils.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
namespace Repos.MSR100Controller;
internal static class DataUtils
using MSR_100_Magnetic_Stripe_Parser;
namespace Repos.MSR100Controller
{
internal static void SetNameData(ref Track01Info cardinfoTrack1, string nameInfo)
internal static class DataUtils
{
cardinfoTrack1.Name = nameInfo;
var namesplit = cardinfoTrack1.Name.Split('/');

cardinfoTrack1.LastName = namesplit[0].Trim();
cardinfoTrack1.FirstName = string.Empty;
if (namesplit.Length > 1)
internal static void SetNameData(ref Track01Info cardinfoTrack1, string nameInfo)
{
cardinfoTrack1.FirstName = namesplit[1].Trim();
}
cardinfoTrack1.Name = nameInfo;
var namesplit = cardinfoTrack1.Name.Split('/');

cardinfoTrack1.Name = $"{cardinfoTrack1.FirstName} {cardinfoTrack1.LastName}";
}
internal static DateTime ExpirationDate(string date)
{
var year = Convert.ToInt16(date.Substring(0, 2)) + 2000;
var month = Convert.ToInt16(date.Substring(2, 2));
return new DateTime(year, month, 1);
}
internal static void SetDiscretionaryData(ref Track01Info cardinfoTrack1, string[] fieldsTrack01)
{
cardinfoTrack1.ExpirationDate = new DateTime();
cardinfoTrack1.ServiceCode = null;
cardinfoTrack1.LastName = namesplit[0].Trim();
cardinfoTrack1.FirstName = string.Empty;
if (namesplit.Length > 1)
{
cardinfoTrack1.FirstName = namesplit[1].Trim();
}

if (fieldsTrack01.Length == 3)
cardinfoTrack1.Name = $"{cardinfoTrack1.FirstName} {cardinfoTrack1.LastName}";
}
internal static System.DateTime ExpirationDate(string date)
{
cardinfoTrack1.ExpirationDate = ExpirationDate(fieldsTrack01[2]);
cardinfoTrack1.ServiceCode = fieldsTrack01[2].Substring(4, 3);
cardinfoTrack1.DiscretionaryData = fieldsTrack01[2].Substring(7);
var year = System.Convert.ToInt16(date.Substring(0, 2)) + 2000;
var month = System.Convert.ToInt16(date.Substring(2, 2));
return new System.DateTime(year, month, 1);
}
else
internal static void SetDiscretionaryData(ref Track01Info cardinfoTrack1, string[] fieldsTrack01)
{
if (fieldsTrack01.Length == 4)
cardinfoTrack1.ExpirationDate = new System.DateTime();
cardinfoTrack1.ServiceCode = null;

if (fieldsTrack01.Length == 3)
{
cardinfoTrack1.ExpirationDate = ExpirationDate(fieldsTrack01[2]);
cardinfoTrack1.ServiceCode = fieldsTrack01[2].Substring(4, 3);
cardinfoTrack1.DiscretionaryData = fieldsTrack01[2].Substring(7);
}
else
{
if (fieldsTrack01[2].Length > 1) //has date
if (fieldsTrack01.Length == 4)
{
cardinfoTrack1.ExpirationDate = ExpirationDate(fieldsTrack01[2]);
if (fieldsTrack01[2].Length > 1) //has date
{
cardinfoTrack1.ExpirationDate = ExpirationDate(fieldsTrack01[2]);
}
else // has service code
{
cardinfoTrack1.ServiceCode = fieldsTrack01[3];
}

cardinfoTrack1.DiscretionaryData = fieldsTrack01[3];
}
else // has service code
else if (fieldsTrack01.Length == 5)
{
cardinfoTrack1.ServiceCode = fieldsTrack01[3];
cardinfoTrack1.DiscretionaryData = fieldsTrack01[4];
}

cardinfoTrack1.DiscretionaryData = fieldsTrack01[3];
}
else if (fieldsTrack01.Length == 5)
{
cardinfoTrack1.DiscretionaryData = fieldsTrack01[4];
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions ITRack.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace Repos.MSR100Controller;
public interface ITRack
namespace Repos.MSR100Controller
{
string Raw { get; set; }
string PAN { get; set; }
DateTime ExpirationDate { get; set; }
string ServiceCode { get; set; }
string DiscretionaryData { get; set; }
public interface ITRack
{
string Raw { get; set; }
string PAN { get; set; }
System.DateTime ExpirationDate { get; set; }
string ServiceCode { get; set; }
string DiscretionaryData { get; set; }
}
}
6 changes: 4 additions & 2 deletions MSR-100-Magnetic-Stripe-Parser.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net5.0;net6.0</TargetFrameworks>
<PackageId>MSR-100-Magnetic-Stripe-Parser</PackageId>
<Version>1.0.1</Version>
<RootNamespace>MSR_100_Magnetic_Stripe_Parser</RootNamespace>
Expand All @@ -16,6 +16,8 @@
<None Remove="MSR-100-Magnetic-Stripe-ParserTests\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.IO.Ports" Version="6.0.0" />
<PackageReference Include="System.IO.Ports" Version="5.0.1" Condition="'$(TargetFramework)' == 'net5.0'" />
<PackageReference Include="System.IO.Ports" Version="6.0.0" Condition="'$(TargetFramework)' == 'net6.0'" />
<PackageReference Include="System.IO.Ports" Version="6.0.0" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
</ItemGroup>
</Project>
90 changes: 46 additions & 44 deletions MSR100Controller.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
global using System;
using System;
using System.IO.Ports;
namespace Repos.MSR100Controller;
public delegate void CardHandler(MagneticCardInfo cardinfo);
public class MSR100Controller : IDisposable
namespace Repos.MSR100Controller
{
public MSR100Controller(string PortName, int baudRate)
public delegate void CardHandler(MagneticCardInfo cardinfo);
public class MSR100Controller : IDisposable
{
_SerialPort = new SerialPort(PortName, baudRate);
_SerialPort.DataReceived += DataRecived;
_SerialPort.Open();
}
public MSR100Controller()
{
_SerialPort = new SerialPort("COM1", 9600);
_SerialPort.DataReceived += DataRecived;
_SerialPort.Open();
}
private readonly SerialPort _SerialPort;
public event CardHandler OnCardSwiped;
void DataRecived(object sender, SerialDataReceivedEventArgs e)
{
var sp = (SerialPort)sender;
var data = sp.ReadLine();
var cardinfo = ParseData(data);
OnCardSwiped?.Invoke(cardinfo);
}
public static MagneticCardInfo ParseData(string data)
{
data = data.Replace("\r", null);
var cardinfo = new MagneticCardInfo
public MSR100Controller(string PortName, int baudRate)
{
_SerialPort = new SerialPort(PortName, baudRate);
_SerialPort.DataReceived += DataRecived;
_SerialPort.Open();
}
public MSR100Controller()
{
_SerialPort = new SerialPort("COM1", 9600);
_SerialPort.DataReceived += DataRecived;
_SerialPort.Open();
}
private readonly SerialPort _SerialPort;
public event CardHandler OnCardSwiped;
void DataRecived(object sender, SerialDataReceivedEventArgs e)
{
Raw = data
};
var sp = (SerialPort)sender;
var data = sp.ReadLine();
var cardinfo = ParseData(data);
OnCardSwiped?.Invoke(cardinfo);
}
public static MagneticCardInfo ParseData(string data)
{
data = data.Replace("\r", null);
var cardinfo = new MagneticCardInfo
{
Raw = data
};

var tracks = data.Split('?'); //split end sentinel
var tracks = data.Split('?'); //split end sentinel

if (tracks.Length > 0)
{
cardinfo.Track1.ParseTrack01(tracks[0]);

if (tracks.Length > 1)
if (tracks.Length > 0)
{
if (tracks[1].Length <= 40 && tracks[1].Length > 1)
cardinfo.Track1.ParseTrack01(tracks[0]);

if (tracks.Length > 1)
{
cardinfo.Track1.DiscretionaryData = cardinfo.Track2.ParseTrack02( tracks[1]);
if (tracks[1].Length <= 40 && tracks[1].Length > 1)
{
cardinfo.Track1.DiscretionaryData = cardinfo.Track2.ParseTrack02(tracks[1]);
}
}
}
return cardinfo;
}
public void Dispose()
{
GC.SuppressFinalize(this);
_SerialPort.Close();
}
return cardinfo;
}
public void Dispose()
{
GC.SuppressFinalize(this);
_SerialPort.Close();
}
}
20 changes: 12 additions & 8 deletions MagneticCardInfo.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
namespace Repos.MSR100Controller;
public class MagneticCardInfo
using MSR_100_Magnetic_Stripe_Parser;

namespace Repos.MSR100Controller
{
public MagneticCardInfo()
public class MagneticCardInfo
{
Track1 = new Track01Info();
Track2 = new Track02Info();
public MagneticCardInfo()
{
Track1 = new Track01Info();
Track2 = new Track02Info();
}
public Track01Info Track1 { get; set; }
public Track02Info Track2 { get; set; }
public string Raw { get; set; }
}
public Track01Info Track1 { get; set; }
public Track02Info Track2 { get; set; }
public string Raw { get; set; }
}
25 changes: 14 additions & 11 deletions Track01Info.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
namespace Repos.MSR100Controller;
public class Track01Info : ITRack
using Repos.MSR100Controller;
namespace MSR_100_Magnetic_Stripe_Parser
{
public char FormatCode { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Name { get; set; }
public string Raw { get; set; }
public string PAN { get; set; }
public DateTime ExpirationDate { get; set; }
public string ServiceCode { get; set; }
public string DiscretionaryData { get; set; }
public class Track01Info : ITRack
{
public char FormatCode { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Name { get; set; }
public string Raw { get; set; }
public string PAN { get; set; }
public System.DateTime ExpirationDate { get; set; }
public string ServiceCode { get; set; }
public string DiscretionaryData { get; set; }
}
}
17 changes: 10 additions & 7 deletions Track02Info.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
namespace Repos.MSR100Controller;
public class Track02Info : ITRack
using Repos.MSR100Controller;
namespace MSR_100_Magnetic_Stripe_Parser
{
public string Raw { get; set; }
public string PAN { get; set; }
public DateTime ExpirationDate { get; set; }
public string ServiceCode { get; set; }
public string DiscretionaryData { get; set; }
public class Track02Info : ITRack
{
public string Raw { get; set; }
public string PAN { get; set; }
public System.DateTime ExpirationDate { get; set; }
public string ServiceCode { get; set; }
public string DiscretionaryData { get; set; }
}
}
45 changes: 24 additions & 21 deletions TrackParsers.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
namespace Repos.MSR100Controller;
public static class TrackParsers
using MSR_100_Magnetic_Stripe_Parser;
namespace Repos.MSR100Controller
{
public static void ParseTrack01(this Track01Info cardinfoTrack1, string tracks0)
public static class TrackParsers
{
cardinfoTrack1.Raw = tracks0;
if (tracks0.Length <= 79)
public static void ParseTrack01(this Track01Info cardinfoTrack1, string tracks0)
{
var fieldsTrack01 = tracks0.Substring(2).Split('^');
cardinfoTrack1.FormatCode = tracks0[1];
cardinfoTrack1.PAN = fieldsTrack01[0];
DataUtils.SetNameData(ref cardinfoTrack1, fieldsTrack01[1]);
DataUtils.SetDiscretionaryData(ref cardinfoTrack1, fieldsTrack01);
cardinfoTrack1.Raw = tracks0;
if (tracks0.Length <= 79)
{
var fieldsTrack01 = tracks0.Substring(2).Split('^');
cardinfoTrack1.FormatCode = tracks0[1];
cardinfoTrack1.PAN = fieldsTrack01[0];
DataUtils.SetNameData(ref cardinfoTrack1, fieldsTrack01[1]);
DataUtils.SetDiscretionaryData(ref cardinfoTrack1, fieldsTrack01);
}
}
public static string ParseTrack02(this Track02Info cardinfoTrack2, string tracks1)
{
cardinfoTrack2.Raw = tracks1;
var fieldsTrack02 = tracks1.Substring(1).Split('=');
cardinfoTrack2.PAN = fieldsTrack02[0];
var year = System.Convert.ToInt16(fieldsTrack02[1].Substring(0, 2)) + 2000;
var month = System.Convert.ToInt16(fieldsTrack02[1].Substring(2, 2));
cardinfoTrack2.ExpirationDate = new System.DateTime(year, month, 1);
cardinfoTrack2.ServiceCode = fieldsTrack02[1].Substring(4, 3);
return fieldsTrack02[1].Substring(7);
}
}
public static string ParseTrack02(this Track02Info cardinfoTrack2, string tracks1)
{
cardinfoTrack2.Raw = tracks1;
var fieldsTrack02 = tracks1.Substring(1).Split('=');
cardinfoTrack2.PAN = fieldsTrack02[0];
var year = Convert.ToInt16(fieldsTrack02[1].Substring(0, 2)) + 2000;
var month = Convert.ToInt16(fieldsTrack02[1].Substring(2, 2));
cardinfoTrack2.ExpirationDate = new DateTime(year, month, 1);
cardinfoTrack2.ServiceCode = fieldsTrack02[1].Substring(4, 3);
return fieldsTrack02[1].Substring(7);
}
}

0 comments on commit acd005d

Please sign in to comment.