#!/usr/bin/env elixir # Test with actual device packet for YSP upstream routing Mix.install([]) # Simulate sending a real device packet to the system defmodule DevicePacketTest do def send_test_packet do # This simulates what would come from a real POS device # Format: Length (2 bytes) + ISO8583 message # Real ISO8583 message for a $26.00 purchase transaction iso_hex = "30323030" <> # MTI: 0200 "F23C448108E08000" <> # Bitmap "3438353439383036303037333637343000" <> # DE2: PAN (16 digits) "303030303030" <> # DE3: Processing Code (000000) "303030303030303032363030" <> # DE4: Amount ($26.00) "303030343535" <> # DE11: STAN (000455) "313434353035" <> # DE12: Time (144505) "30393234" <> # DE13: Date (0924) "303531" <> # DE22: POS Entry Mode (051) "303030" <> # DE23: Card Sequence Number (000) "373832" <> # DE24: NII - YSP Network (782) - This will trigger YSP routing "3030" <> # DE25: POS Condition Code (00) "3435343939383036303037333637343030443331303532303630303131373930" <> # DE35: Track 2 "32353236373134343334302020202020" <> # DE37: Reference Number "31323334353637312020" <> # DE41: Terminal ID "31323334353637383930313233343536" # DE42: Merchant ID # Calculate message length (hex string length / 2) message_length = byte_size(iso_hex) / 2 |> trunc() length_bytes = <> # Convert hex string to binary iso_binary = Base.decode16!(iso_hex) # Complete packet with length prefix packet = length_bytes <> iso_binary IO.puts "=== Testing YSP Transaction with Real Device Packet ===" IO.puts "Packet length: #{byte_size(packet)} bytes" IO.puts "Message length: #{message_length} bytes" IO.puts "ISO Hex: #{iso_hex}" IO.puts "Expected routing: YSP (DE24=782)" IO.puts "" # Send to the enhanced protocol listener on port 8583 case :gen_tcp.connect(~c"localhost", 8583, [:binary, active: false]) do {:ok, socket} -> IO.puts "✅ Connected to device listener on port 8583" case :gen_tcp.send(socket, packet) do :ok -> IO.puts "✅ Sent device packet successfully" # Wait for response case :gen_tcp.recv(socket, 0, 10_000) do {:ok, response} -> IO.puts "✅ Received response: #{byte_size(response)} bytes" # Parse response (first 2 bytes are length) <> = response response_hex = Base.encode16(response_data) IO.puts "Response length: #{resp_length} bytes" IO.puts "Response hex: #{response_hex}" # Try to parse the response MTI if byte_size(response_data) >= 4 do <> = response_data mti = String.trim(mti_bytes, <<0>>) IO.puts "Response MTI: #{mti}" # Check if it's a successful response (should be 0210 for 0200 request) if mti == "0210" do IO.puts "✅ Received proper sale response (0210)" else IO.puts "⚠️ Unexpected MTI: #{mti}" end end {:error, reason} -> IO.puts "❌ Failed to receive response: #{inspect(reason)}" end {:error, reason} -> IO.puts "❌ Failed to send packet: #{inspect(reason)}" end :gen_tcp.close(socket) {:error, reason} -> IO.puts "❌ Failed to connect to device listener: #{inspect(reason)}" IO.puts "Make sure the application is running with: mix run --no-halt" end IO.puts "\n=== Test Complete ===" end end # Run the test DevicePacketTest.send_test_packet()