Class: Myrr::Pay::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/myrr/pay/client.rb

Overview

Client for a seller's Myrr Pay API.

Wraps the HTTP endpoints that sellers expose for agents to create customers, check balances, top-up, and manage subscriptions.

Examples:

client = Myrr::Pay::Client.new(base_url: "https://seller.example.com")
client.create_customer(agent_did: "did:myrr:abc123")
client.get_balance(agent_did: "did:myrr:abc123")

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, api_key: nil, open_timeout: 5, read_timeout: 10) ⇒ Client

Returns a new instance of Client.

Parameters:

  • base_url (String)

    The seller's Myrr protocol server URL

  • api_key (String, nil) (defaults to: nil)

    Optional API key for internal endpoints

  • open_timeout (Integer) (defaults to: 5)

    HTTP open timeout (seconds)

  • read_timeout (Integer) (defaults to: 10)

    HTTP read timeout (seconds)



21
22
23
24
25
26
27
28
29
# File 'lib/myrr/pay/client.rb', line 21

def initialize(base_url:, api_key: nil, open_timeout: 5, read_timeout: 10)
  require "net/http"
  require "uri"

  @base_url = base_url
  @api_key = api_key
  @open_timeout = open_timeout
  @read_timeout = read_timeout
end

Instance Method Details

#create_customer(agent_did:) ⇒ Hash

Create a Stripe Customer for the agent on this seller's account.

Parameters:

  • agent_did (String)

    The did:myrr: identifier

Returns:

  • (Hash)

    Response with stripe_customer_id, balance, currency



35
36
37
# File 'lib/myrr/pay/client.rb', line 35

def create_customer(agent_did:)
  post("/v1/pay/customers", { agent_did: agent_did })
end

#create_subscription(agent_did:, payment_method_id:, plan_name:) ⇒ Hash

Create a subscription for the agent.

Parameters:

  • agent_did (String)

    The did:myrr: identifier

  • payment_method_id (String)

    Stripe PaymentMethod ID

  • plan_name (String)

    Name of the plan (matches pricing config)

Returns:

  • (Hash)

    Response with subscription details



86
87
88
89
90
91
92
# File 'lib/myrr/pay/client.rb', line 86

def create_subscription(agent_did:, payment_method_id:, plan_name:)
  post("/v1/pay/subscriptions", {
    agent_did: agent_did,
    payment_method_id: payment_method_id,
    plan_name: plan_name
  })
end

#debit(agent_did:, amount_cents:, endpoint_path: "", token_count: 0, description: nil) ⇒ Hash

Debit the agent's balance (internal — requires API key).

Parameters:

  • agent_did (String)

    The did:myrr: identifier

  • amount_cents (Integer)

    Amount in cents

  • endpoint_path (String) (defaults to: "")

    The endpoint being accessed

  • token_count (Integer) (defaults to: 0)

    Number of tokens consumed

  • description (String, nil) (defaults to: nil)

    Optional description

Returns:

  • (Hash)

    Response with status, amount_cents, balance_cents



69
70
71
72
73
74
75
76
77
78
# File 'lib/myrr/pay/client.rb', line 69

def debit(agent_did:, amount_cents:, endpoint_path: "", token_count: 0, description: nil)
  body = {
    agent_did: agent_did,
    amount_cents: amount_cents,
    endpoint_path: endpoint_path,
    token_count: token_count
  }
  body[:description] = description if description
  post("/v1/pay/debit", body)
end

#get(path, params = {}) ⇒ Object

Public method for direct API calls (used by middleware)



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/myrr/pay/client.rb', line 103

def get(path, params = {})
  uri = URI.join(@base_url, path)
  uri.query = URI.encode_www_form(params) unless params.empty?

  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = @open_timeout
  http.read_timeout = @read_timeout
  http.use_ssl = (uri.scheme == "https")

  request = Net::HTTP::Get.new(uri)
  request["Content-Type"] = "application/json"
  request["Authorization"] = "Bearer #{@api_key}" if @api_key

  response = http.request(request)
  parse_response(response)
end

#get_balance(agent_did:) ⇒ Hash

Get the agent's current balance on this seller.

Parameters:

  • agent_did (String)

    The did:myrr: identifier

Returns:

  • (Hash)

    Response with balance_cents, currency



43
44
45
# File 'lib/myrr/pay/client.rb', line 43

def get_balance(agent_did:)
  get("/v1/pay/balance", { agent_did: agent_did })
end

#get_subscription(agent_did:) ⇒ Hash

Get the agent's active subscription.

Parameters:

  • agent_did (String)

    The did:myrr: identifier

Returns:

  • (Hash)

    Response with subscription details or nil



98
99
100
# File 'lib/myrr/pay/client.rb', line 98

def get_subscription(agent_did:)
  get("/v1/pay/subscriptions", { agent_did: agent_did })
end

#parse_response(response) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/myrr/pay/client.rb', line 137

def parse_response(response)
  data = JSON.parse(response.body)
  case response.code.to_i
  when 200..299
    data
  when 402
    raise Myrr::Pay::InsufficientFunds, data["error"]
  when 404
    raise Myrr::Pay::NotFound, data["error"]
  else
    raise Myrr::Pay::Error, data["error"] || "HTTP #{response.code}"
  end
rescue JSON::ParserError
  raise Myrr::Pay::Error, "Invalid JSON response (HTTP #{response.code})"
end

#post(path, body = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/myrr/pay/client.rb', line 120

def post(path, body = {})
  uri = URI.join(@base_url, path)

  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = @open_timeout
  http.read_timeout = @read_timeout
  http.use_ssl = (uri.scheme == "https")

  request = Net::HTTP::Post.new(uri)
  request["Content-Type"] = "application/json"
  request["Authorization"] = "Bearer #{@api_key}" if @api_key
  request.body = body.to_json

  response = http.request(request)
  parse_response(response)
end

#topup(agent_did:, amount_cents:, payment_method_id:) ⇒ Hash

Top up the agent's balance via off_session PaymentIntent.

Parameters:

  • agent_did (String)

    The did:myrr: identifier

  • amount_cents (Integer)

    Amount in cents

  • payment_method_id (String)

    Stripe PaymentMethod ID

Returns:

  • (Hash)

    Response with payment_intent_id, status, amount_cents



53
54
55
56
57
58
59
# File 'lib/myrr/pay/client.rb', line 53

def topup(agent_did:, amount_cents:, payment_method_id:)
  post("/v1/pay/topup", {
    agent_did: agent_did,
    amount_cents: amount_cents,
    payment_method_id: payment_method_id
  })
end