Class: Myrr::Wallet

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

Overview

Wallet client for the Myrr protocol server.

Examples:

wallet = Myrr::Wallet.create(agent_did: "did:myrr:abc123")
wallet = Myrr::Wallet.find("wallet-uuid")
wallet = Myrr::Wallet.find_by_agent("did:myrr:abc123")
wallet.fund(amount_cents: 50_00)
wallet.cards  # => Array of Myrr::Card
wallet.transactions(limit: 10)
wallet.topup(seller_did: "did:myrr:seller", seller_name: "DataPulse", amount_cents: 10_00, payment_intent_id: "pi_xxx")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Wallet

Returns a new instance of Wallet.

Parameters:

  • attrs (Hash)

    Wallet attributes from the API



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/myrr/wallet.rb', line 22

def initialize(attrs)
  @id = attrs["id"]
  @agent_did = attrs["agent_did"]
  @balance = attrs["balance"]
  @currency = attrs["currency"] || "usd"
  @status = attrs["status"]
  @stripe_customer_id = attrs["stripe_customer_id"]
  @metadata = attrs["metadata"]
  @created_at = attrs["created_at"]
  @updated_at = attrs["updated_at"]
end

Instance Attribute Details

#agent_didObject (readonly)

Returns the value of attribute agent_did.



18
19
20
# File 'lib/myrr/wallet.rb', line 18

def agent_did
  @agent_did
end

#balanceObject (readonly)

Returns the value of attribute balance.



18
19
20
# File 'lib/myrr/wallet.rb', line 18

def balance
  @balance
end

#created_atObject (readonly)

Returns the value of attribute created_at.



18
19
20
# File 'lib/myrr/wallet.rb', line 18

def created_at
  @created_at
end

#currencyObject (readonly)

Returns the value of attribute currency.



18
19
20
# File 'lib/myrr/wallet.rb', line 18

def currency
  @currency
end

#idObject (readonly)

Returns the value of attribute id.



18
19
20
# File 'lib/myrr/wallet.rb', line 18

def id
  @id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



18
19
20
# File 'lib/myrr/wallet.rb', line 18

def 
  @metadata
end

#statusObject (readonly)

Returns the value of attribute status.



18
19
20
# File 'lib/myrr/wallet.rb', line 18

def status
  @status
end

#stripe_customer_idObject (readonly)

Returns the value of attribute stripe_customer_id.



18
19
20
# File 'lib/myrr/wallet.rb', line 18

def stripe_customer_id
  @stripe_customer_id
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



18
19
20
# File 'lib/myrr/wallet.rb', line 18

def updated_at
  @updated_at
end

Class Method Details

.clientObject



127
128
129
# File 'lib/myrr/wallet.rb', line 127

def self.client
  Myrr.client
end

.create(agent_did:, metadata: {}, client: nil) ⇒ Myrr::Wallet

Create a new wallet for the given agent.

Parameters:

  • agent_did (String)

    The did:myrr: identifier

  • metadata (Hash) (defaults to: {})

    Optional metadata

  • client (Myrr::Client, nil) (defaults to: nil)

    Optional custom client

Returns:



40
41
42
43
44
45
46
# File 'lib/myrr/wallet.rb', line 40

def self.create(agent_did:, metadata: {}, client: nil)
  client ||= Myrr.client
  body = { agent_did: agent_did }
  body[:metadata] =  if .any?
  resp = client.send(:post, "/v1/wallets", body)
  new(resp)
end

.find(id, client: nil) ⇒ Myrr::Wallet?

Find a wallet by its UUID.

Parameters:

  • id (String)

    Wallet UUID

  • client (Myrr::Client, nil) (defaults to: nil)

    Optional custom client

Returns:



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

def self.find(id, client: nil)
  client ||= Myrr.client
  resp = client.send(:get, "/v1/wallets/#{id}")
  new(resp)
rescue Myrr::Error => e
  raise unless e.message.include?("not_found")
  nil
end

.find_by_agent(agent_did, client: nil) ⇒ Myrr::Wallet?

Find a wallet by its agent did:myrr: identifier.

Parameters:

  • agent_did (String)

    The did:myrr: identifier

  • client (Myrr::Client, nil) (defaults to: nil)

    Optional custom client

Returns:



67
68
69
70
71
72
73
74
# File 'lib/myrr/wallet.rb', line 67

def self.find_by_agent(agent_did, client: nil)
  client ||= Myrr.client
  resp = client.send(:get, "/v1/wallets?agent_did=#{agent_did}")
  new(resp)
rescue Myrr::Error => e
  raise unless e.message.include?("not_found")
  nil
end

Instance Method Details

#cardsArray<Myrr::Card>

List virtual cards for this wallet.

Returns:



107
108
109
110
# File 'lib/myrr/wallet.rb', line 107

def cards
  resp = self.class.client.send(:get, "/v1/wallets/#{id}/cards")
  (resp["cards"] || []).map { |c| Myrr::Card.new(c) }
end

#fund(amount_cents:) ⇒ String

Create a Stripe Checkout session to fund this wallet.

Parameters:

  • amount_cents (Integer)

    Amount in cents (minimum 50)

Returns:

  • (String)

    Checkout URL



99
100
101
102
# File 'lib/myrr/wallet.rb', line 99

def fund(amount_cents:)
  resp = self.class.client.send(:post, "/v1/wallets/#{id}/fund", { amount_cents: amount_cents })
  resp["checkout_url"]
end

#topup(seller_did:, seller_name:, amount_cents:, payment_intent_id: nil, metadata: {}) ⇒ Hash

Pre-approve a Myrr Pay top-up before confirming the PaymentIntent.

Parameters:

  • seller_did (String)

    Seller's did:myrr: identifier

  • seller_name (String)

    Seller's display name

  • amount_cents (Integer)

    Top-up amount in cents

  • payment_intent_id (String) (defaults to: nil)

    Stripe PaymentIntent ID

  • metadata (Hash) (defaults to: {})

    Optional metadata

Returns:

  • (Hash)

    { "status" => "approved"/"declined", "reason" => String? }



84
85
86
87
88
89
90
91
92
93
# File 'lib/myrr/wallet.rb', line 84

def topup(seller_did:, seller_name:, amount_cents:, payment_intent_id: nil, metadata: {})
  body = {
    seller_did: seller_did,
    seller_name: seller_name,
    amount_cents: amount_cents,
    payment_intent_id: payment_intent_id
  }
  body[:metadata] =  if .any?
  self.class.client.send(:post, "/v1/wallets/#{id}/topups", body)
end

#transactions(limit: 20, before: nil) ⇒ Array<Myrr::Transaction>

List transactions for this wallet.

Parameters:

  • limit (Integer) (defaults to: 20)

    Max results (default 20, max 100)

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

    Cursor for pagination

Returns:



117
118
119
120
121
122
123
# File 'lib/myrr/wallet.rb', line 117

def transactions(limit: 20, before: nil)
  query = "limit=#{limit}"
  query += "&before=#{before}" if before
  resp = self.class.client.send(:get, "/v1/wallets/#{id}/transactions?#{query}")
  txns = (resp["transactions"] || []).map { |t| Myrr::Transaction.new(t) }
  [txns, resp["has_more"], resp["next_cursor"]]
end