Phase 2: money
Lightning payments through LNbits, wallets, passes, credits, the entitlement resolver and priority lanes. - internal/entitle: pure resolver (one-off, own pass, group pass, group credits, own credits, free) with a table-driven suite and an invariant check over every input combination - internal/billing: wallets, append-only ledger with dedupe keys, stacking passes, purchases, gift codes, referral reward - LNbits client verified against v0.12.12 and v1.7.1 source - Settlement by one conditional UPDATE: idempotent under any mix of webhooks, polls and the check button. Webhooks are unsigned, so every payment is confirmed with LNbits before it is credited - Polling fallback, reconciliation of all unsettled invoices at startup, late payments still honored, no expiry while LNbits is unreachable - Charge, quota and job are written in one transaction; failed, cancelled and undeliverable downloads are refunded exactly once - Paid lane first with one slot of the fleet reserved for free users - Daily download quota and daily volume cap, per-user and per-group rate limits - Bot: /buy /account /limits /credits /redeem /referral /donate, QR invoice message that flips to paid, over-limit offer (passes or this file only), deep links from groups, skip the queue - Fake LNbits server for tests; end-to-end money flows including a payment made while the core was down Cashu redemption is behind an interface and not implemented. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
3b361c17a1
commit
0d50f453a0
@@ -7,13 +7,13 @@ import (
|
||||
|
||||
const deliveryCols = `id, job_id, request_id, user_id, chat_id, chat_type, reply_to_message_id,
|
||||
status_message_id, status_is_photo, lang, status, attempts, error_code, tg_file_id,
|
||||
tg_message_id, created_at, updated_at`
|
||||
tg_message_id, created_at, updated_at, source, wallet_id, charge_sats, quota_day, released`
|
||||
|
||||
func scanDelivery(sc interface{ Scan(...any) error }) (*Delivery, error) {
|
||||
var d Delivery
|
||||
err := sc.Scan(&d.ID, &d.JobID, &d.RequestID, &d.UserID, &d.ChatID, &d.ChatType, &d.ReplyToMessageID,
|
||||
&d.StatusMessageID, &d.StatusIsPhoto, &d.Lang, &d.Status, &d.Attempts, &d.ErrorCode, &d.TgFileID,
|
||||
&d.TgMessageID, &d.CreatedAt, &d.UpdatedAt)
|
||||
&d.TgMessageID, &d.CreatedAt, &d.UpdatedAt, &d.Source, &d.WalletID, &d.ChargeSats, &d.QuotaDay, &d.Released)
|
||||
if err != nil {
|
||||
return nil, notFound(err)
|
||||
}
|
||||
@@ -42,11 +42,14 @@ func (s *Store) InsertDelivery(ctx context.Context, d *Delivery) error {
|
||||
d.CreatedAt = NowMilli()
|
||||
}
|
||||
d.UpdatedAt = d.CreatedAt
|
||||
if d.Source == "" {
|
||||
d.Source = "free"
|
||||
}
|
||||
_, err := s.exec(ctx, `INSERT INTO deliveries (`+deliveryCols+`)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
d.ID, d.JobID, d.RequestID, d.UserID, d.ChatID, d.ChatType, d.ReplyToMessageID,
|
||||
d.StatusMessageID, d.StatusIsPhoto, d.Lang, d.Status, d.Attempts, d.ErrorCode, d.TgFileID,
|
||||
d.TgMessageID, d.CreatedAt, d.UpdatedAt)
|
||||
d.TgMessageID, d.CreatedAt, d.UpdatedAt, d.Source, d.WalletID, d.ChargeSats, d.QuotaDay, d.Released)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -135,3 +138,27 @@ func (s *Store) PendingDeliveryJobIDs(ctx context.Context, jobStatus string, lim
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// MarkDeliveryReleased flags a delivery as refunded and reports whether
|
||||
// this call was the one that flagged it. It is the idempotency guard for
|
||||
// giving back a charge and a quota slot.
|
||||
func (s *Store) MarkDeliveryReleased(ctx context.Context, id string) (bool, error) {
|
||||
n, err := s.exec(ctx, `UPDATE deliveries SET released = ?, updated_at = ? WHERE id = ? AND released = ?`,
|
||||
true, NowMilli(), id, false)
|
||||
return n == 1, err
|
||||
}
|
||||
|
||||
// UnreleasedDeliveries lists deliveries that ended without a file and have
|
||||
// not been refunded yet. It is the safety net behind the refund events.
|
||||
func (s *Store) UnreleasedDeliveries(ctx context.Context, limit int) ([]*Delivery, error) {
|
||||
return scanDeliveries(s.rows(ctx, `SELECT `+deliveryCols+` FROM deliveries
|
||||
WHERE status IN ('failed', 'cancelled') AND released = ?
|
||||
AND (charge_sats > 0 OR quota_day > 0) ORDER BY id LIMIT ?`, false, limit))
|
||||
}
|
||||
|
||||
// CountDeliveriesForRequest counts the deliveries created from a request.
|
||||
func (s *Store) CountDeliveriesForRequest(ctx context.Context, requestID string) (int, error) {
|
||||
var n int
|
||||
err := s.row(ctx, `SELECT COUNT(*) FROM deliveries WHERE request_id = ?`, requestID).Scan(&n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user