module ISO8583

Copyright 2009 by Tim Becker (tim.becker@kuriostaet.de) MIT License, for details, see the LICENSE file accompaning this distribution

require 'date'

Copyright 2009 by Tim Becker (tim.becker@kuriostaet.de) MIT License, for details, see the LICENSE file accompaning this distribution

Copyright 2009 by Tim Becker (tim.becker@kuriostaet.de) MIT License, for details, see the LICENSE file accompaning this distribution

Constants

A

Fixed length ASCII letters [A-Za-z]

AN

Fixed lengh ASCII [A-Za-z0-9], padding left justified using spaces.

ANP

Fixed lengh ASCII [A-Za-z0-9] and space, padding left, spaces

ANP_Codec
ANS

Fixed length ASCII [x20-x7E], padding left, spaces

ANS_Codec
AN_Codec
ASCII_Number

ASCII_Number

A_Codec
B

Binary data, padding left using nulls (0x00)

Hhmmss
HhmmssCodec
IBM0372US_ASCII
LL

Special form to de/encode variable length indicators, two bytes ASCII numerals

LLL

Special form to de/encode variable length indicators, three bytes ASCII numerals

LLLVAR_AN

Three byte variable length ASCII numeral, payload ASCII, fixed length, zeropadded (right)

LLLVAR_ANS

Three byte variable length ASCII numeral, payload ASCII+special

LLLVAR_B

Three byte variable length binary payload

LLLVAR_N

Three byte variable length ASCII numeral, payload ASCII numerals

LLLVAR_Z

Thre byte variable length ASCII numeral, payload Track2 data

LLVAR_AN

Two byte variable length ASCII numeral, payload ASCII, fixed length, zeropadded (right)

LLVAR_ANS

Two byte variable length ASCII numeral, payload ASCII+special

LLVAR_B

Two byte variable length binary payload

LLVAR_N

Two byte variable length ASCII numeral, payload ASCII numerals

LLVAR_Z

Two byte variable length ASCII numeral, payload Track2 data

LL_BCD
MMDD
MMDDCodec
MMDDhhmmss

Date, formatted as described in ASCII numerals

MMDDhhmmssCodec
N

Fixed lengh numerals, repesented in ASCII, padding right justified using zeros

N_BCD
Null_Codec
PADDING_LEFT_JUSTIFIED_SPACES
PASS_THROUGH_DECODER
Packed_Number

Takes a number or str representation of a number and BCD encodes it, e.g. “1234” => “x12x34” 3456 => “x34x56”

right justified with null … (correct to do this? almost certainly not…)

Track2
UNKNOWN

Unknown

US_ASCII2IBM037

The charsets supported by iconv aren't guranteed. At the very least MACs don't support ebcdic, so providing rudimentary mappings here.

VERSION
XN
YYMM

Date, formatted as described in ASCII numerals

YYMMCodec
YYMMDDhhmmss

Date, formatted as described in ASCII numerals

YYMMDDhhmmssCodec
Z

Two byte variable length ASCII numeral, payload Track2 data

Public Class Methods

_date_codec(fmt) click to toggle source
# File lib/iso8583/codec.rb, line 168
def self._date_codec(fmt)
  c = Codec.new
  c.encoder = lambda {|date|
    enc = case date
          when DateTime, Date, Time
            date.strftime(fmt)
          when String
            begin
              dt = DateTime.strptime(date, fmt)
              dt.strftime(fmt)
            rescue => e
              msg = "Invalid format encoding: #{date}, must be #{fmt}."
              ContextLog.exception(e, e.backtrace, msg) if Object.defined?(:ContextLog)
              raise ISO8583Exception.new(msg)
            end
          else
            raise ISO8583Exception.new("Don't know how to encode: #{date.class} to a time.")
          end
    return enc
  }
  c.decoder = lambda {|str|
    begin
      DateTime.strptime(str, fmt)
    rescue => e
      msg = "Invalid format decoding: #{str}, must be #{fmt}."
      ContextLog.exception(e, e.backtrace, msg) if Object.defined?(:ContextLog)
      raise ISO8583Exception.new(msg)
    end
  }

  c
end

Public Instance Methods

_conv(str, mapping) click to toggle source
# File lib/iso8583/util.rb, line 32
def _conv(str, mapping)
  _str = String.new("", encoding: "ASCII-8BIT")
  str.each_byte{|byte|
    _str << mapping[byte]
  }
  _str
end
ascii2ebcdic(ascii) click to toggle source

Convert a String of ASCII chars to EBCDIC

# File lib/iso8583/util.rb, line 43
def ascii2ebcdic(ascii)
  _conv(ascii, US_ASCII2IBM037)
end
b2hex(byte_string) click to toggle source

general utilities

Convert a String of bytes to their hex representation E.g.:

b2hex "\x12\x34\xab" => "1234AB"
# File lib/iso8583/util.rb, line 14
def b2hex(byte_string)
  r = byte_string.unpack("H*")[0]
  r.length > 1 ? r : "  "
end
ebcdic2ascii(ebcdic) click to toggle source

Convert an EBCDIC String to ASCII

# File lib/iso8583/util.rb, line 50
def ebcdic2ascii(ebcdic)
  _conv(ebcdic, IBM0372US_ASCII)
end
hex2b(hex_string) click to toggle source

Convert a String containing hex data to a String containing the corresponding bytes:

hex2b "abcd12" => "\xa\cd\12"
# File lib/iso8583/util.rb, line 25
def hex2b(hex_string)
  string = hex_string.to_s.gsub(/\s+/, "")
  raise ISO8583Exception.new("Invalid Hex chars: #{hex_string}") unless string =~ /^[A-Fa-f0-9]*$/
  raise ISO8583Exception.new("Uneven number of Hex chars #{hex_string}") unless ( (string.length % 2) == 0)
  [string].pack("H*")
end