Module: Billsafe

Defined in:
lib/billsafe.rb,
lib/billsafe/client.rb,
lib/billsafe/version.rb

Defined Under Namespace

Classes: APIError, ArgumentError, AuthenticationError, BillsafeError, Client

Constant Summary collapse

API_BASE =
"nvp.billsafe.de"
API_BASE_SANDBOX =
"sandbox-nvp.billsafe.de"
API_VERSION =
"V209"
PAYMENT_BASE =
"payment.billsafe.de"
PAYMENT_BASE_SANDBOX =
"sandbox-payment.billsafe.de"
PAYMENT_VERSION =
"V200"
VERSION =
"0.2.1"
@@sandbox =
true

Class Method Summary collapse

Class Method Details

.deflatten_params(hash) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/billsafe.rb', line 76

def deflatten_params(hash)
  new_hash = {}
  hash.each_pair do |key, val|
    if key.include? "_"
      parts = key.split("_")
      parts.reverse.each do |part|
        val = { part => val }
      end
    else 
      val = { key => val }
    end
    new_hash.deep_merge!(val)  
  end        
  new_hash
end

.flatten_params(hash, prefix = "") ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/billsafe.rb', line 64

def flatten_params(hash, prefix = "")
  new_hash = {}
  hash.each_pair do |key, val|
    if val.is_a?(Hash)
      new_hash.merge!(flatten_params(val, prefix + key.to_s + "_"))
    else
      new_hash[prefix + key.to_s] = val
    end
  end
  new_hash
end

.nvp_params(params) ⇒ Object



59
60
61
62
# File 'lib/billsafe.rb', line 59

def nvp_params(params)
  flattened_params = flatten_params(params)
  URI.encode_www_form(flattened_params)
end

.payment_urlObject



34
35
36
37
# File 'lib/billsafe.rb', line 34

def payment_url
  api_base = Billsafe.sandbox? ? PAYMENT_BASE_SANDBOX : PAYMENT_BASE
  "https://#{api_base}/#{PAYMENT_VERSION}"
end

.request(api_method, data) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/billsafe.rb', line 39

def request(api_method, data)
  api_base = Billsafe.sandbox? ? API_BASE_SANDBOX : API_BASE
  https = Net::HTTP.new(api_base , Net::HTTP.https_default_port)
  https.use_ssl = true
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  https.start do |connection|
    path = "/#{API_VERSION}"
    https_request = Net::HTTP::Post.new(path)
    https_request.body = nvp_params(data.merge(method: api_method, format: 'JSON'))
    @response = https.request(https_request)
  end
  raise AuthenticationError if @response.code.to_i == 401
  raise APIError if @response.code.to_i >= 500

  data = JSON.parse(@response.body)
  raise APIError.new(data['errorList'].
                         map{ |err| [err['code'], err['message']].join(" ")}.join("\r\n")) if data["ack"] == "ERROR"
  deflatten_params(data)
end

.sandbox=(sandbox) ⇒ Object



30
31
32
# File 'lib/billsafe.rb', line 30

def sandbox=(sandbox)
  @@sandbox = sandbox
end

.sandbox?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/billsafe.rb', line 26

def sandbox?
  @@sandbox
end