Module: ZeroClick::Sellers::Responses

Defined in:
lib/zeroclick/sellers/responses.rb

Overview

The exact responses ZeroClick expects a seller to return.

Class Method Summary collapse

Class Method Details

.allowance_unavailableObject



32
33
34
# File 'lib/zeroclick/sellers/responses.rb', line 32

def allowance_unavailable
  Response.json({ "error" => "allowance_unavailable" }, status: 503)
end

.invalid_zeroclick_signatureObject



28
29
30
# File 'lib/zeroclick/sellers/responses.rb', line 28

def invalid_zeroclick_signature
  Response.json({ "error" => "invalid_zeroclick_signature" }, status: 401)
end

.payment_required(service_slug:, usage:, plan_slug: nil) ⇒ Object

The 402 body ZeroClick answers with a payment challenge.

An empty usage is meaningful, not a mistake: it is the free identity-scoped refusal, which the proxy answers with a $0 identity challenge and retries with zc-agent-id attached.



19
20
21
22
23
24
25
26
# File 'lib/zeroclick/sellers/responses.rb', line 19

def payment_required(service_slug:, usage:, plan_slug: nil)
  items = Sellers.normalize_usage(usage, operation: "payment_required", allow_empty: true)
  body = { "error" => "payment_required", "serviceSlug" => service_slug }
  body["planSlug"] = plan_slug unless plan_slug.nil?
  # Key order matters only for readability; the proxy parses by name.
  body["usage"] = items.map(&:to_wire)
  Response.json(body, status: 402)
end

.usage_header(usage) ⇒ Object

Build the zc-usage header value.

Returned as a string rather than applied to a response object, so each adapter can attach it to its own framework's response without the SDK ever having to reconstruct one.



41
42
43
44
45
46
47
48
49
50
# File 'lib/zeroclick/sellers/responses.rb', line 41

def usage_header(usage)
  items = Array(usage)
  items.each do |item|
    unless item.is_a?(SyncUsageItem)
      raise Error.new("malformed_input", operation: "usage_header",
                                         message: "usage entries must be SyncUsageItem instances")
    end
  end
  JSON.generate(items.map(&:to_wire))
end

.with_usage(response, usage) ⇒ Object

Attach reported usage to a successful response.



53
54
55
56
57
58
59
# File 'lib/zeroclick/sellers/responses.rb', line 53

def with_usage(response, usage)
  Response.new(
    status: response.status,
    body: response.body,
    headers: response.headers.merge("zc-usage" => usage_header(usage))
  )
end