#!/usr/bin/env elixir # Direct test to the MPGS simulator to see what it expects and sends back defmodule SimulatorTest do require Logger def test_raw_iso8583 do Logger.info("Testing raw ISO8583 message (no YSP framing)") host = "demo.ctrmv.com" port = 4444 timeout = 5000 # Simple 0200 (sale) request in raw ISO8583 message = create_simple_0200() case :gen_tcp.connect(String.to_charlist(host), port, [:binary, active: false], timeout) do {:ok, socket} -> IO.puts("✅ Connected to #{host}:#{port}") # Send raw ISO8583 case :gen_tcp.send(socket, message) do :ok -> IO.puts("✅ Sent #{byte_size(message)} bytes:") IO.puts(" Hex: #{Base.encode16(message)}") # Try to receive response case :gen_tcp.recv(socket, 0, timeout) do {:ok, response} -> IO.puts("✅ Received response (#{byte_size(response)} bytes):") IO.puts(" Hex: #{Base.encode16(response)}") IO.puts("\nResponse analysis:") analyze_response(response) {:error, :timeout} -> IO.puts("❌ TIMEOUT - Simulator did not respond within #{timeout}ms") {:error, reason} -> IO.puts("❌ ERROR receiving: #{inspect(reason)}") end :gen_tcp.close(socket) {:error, reason} -> IO.puts("❌ ERROR sending: #{inspect(reason)}") :gen_tcp.close(socket) end {:error, reason} -> IO.puts("❌ Connection failed to #{host}:#{port}: #{inspect(reason)}") end end defp create_simple_0200 do # Very simple raw ISO8583 0200 (sale) # MTI: 0200 (2 bytes) # Bitmap: 8 bytes (fields 2,3,4,11,12,13,14,41) # Fields: # - Field 2 (PAN): LLVAR 16 bytes # - Field 3 (Processing Code): 6 bytes # - Field 4 (Amount): 12 bytes (BCD) # - Field 11 (STAN): 6 bytes (BCD) # - Field 12 (Time): 6 bytes (BCD) # - Field 13 (Date): 4 bytes (BCD) # - Field 14 (Expiry): 4 bytes (BCD) # - Field 41 (Terminal ID): 8 bytes mti = "0200" # Bitmap for fields 2,3,4,11,12,13,14,41 # Binary: 11110000 00000000 00000000 00001000 = 0xF0000008 bitmap = <<0xF0, 0x00, 0x00, 0x08>> # Field 2 - PAN (16 bytes) field2 = "5559426552868466" field2_encoded = <> <> field2 # Field 3 - Processing code (6 bytes) field3 = "000000" # Field 4 - Amount (12 bytes BCD) field4 = "000000020000" # Field 11 - STAN (6 bytes BCD) field11 = "000063" # Field 12 - Time (6 bytes BCD) field12 = "045238" # Field 13 - Date (4 bytes BCD) field13 = "0605" # Field 14 - Expiry (4 bytes BCD) field14 = "2908" # Field 41 - Terminal ID (8 bytes) field41 = "12345671" message = mti <> bitmap <> field2_encoded <> field3 <> field4 <> field11 <> field12 <> field13 <> field14 <> field41 IO.puts("Raw ISO8583 message created: #{byte_size(message)} bytes") message end defp analyze_response(data) when byte_size(data) >= 4 do # Try to detect response format case data do <<0x03, 0x25, _::binary>> -> IO.puts(" Format: YSP-framed (starts with BCD length 0325)") <<"0410", _::binary>> -> IO.puts(" Format: ISO8583 0410 (sale response)") <<"0430", _::binary>> -> IO.puts(" Format: ISO8583 0430 (reversal response)") _ -> # Try to decode as ISO8583 case decode_iso_mti(data) do {:ok, mti} -> IO.puts(" Format: ISO8583 with MTI: #{mti}") :error -> IO.puts(" Format: Unknown - raw data") IO.inspect(data, limit: 50) end end end defp analyze_response(data) do IO.puts(" Response too short: #{byte_size(data)} bytes") end defp decode_iso_mti(<>) do {:ok, <>} rescue _ -> :error end end # Run the test SimulatorTest.test_raw_iso8583()