Class: Veritrans::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/veritrans/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Config

Returns a new instance of Config.



9
10
11
12
# File 'lib/veritrans/config.rb', line 9

def initialize(options = nil)
  @api_host = "https://api.sandbox.midtrans.com"
  apply(options) if options
end

Instance Method Details

#api_hostObject



51
52
53
# File 'lib/veritrans/config.rb', line 51

def api_host
  @api_host
end

#api_host=(value) ⇒ Object

API Server hostname, this allow to switch between production and sandbox

Should be “api.sandbox.midtrans.com” or “api.midtrans.com

Default is “api.sandbox.midtrans.com



47
48
49
# File 'lib/veritrans/config.rb', line 47

def api_host=(value)
  @api_host = value
end

#append_notif_urlObject



93
94
95
# File 'lib/veritrans/config.rb', line 93

def append_notif_url
  @append_notif_url
end

#append_notif_url=(value) ⇒ Object



89
90
91
# File 'lib/veritrans/config.rb', line 89

def append_notif_url=(value)
  @append_notif_url = value
end

#client_keyObject



23
24
25
# File 'lib/veritrans/config.rb', line 23

def client_key
  @client_key
end

#client_key=(value) ⇒ Object

Merhcant’s Client key, used to make getToken request. (only for VT-Direct)

Can be found in merchant portal: Settings -> Access Keys



19
20
21
# File 'lib/veritrans/config.rb', line 19

def client_key=(value)
  @client_key = value
end

#http_optionsObject



105
106
107
# File 'lib/veritrans/config.rb', line 105

def http_options
  @http_options
end

#http_options=(options) ⇒ Object

This will override http request settings for api calls. http_options should be hash, it will be merged with connection options for every request.

Full list of options: github.com/excon/excon/blob/master/lib/excon/constants.rb

For unsupported key it will raise ArgumentError

Veritrans.config.http_options = {tcp_nodelay: true, ssl_version: 'TLSv1'}


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/veritrans/config.rb', line 65

def http_options=(options)
  unless options.is_a?(Hash)
    raise ArgumentError, "http_options should be a hash"
  end

  # Validate allowed keys
  diff = options.keys.map(&:to_sym) - Excon::VALID_CONNECTION_KEYS
  if diff.size > 0
    raise ArgumentError,
          "http_options contain unsupported keys: #{diff.inspect}\n" +
            "Supported keys are: #{Excon::VALID_CONNECTION_KEYS.inspect}"
  end

  @http_options = options
end

#idempotency_keyObject



85
86
87
# File 'lib/veritrans/config.rb', line 85

def idempotency_key
  @idempotency_key
end

#idempotency_key=(value) ⇒ Object



81
82
83
# File 'lib/veritrans/config.rb', line 81

def idempotency_key=(value)
  @idempotency_key = value
end

#inspectObject



156
157
158
159
160
161
162
# File 'lib/veritrans/config.rb', line 156

def inspect
  "<Veritrans::Config " +
    "@api_host=#{@api_host.inspect} " +
    "@server_key=#{@server_key.inspect} " +
    "@client_key=#{@client_key.inspect} " +
    "@http_options=#{@http_options.inspect}>"
end

#load_config(filename, yml_section = nil) ⇒ Object Also known as: load_yml

Loads YAML file and assign config values

Supports #section in filename to choose one section. If you are using Rails, it will try to use Rails.env as a section name

Available config keys: server_key, client_key, api_host, http_options

Veritrans.setup do
  config.load_yml "#{Rails.root.to_s}/config/veritrans.yml#development"
  # or
  config.load_yml "#{Rails.root.to_s}/config/veritrans.yml", :development
end


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/veritrans/config.rb', line 123

def load_config(filename, yml_section = nil)
  yml_file, file_yml_section = filename.to_s.split('#')
  config_data = YAML.load(ERB.new(File.read(yml_file)).result)

  yml_section ||= file_yml_section
  if defined?(Rails) && !yml_section
    yml_section = Rails.env.to_s
  end

  if yml_section && !config_data.has_key?(yml_section)
    STDERR.puts "Veritrans: Can not find section #{yml_section.inspect} in file #{yml_file}"
    STDERR.puts "           Available sections: #{config_data.keys}"

    if config_data['development'] && config_data['development']['server_key']
      new_section = 'development'
    end

    first_key = config_data.keys.first
    if config_data[first_key]['server_key']
      new_section = first_key
    end

    if new_section
      STDERR.puts "Veritrans: Using first section #{new_section.inspect}"
      yml_section = new_section
    end
  end

  apply(yml_section ? config_data[yml_section] : config_data)
end

#override_notif_urlObject



101
102
103
# File 'lib/veritrans/config.rb', line 101

def override_notif_url
  @override_notif_url
end

#override_notif_url=(value) ⇒ Object



97
98
99
# File 'lib/veritrans/config.rb', line 97

def override_notif_url=(value)
  @override_notif_url = value
end

#server_keyObject



36
37
38
# File 'lib/veritrans/config.rb', line 36

def server_key
  @server_key
end

#server_key=(value) ⇒ Object

Merhcant’s Server key, used to sign every http API call.

Can be found in merchant portal: Settings -> Access Keys



32
33
34
# File 'lib/veritrans/config.rb', line 32

def server_key=(value)
  @server_key = value
end