Class: Hawese

Inherits:
Object
  • Object
show all
Defined in:
contrib/lib/hawese.rb

Overview

Hawese client.

Environment variables

  • HAWESE_RETURN_URL defaults to LEANWEB_ENDPOINT/checkout.
  • HAWESE_ENDPOINT
  • HAWESE_ORIGIN

Constant Summary collapse

ENDPOINT =
ENV.fetch('HAWESE_ENDPOINT')
RETURN_URL =
ENV.fetch('HAWESE_RETURN_URL') do
  "#{ENV.fetch('LEANWEB_ENDPOINT')}/checkout"
end
ORIGIN =
ENV.fetch('HAWESE_ORIGIN')

Class Method Summary collapse

Class Method Details

.gateway_schema(gateway, query_params, schema = 'purchase') ⇒ Object



40
41
42
43
44
# File 'contrib/lib/hawese.rb', line 40

def gateway_schema(gateway, query_params, schema = 'purchase')
  uri = URI("#{ENDPOINT}/gateways/#{gateway}/schemas/#{schema}")
  uri.query = URI.encode_www_form(query_params)
  JSON.parse(Net::HTTP.get(uri), symbolize_names: true)
end

.payment(uuid) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'contrib/lib/hawese.rb', line 66

def payment(uuid)
  uri = URI("#{ENDPOINT}/payments/#{uuid}")
  Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    req = Net::HTTP::Get.new(uri)
    req['Authorization'] = "Bearer #{ENV.fetch('HAWESE_AUTH_TOKEN')}"
    response = http.request(req)
    JSON.parse(response.body, symbolize_names: true)
  end
end

.payment_methods(country = '*', currency = nil) ⇒ Object



31
32
33
34
35
36
37
38
# File 'contrib/lib/hawese.rb', line 31

def payment_methods(country = '*', currency = nil)
  endpoint = String.new(
    "#{ENDPOINT}/gateways/payment-methods/purchase?country=#{country}"
  )
  endpoint << "&currency=#{currency}" if currency
  uri = URI(endpoint)
  JSON.parse(Net::HTTP.get(uri), symbolize_names: true)
end

.purchase(gateway, fields) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'contrib/lib/hawese.rb', line 46

def purchase(gateway, fields)
  fields[:origin] = ORIGIN
  uri = URI("#{ENDPOINT}/gateways/#{gateway}/purchase")
  uri.query = URI.encode_www_form(return_url: RETURN_URL)
  response = Net::HTTP.post(
    uri,
    fields.to_json,
    'Content-Type' => 'application/json'
  )
  JSON.parse(response.body, symbolize_names: true)
end

.purchase_from_schema(gateway, schema) ⇒ Object



58
59
60
61
62
63
64
# File 'contrib/lib/hawese.rb', line 58

def purchase_from_schema(gateway, schema)
  fields = {}
  schema[:properties].each do |property, contents|
    fields[property] = contents[:default] if contents.include?(:default)
  end
  purchase(gateway, fields)
end