defmodule DaProductApp.Acquirer.YSP.Config do @moduledoc """ Centralized YSP configuration management. This module provides a unified interface to access YSP configuration from the consolidated upstream_networks configuration. """ @doc """ Gets the complete YSP configuration from the upstream networks configuration. Returns the ysp_config section from either ysp_ssl or ysp_plain network configuration. Falls back to default values if configuration is not found. """ def get_ysp_config do upstream_networks = Application.get_env(:da_product_app, :upstream_networks, %{}) # Try to get from ysp_ssl first, then ysp_plain as fallback case upstream_networks do %{ysp_ssl: %{ysp_config: config}} when is_map(config) -> config %{ysp_plain: %{ysp_config: config}} when is_map(config) -> config _ -> default_ysp_config() end end @doc """ Gets a specific configuration value from YSP config. """ def get(key, default \\ nil) do get_ysp_config() |> Map.get(key, default) end @doc """ Gets the required fields for a specific MTI. """ def get_required_fields(mti) do required_fields_map = get(:required_fields, %{}) Map.get(required_fields_map, mti, [2, 3, 4, 11]) # Default fallback end @doc """ Gets processing code transformation for a given code. """ def get_processing_code(original_code) do processing_codes = get(:processing_codes, %{}) Map.get(processing_codes, original_code, original_code) end @doc """ Gets the network identifier (NII). """ def get_nii do get(:network_identifier, "782") end @doc """ Gets the acquiring institution code. """ def get_acquiring_institution_code do get(:acquiring_institution_code, "999999") end @doc """ Gets default terminal ID. """ def get_default_terminal_id do get(:default_terminal_id, "YSP00001") end @doc """ Gets default merchant ID. """ def get_default_merchant_id do get(:default_merchant_id, "MERCURYPAY001") end @doc """ Gets the default master key (for development - replace with proper key management). """ def get_default_master_key do get(:default_master_key, "0123456789ABCDEF0123456789ABCDEF") end @doc """ Gets processing timeout in milliseconds. """ def get_processing_timeout do get(:processing_timeout, 30_000) end @doc """ Gets maximum retry attempts. """ def get_max_retries do get(:max_retries, 3) end @doc """ Gets retry delay in milliseconds. """ def get_retry_delay do get(:retry_delay, 5_000) end @doc """ Gets supported MTIs. """ def get_supported_mtis do get(:supported_mtis, ["0100", "0200", "0220", "0400", "0800"]) end @doc """ Gets supported fields. """ def get_supported_fields do get(:supported_fields, [2, 3, 4, 11, 12, 13, 14, 22, 23, 24, 25, 35, 37, 38, 39, 41, 42, 49, 52, 55, 62, 63]) end @doc """ Gets echo response code. """ def get_echo_response_code do get(:echo_response_code, "301") end @doc """ Gets PIN IPEK response code. """ def get_pin_ipek_response_code do get(:pin_ipek_response_code, "161") end @doc """ Gets message framing configuration. """ def get_message_framing_config do get(:message_framing, %{ type: :ysp_standard, length_prefix_size: 2, header_size: 2, header_value: <<0x30, 0x22>>, max_packet_size: 65535, endianness: :big }) end @doc """ Gets the YSP packet header value. """ def get_packet_header do framing_config = get_message_framing_config() Map.get(framing_config, :header_value, <<0x30, 0x22>>) end @doc """ Gets the maximum packet size for YSP. """ def get_max_packet_size do framing_config = get_message_framing_config() Map.get(framing_config, :max_packet_size, 65535) end # Private helper for default configuration defp default_ysp_config do %{ acquiring_institution_code: "999999", network_identifier: "782", nii: "782", # Added for compatibility acquiring_inst_code: "784", # Added for compatibility default_terminal_id: "YSP00001", default_merchant_id: "MERCURYPAY001", packager: DaProductApp.MercuryISO8583.Packagers.ISO87DPackager, message_framing: %{ type: :ysp_standard, length_prefix_size: 2, header_size: 2, header_value: <<0x30, 0x22>>, # Fixed YSP header (3022) max_packet_size: 65535, endianness: :big }, defaults: %{ terminal_id: "8900901 ", merchant_id: "01000006 ", merchant_name: "YSP Test Merchant", currency_code: "784" # UAE Dirham }, supported_mtis: ["0100", "0200", "0220", "0400", "0800"], supported_fields: [2, 3, 4, 11, 12, 13, 14, 22, 23, 24, 25, 35, 37, 38, 39, 41, 42, 49, 52, 55, 62, 63], echo_response_code: "301", pin_ipek_response_code: "161", default_master_key: "0123456789ABCDEF0123456789ABCDEF", processing_timeout: 30_000, max_retries: 3, retry_delay: 5_000, required_fields: %{ "0100" => [2, 3, 4, 11, 12, 13, 14, 22, 24, 25, 35, 41, 42, 49, 55], "0200" => [2, 3, 4, 11, 12, 13, 14, 22, 24, 25, 35, 37, 41, 42, 49, 55], "0220" => [2, 3, 4, 11, 24, 25, 35, 37, 38, 39, 41, 42, 49], "0400" => [2, 3, 4, 11, 24, 25, 37, 41, 42, 49, 90], "0800" => [11, 24, 70], "0810" => [11, 24, 39, 70] }, processing_codes: %{ "000000" => "000000", # Sale "000001" => "000000", # Sale adjustment "020000" => "020000", # Void "330000" => "330000", # PreAuth "preauth" => "330000", "tmk" => "991380", "pin_ipek" => "990280" }, timeouts: %{ sale: 30_000, void: 25_000, preauth: 30_000, completion: 25_000, reversal: 20_000, network_mgmt: 15_000 } } end end