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}/#{ORIGIN}/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



61
62
63
64
65
66
67
68
69
# File 'contrib/lib/hawese.rb', line 61

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}/#{ORIGIN}/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
# File 'contrib/lib/hawese.rb', line 46

def purchase(gateway, fields)
  uri = URI("#{ENDPOINT}/#{ORIGIN}/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



57
58
59
# File 'contrib/lib/hawese.rb', line 57

def purchase_from_schema(gateway, schema)
  purchase(gateway, fields_to_schema_defaults(schema))
end