Module: Barzahlen

Defined in:
lib/barzahlen/slip.rb,
lib/barzahlen/error.rb,
lib/barzahlen/version.rb,
lib/barzahlen/middleware.rb,
lib/barzahlen/configuration.rb

Defined Under Namespace

Modules: Error, Middleware Classes: Configuration, CreateSlipRequest

Constant Summary collapse

IDEMPOTENCY_ENABLED =
true
VERSION =
'2.4.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



18
19
20
# File 'lib/barzahlen/configuration.rb', line 18

def configuration
  @configuration
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



28
29
30
# File 'lib/barzahlen/configuration.rb', line 28

def configure
  yield(configuration)
end

.execute_with_error_handlingObject



104
105
106
107
108
109
110
111
112
# File 'lib/barzahlen/slip.rb', line 104

def self.execute_with_error_handling
  begin
    yield
  rescue Grac::Exception::RequestFailed => e
    raise Barzahlen::Error.generate_error_from_response("")
  rescue  Grac::Exception::ClientException => e
    raise Barzahlen::Error.generate_error_from_response(e.body)
  end
end

.get_grac_client(idempotency = false) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/barzahlen/slip.rb', line 90

def self.get_grac_client(idempotency = false)
  @@grac_client ||= Grac::Client.new(
      Barzahlen.configuration.sandbox ?
        Barzahlen::Configuration::API_HOST_SANDBOX : Barzahlen::Configuration::API_HOST,
      middleware: [ [ Barzahlen::Middleware::Signature, Barzahlen.configuration ] ]
      )

  if idempotency
    return @@grac_client.set( headers: { "Idempotency-Key" => SecureRandom.uuid} )
  else
    return @@grac_client
  end
end

.invalidate_slip(slip_id) ⇒ Object



52
53
54
55
56
# File 'lib/barzahlen/slip.rb', line 52

def self.invalidate_slip(slip_id)
  self.execute_with_error_handling do
    self.get_grac_client.path("/slips/{id}/invalidate", id: slip_id.to_s).post
  end
end

.resend_email(slip_id) ⇒ Object



40
41
42
43
44
# File 'lib/barzahlen/slip.rb', line 40

def self.resend_email(slip_id)
  self.execute_with_error_handling do
    self.get_grac_client.path("/slips/{id}/resend/email", id: slip_id.to_s).post
  end
end

.resend_text_message(slip_id) ⇒ Object



46
47
48
49
50
# File 'lib/barzahlen/slip.rb', line 46

def self.resend_text_message(slip_id)
  self.execute_with_error_handling do
    self.get_grac_client.path("/slips/{id}/resend/text_message", id: slip_id.to_s).post
  end
end

.resetObject



24
25
26
# File 'lib/barzahlen/configuration.rb', line 24

def reset
  @configuration = Configuration.new
end

.retrieve_slip(slip_id) ⇒ Object

If idempotency is not important a simple request is more than enough



28
29
30
31
32
# File 'lib/barzahlen/slip.rb', line 28

def self.retrieve_slip(slip_id)
  self.execute_with_error_handling do
    self.get_grac_client.path("/slips/{id}", id: slip_id.to_s).get
  end
end

.update_slip(slip_id, opts = {}) ⇒ Object



34
35
36
37
38
# File 'lib/barzahlen/slip.rb', line 34

def self.update_slip(slip_id, opts = {})
  self.execute_with_error_handling do
    self.get_grac_client.path("/slips/{id}", id: slip_id.to_s).patch(opts)
  end
end

.webhook_request(request) ⇒ Object

Handle a webhook request



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/barzahlen/slip.rb', line 60

def self.webhook_request(request)
  bz_hook_format = request["Bz-Hook-Format"]

  #stop processing when bz-hook-format = v1 because it will be or was send as v2
  if bz_hook_format.include? "v1"
    return nil
  end

  signature = Barzahlen::Middleware.generate_bz_signature(
    Barzahlen.configuration.payment_key,
    request["Host"] + ":" + (request["Port"] || "443"),
    request["Method"] ? request["Method"].upcase : "POST",
    request["Date"],
    request["Path"].split("?")[0] || request["Path"],
    request["Path"].split("?")[1] || "",
    request["Body"]
  )

  if request["Bz-Signature"].include? signature
    return JSON.parse(request["Body"])
  else
    raise Barzahlen::Error::SignatureError.new("Signature not valid")
  end
end