Files
strike_me-api/lib/strike_me/api/invoices.rb
T
bterkuileandClaude Sonnet 5 90ab7c74b1 Add invoice creation, quoting, and webhook signature verification
Finishes the two gaps that mattered most for real use: Invoices#create
(POST invoices) + Invoices#quote (POST invoices/{id}/quote, returning
the new Quote model with the actual payable bolt11 — an invoice alone
has nothing payable until quoted) and Webhook.valid_signature?
(HMAC-SHA256 over the raw body via X-Webhook-Signature, per Strike's own
documented reference implementation, timing-safe compare). Also finishes
Invoices#find (was a stub with a leftover binding.pry) and adds
WebhookEvent to parse the {id, eventType, data: {entityId, changes}}
payload shape — deliberately doesn't expose changed values, since
Strike's webhooks never carry them; a real value only ever comes from
re-fetching the resource.

Client gains #post/#patch (only #get existed before), needed for both
of the above.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0199wudpuAtkhouHKSEGxzpa
2026-09-15 09:34:45 -05:00

77 lines
2.1 KiB
Ruby

# frozen_string_literal: true
module StrikeMe
module API
# Factory helper object connecting the client to the models
class Invoices
include BaseClient
include Enumerable
autoload_default :Invoice
autoload_default :Quote
delegate :first, :last, :size, :any?, to: :list
def each(&block)
list.each(&block)
end
def list
return @list if @list.present?
url = 'invoices'
response = client.get url
raise ResponseError.new(url, response.body) unless response.success?
@list = response.body['items'].map { |item| Invoice.new item }
end
def paid
select(&:paid?)
end
def find(id)
url = "invoices/#{id}"
response = client.get url
raise ResponseError.new(url, response.body) unless response.success?
Invoice.new response.body
end
# POST invoices — amount is a decimal string per Strike's own API
# (not a float/BTC value), matching what #list/#find already hand
# back untouched in Invoice#amount.
def create(amount:, currency:, description: nil, correlation_id: nil)
url = 'invoices'
body = {
amount: { amount: amount, currency: currency },
description: description,
correlationId: correlation_id
}.compact
response = client.post(url, body)
raise ResponseError.new(url, response.body) unless response.success?
Invoice.new response.body
end
# POST invoices/{id}/quote — the second step of actually getting a
# payable BOLT 11 (Invoice#create alone only reserves the invoice
# shell; nothing is payable until a quote exists). Quotes expire
# (Quote#expiration) — callers request a fresh one per payment
# attempt rather than caching.
def quote(id, description_hash: nil)
url = "invoices/#{id}/quote"
body = { descriptionHash: description_hash }.compact
response = client.post(url, body)
raise ResponseError.new(url, response.body) unless response.success?
Quote.new response.body
end
end
end
end