class Veritrans::Config
Constants
- AVAILABLE_KEYS
Public Class Methods
# File lib/veritrans/config.rb, line 9 def initialize(options = nil) @api_host = "https://api.sandbox.midtrans.com" apply(options) if options end
Public Instance Methods
# File lib/veritrans/config.rb, line 52 def api_host @api_host end
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”
# File lib/veritrans/config.rb, line 48 def api_host=(value) @api_host = value end
# File lib/veritrans/config.rb, line 24 def client_key @client_key end
Merhcant's Client
key, used to make getToken request. (only for VT-Direct)
Can be found in merchant portal: Settings -> Access Keys
# File lib/veritrans/config.rb, line 19 def client_key=(value) @client_key = value end
# File lib/veritrans/config.rb, line 82 def http_options @http_options end
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'}
# File lib/veritrans/config.rb, line 66 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
# File lib/veritrans/config.rb, line 133 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
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
# File lib/veritrans/config.rb, line 100 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
# File lib/veritrans/config.rb, line 37 def server_key @server_key end
Merhcant's Server key, used to sign every http API call.
Can be found in merchant portal: Settings -> Access Keys
# File lib/veritrans/config.rb, line 33 def server_key=(value) @server_key = value end
Private Instance Methods
# File lib/veritrans/config.rb, line 145 def apply(hash) hash.each do |key, value| unless AVAILABLE_KEYS.include?(key.to_s.to_sym) raise ArgumentError, "Unknown option #{key.inspect}, available keys: #{AVAILABLE_KEYS.map(&:inspect).join(", ")}" end send(:"#{key}=", value) end end