private static void CheckTrack(string track, MagneticTrackKind trackKind, bool containsChecksum = false) { int length = track.Length; if (containsChecksum) { length--; } char sentineStart; char sentineEnd; switch (trackKind) { case MagneticTrackKind.Track1: sentineStart = MagneticStripeParser.Track1StartSentinel; sentineEnd = MagneticStripeParser.Track1EndSentinel; break; case MagneticTrackKind.Track2: sentineStart = MagneticStripeParser.Track2StartSentinel; sentineEnd = MagneticStripeParser.Track2EndSentinel; break; default: throw new NotSupportedException(string.Format("Retrieval of track '{0}' not supported.", trackKind)); } // Checking for track sentines if (length < 2 || track[0] != sentineStart || track[length - 1] != sentineEnd) { throw new FormatException("The track doesn't have start and end sentines."); } }
/// <summary> /// Extracts card number from a given track 1. /// </summary> /// <param name="track">The track of a payment card.</param> /// <param name="trackKind">The kind of the track.</param> /// <returns>The extracted card number.</returns> /// <remarks> /// Only format B for track 1 is supported. /// </remarks> public static string ExtractCardNumber(string track, MagneticTrackKind trackKind) { if (track == null) { throw new ArgumentNullException("track"); } CheckTrack(track, trackKind); switch (trackKind) { case MagneticTrackKind.Track1: return(ExtractTrack1BCardNumber(track)); case MagneticTrackKind.Track2: return(ExtractTrack2CardNumber(track)); default: throw new NotSupportedException(string.Format("Retrieval of card number from track '{0}' not supported.", trackKind)); } }
/// <summary> /// Extracts payment card track of given kind from a given raw magnetic stipe track. /// </summary> /// <param name="rawTrack">The raw magnetic stripe track.</param> /// <param name="offset">The offset in the raw track.</param> /// <param name="length">The length of the track.</param> /// <param name="trackKind">The kind of the track.</param> /// <param name="excludeChecksum">True to exclude trailing checksum from the extracted track; otherwise - false.</param> /// <returns>The extracted track.</returns> public static string ExtractTrack(byte[] rawTrack, int offset, int length, MagneticTrackKind trackKind, bool excludeChecksum = true) { if (rawTrack == null) { throw new ArgumentNullException("rawTrack"); } if (length == 0) { return(null); } var track = AsciiStringConverter.Ascii8BitToString(rawTrack, offset, length); CheckTrack(track, trackKind, !excludeChecksum); return(track); }