Class: CloverSandboxSimulator::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/clover_sandbox_simulator/configuration.rb

Constant Summary collapse

MERCHANTS_FILE =

Path to merchants JSON file

File.join(File.dirname(__FILE__), "..", "..", ".env.json")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/clover_sandbox_simulator/configuration.rb', line 12

def initialize
  @merchant_id   = ENV.fetch("CLOVER_MERCHANT_ID", nil)
  @merchant_name = ENV.fetch("CLOVER_MERCHANT_NAME", nil)
  @api_token     = ENV.fetch("CLOVER_API_TOKEN", nil)
  @environment   = normalize_url(ENV.fetch("CLOVER_ENVIRONMENT", "https://sandbox.dev.clover.com/"))
  @log_level     = parse_log_level(ENV.fetch("LOG_LEVEL", "INFO"))
  @tax_rate      = ENV.fetch("TAX_RATE", "8.25").to_f
  @business_type = ENV.fetch("BUSINESS_TYPE", "restaurant").to_sym

  # Ecommerce API tokens (for card payments and refunds)
  @public_token  = ENV.fetch("PUBLIC_TOKEN", nil)
  @private_token = ENV.fetch("PRIVATE_TOKEN", nil)
  @ecommerce_environment = normalize_url(ENV.fetch("ECOMMERCE_ENVIRONMENT", "https://scl-sandbox.dev.clover.com/"))
  @tokenizer_environment = normalize_url(ENV.fetch("TOKENIZER_ENVIRONMENT", "https://token-sandbox.dev.clover.com/"))

  # OAuth credentials (for token refresh)
  @app_id = ENV.fetch("CLOVER_APP_ID", nil)
  @app_secret = ENV.fetch("CLOVER_APP_SECRET", nil)
  @refresh_token = ENV.fetch("CLOVER_REFRESH_TOKEN", nil)

  # Load from .env.json if merchant_id not set in ENV
  load_from_merchants_file if @merchant_id.nil? || @merchant_id.empty?
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def api_token
  @api_token
end

#app_idObject

Returns the value of attribute app_id.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def app_id
  @app_id
end

#app_secretObject

Returns the value of attribute app_secret.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def app_secret
  @app_secret
end

#business_typeObject

Returns the value of attribute business_type.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def business_type
  @business_type
end

#ecommerce_environmentObject

Returns the value of attribute ecommerce_environment.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def ecommerce_environment
  @ecommerce_environment
end

#environmentObject

Returns the value of attribute environment.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def environment
  @environment
end

#log_levelObject

Returns the value of attribute log_level.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def log_level
  @log_level
end

#merchant_idObject

Returns the value of attribute merchant_id.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def merchant_id
  @merchant_id
end

#merchant_nameObject

Returns the value of attribute merchant_name.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def merchant_name
  @merchant_name
end

#private_tokenObject

Returns the value of attribute private_token.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def private_token
  @private_token
end

#public_tokenObject

Returns the value of attribute public_token.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def public_token
  @public_token
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def refresh_token
  @refresh_token
end

#tax_rateObject

Returns the value of attribute tax_rate.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def tax_rate
  @tax_rate
end

#tokenizer_environmentObject

Returns the value of attribute tokenizer_environment.



5
6
7
# File 'lib/clover_sandbox_simulator/configuration.rb', line 5

def tokenizer_environment
  @tokenizer_environment
end

Instance Method Details

#available_merchantsArray<Hash>

List all available merchants from .env.json

Returns:

  • (Array<Hash>)

    Array of merchant configs



72
73
74
75
76
77
78
79
80
# File 'lib/clover_sandbox_simulator/configuration.rb', line 72

def available_merchants
  load_merchants_file.map do |m|
    {
      id: m["CLOVER_MERCHANT_ID"],
      name: m["CLOVER_MERCHANT_NAME"],
      has_ecommerce: !m["PUBLIC_TOKEN"].to_s.empty? && !m["PRIVATE_TOKEN"].to_s.empty?
    }
  end
end

#ecommerce_enabled?Boolean

Check if Ecommerce API is configured

Returns:

  • (Boolean)


104
105
106
107
# File 'lib/clover_sandbox_simulator/configuration.rb', line 104

def ecommerce_enabled?
  !public_token.nil? && !public_token.empty? &&
    !private_token.nil? && !private_token.empty?
end

#load_merchant(merchant_id: nil, index: nil) ⇒ self

Load configuration for a specific merchant from .env.json

Parameters:

  • merchant_id (String, nil) (defaults to: nil)

    Merchant ID to load (nil for first merchant)

  • index (Integer, nil) (defaults to: nil)

    Index of merchant in the list (0-based)

Returns:

  • (self)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/clover_sandbox_simulator/configuration.rb', line 47

def load_merchant(merchant_id: nil, index: nil)
  merchants = load_merchants_file
  return self if merchants.empty?

  merchant = if merchant_id
               merchants.find { |m| m["CLOVER_MERCHANT_ID"] == merchant_id }
             elsif index
               merchants[index]
             else
               merchants.first
             end

  if merchant
    apply_merchant_config(merchant)
    logger.info "Loaded merchant: #{@merchant_name} (#{@merchant_id})"
  else
    logger.warn "Merchant not found: #{merchant_id || "index #{index}"}"
  end

  self
end

#loggerObject



117
118
119
120
121
122
123
124
125
# File 'lib/clover_sandbox_simulator/configuration.rb', line 117

def logger
  @logger ||= Logger.new($stdout).tap do |log|
    log.level = @log_level
    log.formatter = proc do |severity, datetime, _progname, msg|
      timestamp = datetime.strftime("%Y-%m-%d %H:%M:%S")
      "[#{timestamp}] #{severity.ljust(5)} | #{msg}\n"
    end
  end
end

#oauth_enabled?Boolean

Check if OAuth is configured for token refresh

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/clover_sandbox_simulator/configuration.rb', line 37

def oauth_enabled?
  !app_id.nil? && !app_id.empty? &&
    !app_secret.nil? && !app_secret.empty?
end

#platform_enabled?Boolean

Check if Platform API is configured (has OAuth token)

Returns:

  • (Boolean)


99
100
101
# File 'lib/clover_sandbox_simulator/configuration.rb', line 99

def platform_enabled?
  !api_token.nil? && !api_token.empty? && api_token != "NEEDS_REFRESH"
end

#validate!Object

Raises:



82
83
84
85
86
87
88
# File 'lib/clover_sandbox_simulator/configuration.rb', line 82

def validate!
  raise ConfigurationError, "CLOVER_MERCHANT_ID is required" if merchant_id.nil? || merchant_id.empty?

  # API token is only required for Platform API operations
  # Ecommerce-only operations can work without it
  true
end

#validate_ecommerce!Object

Validate Ecommerce configuration

Raises:



110
111
112
113
114
115
# File 'lib/clover_sandbox_simulator/configuration.rb', line 110

def validate_ecommerce!
  raise ConfigurationError, "PUBLIC_TOKEN is required for Ecommerce API" if public_token.nil? || public_token.empty?
  raise ConfigurationError, "PRIVATE_TOKEN is required for Ecommerce API" if private_token.nil? || private_token.empty?

  true
end

#validate_platform!Object

Validate for Platform API operations (requires OAuth token)

Raises:



91
92
93
94
95
96
# File 'lib/clover_sandbox_simulator/configuration.rb', line 91

def validate_platform!
  validate!
  raise ConfigurationError, "CLOVER_API_TOKEN is required for Platform API" if api_token.nil? || api_token.empty?

  true
end