Class: Taler::Client
- Inherits:
-
Object
- Object
- Taler::Client
- Defined in:
- lib/taler/client.rb,
sig/taler.rbs
Overview
Access the GNU Taler merchant backend API.
Instance Method Summary collapse
- #auth_token ⇒ String
-
#create_order(amount:, summary:, fulfillment_url: nil, fulfillment_message: nil) ⇒ Object
@param
amount— e.g. -
#fetch_order(order_id) ⇒ ::Hash[untyped, untyped]
@param
order_id. -
#initialize(backend_url, password) ⇒ Client
constructor
@param
backend_url— e.g. -
#order_status_url(order_id) ⇒ String
@param
order_id. -
#refund_order(order_id, refund:, reason:) ⇒ ::Hash[untyped, untyped]
@param
order_id. -
#request(url, token: auth_token, payload: nil) ⇒ String
@param
url. -
#request_token ⇒ String
Obtain an access token to authenticate all other API calls.
Constructor Details
#initialize(backend_url, password) ⇒ Client
@param backend_url — e.g. "https://backend.demo.taler.net/instances/sandbox"
@param password
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_token ⇒ 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] = unless .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.
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
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.
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
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_token ⇒ String
Obtain an access token to authenticate all other API calls.
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 |