Class: Taler::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/taler/client.rb,
sig/taler.rbs

Overview

Access the GNU Taler merchant backend API.

See: https://docs.taler.net/core/api-merchant.html

Instance Method Summary collapse

Constructor Details

#initialize(backend_url, password) ⇒ Client

@param backend_url — e.g. "https://backend.demo.taler.net/instances/sandbox"

@param password

Parameters:

  • backend_url (String)
  • password (String)


13
14
15
16
# File 'lib/taler/client.rb', line 13

def initialize(backend_url, password)
  @backend_url = backend_url
  @password = password
end

Instance Method Details

#auth_tokenString

Returns:

  • (String)


75
76
77
# File 'lib/taler/client.rb', line 75

def auth_token
  "secret-token:#{@password}"
end

#create_order(amount:, summary:, fulfillment_url: nil, fulfillment_message: nil) ⇒ Object

@param amount — e.g. "KUDOS:200"

@param summary — A description of the order displayed to the user.

@param fulfillment_url — The user is redirected to this URL after payment. The order status page also links here.

@param fulfillment_message — Text to display to the user after payment.

@return — The resonse from the backend, usually just containing an order id, e.g. {order_id: "xxxxx"}



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/taler/client.rb', line 36

def create_order(amount:, summary:, fulfillment_url: nil, fulfillment_message: nil)
  url = "#{@backend_url}/private/orders"
  order = {
    amount: amount,
    summary: summary
  }
  order[:fulfillment_url] = fulfillment_url unless fulfillment_url.nil?
  order[:fulfillment_message] = fulfillment_message unless fulfillment_message.nil?
  payload = {order:, create_token: false}
  request(url, payload:)
end

#fetch_order(order_id) ⇒ ::Hash[untyped, untyped]

@param order_id

@return — The order status returned by the backend.

Parameters:

  • order_id (String)

Returns:

  • (::Hash[untyped, untyped])


56
57
58
59
# File 'lib/taler/client.rb', line 56

def fetch_order(order_id)
  url = "#{@backend_url}/private/orders/#{order_id}"
  request(url)
end

#order_status_url(order_id) ⇒ String

@param order_id

Parameters:

  • order_id (String)

Returns:

  • (String)


50
51
52
# File 'lib/taler/client.rb', line 50

def order_status_url(order_id)
  "#{@backend_url}/orders/#{order_id}"
end

#refund_order(order_id, refund:, reason:) ⇒ ::Hash[untyped, untyped]

@param order_id

@param refund — The amount with currency.

@param reason — Why are you refunding?

@return — Response from the merchant backend.

Parameters:

  • order_id (String)
  • refund: (String)
  • reason: (String)

Returns:

  • (::Hash[untyped, untyped])


66
67
68
69
70
# File 'lib/taler/client.rb', line 66

def refund_order(order_id, refund:, reason:)
  url = "#{@backend_url}/private/orders/#{order_id}/refund"
  payload = {refund:, reason:}
  request(url, payload:)
end

#request(url, token: auth_token, payload: nil) ⇒ String

@param url

@param token

@param payload

Parameters:

  • url (String)
  • token: (String) (defaults to: auth_token)
  • payload: (::Hash[untyped, untyped], nil) (defaults to: nil)

Returns:

  • (String)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/taler/client.rb', line 83

def request(url, token: auth_token, payload: nil)
  uri = URI(url)
  headers = {
    "Authorization" => "Bearer #{token}",
    "Accept" => "application/json",
    "User-Agent" => "Taler Ruby"
  }

  if payload.nil?
    body = Net::HTTP.get(uri, headers)
  else
    headers["Content-Type"] = "application/json"
    data = JSON.dump(payload)
    response = Net::HTTP.post(uri, data, headers)
    body = response.body
  end

  JSON.parse(body)
end

#request_tokenString

Obtain an access token to authenticate all other API calls.

Returns:

  • (String)


21
22
23
24
25
26
# File 'lib/taler/client.rb', line 21

def request_token
  url = "#{@backend_url}/private/token"
  payload = {scope: "write"}
  result = request(url, token: auth_token, payload:)
  result.fetch("token")
end