Class: Taler::Order
- Inherits:
-
Object
- Object
- Taler::Order
- Defined in:
- lib/taler/order.rb,
sig/taler.rbs
Overview
Order representation with convenient access to the merchant API.
Instance Method Summary collapse
-
#create(amount:, summary:, fulfillment_url: nil, fulfillment_message: nil) ⇒ Object
Create a new order record ready to take a payment.
-
#fetch(key) ⇒ String, bool
Access order status fields.
-
#initialize(backend_url:, password:, id: nil) ⇒ Order
constructor
Connect an Order to the Taler merchant backend.
- #inspect ⇒ String
-
#refund(refund:, reason:) ⇒ ::Hash[untyped, untyped]
Issue a refund request for this order.
-
#reload ⇒ ::Hash[untyped, untyped]
Query the latest order information from the backend.
-
#status_url ⇒ String
The page where the customer can pay or accept a refund, depending on the state of the order.
- #to_s ⇒ String
Constructor Details
#initialize(backend_url:, password:, id: nil) ⇒ Order
Connect an Order to the Taler merchant backend.
@param backend_url — e.g. "https://backend.demo.taler.net/instances/sandbox"
@param password — e.g. "sandbox"
@param id — The order id of an existing order.
Connect to the official demo backend
Taler::Order.new(
backend_url: "https://backend.demo.taler.net/instances/sandbox",
password: "sandbox"
)
16 17 18 19 |
# File 'lib/taler/order.rb', line 16 def initialize(backend_url:, password:, id: nil) @client = Client.new(backend_url, password) @id = id end |
Instance Method Details
#create(amount:, summary:, fulfillment_url: nil, fulfillment_message: nil) ⇒ Object
Create a new order record ready to take a payment.
@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 id of the new order.
30 31 32 33 34 35 |
# File 'lib/taler/order.rb', line 30 def create(amount:, summary:, fulfillment_url: nil, fulfillment_message: nil) response = @client.create_order( amount:, summary:, fulfillment_url:, fulfillment_message: ) @id = response.fetch("order_id") end |
#fetch(key) ⇒ String, bool
Access order status fields.
It queries the backend if it hasn't done that already. Call #reload beforehand to get the latest status from the backend.
@param key — A field in the order status response of the backend, e.g. order_status.
@return — The value of the field. Any simple type supported by JSON.
fetch("order_status") #=> "unpaid"
60 61 62 63 |
# File 'lib/taler/order.rb', line 60 def fetch(key) reload unless @status @status.fetch(key) end |
#inspect ⇒ String
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/taler/order.rb', line 86 def inspect status = @status.to_h.slice(*%w[ order_status deposit_total wired refunded refund_pending refund_amount ]) "#<#{self.class.name} #{status_url} #{status.inspect}>" end |
#refund(refund:, reason:) ⇒ ::Hash[untyped, untyped]
Issue a refund request for this order.
@param refund — The amount with currency.
@param reason — Why are you refunding?
@return — Response from the merchant backend.
81 82 83 |
# File 'lib/taler/order.rb', line 81 def refund(refund:, reason:) @client.refund_order(@id, refund:, reason:) end |
#reload ⇒ ::Hash[untyped, untyped]
Query the latest order information from the backend.
If you called #fetch in the past and are expecting updates from user interaction, you want to call this.
@return — The order status returned by the backend.
71 72 73 |
# File 'lib/taler/order.rb', line 71 def reload @status = @client.fetch_order(@id) end |
#status_url ⇒ String
The page where the customer can pay or accept a refund, depending on the state of the order.
The merchant backend opens the Taler plugin or app if it can. Otherwise it shows a QR code to scan in the app and provides installation instructions.
45 46 47 |
# File 'lib/taler/order.rb', line 45 def status_url @client.order_status_url(@id) end |
#to_s ⇒ String
99 100 101 102 |
# File 'lib/taler/order.rb', line 99 def to_s status = @status.to_h.slice("order_status", "refunded") "#<#{self.class.name} #{status_url} #{status.inspect}>" end |