Class: Taler::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/taler/order.rb

Overview

Order representation with convenient access to the merchant API.

Instance Method Summary collapse

Constructor Details

#initialize(backend_url:, password:, id: nil) ⇒ Order

Connect an Order to the Taler merchant backend.

Examples:

Connect to the official demo backend

Taler::Order.new(
  backend_url: "https://backend.demo.taler.net/instances/sandbox",
  password: "sandbox"
)

Parameters:

  • backend_url (String)

    e.g. "https://backend.demo.taler.net/instances/sandbox"

  • password (String)

    e.g. "sandbox"

  • id (String) (defaults to: nil)

    The order id of an existing order.



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) ⇒ String

Create a new order record ready to take a payment.

Parameters:

  • amount (String)

    e.g. "KUDOS:200"

  • summary (String)

    A description of the order displayed to the user.

  • fulfillment_url (String) (defaults to: nil)

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

  • fulfillment_message (String) (defaults to: nil)

    Text to display to the user after payment.

Returns:

  • (String)

    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, ...

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.

Examples:

fetch("order_status") #=> "unpaid"

Parameters:

  • key (String)

    A field in the order status response of the backend, e.g. order_status.

Returns:

  • (String, true, false)

    The value of the field. Any simple type supported by JSON.



60
61
62
63
# File 'lib/taler/order.rb', line 60

def fetch(key)
  reload unless @status
  @status.fetch(key)
end

#inspectString

Returns:

  • (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

Issue a refund request for this order.

Parameters:

  • refund (String)

    The amount with currency.

  • reason (String)

    Why are you refunding?

Returns:

  • (Hash)

    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

#reloadHash

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.

Returns:

  • (Hash)

    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_urlString

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.

Returns:

  • (String)


45
46
47
# File 'lib/taler/order.rb', line 45

def status_url
  @client.order_status_url(@id)
end

#to_sString

Returns:

  • (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