#!/usr/bin/env elixir # Test the corrected BusinessLogic and IncomingMessageProcessor architecture # Load required modules in dependency order Code.require_file("lib/da_product_app/mercury_iso8583/packagers/iso_component.ex") Code.require_file("lib/da_product_app/mercury_iso8583/packagers/iso_msg.ex") Code.require_file("lib/da_product_app/mercury_iso8583/processors/business_logic.ex") alias DaProductApp.MercuryISO8583.Packagers.ISOMsg # Create test message using ISOMsg functions test_message = ISOMsg.new() |> ISOMsg.set_mti("0200") |> ISOMsg.set(3, "000000") # Processing code |> ISOMsg.set(4, "000000005000") # Transaction amount (50.00) |> ISOMsg.set(11, "123456") # STAN |> ISOMsg.set(12, "123456") # Time |> ISOMsg.set(13, "1225") # Date IO.puts("๐Ÿงช Testing Corrected Architecture") IO.puts("=" |> String.duplicate(50)) # Test BusinessLogic with normal amount IO.puts("\n1๏ธโƒฃ Testing BusinessLogic.process_message/1 - Normal Amount") result = DaProductApp.MercuryISO8583.Processors.BusinessLogic.process_message(test_message) IO.puts("Result: #{inspect(result)}") case result do {:approve, enriched_msg} -> IO.puts("โœ… APPROVED: Message will be routed to upstream") IO.puts(" Enriched message: #{inspect(enriched_msg)}") {:decline, reason, enriched_msg} -> IO.puts("โŒ DECLINED: #{inspect(reason)} - Local response will be created") IO.puts(" Enriched message: #{inspect(enriched_msg)}") {:refer, enriched_msg} -> IO.puts("โš ๏ธ REFERRED: Needs manual review") IO.puts(" Enriched message: #{inspect(enriched_msg)}") {:error, reason} -> IO.puts("๐Ÿ’ฅ ERROR: #{inspect(reason)}") end # Test BusinessLogic with high amount (should refer) IO.puts("\n2๏ธโƒฃ Testing BusinessLogic.process_message/1 - High Amount") high_amount_message = ISOMsg.new() |> ISOMsg.set_mti("0200") |> ISOMsg.set(3, "000000") |> ISOMsg.set(4, "000000500001") # $5000.01 - above high_value_threshold |> ISOMsg.set(11, "123457") |> ISOMsg.set(12, "123456") |> ISOMsg.set(13, "1225") result2 = DaProductApp.MercuryISO8583.Processors.BusinessLogic.process_message(high_amount_message) IO.puts("Result: #{inspect(result2)}") case result2 do {:approve, _} -> IO.puts("โœ… APPROVED") {:decline, reason, _} -> IO.puts("โŒ DECLINED: #{inspect(reason)}") {:refer, _} -> IO.puts("โš ๏ธ REFERRED: High value transaction") {:error, reason} -> IO.puts("๐Ÿ’ฅ ERROR: #{inspect(reason)}") end # Test BusinessLogic with zero amount (should decline) IO.puts("\n3๏ธโƒฃ Testing BusinessLogic.process_message/1 - Invalid Amount") zero_amount_message = ISOMsg.new() |> ISOMsg.set_mti("0200") |> ISOMsg.set(3, "000000") |> ISOMsg.set(4, "000000000000") # $0.00 - invalid |> ISOMsg.set(11, "123458") |> ISOMsg.set(12, "123456") |> ISOMsg.set(13, "1225") result3 = DaProductApp.MercuryISO8583.Processors.BusinessLogic.process_message(zero_amount_message) IO.puts("Result: #{inspect(result3)}") case result3 do {:approve, _} -> IO.puts("โœ… APPROVED") {:decline, reason, _} -> IO.puts("โŒ DECLINED: #{inspect(reason)}") {:refer, _} -> IO.puts("โš ๏ธ REFERRED") {:error, reason} -> IO.puts("๐Ÿ’ฅ ERROR: #{inspect(reason)}") end IO.puts("\n" <> "=" |> String.duplicate(50)) IO.puts("๐Ÿ—๏ธ **ARCHITECTURE SUMMARY**") IO.puts("=" |> String.duplicate(50)) IO.puts(""" โœ… **FIXED ARCHITECTURE:** 1. **BusinessLogic.process_message/1 Returns:** - {:approve, enriched_message} โ†’ Route to upstream - {:decline, reason, enriched_message} โ†’ Create local response - {:refer, enriched_message} โ†’ Create referral response - {:error, reason} โ†’ Handle processing error 2. **IncomingMessageProcessor Flow:** ``` validate_message โ†’ enrich_message โ†’ apply_business_logic โ†“ โ†“ {:approve, msg} โ†’ route_to_upstream โ†’ process_upstream_response {:decline, reason, msg} โ†’ create_decline_response (local) {:refer, msg} โ†’ create_referral_response (local) ``` 3. **Eliminated Overlaps:** โŒ Removed response generation from BusinessLogic โŒ Removed duplicate validation logic โŒ Removed duplicate error handling โœ… BusinessLogic focuses on decisions only โœ… IncomingMessageProcessor handles orchestration and responses โœ… Clear separation of concerns 4. **Benefits:** - BusinessLogic is now a pure decision maker - No more architectural confusion about response generation - IncomingMessageProcessor properly orchestrates the flow - Upstream routing only happens when approved - Local responses for declines/referrals """) IO.puts("\nโœจ Architecture fix completed successfully!")