Class: Myrr::Pay::Client
- Inherits:
-
Object
- Object
- Myrr::Pay::Client
- 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.
Instance Method Summary collapse
-
#create_customer(agent_did:) ⇒ Hash
Create a Stripe Customer for the agent on this seller's account.
-
#create_subscription(agent_did:, payment_method_id:, plan_name:) ⇒ Hash
Create a subscription for the agent.
-
#debit(agent_did:, amount_cents:, endpoint_path: "", token_count: 0, description: nil) ⇒ Hash
Debit the agent's balance (internal — requires API key).
-
#get(path, params = {}) ⇒ Object
Public method for direct API calls (used by middleware).
-
#get_balance(agent_did:) ⇒ Hash
Get the agent's current balance on this seller.
-
#get_subscription(agent_did:) ⇒ Hash
Get the agent's active subscription.
-
#initialize(base_url:, api_key: nil, open_timeout: 5, read_timeout: 10) ⇒ Client
constructor
A new instance of Client.
- #parse_response(response) ⇒ Object
- #post(path, body = {}) ⇒ Object
-
#topup(agent_did:, amount_cents:, payment_method_id:) ⇒ Hash
Top up the agent's balance via off_session PaymentIntent.
Constructor Details
#initialize(base_url:, api_key: nil, open_timeout: 5, read_timeout: 10) ⇒ Client
Returns a new instance of Client.
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.
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.
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).
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.
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.
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.
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 |