Rework billing
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
--ignore-dir=target
|
--ignore-dir=target
|
||||||
|
--ignore-dir=dist
|
||||||
--ignore-dir=ref
|
--ignore-dir=ref
|
||||||
|
|||||||
@@ -2,3 +2,4 @@ ref
|
|||||||
target
|
target
|
||||||
.agents
|
.agents
|
||||||
.playwright-cli
|
.playwright-cli
|
||||||
|
node_modules
|
||||||
|
|||||||
@@ -28,3 +28,5 @@ LIVEKIT_API_SECRET=
|
|||||||
|
|
||||||
# Billing
|
# Billing
|
||||||
NWC_URL= # Nostr Wallet Connect URL for generating Lightning invoices
|
NWC_URL= # Nostr Wallet Connect URL for generating Lightning invoices
|
||||||
|
STRIPE_SECRET_KEY= # Stripe API secret key (sk_...)
|
||||||
|
STRIPE_WEBHOOK_SECRET= # Secret for verifying Stripe webhook signatures (whsec_...)
|
||||||
|
|||||||
Generated
+44
-17
@@ -206,11 +206,14 @@ dependencies = [
|
|||||||
"chrono",
|
"chrono",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
"hex",
|
"hex",
|
||||||
|
"hmac",
|
||||||
"nostr-sdk",
|
"nostr-sdk",
|
||||||
|
"nwc",
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"sha2",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tower",
|
"tower",
|
||||||
@@ -1293,6 +1296,12 @@ version = "0.4.29"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
|
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lru"
|
||||||
|
version = "0.16.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a1dc47f592c06f33f8e3aea9591776ec7c9f9e4124778ff8a3c3b87159f7e593"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lru-slab"
|
name = "lru-slab"
|
||||||
version = "0.1.2"
|
version = "0.1.2"
|
||||||
@@ -1370,12 +1379,6 @@ dependencies = [
|
|||||||
"tempfile",
|
"tempfile",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "negentropy"
|
|
||||||
version = "0.3.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e664971378a3987224f7a0e10059782035e89899ae403718ee07de85bec42afe"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "negentropy"
|
name = "negentropy"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
@@ -1394,9 +1397,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nostr"
|
name = "nostr"
|
||||||
version = "0.39.0"
|
version = "0.44.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d90b55eff1f0747d9e423972179672e1aacac3d3ccee4c1281147eaa90d6491e"
|
checksum = "3aa5e3b6a278ed061835fe1ee293b71641e6bf8b401cfe4e1834bbf4ef0a34e1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aes",
|
"aes",
|
||||||
"base64 0.22.1",
|
"base64 0.22.1",
|
||||||
@@ -1407,6 +1410,7 @@ dependencies = [
|
|||||||
"chacha20",
|
"chacha20",
|
||||||
"chacha20poly1305",
|
"chacha20poly1305",
|
||||||
"getrandom 0.2.17",
|
"getrandom 0.2.17",
|
||||||
|
"hex",
|
||||||
"instant",
|
"instant",
|
||||||
"scrypt",
|
"scrypt",
|
||||||
"secp256k1",
|
"secp256k1",
|
||||||
@@ -1418,25 +1422,36 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nostr-database"
|
name = "nostr-database"
|
||||||
version = "0.39.0"
|
version = "0.44.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ce07b47c77b8e5a856727885fe0ae47b9aa53d8d853a2190dd479b5a0d6e4f52"
|
checksum = "7462c9d8ae5ef6a28d66a192d399ad2530f1f2130b13186296dbb11bdef5b3d1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"lru",
|
||||||
"nostr",
|
"nostr",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nostr-relay-pool"
|
name = "nostr-gossip"
|
||||||
version = "0.39.0"
|
version = "0.44.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "211ac5bbdda1a8eec0c21814a838da832038767a5d354fe2fcc1ca438cae56fd"
|
checksum = "ade30de16869618919c6b5efc8258f47b654a98b51541eb77f85e8ec5e3c83a6"
|
||||||
|
dependencies = [
|
||||||
|
"nostr",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nostr-relay-pool"
|
||||||
|
version = "0.44.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4b1073ccfbaea5549fb914a9d52c68dab2aecda61535e5143dd73e95445a804b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-utility",
|
"async-utility",
|
||||||
"async-wsocket",
|
"async-wsocket",
|
||||||
"atomic-destructor",
|
"atomic-destructor",
|
||||||
"negentropy 0.3.1",
|
"hex",
|
||||||
"negentropy 0.5.0",
|
"lru",
|
||||||
|
"negentropy",
|
||||||
"nostr",
|
"nostr",
|
||||||
"nostr-database",
|
"nostr-database",
|
||||||
"tokio",
|
"tokio",
|
||||||
@@ -1445,13 +1460,14 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nostr-sdk"
|
name = "nostr-sdk"
|
||||||
version = "0.39.0"
|
version = "0.44.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5baca581deb810a88bb51c54d1d7980f4506a64a3e9a19270829b406e47adf31"
|
checksum = "471732576710e779b64f04c55e3f8b5292f865fea228436daf19694f0bf70393"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-utility",
|
"async-utility",
|
||||||
"nostr",
|
"nostr",
|
||||||
"nostr-database",
|
"nostr-database",
|
||||||
|
"nostr-gossip",
|
||||||
"nostr-relay-pool",
|
"nostr-relay-pool",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
@@ -1512,6 +1528,17 @@ dependencies = [
|
|||||||
"libm",
|
"libm",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nwc"
|
||||||
|
version = "0.44.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f651e3c28dd9da0873151233e804a92b6240a1db1260f3c9d727950adb8e9036"
|
||||||
|
dependencies = [
|
||||||
|
"nostr",
|
||||||
|
"nostr-relay-pool",
|
||||||
|
"tracing",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell"
|
name = "once_cell"
|
||||||
version = "1.21.3"
|
version = "1.21.3"
|
||||||
|
|||||||
+4
-1
@@ -12,13 +12,16 @@ sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "sqlite", "macros"
|
|||||||
tokio = { version = "1.36", features = ["full"] }
|
tokio = { version = "1.36", features = ["full"] }
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||||
nostr-sdk = { version = "0.39", features = ["nip04", "nip47", "nip59", "nip98"] }
|
nostr-sdk = { version = "0.44", features = ["nip59", "nip98"] }
|
||||||
|
nwc = "0.44"
|
||||||
uuid = { version = "1.7", features = ["v4"] }
|
uuid = { version = "1.7", features = ["v4"] }
|
||||||
chrono = { version = "0.4", features = ["serde"] }
|
chrono = { version = "0.4", features = ["serde"] }
|
||||||
tower-http = { version = "0.5", features = ["cors"] }
|
tower-http = { version = "0.5", features = ["cors"] }
|
||||||
reqwest = { version = "0.12", features = ["json", "rustls-tls"] }
|
reqwest = { version = "0.12", features = ["json", "rustls-tls"] }
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
hex = "0.4"
|
hex = "0.4"
|
||||||
|
hmac = "0.12"
|
||||||
|
sha2 = "0.10"
|
||||||
dotenvy = "0.15.7"
|
dotenvy = "0.15.7"
|
||||||
base64 = "0.22"
|
base64 = "0.22"
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,11 @@ CREATE TABLE IF NOT EXISTS activity (
|
|||||||
CREATE TABLE IF NOT EXISTS tenant (
|
CREATE TABLE IF NOT EXISTS tenant (
|
||||||
pubkey TEXT PRIMARY KEY,
|
pubkey TEXT PRIMARY KEY,
|
||||||
nwc_url TEXT NOT NULL DEFAULT '',
|
nwc_url TEXT NOT NULL DEFAULT '',
|
||||||
|
nwc_error TEXT,
|
||||||
created_at INTEGER NOT NULL,
|
created_at INTEGER NOT NULL,
|
||||||
stripe_customer_id TEXT NOT NULL DEFAULT '',
|
stripe_customer_id TEXT NOT NULL DEFAULT '',
|
||||||
stripe_subscription_id TEXT NOT NULL DEFAULT ''
|
stripe_subscription_id TEXT,
|
||||||
|
past_due_at INTEGER
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS relay (
|
CREATE TABLE IF NOT EXISTS relay (
|
||||||
|
|||||||
+50
-4
@@ -47,8 +47,8 @@ Notes:
|
|||||||
- Serves `GET /identity`
|
- Serves `GET /identity`
|
||||||
- Authorizes anyone, but must be authorized
|
- Authorizes anyone, but must be authorized
|
||||||
- If a tenant for the identity doesn't exist:
|
- If a tenant for the identity doesn't exist:
|
||||||
- Call the Stripe API to create a new customer and subscription
|
- Call the Stripe API to create a new customer
|
||||||
- Create a new tenant using `command.create_tenant` with payload and stripe info
|
- Create a new tenant using `command.create_tenant` with payload and `stripe_customer_id`. No subscription is created yet — that happens when the first relay is added.
|
||||||
- Return `data` is an `Identity` struct
|
- Return `data` is an `Identity` struct
|
||||||
|
|
||||||
--- Tenant routes
|
--- Tenant routes
|
||||||
@@ -122,7 +122,7 @@ Notes:
|
|||||||
- Serves `POST /relays/:id/deactivate`
|
- Serves `POST /relays/:id/deactivate`
|
||||||
- Authorizes admin or relay owner
|
- Authorizes admin or relay owner
|
||||||
- If relay is already inactive, return a `400` with `code=relay-is-inactive`
|
- If relay is already inactive, return a `400` with `code=relay-is-inactive`
|
||||||
- Call `billing.deactivate_relay`
|
- Call `command.deactivate_relay`
|
||||||
- Return `data` is empty
|
- Return `data` is empty
|
||||||
|
|
||||||
## `async fn reactivate_relay(...) -> Response`
|
## `async fn reactivate_relay(...) -> Response`
|
||||||
@@ -130,9 +130,55 @@ Notes:
|
|||||||
- Serves `POST /relays/:id/reactivate`
|
- Serves `POST /relays/:id/reactivate`
|
||||||
- Authorizes admin or relay owner
|
- Authorizes admin or relay owner
|
||||||
- If relay is already active, return a `400` with `code=relay-is-active`
|
- If relay is already active, return a `400` with `code=relay-is-active`
|
||||||
- Call `billing.reactivate_relay`
|
- Call `command.activate_relay`
|
||||||
- Return `data` is empty
|
- Return `data` is empty
|
||||||
|
|
||||||
|
--- Invoice routes
|
||||||
|
|
||||||
|
## `async fn list_tenant_invoices(...) -> Response`
|
||||||
|
|
||||||
|
- Serves `GET /tenants/:pubkey/invoices`
|
||||||
|
- Authorizes admin or matching tenant
|
||||||
|
- Looks up tenant by pubkey, fetches invoices from Stripe API using `stripe_customer_id`
|
||||||
|
- Return `data` is a list of Stripe invoice objects: `{ id, status, amount_due, currency, hosted_invoice_url, period_start, period_end }`
|
||||||
|
|
||||||
|
## `async fn get_invoice(...) -> Response`
|
||||||
|
|
||||||
|
- Serves `GET /invoices/:id`
|
||||||
|
- Fetches invoice from Stripe API by ID
|
||||||
|
- Looks up tenant by the invoice's `customer` field, authorizes admin or matching tenant
|
||||||
|
- Return `data` is a single Stripe invoice object
|
||||||
|
- If invoice does not exist, return `404` with `code=not-found`
|
||||||
|
|
||||||
|
## `async fn get_invoice_bolt11(...) -> Response`
|
||||||
|
|
||||||
|
- Serves `GET /invoices/:id/bolt11`
|
||||||
|
- Fetches invoice from Stripe API by ID
|
||||||
|
- Looks up tenant by the invoice's `customer` field, authorizes admin or matching tenant
|
||||||
|
- If invoice `status` is not `open`, return `400` with `code=invoice-not-open`
|
||||||
|
- Creates a bolt11 Lightning invoice for the invoice's `amount_due` using `billing.create_bolt11(amount_due)`
|
||||||
|
- Return `data` is `{ bolt11 }`
|
||||||
|
|
||||||
|
--- Stripe session route
|
||||||
|
|
||||||
|
## `async fn create_stripe_session(...) -> Response`
|
||||||
|
|
||||||
|
- Serves `GET /tenants/:pubkey/stripe/session`
|
||||||
|
- Authorizes admin or matching tenant
|
||||||
|
- Looks up tenant by pubkey
|
||||||
|
- Creates a Stripe Customer Portal session for the tenant's `stripe_customer_id`
|
||||||
|
- Return `data` is `{ url }` — the portal session URL
|
||||||
|
|
||||||
|
--- Stripe webhook route
|
||||||
|
|
||||||
|
## `async fn stripe_webhook(...) -> Response`
|
||||||
|
|
||||||
|
- Serves `POST /stripe/webhook`
|
||||||
|
- No NIP-98 authentication — uses Stripe signature verification instead
|
||||||
|
- Reads raw request body and `Stripe-Signature` header
|
||||||
|
- Calls `billing.handle_webhook(payload, signature)`
|
||||||
|
- Returns `200` on success, `400` on signature verification failure
|
||||||
|
|
||||||
--- Utilities
|
--- Utilities
|
||||||
|
|
||||||
## `extract_auth_pubkey(&self, headers: &HeaderMap) -> Result<String>`
|
## `extract_auth_pubkey(&self, headers: &HeaderMap) -> Result<String>`
|
||||||
|
|||||||
+95
-9
@@ -1,10 +1,11 @@
|
|||||||
# `pub struct Billing`
|
# `pub struct Billing`
|
||||||
|
|
||||||
Billing encapsulates logic related to synchronizing state with Stripe.
|
Billing encapsulates logic related to synchronizing state with Stripe, processing payments via NWC, and managing subscription lifecycle.
|
||||||
|
|
||||||
Members:
|
Members:
|
||||||
|
|
||||||
- `nwc_url: String` - a nostr wallet connect URL used to **create** bolt11 invoices
|
- `nwc_url: String` - a nostr wallet connect URL used to **create** bolt11 invoices (i.e. receive payments), from `NWC_URL`
|
||||||
|
- `stripe_webhook_secret: String` - secret for verifying Stripe webhook signatures, from `STRIPE_WEBHOOK_SECRET`
|
||||||
- `query: Query`
|
- `query: Query`
|
||||||
- `command: Command`
|
- `command: Command`
|
||||||
- `robot: Robot`
|
- `robot: Robot`
|
||||||
@@ -15,12 +16,97 @@ Members:
|
|||||||
|
|
||||||
## `pub fn start(&self)`
|
## `pub fn start(&self)`
|
||||||
|
|
||||||
- Subscribes to `command.notify.notified`
|
- Subscribes to `command.notify.subscribe()`
|
||||||
- On `create_relay`, `update_relay`, `activate_relay`, `deactivate_relay`, `fail_relay_sync`, and `complete_relay_sync`, call `self.sync_relay_subscription_item`.
|
- On `create_relay`, `update_relay`, `activate_relay`, `deactivate_relay`, `fail_relay_sync`, and `complete_relay_sync`, call `self.sync_relay_subscription`.
|
||||||
|
|
||||||
## `pub fn sync_relay_subscription_item(&self, activity: &Activity)`
|
## `pub fn sync_relay_subscription(&self, activity: &Activity)`
|
||||||
|
|
||||||
|
Manages the Stripe subscription and subscription items for a relay's tenant. Only paid (non-free) relays interact with Stripe. Free-only tenants have no subscription. Must be idempotent.
|
||||||
|
|
||||||
|
- Fetch the relay and tenant associated with the `activity`
|
||||||
|
- **If relay plan is `free`**: if the relay has a `stripe_subscription_item_id`, delete it via the Stripe API and call `command.delete_relay_subscription_item`. Then check cleanup (below). Return early.
|
||||||
|
- **If relay is `inactive`**: if the relay has a `stripe_subscription_item_id`, delete it via the Stripe API and call `command.delete_relay_subscription_item`. Then check cleanup (below). Return early.
|
||||||
|
- **If relay is `active` and on a paid plan**:
|
||||||
|
- **Ensure subscription exists**: If the tenant has no `stripe_subscription_id`, create a Stripe subscription for the customer with `collection_method: "charge_automatically"` and the relay's price as the first item. Save the subscription ID via `command.set_tenant_subscription` and the item ID via `command.set_relay_subscription_item`. Return early.
|
||||||
|
- **Sync the subscription item**: If the tenant already has a subscription, create or update the relay's Stripe subscription item to the plan's `stripe_price_id` via the Stripe API, then call `command.set_relay_subscription_item`.
|
||||||
|
- **Clean up empty subscription**: After any item deletion, check if the tenant has any remaining active paid relays. If none and the tenant has a `stripe_subscription_id`, cancel the Stripe subscription immediately and call `command.clear_tenant_subscription`.
|
||||||
|
|
||||||
|
## `pub fn handle_webhook(&self, payload: &str, signature: &str) -> Result<()>`
|
||||||
|
|
||||||
|
- Verify the webhook signature using `self.stripe_webhook_secret`
|
||||||
|
- Parse the event and dispatch by type:
|
||||||
|
- `invoice.created` -> `self.handle_invoice_created`
|
||||||
|
- `invoice.paid` -> `self.handle_invoice_paid`
|
||||||
|
- `invoice.payment_failed` -> `self.handle_invoice_payment_failed`
|
||||||
|
- `invoice.overdue` -> `self.handle_invoice_overdue`
|
||||||
|
- `customer.subscription.updated` -> `self.handle_subscription_updated`
|
||||||
|
- `customer.subscription.deleted` -> `self.handle_subscription_deleted`
|
||||||
|
- Unknown event types are ignored (return Ok)
|
||||||
|
|
||||||
|
## `pub async fn stripe_list_invoices(&self, customer_id: &str) -> Result<Value>`
|
||||||
|
|
||||||
|
- Fetches invoices from Stripe API for the given customer
|
||||||
|
- Returns the `data` array from the Stripe response
|
||||||
|
|
||||||
|
## `pub async fn stripe_get_invoice(&self, invoice_id: &str) -> Result<Value>`
|
||||||
|
|
||||||
|
- Fetches a single invoice from Stripe API by ID
|
||||||
|
- Returns the full Stripe invoice object
|
||||||
|
|
||||||
|
## `pub async fn create_bolt11(&self, amount_due_cents: i64) -> Result<String>`
|
||||||
|
|
||||||
|
- Creates a bolt11 Lightning invoice for the given amount using the system NWC wallet (`self.nwc_url`)
|
||||||
|
- Returns the bolt11 invoice string
|
||||||
|
|
||||||
|
## `pub async fn stripe_create_portal_session(&self, customer_id: &str) -> Result<String>`
|
||||||
|
|
||||||
|
- Creates a Stripe Customer Portal session for the given customer
|
||||||
|
- Returns the portal session URL
|
||||||
|
|
||||||
|
## `fn handle_invoice_created(&self, invoice: &Invoice)`
|
||||||
|
|
||||||
|
Attempts to pay a new subscription invoice. Payment priority:
|
||||||
|
|
||||||
|
1. **NWC auto-pay**: If the tenant has a `nwc_url`:
|
||||||
|
- Create a bolt11 Lightning invoice for the invoice amount using `self.nwc_url` (the receiving/system wallet)
|
||||||
|
- Pay the bolt11 invoice using the tenant's `nwc_url` (the spending/tenant wallet)
|
||||||
|
- If payment succeeds: call Stripe `POST /v1/invoices/:id/pay` with `paid_out_of_band: true`. Clear `nwc_error` via `command.clear_tenant_nwc_error`.
|
||||||
|
- If payment fails: set `nwc_error` on tenant via `command.set_tenant_nwc_error`. Fall through to next option.
|
||||||
|
2. **Card on file**: If the tenant has a payment method on the Stripe customer, do nothing — Stripe will charge automatically.
|
||||||
|
3. **Manual payment**: If neither NWC nor card is available, send a DM via `robot.send_dm` notifying the tenant that payment is due with a link to the application for manual Lightning payment.
|
||||||
|
|
||||||
|
Skip invoices with `amount_due` of 0.
|
||||||
|
|
||||||
|
## `fn handle_invoice_paid(&self, invoice: &Invoice)`
|
||||||
|
|
||||||
|
- Look up tenant by `stripe_customer_id`
|
||||||
|
- If tenant has `past_due_at` set:
|
||||||
|
- Clear `past_due_at` via `command.clear_tenant_past_due`
|
||||||
|
- Find all `inactive` relays for the tenant that were deactivated due to non-payment (i.e. relays on paid plans that are inactive)
|
||||||
|
- Reactivate each one via `command.activate_relay`
|
||||||
|
|
||||||
|
## `fn handle_invoice_payment_failed(&self, invoice: &Invoice)`
|
||||||
|
|
||||||
|
- Look up tenant by `stripe_customer_id`
|
||||||
|
- If tenant does not already have `past_due_at` set:
|
||||||
|
- Set `past_due_at` to now via `command.set_tenant_past_due`
|
||||||
|
- Send a DM via `robot.send_dm` notifying the tenant that their payment has failed and their relays may be deactivated if not resolved.
|
||||||
|
|
||||||
|
## `fn handle_invoice_overdue(&self, invoice: &Invoice)`
|
||||||
|
|
||||||
|
- Look up tenant by `stripe_customer_id`
|
||||||
|
- Deactivate all active relays on paid plans via `command.deactivate_relay`
|
||||||
|
- Send a DM via `robot.send_dm` notifying the tenant that their paid relays have been deactivated due to non-payment
|
||||||
|
|
||||||
|
## `fn handle_subscription_updated(&self, subscription: &Subscription)`
|
||||||
|
|
||||||
|
- Look up tenant by `stripe_customer_id`
|
||||||
|
- If subscription status is `canceled` or `unpaid`:
|
||||||
|
- Clear `stripe_subscription_id` via `command.clear_tenant_subscription`
|
||||||
|
- Deactivate all active paid relays for the tenant via `command.deactivate_relay`
|
||||||
|
|
||||||
|
## `fn handle_subscription_deleted(&self, subscription: &Subscription)`
|
||||||
|
|
||||||
|
- Look up tenant by `stripe_customer_id`
|
||||||
|
- Clear `stripe_subscription_id` via `command.clear_tenant_subscription`
|
||||||
|
|
||||||
- Fetch the relay associated with the `activity`
|
|
||||||
- If the relay has `sync_error`, `synced` is false, `plan` is `free`, or `status` is `inactive`, delete the relay's subscription item using the Stripe api, and clear it with `command.delete_relay_subscription_item`.
|
|
||||||
- Otherwise, create/update the relay's subscription item to the appropriate Stripe price using the Stripe api and set it with `command.set_relay_subscription_item`.
|
|
||||||
- This method should be idempotent
|
|
||||||
|
|||||||
@@ -69,3 +69,33 @@ Notes:
|
|||||||
|
|
||||||
- Sets `stripe_subscription_item_id`
|
- Sets `stripe_subscription_item_id`
|
||||||
- Does not log activity
|
- Does not log activity
|
||||||
|
|
||||||
|
## `pub fn set_tenant_subscription(&self, pubkey: &str, stripe_subscription_id: &str) -> Result<()>`
|
||||||
|
|
||||||
|
- Sets `stripe_subscription_id` on the tenant
|
||||||
|
- Does not log activity
|
||||||
|
|
||||||
|
## `pub fn clear_tenant_subscription(&self, pubkey: &str) -> Result<()>`
|
||||||
|
|
||||||
|
- Sets `stripe_subscription_id = null` on the tenant
|
||||||
|
- Does not log activity
|
||||||
|
|
||||||
|
## `pub fn set_tenant_nwc_error(&self, pubkey: &str, error: &str) -> Result<()>`
|
||||||
|
|
||||||
|
- Sets `nwc_error` on the tenant
|
||||||
|
- Does not log activity
|
||||||
|
|
||||||
|
## `pub fn clear_tenant_nwc_error(&self, pubkey: &str) -> Result<()>`
|
||||||
|
|
||||||
|
- Sets `nwc_error = null` on the tenant
|
||||||
|
- Does not log activity
|
||||||
|
|
||||||
|
## `pub fn set_tenant_past_due(&self, pubkey: &str) -> Result<()>`
|
||||||
|
|
||||||
|
- Sets `past_due_at` to the current timestamp
|
||||||
|
- Does not log activity
|
||||||
|
|
||||||
|
## `pub fn clear_tenant_past_due(&self, pubkey: &str) -> Result<()>`
|
||||||
|
|
||||||
|
- Sets `past_due_at = null` on the tenant
|
||||||
|
- Does not log activity
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ A plan represents a rate charged for relays at a given feature/usage limit. Plan
|
|||||||
- `members` - the max number of members a relay can have before needing to upgrade. If empty, membership is not limited.
|
- `members` - the max number of members a relay can have before needing to upgrade. If empty, membership is not limited.
|
||||||
- `blossom` - whether blossom media hosting is available on this plan
|
- `blossom` - whether blossom media hosting is available on this plan
|
||||||
- `livekit` - whether livekit audio/video calls are available on this plan
|
- `livekit` - whether livekit audio/video calls are available on this plan
|
||||||
- `stripe_price_id` - the identifier of the price in Stripe
|
- `stripe_price_id` (nullable) - the identifier of the price in Stripe. Null for the free plan.
|
||||||
|
|
||||||
There are three plans available:
|
There are three plans available:
|
||||||
|
|
||||||
@@ -52,10 +52,12 @@ There are three plans available:
|
|||||||
Tenants are customers of the service, identified by a nostr `pubkey`. Public metadata like name etc are pulled from the nostr network. They also have associated billing information.
|
Tenants are customers of the service, identified by a nostr `pubkey`. Public metadata like name etc are pulled from the nostr network. They also have associated billing information.
|
||||||
|
|
||||||
- `pubkey` is the nostr public key identifying the tenant
|
- `pubkey` is the nostr public key identifying the tenant
|
||||||
- `nwc_url` (private) a nostr wallet connect URL used for **paying** invoices generated by the system
|
- `nwc_url` (private) a nostr wallet connect URL used for **paying** invoices generated by the system on the tenant's behalf
|
||||||
|
- `nwc_error` (private) a string indicating the most recent NWC payment error, if any. Cleared on successful NWC payment.
|
||||||
- `created_at` unix timestamp identifying tenant creation time
|
- `created_at` unix timestamp identifying tenant creation time
|
||||||
- `stripe_customer_id` a string identifying the associated stripe customer
|
- `stripe_customer_id` a string identifying the associated stripe customer
|
||||||
- `stripe_subscription_id` a string identifying the associated stripe subscription
|
- `stripe_subscription_id` (nullable) a string identifying the associated stripe subscription. Created when the first paid (non-free) relay is activated, deleted when the last paid relay is removed or deactivated. Free-only tenants have no subscription.
|
||||||
|
- `past_due_at` (nullable) unix timestamp when the tenant's subscription became past due. Set on payment failure, cleared on payment success.
|
||||||
|
|
||||||
# Relay
|
# Relay
|
||||||
|
|
||||||
@@ -66,7 +68,7 @@ A relay is a nostr relay owned by a `tenant` and hosted by the attached zooid in
|
|||||||
- `schema` - the relay's db schema (read_only, calculated based on `subdomain` + `id`)
|
- `schema` - the relay's db schema (read_only, calculated based on `subdomain` + `id`)
|
||||||
- `subdomain` - the relay's subdomain
|
- `subdomain` - the relay's subdomain
|
||||||
- `plan` - the relay's plan
|
- `plan` - the relay's plan
|
||||||
- `stripe_subscription_item_id` - the Stripe subscription item id.
|
- `stripe_subscription_item_id` (nullable) - the Stripe subscription item id. Only set for relays on paid plans.
|
||||||
- `status` - `active|inactive`. Only `active` relays count toward billing.
|
- `status` - `active|inactive`. Only `active` relays count toward billing.
|
||||||
- `synced` - whether the relay has been successfully synced to zooid at least once.
|
- `synced` - whether the relay has been successfully synced to zooid at least once.
|
||||||
- `sync_error` - a string indicating any errors encountered when synchronizing.
|
- `sync_error` - a string indicating any errors encountered when synchronizing.
|
||||||
|
|||||||
@@ -35,6 +35,14 @@ Members:
|
|||||||
|
|
||||||
- Returns matching relay
|
- Returns matching relay
|
||||||
|
|
||||||
|
## `pub fn get_tenant_by_stripe_customer_id(&self, stripe_customer_id: &str) -> Result<Tenant>`
|
||||||
|
|
||||||
|
- Returns the tenant matching the given `stripe_customer_id`
|
||||||
|
|
||||||
|
## `pub fn has_active_paid_relays(&self, tenant_id: &str) -> Result<bool>`
|
||||||
|
|
||||||
|
- Returns true if the tenant has any relays where `status = 'active'` and `plan != 'free'`
|
||||||
|
|
||||||
## `pub fn list_activity_for_relay(&self, relay_id: &str) -> Result<Vec<Activity>>`
|
## `pub fn list_activity_for_relay(&self, relay_id: &str) -> Result<Vec<Activity>>`
|
||||||
|
|
||||||
- Returns all activity where `resource_type = 'relay'` and `resource_id = relay_id`
|
- Returns all activity where `resource_type = 'relay'` and `resource_id = relay_id`
|
||||||
|
|||||||
+140
-27
@@ -16,6 +16,7 @@ use crate::billing::Billing;
|
|||||||
use crate::command::Command;
|
use crate::command::Command;
|
||||||
use crate::models::{Relay, Tenant};
|
use crate::models::{Relay, Tenant};
|
||||||
use crate::query::Query;
|
use crate::query::Query;
|
||||||
|
use axum::body::Bytes;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Api {
|
pub struct Api {
|
||||||
@@ -26,6 +27,27 @@ pub struct Api {
|
|||||||
billing: Billing,
|
billing: Billing,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn stripe_webhook(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
headers: HeaderMap,
|
||||||
|
body: Bytes,
|
||||||
|
) -> Response {
|
||||||
|
let signature = headers
|
||||||
|
.get("Stripe-Signature")
|
||||||
|
.and_then(|v| v.to_str().ok())
|
||||||
|
.unwrap_or("");
|
||||||
|
|
||||||
|
let payload = match std::str::from_utf8(&body) {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(_) => return err(StatusCode::BAD_REQUEST, "bad-request", "invalid payload"),
|
||||||
|
};
|
||||||
|
|
||||||
|
match state.api.billing.handle_webhook(payload, signature).await {
|
||||||
|
Ok(()) => ok(StatusCode::OK, ()),
|
||||||
|
Err(e) => err(StatusCode::BAD_REQUEST, "webhook-error", &e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
api: Arc<Api>,
|
api: Arc<Api>,
|
||||||
@@ -47,6 +69,8 @@ struct ErrorResponse {
|
|||||||
enum ApiError {
|
enum ApiError {
|
||||||
Unauthorized(anyhow::Error),
|
Unauthorized(anyhow::Error),
|
||||||
Forbidden(&'static str),
|
Forbidden(&'static str),
|
||||||
|
NotFound(&'static str),
|
||||||
|
Internal(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoResponse for ApiError {
|
impl IntoResponse for ApiError {
|
||||||
@@ -54,6 +78,8 @@ impl IntoResponse for ApiError {
|
|||||||
match self {
|
match self {
|
||||||
Self::Unauthorized(e) => err(StatusCode::UNAUTHORIZED, "unauthorized", &e.to_string()),
|
Self::Unauthorized(e) => err(StatusCode::UNAUTHORIZED, "unauthorized", &e.to_string()),
|
||||||
Self::Forbidden(message) => err(StatusCode::FORBIDDEN, "forbidden", message),
|
Self::Forbidden(message) => err(StatusCode::FORBIDDEN, "forbidden", message),
|
||||||
|
Self::NotFound(message) => err(StatusCode::NOT_FOUND, "not-found", message),
|
||||||
|
Self::Internal(message) => err(StatusCode::INTERNAL_SERVER_ERROR, "internal", &message),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,6 +119,11 @@ impl Api {
|
|||||||
.route("/relays/:id/activity", get(list_relay_activity))
|
.route("/relays/:id/activity", get(list_relay_activity))
|
||||||
.route("/relays/:id/deactivate", post(deactivate_relay))
|
.route("/relays/:id/deactivate", post(deactivate_relay))
|
||||||
.route("/relays/:id/reactivate", post(reactivate_relay))
|
.route("/relays/:id/reactivate", post(reactivate_relay))
|
||||||
|
.route("/tenants/:pubkey/invoices", get(list_tenant_invoices))
|
||||||
|
.route("/invoices/:id", get(get_invoice))
|
||||||
|
.route("/invoices/:id/bolt11", get(get_invoice_bolt11))
|
||||||
|
.route("/tenants/:pubkey/stripe/session", get(create_stripe_session))
|
||||||
|
.route("/stripe/webhook", post(stripe_webhook))
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,6 +204,14 @@ impl Api {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_tenant_or_404(&self, pubkey: &str) -> std::result::Result<Tenant, ApiError> {
|
||||||
|
match self.query.get_tenant(pubkey).await {
|
||||||
|
Ok(Some(t)) => Ok(t),
|
||||||
|
Ok(None) => Err(ApiError::NotFound("tenant not found")),
|
||||||
|
Err(e) => Err(ApiError::Internal(e.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn prepare_relay(&self, mut relay: Relay) -> anyhow::Result<Relay> {
|
fn prepare_relay(&self, mut relay: Relay) -> anyhow::Result<Relay> {
|
||||||
if !relay
|
if !relay
|
||||||
.subdomain
|
.subdomain
|
||||||
@@ -328,16 +367,17 @@ async fn get_identity(
|
|||||||
|
|
||||||
// Only create if tenant doesn't exist yet
|
// Only create if tenant doesn't exist yet
|
||||||
if let Ok(None) = state.api.query.get_tenant(&pubkey).await {
|
if let Ok(None) = state.api.query.get_tenant(&pubkey).await {
|
||||||
// TODO: Call Stripe API to create customer and subscription
|
// TODO: Call Stripe API to create a new customer
|
||||||
let stripe_customer_id = String::new();
|
let stripe_customer_id = String::new();
|
||||||
let stripe_subscription_id = String::new();
|
|
||||||
|
|
||||||
let tenant = Tenant {
|
let tenant = Tenant {
|
||||||
pubkey: pubkey.clone(),
|
pubkey: pubkey.clone(),
|
||||||
nwc_url: String::new(),
|
nwc_url: String::new(),
|
||||||
|
nwc_error: None,
|
||||||
created_at: now_ts(),
|
created_at: now_ts(),
|
||||||
stripe_customer_id,
|
stripe_customer_id,
|
||||||
stripe_subscription_id,
|
stripe_subscription_id: None,
|
||||||
|
past_due_at: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
match state.api.command.create_tenant(&tenant).await {
|
match state.api.command.create_tenant(&tenant).await {
|
||||||
@@ -376,16 +416,8 @@ async fn get_tenant(
|
|||||||
) -> std::result::Result<Response, ApiError> {
|
) -> std::result::Result<Response, ApiError> {
|
||||||
let auth = state.api.extract_auth_pubkey(&headers)?;
|
let auth = state.api.extract_auth_pubkey(&headers)?;
|
||||||
state.api.require_admin_or_tenant(&auth, &pubkey)?;
|
state.api.require_admin_or_tenant(&auth, &pubkey)?;
|
||||||
|
let tenant = state.api.get_tenant_or_404(&pubkey).await?;
|
||||||
match state.api.query.get_tenant(&pubkey).await {
|
Ok(ok(StatusCode::OK, tenant))
|
||||||
Ok(Some(tenant)) => Ok(ok(StatusCode::OK, tenant)),
|
|
||||||
Ok(None) => Ok(err(StatusCode::NOT_FOUND, "not-found", "tenant not found")),
|
|
||||||
Err(e) => Ok(err(
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
"internal",
|
|
||||||
&e.to_string(),
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn list_relays(
|
async fn list_relays(
|
||||||
@@ -662,7 +694,7 @@ async fn deactivate_relay(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
match state.api.billing.deactivate_relay(&id).await {
|
match state.api.command.deactivate_relay(&relay).await {
|
||||||
Ok(()) => Ok(ok(StatusCode::OK, ())),
|
Ok(()) => Ok(ok(StatusCode::OK, ())),
|
||||||
Err(e) => Ok(err(
|
Err(e) => Ok(err(
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
@@ -701,7 +733,7 @@ async fn reactivate_relay(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
match state.api.billing.reactivate_relay(&id).await {
|
match state.api.command.activate_relay(&relay).await {
|
||||||
Ok(()) => Ok(ok(StatusCode::OK, ())),
|
Ok(()) => Ok(ok(StatusCode::OK, ())),
|
||||||
Err(e) => Ok(err(
|
Err(e) => Ok(err(
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
@@ -711,6 +743,98 @@ async fn reactivate_relay(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn list_tenant_invoices(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
headers: HeaderMap,
|
||||||
|
Path(pubkey): Path<String>,
|
||||||
|
) -> std::result::Result<Response, ApiError> {
|
||||||
|
let auth = state.api.extract_auth_pubkey(&headers)?;
|
||||||
|
state.api.require_admin_or_tenant(&auth, &pubkey)?;
|
||||||
|
let tenant = state.api.get_tenant_or_404(&pubkey).await?;
|
||||||
|
|
||||||
|
match state
|
||||||
|
.api
|
||||||
|
.billing
|
||||||
|
.stripe_list_invoices(&tenant.stripe_customer_id)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(invoices) => Ok(ok(StatusCode::OK, invoices)),
|
||||||
|
Err(e) => Ok(err(
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"internal",
|
||||||
|
&e.to_string(),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_invoice(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
headers: HeaderMap,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> std::result::Result<Response, ApiError> {
|
||||||
|
let auth = state.api.extract_auth_pubkey(&headers)?;
|
||||||
|
let (invoice, tenant) = state.api.billing.get_invoice_with_tenant(&id).await
|
||||||
|
.map_err(|e| ApiError::Internal(e.to_string()))?;
|
||||||
|
state.api.require_admin_or_tenant(&auth, &tenant.pubkey)?;
|
||||||
|
|
||||||
|
Ok(ok(StatusCode::OK, invoice))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_invoice_bolt11(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
headers: HeaderMap,
|
||||||
|
Path(id): Path<String>,
|
||||||
|
) -> std::result::Result<Response, ApiError> {
|
||||||
|
let auth = state.api.extract_auth_pubkey(&headers)?;
|
||||||
|
let (invoice, tenant) = state.api.billing.get_invoice_with_tenant(&id).await
|
||||||
|
.map_err(|e| ApiError::Internal(e.to_string()))?;
|
||||||
|
state.api.require_admin_or_tenant(&auth, &tenant.pubkey)?;
|
||||||
|
|
||||||
|
let status = invoice["status"].as_str().unwrap_or_default();
|
||||||
|
if status != "open" {
|
||||||
|
return Ok(err(
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
"invoice-not-open",
|
||||||
|
"invoice is not open",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let amount_due = invoice["amount_due"].as_i64().unwrap_or(0);
|
||||||
|
|
||||||
|
match state.api.billing.create_bolt11(amount_due).await {
|
||||||
|
Ok(bolt11) => Ok(ok(StatusCode::OK, serde_json::json!({ "bolt11": bolt11 }))),
|
||||||
|
Err(e) => Ok(err(
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"internal",
|
||||||
|
&e.to_string(),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_stripe_session(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
headers: HeaderMap,
|
||||||
|
Path(pubkey): Path<String>,
|
||||||
|
) -> std::result::Result<Response, ApiError> {
|
||||||
|
let auth = state.api.extract_auth_pubkey(&headers)?;
|
||||||
|
state.api.require_admin_or_tenant(&auth, &pubkey)?;
|
||||||
|
let tenant = state.api.get_tenant_or_404(&pubkey).await?;
|
||||||
|
|
||||||
|
match state
|
||||||
|
.api
|
||||||
|
.billing
|
||||||
|
.stripe_create_portal_session(&tenant.stripe_customer_id)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(url) => Ok(ok(StatusCode::OK, serde_json::json!({ "url": url }))),
|
||||||
|
Err(e) => Ok(err(
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"internal",
|
||||||
|
&e.to_string(),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn update_tenant(
|
async fn update_tenant(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
@@ -719,18 +843,7 @@ async fn update_tenant(
|
|||||||
) -> std::result::Result<Response, ApiError> {
|
) -> std::result::Result<Response, ApiError> {
|
||||||
let auth = state.api.extract_auth_pubkey(&headers)?;
|
let auth = state.api.extract_auth_pubkey(&headers)?;
|
||||||
state.api.require_admin_or_tenant(&auth, &pubkey)?;
|
state.api.require_admin_or_tenant(&auth, &pubkey)?;
|
||||||
|
let mut tenant = state.api.get_tenant_or_404(&pubkey).await?;
|
||||||
let mut tenant = match state.api.query.get_tenant(&pubkey).await {
|
|
||||||
Ok(Some(t)) => t,
|
|
||||||
Ok(None) => return Ok(err(StatusCode::NOT_FOUND, "not-found", "tenant not found")),
|
|
||||||
Err(e) => {
|
|
||||||
return Ok(err(
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
"internal",
|
|
||||||
&e.to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(nwc_url) = payload.nwc_url {
|
if let Some(nwc_url) = payload.nwc_url {
|
||||||
tenant.nwc_url = nwc_url;
|
tenant.nwc_url = nwc_url;
|
||||||
|
|||||||
+611
-25
@@ -1,13 +1,38 @@
|
|||||||
use anyhow::Result;
|
use anyhow::{Result, anyhow};
|
||||||
|
use hmac::{Hmac, Mac};
|
||||||
|
use nwc::prelude::{
|
||||||
|
MakeInvoiceRequest, NostrWalletConnectURI, PayInvoiceRequest as NwcPayInvoiceRequest, NWC,
|
||||||
|
};
|
||||||
|
use sha2::Sha256;
|
||||||
|
|
||||||
use crate::command::Command;
|
use crate::command::Command;
|
||||||
use crate::models::Activity;
|
use crate::models::Activity;
|
||||||
use crate::query::Query;
|
use crate::query::Query;
|
||||||
use crate::robot::Robot;
|
use crate::robot::Robot;
|
||||||
|
|
||||||
|
type HmacSha256 = Hmac<Sha256>;
|
||||||
|
|
||||||
|
const STRIPE_API: &str = "https://api.stripe.com/v1";
|
||||||
|
const WEBHOOK_TOLERANCE_SECS: i64 = 300;
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
struct StripeEvent {
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
event_type: String,
|
||||||
|
data: StripeEventData,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
struct StripeEventData {
|
||||||
|
object: serde_json::Value,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Billing {
|
pub struct Billing {
|
||||||
nwc_url: String,
|
nwc_url: String,
|
||||||
|
stripe_secret_key: String,
|
||||||
|
stripe_webhook_secret: String,
|
||||||
|
http: reqwest::Client,
|
||||||
query: Query,
|
query: Query,
|
||||||
command: Command,
|
command: Command,
|
||||||
robot: Robot,
|
robot: Robot,
|
||||||
@@ -16,8 +41,13 @@ pub struct Billing {
|
|||||||
impl Billing {
|
impl Billing {
|
||||||
pub fn new(query: Query, command: Command, robot: Robot) -> Self {
|
pub fn new(query: Query, command: Command, robot: Robot) -> Self {
|
||||||
let nwc_url = std::env::var("NWC_URL").unwrap_or_default();
|
let nwc_url = std::env::var("NWC_URL").unwrap_or_default();
|
||||||
|
let stripe_secret_key = std::env::var("STRIPE_SECRET_KEY").unwrap_or_default();
|
||||||
|
let stripe_webhook_secret = std::env::var("STRIPE_WEBHOOK_SECRET").unwrap_or_default();
|
||||||
Self {
|
Self {
|
||||||
nwc_url,
|
nwc_url,
|
||||||
|
stripe_secret_key,
|
||||||
|
stripe_webhook_secret,
|
||||||
|
http: reqwest::Client::new(),
|
||||||
query,
|
query,
|
||||||
command,
|
command,
|
||||||
robot,
|
robot,
|
||||||
@@ -50,51 +80,607 @@ impl Billing {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if needs_billing_sync {
|
if needs_billing_sync {
|
||||||
self.sync_relay_subscription_item(activity).await?;
|
self.sync_relay_subscription(activity).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn sync_relay_subscription_item(&self, activity: &Activity) -> Result<()> {
|
pub async fn sync_relay_subscription(&self, activity: &Activity) -> Result<()> {
|
||||||
let Some(relay) = self.query.get_relay(&activity.resource_id).await? else {
|
let Some(relay) = self.query.get_relay(&activity.resource_id).await? else {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
let should_delete = !relay.sync_error.is_empty()
|
let Some(tenant) = self.query.get_tenant(&relay.tenant).await? else {
|
||||||
|| relay.synced == 0
|
return Ok(());
|
||||||
|| relay.plan == "free"
|
};
|
||||||
|| relay.status == "inactive";
|
|
||||||
|
|
||||||
if should_delete {
|
// Free plan: remove subscription item if exists, then clean up
|
||||||
if relay.stripe_subscription_item_id.is_some() {
|
if relay.plan == "free" {
|
||||||
// TODO: Delete subscription item via Stripe API
|
if let Some(ref item_id) = relay.stripe_subscription_item_id {
|
||||||
|
self.stripe_delete_subscription_item(item_id).await?;
|
||||||
self.command.delete_relay_subscription_item(&relay.id).await?;
|
self.command.delete_relay_subscription_item(&relay.id).await?;
|
||||||
}
|
}
|
||||||
|
self.cleanup_empty_subscription(&tenant.pubkey).await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inactive relay: remove subscription item if exists, then clean up
|
||||||
|
if relay.status == "inactive" {
|
||||||
|
if let Some(ref item_id) = relay.stripe_subscription_item_id {
|
||||||
|
self.stripe_delete_subscription_item(item_id).await?;
|
||||||
|
self.command.delete_relay_subscription_item(&relay.id).await?;
|
||||||
|
}
|
||||||
|
self.cleanup_empty_subscription(&tenant.pubkey).await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Active relay on a paid plan
|
||||||
|
let plan = Query::list_plans()
|
||||||
|
.into_iter()
|
||||||
|
.find(|p| p.id == relay.plan);
|
||||||
|
|
||||||
|
let Some(plan) = plan else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
let Some(ref stripe_price_id) = plan.stripe_price_id else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
// Ensure subscription exists
|
||||||
|
if tenant.stripe_subscription_id.is_none() {
|
||||||
|
let (subscription_id, item_id) = self
|
||||||
|
.stripe_create_subscription(&tenant.stripe_customer_id, stripe_price_id)
|
||||||
|
.await?;
|
||||||
|
self.command
|
||||||
|
.set_tenant_subscription(&tenant.pubkey, &subscription_id)
|
||||||
|
.await?;
|
||||||
|
self.command
|
||||||
|
.set_relay_subscription_item(&relay.id, &item_id)
|
||||||
|
.await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sync the subscription item: create or update
|
||||||
|
let subscription_id = tenant.stripe_subscription_id.as_ref().unwrap();
|
||||||
|
let item_id = if let Some(ref existing_item_id) = relay.stripe_subscription_item_id {
|
||||||
|
self.stripe_update_subscription_item(existing_item_id, stripe_price_id)
|
||||||
|
.await?
|
||||||
} else {
|
} else {
|
||||||
// TODO: Create or update subscription item via Stripe API
|
self.stripe_create_subscription_item(subscription_id, stripe_price_id)
|
||||||
// let stripe_subscription_item_id = ...;
|
.await?
|
||||||
// self.command.set_relay_subscription_item(&relay.id, &stripe_subscription_item_id).await?;
|
};
|
||||||
|
self.command
|
||||||
|
.set_relay_subscription_item(&relay.id, &item_id)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn cleanup_empty_subscription(&self, tenant_pubkey: &str) -> Result<()> {
|
||||||
|
let has_paid = self.query.has_active_paid_relays(tenant_pubkey).await?;
|
||||||
|
if has_paid {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(tenant) = self.query.get_tenant(tenant_pubkey).await? else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(ref subscription_id) = tenant.stripe_subscription_id {
|
||||||
|
self.stripe_cancel_subscription(subscription_id).await?;
|
||||||
|
self.command.clear_tenant_subscription(tenant_pubkey).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn deactivate_relay(&self, relay_id: &str) -> Result<()> {
|
pub async fn handle_webhook(&self, payload: &str, signature: &str) -> Result<()> {
|
||||||
let relay = self
|
self.verify_webhook_signature(payload, signature)?;
|
||||||
.query
|
|
||||||
.get_relay(relay_id)
|
let event: StripeEvent = serde_json::from_str(payload)?;
|
||||||
.await?
|
let obj = &event.data.object;
|
||||||
.ok_or_else(|| anyhow::anyhow!("relay not found"))?;
|
|
||||||
self.command.deactivate_relay(&relay).await
|
match event.event_type.as_str() {
|
||||||
|
"invoice.created" => {
|
||||||
|
let customer = obj["customer"].as_str().unwrap_or_default();
|
||||||
|
let amount_due = obj["amount_due"].as_i64().unwrap_or(0);
|
||||||
|
let invoice_id = obj["id"].as_str().unwrap_or_default();
|
||||||
|
self.handle_invoice_created(customer, amount_due, invoice_id)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
"invoice.paid" => {
|
||||||
|
let customer = obj["customer"].as_str().unwrap_or_default();
|
||||||
|
self.handle_invoice_paid(customer).await?;
|
||||||
|
}
|
||||||
|
"invoice.payment_failed" => {
|
||||||
|
let customer = obj["customer"].as_str().unwrap_or_default();
|
||||||
|
self.handle_invoice_payment_failed(customer).await?;
|
||||||
|
}
|
||||||
|
"invoice.overdue" => {
|
||||||
|
let customer = obj["customer"].as_str().unwrap_or_default();
|
||||||
|
self.handle_invoice_overdue(customer).await?;
|
||||||
|
}
|
||||||
|
"customer.subscription.updated" => {
|
||||||
|
let customer = obj["customer"].as_str().unwrap_or_default();
|
||||||
|
let status = obj["status"].as_str().unwrap_or_default();
|
||||||
|
self.handle_subscription_updated(customer, status).await?;
|
||||||
|
}
|
||||||
|
"customer.subscription.deleted" => {
|
||||||
|
let customer = obj["customer"].as_str().unwrap_or_default();
|
||||||
|
self.handle_subscription_deleted(customer).await?;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn reactivate_relay(&self, relay_id: &str) -> Result<()> {
|
fn verify_webhook_signature(&self, payload: &str, sig_header: &str) -> Result<()> {
|
||||||
let relay = self
|
let mut timestamp = None;
|
||||||
|
let mut signature = None;
|
||||||
|
for part in sig_header.split(',') {
|
||||||
|
if let Some(t) = part.strip_prefix("t=") {
|
||||||
|
timestamp = Some(t);
|
||||||
|
} else if let Some(v) = part.strip_prefix("v1=") {
|
||||||
|
signature = Some(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let timestamp = timestamp.ok_or_else(|| anyhow!("missing webhook timestamp"))?;
|
||||||
|
let signature = signature.ok_or_else(|| anyhow!("missing webhook signature"))?;
|
||||||
|
|
||||||
|
let signed_payload = format!("{timestamp}.{payload}");
|
||||||
|
let mut mac = HmacSha256::new_from_slice(self.stripe_webhook_secret.as_bytes())
|
||||||
|
.map_err(|e| anyhow!("invalid webhook secret: {e}"))?;
|
||||||
|
mac.update(signed_payload.as_bytes());
|
||||||
|
let expected = hex::encode(mac.finalize().into_bytes());
|
||||||
|
|
||||||
|
if expected != signature {
|
||||||
|
return Err(anyhow!("webhook signature mismatch"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let ts: i64 = timestamp.parse().map_err(|_| anyhow!("bad webhook timestamp"))?;
|
||||||
|
let now = chrono::Utc::now().timestamp();
|
||||||
|
if (now - ts).abs() > WEBHOOK_TOLERANCE_SECS {
|
||||||
|
return Err(anyhow!("webhook timestamp outside tolerance"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_invoice_created(
|
||||||
|
&self,
|
||||||
|
stripe_customer_id: &str,
|
||||||
|
amount_due: i64,
|
||||||
|
invoice_id: &str,
|
||||||
|
) -> Result<()> {
|
||||||
|
if amount_due == 0 {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(tenant) = self
|
||||||
.query
|
.query
|
||||||
.get_relay(relay_id)
|
.get_tenant_by_stripe_customer_id(stripe_customer_id)
|
||||||
.await?
|
.await?
|
||||||
.ok_or_else(|| anyhow::anyhow!("relay not found"))?;
|
else {
|
||||||
self.command.activate_relay(&relay).await
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
// 1. NWC auto-pay: if the tenant has a nwc_url
|
||||||
|
if !tenant.nwc_url.is_empty() {
|
||||||
|
match self.nwc_pay_invoice(amount_due, &tenant.nwc_url).await {
|
||||||
|
Ok(()) => {
|
||||||
|
self.stripe_pay_invoice_out_of_band(invoice_id).await?;
|
||||||
|
self.command
|
||||||
|
.clear_tenant_nwc_error(&tenant.pubkey)
|
||||||
|
.await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
let error_msg = format!("{e}");
|
||||||
|
self.command
|
||||||
|
.set_tenant_nwc_error(&tenant.pubkey, &error_msg)
|
||||||
|
.await?;
|
||||||
|
// Fall through to next option
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Card on file: if the tenant has a payment method, Stripe charges automatically
|
||||||
|
if self
|
||||||
|
.stripe_has_payment_method(&tenant.stripe_customer_id)
|
||||||
|
.await?
|
||||||
|
{
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Manual payment: send a DM
|
||||||
|
self.robot
|
||||||
|
.send_dm(
|
||||||
|
&tenant.pubkey,
|
||||||
|
"Payment is due for your relay subscription. Please visit the application to complete a manual Lightning payment.",
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_invoice_paid(&self, stripe_customer_id: &str) -> Result<()> {
|
||||||
|
let Some(tenant) = self
|
||||||
|
.query
|
||||||
|
.get_tenant_by_stripe_customer_id(stripe_customer_id)
|
||||||
|
.await?
|
||||||
|
else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
if tenant.past_due_at.is_some() {
|
||||||
|
self.command.clear_tenant_past_due(&tenant.pubkey).await?;
|
||||||
|
|
||||||
|
let relays = self.query.list_relays_for_tenant(&tenant.pubkey).await?;
|
||||||
|
for relay in relays {
|
||||||
|
if relay.status == "inactive" && relay.plan != "free" {
|
||||||
|
self.command.activate_relay(&relay).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_invoice_payment_failed(&self, stripe_customer_id: &str) -> Result<()> {
|
||||||
|
let Some(tenant) = self
|
||||||
|
.query
|
||||||
|
.get_tenant_by_stripe_customer_id(stripe_customer_id)
|
||||||
|
.await?
|
||||||
|
else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
if tenant.past_due_at.is_none() {
|
||||||
|
self.command.set_tenant_past_due(&tenant.pubkey).await?;
|
||||||
|
self.robot
|
||||||
|
.send_dm(
|
||||||
|
&tenant.pubkey,
|
||||||
|
"Your payment has failed. Your relays may be deactivated if not resolved within a week.",
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_subscription_updated(
|
||||||
|
&self,
|
||||||
|
stripe_customer_id: &str,
|
||||||
|
status: &str,
|
||||||
|
) -> Result<()> {
|
||||||
|
if status != "canceled" && status != "unpaid" {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(tenant) = self
|
||||||
|
.query
|
||||||
|
.get_tenant_by_stripe_customer_id(stripe_customer_id)
|
||||||
|
.await?
|
||||||
|
else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
self.command
|
||||||
|
.clear_tenant_subscription(&tenant.pubkey)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let relays = self.query.list_relays_for_tenant(&tenant.pubkey).await?;
|
||||||
|
for relay in relays {
|
||||||
|
if relay.status == "active" && relay.plan != "free" {
|
||||||
|
self.command.deactivate_relay(&relay).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_subscription_deleted(&self, stripe_customer_id: &str) -> Result<()> {
|
||||||
|
let Some(tenant) = self
|
||||||
|
.query
|
||||||
|
.get_tenant_by_stripe_customer_id(stripe_customer_id)
|
||||||
|
.await?
|
||||||
|
else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
self.command
|
||||||
|
.clear_tenant_subscription(&tenant.pubkey)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_invoice_overdue(&self, stripe_customer_id: &str) -> Result<()> {
|
||||||
|
let Some(tenant) = self
|
||||||
|
.query
|
||||||
|
.get_tenant_by_stripe_customer_id(stripe_customer_id)
|
||||||
|
.await?
|
||||||
|
else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
let relays = self.query.list_relays_for_tenant(&tenant.pubkey).await?;
|
||||||
|
for relay in relays {
|
||||||
|
if relay.status == "active" && relay.plan != "free" {
|
||||||
|
self.command.deactivate_relay(&relay).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.robot
|
||||||
|
.send_dm(
|
||||||
|
&tenant.pubkey,
|
||||||
|
"Your paid relays have been deactivated due to non-payment.",
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Public API helpers ---
|
||||||
|
|
||||||
|
pub async fn get_invoice_with_tenant(
|
||||||
|
&self,
|
||||||
|
invoice_id: &str,
|
||||||
|
) -> Result<(serde_json::Value, crate::models::Tenant)> {
|
||||||
|
let invoice = self.stripe_get_invoice(invoice_id).await?;
|
||||||
|
let customer_id = invoice["customer"]
|
||||||
|
.as_str()
|
||||||
|
.ok_or_else(|| anyhow!("invoice missing customer"))?;
|
||||||
|
let tenant = self
|
||||||
|
.query
|
||||||
|
.get_tenant_by_stripe_customer_id(customer_id)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| anyhow!("tenant not found for customer"))?;
|
||||||
|
Ok((invoice, tenant))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn stripe_list_invoices(&self, customer_id: &str) -> Result<serde_json::Value> {
|
||||||
|
let resp = self
|
||||||
|
.http
|
||||||
|
.get(format!("{STRIPE_API}/invoices"))
|
||||||
|
.bearer_auth(&self.stripe_secret_key)
|
||||||
|
.query(&[("customer", customer_id)])
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let body: serde_json::Value = resp.error_for_status()?.json().await?;
|
||||||
|
Ok(body["data"].clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn stripe_get_invoice(&self, invoice_id: &str) -> Result<serde_json::Value> {
|
||||||
|
let resp = self
|
||||||
|
.http
|
||||||
|
.get(format!("{STRIPE_API}/invoices/{invoice_id}"))
|
||||||
|
.bearer_auth(&self.stripe_secret_key)
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let body: serde_json::Value = resp.error_for_status()?.json().await?;
|
||||||
|
Ok(body)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_bolt11(&self, amount_due_cents: i64) -> Result<String> {
|
||||||
|
let amount_msats = (amount_due_cents as u64) * 1000; // placeholder conversion
|
||||||
|
|
||||||
|
let system_uri: NostrWalletConnectURI = self.nwc_url.parse()
|
||||||
|
.map_err(|_| anyhow!("invalid system NWC URL"))?;
|
||||||
|
let system_nwc = NWC::new(system_uri);
|
||||||
|
|
||||||
|
let make_req = MakeInvoiceRequest {
|
||||||
|
amount: amount_msats,
|
||||||
|
description: Some("Relay subscription payment".to_string()),
|
||||||
|
description_hash: None,
|
||||||
|
expiry: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let invoice_response = system_nwc
|
||||||
|
.make_invoice(make_req)
|
||||||
|
.await
|
||||||
|
.map_err(|e| anyhow!("failed to create invoice: {e}"))?;
|
||||||
|
|
||||||
|
system_nwc.shutdown().await;
|
||||||
|
|
||||||
|
Ok(invoice_response.invoice)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn stripe_create_portal_session(&self, customer_id: &str) -> Result<String> {
|
||||||
|
let resp = self
|
||||||
|
.http
|
||||||
|
.post(format!("{STRIPE_API}/billing_portal/sessions"))
|
||||||
|
.bearer_auth(&self.stripe_secret_key)
|
||||||
|
.form(&[("customer", customer_id)])
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let body: serde_json::Value = resp.error_for_status()?.json().await?;
|
||||||
|
let url = body["url"]
|
||||||
|
.as_str()
|
||||||
|
.ok_or_else(|| anyhow!("missing portal session url"))?
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
Ok(url)
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Stripe API helpers ---
|
||||||
|
|
||||||
|
async fn stripe_create_subscription(
|
||||||
|
&self,
|
||||||
|
customer_id: &str,
|
||||||
|
price_id: &str,
|
||||||
|
) -> Result<(String, String)> {
|
||||||
|
let resp = self
|
||||||
|
.http
|
||||||
|
.post(format!("{STRIPE_API}/subscriptions"))
|
||||||
|
.bearer_auth(&self.stripe_secret_key)
|
||||||
|
.form(&[
|
||||||
|
("customer", customer_id),
|
||||||
|
("collection_method", "charge_automatically"),
|
||||||
|
("items[0][price]", price_id),
|
||||||
|
])
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let body: serde_json::Value = resp.error_for_status()?.json().await?;
|
||||||
|
let subscription_id = body["id"]
|
||||||
|
.as_str()
|
||||||
|
.ok_or_else(|| anyhow!("missing subscription id"))?
|
||||||
|
.to_string();
|
||||||
|
let item_id = body["items"]["data"][0]["id"]
|
||||||
|
.as_str()
|
||||||
|
.ok_or_else(|| anyhow!("missing subscription item id"))?
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
Ok((subscription_id, item_id))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn stripe_create_subscription_item(
|
||||||
|
&self,
|
||||||
|
subscription_id: &str,
|
||||||
|
price_id: &str,
|
||||||
|
) -> Result<String> {
|
||||||
|
let resp = self
|
||||||
|
.http
|
||||||
|
.post(format!("{STRIPE_API}/subscription_items"))
|
||||||
|
.bearer_auth(&self.stripe_secret_key)
|
||||||
|
.form(&[
|
||||||
|
("subscription", subscription_id),
|
||||||
|
("price", price_id),
|
||||||
|
])
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let body: serde_json::Value = resp.error_for_status()?.json().await?;
|
||||||
|
let item_id = body["id"]
|
||||||
|
.as_str()
|
||||||
|
.ok_or_else(|| anyhow!("missing subscription item id"))?
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
Ok(item_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn stripe_update_subscription_item(
|
||||||
|
&self,
|
||||||
|
item_id: &str,
|
||||||
|
price_id: &str,
|
||||||
|
) -> Result<String> {
|
||||||
|
let resp = self
|
||||||
|
.http
|
||||||
|
.post(format!("{STRIPE_API}/subscription_items/{item_id}"))
|
||||||
|
.bearer_auth(&self.stripe_secret_key)
|
||||||
|
.form(&[("price", price_id)])
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let body: serde_json::Value = resp.error_for_status()?.json().await?;
|
||||||
|
let id = body["id"]
|
||||||
|
.as_str()
|
||||||
|
.ok_or_else(|| anyhow!("missing subscription item id"))?
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
Ok(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn stripe_delete_subscription_item(&self, item_id: &str) -> Result<()> {
|
||||||
|
self.http
|
||||||
|
.delete(format!("{STRIPE_API}/subscription_items/{item_id}"))
|
||||||
|
.bearer_auth(&self.stripe_secret_key)
|
||||||
|
.send()
|
||||||
|
.await?
|
||||||
|
.error_for_status()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn stripe_cancel_subscription(&self, subscription_id: &str) -> Result<()> {
|
||||||
|
self.http
|
||||||
|
.delete(format!("{STRIPE_API}/subscriptions/{subscription_id}"))
|
||||||
|
.bearer_auth(&self.stripe_secret_key)
|
||||||
|
.send()
|
||||||
|
.await?
|
||||||
|
.error_for_status()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn stripe_pay_invoice_out_of_band(&self, invoice_id: &str) -> Result<()> {
|
||||||
|
self.http
|
||||||
|
.post(format!("{STRIPE_API}/invoices/{invoice_id}/pay"))
|
||||||
|
.bearer_auth(&self.stripe_secret_key)
|
||||||
|
.form(&[("paid_out_of_band", "true")])
|
||||||
|
.send()
|
||||||
|
.await?
|
||||||
|
.error_for_status()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn stripe_has_payment_method(&self, customer_id: &str) -> Result<bool> {
|
||||||
|
let resp = self
|
||||||
|
.http
|
||||||
|
.get(format!("{STRIPE_API}/payment_methods"))
|
||||||
|
.bearer_auth(&self.stripe_secret_key)
|
||||||
|
.query(&[("customer", customer_id), ("type", "card")])
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let body: serde_json::Value = resp.error_for_status()?.json().await?;
|
||||||
|
let has_method = body["data"]
|
||||||
|
.as_array()
|
||||||
|
.map(|a| !a.is_empty())
|
||||||
|
.unwrap_or(false);
|
||||||
|
|
||||||
|
Ok(has_method)
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- NWC helpers ---
|
||||||
|
|
||||||
|
async fn nwc_pay_invoice(&self, amount_due_cents: i64, tenant_nwc_url: &str) -> Result<()> {
|
||||||
|
// Convert USD cents to millisatoshis (approximate: 1 sat ≈ variable USD)
|
||||||
|
// amount_due is in cents from Stripe. We create a Lightning invoice for the exact amount.
|
||||||
|
// The NWC make_invoice amount is in millisatoshis.
|
||||||
|
let amount_msats = (amount_due_cents as u64) * 1000; // placeholder conversion, actual rate would come from exchange
|
||||||
|
|
||||||
|
// Create a bolt11 invoice using the system wallet (self.nwc_url)
|
||||||
|
let system_uri: NostrWalletConnectURI = self.nwc_url.parse()
|
||||||
|
.map_err(|_| anyhow!("invalid system NWC URL"))?;
|
||||||
|
let system_nwc = NWC::new(system_uri);
|
||||||
|
|
||||||
|
let make_req = MakeInvoiceRequest {
|
||||||
|
amount: amount_msats,
|
||||||
|
description: Some("Relay subscription payment".to_string()),
|
||||||
|
description_hash: None,
|
||||||
|
expiry: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let invoice_response = system_nwc
|
||||||
|
.make_invoice(make_req)
|
||||||
|
.await
|
||||||
|
.map_err(|e| anyhow!("failed to create invoice: {e}"))?;
|
||||||
|
|
||||||
|
system_nwc.shutdown().await;
|
||||||
|
|
||||||
|
// Pay the bolt11 invoice using the tenant's wallet
|
||||||
|
let tenant_uri: NostrWalletConnectURI = tenant_nwc_url.parse()
|
||||||
|
.map_err(|_| anyhow!("invalid tenant NWC URL"))?;
|
||||||
|
let tenant_nwc = NWC::new(tenant_uri);
|
||||||
|
|
||||||
|
let pay_req = NwcPayInvoiceRequest::new(invoice_response.invoice);
|
||||||
|
|
||||||
|
tenant_nwc
|
||||||
|
.pay_invoice(pay_req)
|
||||||
|
.await
|
||||||
|
.map_err(|e| anyhow!("failed to pay invoice: {e}"))?;
|
||||||
|
|
||||||
|
tenant_nwc.shutdown().await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+54
-3
@@ -67,14 +67,13 @@ impl Command {
|
|||||||
let mut tx = self.pool.begin().await?;
|
let mut tx = self.pool.begin().await?;
|
||||||
|
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"INSERT INTO tenant (pubkey, nwc_url, created_at, stripe_customer_id, stripe_subscription_id)
|
"INSERT INTO tenant (pubkey, nwc_url, created_at, stripe_customer_id)
|
||||||
VALUES (?, ?, ?, ?, ?)",
|
VALUES (?, ?, ?, ?)",
|
||||||
)
|
)
|
||||||
.bind(&tenant.pubkey)
|
.bind(&tenant.pubkey)
|
||||||
.bind(&tenant.nwc_url)
|
.bind(&tenant.nwc_url)
|
||||||
.bind(tenant.created_at)
|
.bind(tenant.created_at)
|
||||||
.bind(&tenant.stripe_customer_id)
|
.bind(&tenant.stripe_customer_id)
|
||||||
.bind(&tenant.stripe_subscription_id)
|
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -255,4 +254,56 @@ impl Command {
|
|||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn set_tenant_subscription(&self, pubkey: &str, stripe_subscription_id: &str) -> Result<()> {
|
||||||
|
sqlx::query("UPDATE tenant SET stripe_subscription_id = ? WHERE pubkey = ?")
|
||||||
|
.bind(stripe_subscription_id)
|
||||||
|
.bind(pubkey)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn clear_tenant_subscription(&self, pubkey: &str) -> Result<()> {
|
||||||
|
sqlx::query("UPDATE tenant SET stripe_subscription_id = NULL WHERE pubkey = ?")
|
||||||
|
.bind(pubkey)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_tenant_nwc_error(&self, pubkey: &str, error: &str) -> Result<()> {
|
||||||
|
sqlx::query("UPDATE tenant SET nwc_error = ? WHERE pubkey = ?")
|
||||||
|
.bind(error)
|
||||||
|
.bind(pubkey)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn clear_tenant_nwc_error(&self, pubkey: &str) -> Result<()> {
|
||||||
|
sqlx::query("UPDATE tenant SET nwc_error = NULL WHERE pubkey = ?")
|
||||||
|
.bind(pubkey)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_tenant_past_due(&self, pubkey: &str) -> Result<()> {
|
||||||
|
let now = chrono::Utc::now().timestamp();
|
||||||
|
sqlx::query("UPDATE tenant SET past_due_at = ? WHERE pubkey = ?")
|
||||||
|
.bind(now)
|
||||||
|
.bind(pubkey)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn clear_tenant_past_due(&self, pubkey: &str) -> Result<()> {
|
||||||
|
sqlx::query("UPDATE tenant SET past_due_at = NULL WHERE pubkey = ?")
|
||||||
|
.bind(pubkey)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,17 +18,18 @@ pub struct Plan {
|
|||||||
pub members: Option<i64>,
|
pub members: Option<i64>,
|
||||||
pub blossom: bool,
|
pub blossom: bool,
|
||||||
pub livekit: bool,
|
pub livekit: bool,
|
||||||
pub stripe_price_id: String,
|
pub stripe_price_id: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
|
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
|
||||||
pub struct Tenant {
|
pub struct Tenant {
|
||||||
pub pubkey: String,
|
pub pubkey: String,
|
||||||
#[serde(skip_serializing)]
|
|
||||||
pub nwc_url: String,
|
pub nwc_url: String,
|
||||||
|
pub nwc_error: Option<String>,
|
||||||
pub created_at: i64,
|
pub created_at: i64,
|
||||||
pub stripe_customer_id: String,
|
pub stripe_customer_id: String,
|
||||||
pub stripe_subscription_id: String,
|
pub stripe_subscription_id: Option<String>,
|
||||||
|
pub past_due_at: Option<i64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
|
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
|
||||||
|
|||||||
+27
-5
@@ -15,7 +15,7 @@ impl Query {
|
|||||||
|
|
||||||
pub async fn list_tenants(&self) -> Result<Vec<Tenant>> {
|
pub async fn list_tenants(&self) -> Result<Vec<Tenant>> {
|
||||||
let rows = sqlx::query_as::<_, Tenant>(
|
let rows = sqlx::query_as::<_, Tenant>(
|
||||||
"SELECT pubkey, nwc_url, created_at, stripe_customer_id, stripe_subscription_id
|
"SELECT pubkey, nwc_url, nwc_error, created_at, stripe_customer_id, stripe_subscription_id, past_due_at
|
||||||
FROM tenant
|
FROM tenant
|
||||||
ORDER BY pubkey",
|
ORDER BY pubkey",
|
||||||
)
|
)
|
||||||
@@ -26,7 +26,7 @@ impl Query {
|
|||||||
|
|
||||||
pub async fn get_tenant(&self, pubkey: &str) -> Result<Option<Tenant>> {
|
pub async fn get_tenant(&self, pubkey: &str) -> Result<Option<Tenant>> {
|
||||||
let row = sqlx::query_as::<_, Tenant>(
|
let row = sqlx::query_as::<_, Tenant>(
|
||||||
"SELECT pubkey, nwc_url, created_at, stripe_customer_id, stripe_subscription_id
|
"SELECT pubkey, nwc_url, nwc_error, created_at, stripe_customer_id, stripe_subscription_id, past_due_at
|
||||||
FROM tenant
|
FROM tenant
|
||||||
WHERE pubkey = ?",
|
WHERE pubkey = ?",
|
||||||
)
|
)
|
||||||
@@ -45,7 +45,7 @@ impl Query {
|
|||||||
members: Some(10),
|
members: Some(10),
|
||||||
blossom: false,
|
blossom: false,
|
||||||
livekit: false,
|
livekit: false,
|
||||||
stripe_price_id: String::new(),
|
stripe_price_id: None,
|
||||||
},
|
},
|
||||||
Plan {
|
Plan {
|
||||||
id: "basic".to_string(),
|
id: "basic".to_string(),
|
||||||
@@ -54,7 +54,7 @@ impl Query {
|
|||||||
members: Some(100),
|
members: Some(100),
|
||||||
blossom: true,
|
blossom: true,
|
||||||
livekit: true,
|
livekit: true,
|
||||||
stripe_price_id: std::env::var("STRIPE_PRICE_BASIC").unwrap_or_default(),
|
stripe_price_id: Some(std::env::var("STRIPE_PRICE_BASIC").unwrap_or_default()),
|
||||||
},
|
},
|
||||||
Plan {
|
Plan {
|
||||||
id: "growth".to_string(),
|
id: "growth".to_string(),
|
||||||
@@ -63,7 +63,7 @@ impl Query {
|
|||||||
members: None,
|
members: None,
|
||||||
blossom: true,
|
blossom: true,
|
||||||
livekit: true,
|
livekit: true,
|
||||||
stripe_price_id: std::env::var("STRIPE_PRICE_GROWTH").unwrap_or_default(),
|
stripe_price_id: Some(std::env::var("STRIPE_PRICE_GROWTH").unwrap_or_default()),
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -119,6 +119,28 @@ impl Query {
|
|||||||
Ok(row)
|
Ok(row)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_tenant_by_stripe_customer_id(&self, stripe_customer_id: &str) -> Result<Option<Tenant>> {
|
||||||
|
let row = sqlx::query_as::<_, Tenant>(
|
||||||
|
"SELECT pubkey, nwc_url, nwc_error, created_at, stripe_customer_id, stripe_subscription_id, past_due_at
|
||||||
|
FROM tenant
|
||||||
|
WHERE stripe_customer_id = ?",
|
||||||
|
)
|
||||||
|
.bind(stripe_customer_id)
|
||||||
|
.fetch_optional(&self.pool)
|
||||||
|
.await?;
|
||||||
|
Ok(row)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn has_active_paid_relays(&self, tenant_id: &str) -> Result<bool> {
|
||||||
|
let count = sqlx::query_scalar::<_, i64>(
|
||||||
|
"SELECT COUNT(*) FROM relay WHERE tenant = ? AND status = 'active' AND plan != 'free'",
|
||||||
|
)
|
||||||
|
.bind(tenant_id)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await?;
|
||||||
|
Ok(count > 0)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn list_activity_for_relay(&self, relay_id: &str) -> Result<Vec<Activity>> {
|
pub async fn list_activity_for_relay(&self, relay_id: &str) -> Result<Vec<Activity>> {
|
||||||
let rows = sqlx::query_as::<_, Activity>(
|
let rows = sqlx::query_as::<_, Activity>(
|
||||||
"SELECT id, tenant, created_at, activity_type, resource_type, resource_id
|
"SELECT id, tenant, created_at, activity_type, resource_type, resource_id
|
||||||
|
|||||||
@@ -2,19 +2,14 @@ import { For, Show } from "solid-js"
|
|||||||
import type { Activity } from "@/lib/hooks"
|
import type { Activity } from "@/lib/hooks"
|
||||||
|
|
||||||
const ACTIVITY_LABELS: Record<string, string> = {
|
const ACTIVITY_LABELS: Record<string, string> = {
|
||||||
create_relay: "Relay created",
|
create_relay: "Relay created",
|
||||||
update_relay: "Relay updated",
|
update_relay: "Relay updated",
|
||||||
deactivate_relay: "Relay deactivated",
|
deactivate_relay: "Relay deactivated",
|
||||||
fail_relay_sync: "Relay sync failed",
|
activate_relay: "Relay activated",
|
||||||
create_tenant: "Account created",
|
fail_relay_sync: "Relay sync failed",
|
||||||
update_tenant_billing_anchor: "Billing anchor updated",
|
complete_relay_sync: "Relay sync completed",
|
||||||
update_tenant_nwc_url: "Wallet connection updated",
|
create_tenant: "Account created",
|
||||||
create_invoice: "Invoice created",
|
update_tenant: "Account updated",
|
||||||
mark_invoice_paid: "Invoice paid",
|
|
||||||
mark_invoice_attempted: "Invoice payment attempted",
|
|
||||||
mark_invoice_sent: "Invoice sent",
|
|
||||||
mark_invoice_closed: "Invoice closed",
|
|
||||||
mark_relay_synced: "Relay synchronized",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatDate(ts: number) {
|
function formatDate(ts: number) {
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import { A, useLocation } from "@solidjs/router"
|
import { A, useLocation } from "@solidjs/router"
|
||||||
import { createEffect, createMemo, createSignal, For, onCleanup, Show } from "solid-js"
|
import { createEffect, createMemo, createSignal, For, onCleanup, Show } from "solid-js"
|
||||||
import Fuse from "fuse.js"
|
import Fuse from "fuse.js"
|
||||||
import { primeProfiles, useProfilePicture, useTenantRelays, type Relay } from "@/lib/hooks"
|
import { primeProfiles, useProfilePicture, useTenant, useTenantRelays, type Relay } from "@/lib/hooks"
|
||||||
|
import { listTenantInvoices, type Invoice } from "@/lib/api"
|
||||||
import { account, eventStore, identity } from "@/lib/state"
|
import { account, eventStore, identity } from "@/lib/state"
|
||||||
import serverIcon from "@/assets/server.svg"
|
import serverIcon from "@/assets/server.svg"
|
||||||
import Modal from "@/components/Modal"
|
import Modal from "@/components/Modal"
|
||||||
|
import PaymentDialog from "@/components/PaymentDialog"
|
||||||
|
|
||||||
type Profile = {
|
type Profile = {
|
||||||
name?: string
|
name?: string
|
||||||
@@ -33,10 +35,28 @@ function RelayIcon() {
|
|||||||
export default function AppShell(props: { children?: any }) {
|
export default function AppShell(props: { children?: any }) {
|
||||||
const location = useLocation()
|
const location = useLocation()
|
||||||
const picture = useProfilePicture(() => account()?.pubkey)
|
const picture = useProfilePicture(() => account()?.pubkey)
|
||||||
|
const [tenant] = useTenant()
|
||||||
const [tenantRelays] = useTenantRelays()
|
const [tenantRelays] = useTenantRelays()
|
||||||
const [profile, setProfile] = createSignal<Profile>({})
|
const [profile, setProfile] = createSignal<Profile>({})
|
||||||
const [searchOpen, setSearchOpen] = createSignal(false)
|
const [searchOpen, setSearchOpen] = createSignal(false)
|
||||||
const [searchQuery, setSearchQuery] = createSignal("")
|
const [searchQuery, setSearchQuery] = createSignal("")
|
||||||
|
const [pastDueInvoice, setPastDueInvoice] = createSignal<Invoice | undefined>()
|
||||||
|
const [showPaymentDialog, setShowPaymentDialog] = createSignal(false)
|
||||||
|
|
||||||
|
createEffect(async () => {
|
||||||
|
const t = tenant()
|
||||||
|
if (!t?.past_due_at) {
|
||||||
|
setPastDueInvoice(undefined)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const invoices = await listTenantInvoices(t.pubkey)
|
||||||
|
const openInvoice = invoices.find(inv => inv.status === "open")
|
||||||
|
setPastDueInvoice(openInvoice)
|
||||||
|
} catch {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const username = createMemo(() => profile().name || profile().display_name || shortenPubkey(account()?.pubkey))
|
const username = createMemo(() => profile().name || profile().display_name || shortenPubkey(account()?.pubkey))
|
||||||
const nip05 = createMemo(() => profile().nip05 || "No NIP-05")
|
const nip05 = createMemo(() => profile().nip05 || "No NIP-05")
|
||||||
@@ -138,9 +158,36 @@ export default function AppShell(props: { children?: any }) {
|
|||||||
</aside>
|
</aside>
|
||||||
|
|
||||||
<div class="md:pl-[260px] min-h-screen pb-20 md:pb-0">
|
<div class="md:pl-[260px] min-h-screen pb-20 md:pb-0">
|
||||||
|
<Show when={tenant()?.past_due_at}>
|
||||||
|
<div class="bg-red-600 text-white px-4 py-3 text-sm flex items-center justify-between">
|
||||||
|
<span>Your account has an overdue balance.</span>
|
||||||
|
<Show when={pastDueInvoice()}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setShowPaymentDialog(true)}
|
||||||
|
class="font-medium underline hover:no-underline"
|
||||||
|
>
|
||||||
|
Pay now
|
||||||
|
</button>
|
||||||
|
</Show>
|
||||||
|
</div>
|
||||||
|
</Show>
|
||||||
<main>{props.children}</main>
|
<main>{props.children}</main>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<Show when={pastDueInvoice() && showPaymentDialog()}>
|
||||||
|
{(_) => {
|
||||||
|
const invoice = pastDueInvoice()!
|
||||||
|
return (
|
||||||
|
<PaymentDialog
|
||||||
|
invoice={invoice}
|
||||||
|
open={true}
|
||||||
|
onClose={() => setShowPaymentDialog(false)}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
</Show>
|
||||||
|
|
||||||
<nav
|
<nav
|
||||||
class="fixed inset-x-0 bottom-0 z-20 border-t border-gray-200 bg-white md:hidden"
|
class="fixed inset-x-0 bottom-0 z-20 border-t border-gray-200 bg-white md:hidden"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
|||||||
@@ -1,31 +1,44 @@
|
|||||||
import { createEffect, createSignal, Show } from "solid-js"
|
import { createEffect, createSignal, Show } from "solid-js"
|
||||||
import QRCode from "qrcode"
|
import QRCode from "qrcode"
|
||||||
import Modal from "@/components/Modal"
|
import Modal from "@/components/Modal"
|
||||||
import { getInvoice, type Invoice } from "@/lib/api"
|
import PaymentSetup from "@/components/PaymentSetup"
|
||||||
|
import { getInvoice, getInvoiceBolt11 } from "@/lib/api"
|
||||||
|
import { tenantNeedsPaymentSetup } from "@/lib/hooks"
|
||||||
|
|
||||||
type Tab = "bitcoin" | "card"
|
|
||||||
type PayStatus = "idle" | "loading" | "success" | "error"
|
type PayStatus = "idle" | "loading" | "success" | "error"
|
||||||
|
|
||||||
|
type PaymentInvoice = {
|
||||||
|
id: string
|
||||||
|
amount_due: number
|
||||||
|
}
|
||||||
|
|
||||||
type PaymentDialogProps = {
|
type PaymentDialogProps = {
|
||||||
invoice: Invoice
|
invoice: PaymentInvoice
|
||||||
open: boolean
|
open: boolean
|
||||||
onClose: () => void
|
onClose: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function PaymentDialog(props: PaymentDialogProps) {
|
export default function PaymentDialog(props: PaymentDialogProps) {
|
||||||
const [tab, setTab] = createSignal<Tab>("bitcoin")
|
const [bolt11, setBolt11] = createSignal("")
|
||||||
const [qrDataUrl, setQrDataUrl] = createSignal("")
|
const [qrDataUrl, setQrDataUrl] = createSignal("")
|
||||||
const [payStatus, setPayStatus] = createSignal<PayStatus>("idle")
|
const [payStatus, setPayStatus] = createSignal<PayStatus>("idle")
|
||||||
const [payError, setPayError] = createSignal("")
|
const [payError, setPayError] = createSignal("")
|
||||||
|
const [showSetup, setShowSetup] = createSignal(false)
|
||||||
|
const [showPaymentSetup, setShowPaymentSetup] = createSignal(false)
|
||||||
|
|
||||||
createEffect(async () => {
|
createEffect(async () => {
|
||||||
const bolt11 = props.invoice?.bolt11
|
if (!props.open || !props.invoice.id) return
|
||||||
if (!bolt11) return
|
try {
|
||||||
setQrDataUrl(await QRCode.toDataURL(bolt11, { width: 256, margin: 2 }))
|
const { bolt11: invoice } = await getInvoiceBolt11(props.invoice.id)
|
||||||
|
setBolt11(invoice)
|
||||||
|
setQrDataUrl(await QRCode.toDataURL(invoice, { width: 256, margin: 2 }))
|
||||||
|
} catch {
|
||||||
|
// bolt11 generation may fail
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
function copyBolt11() {
|
function copyBolt11() {
|
||||||
void navigator.clipboard.writeText(props.invoice.bolt11)
|
void navigator.clipboard.writeText(bolt11())
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkPayment() {
|
async function checkPayment() {
|
||||||
@@ -35,6 +48,7 @@ export default function PaymentDialog(props: PaymentDialogProps) {
|
|||||||
const invoice = await getInvoice(props.invoice.id)
|
const invoice = await getInvoice(props.invoice.id)
|
||||||
if (invoice.status === "paid") {
|
if (invoice.status === "paid") {
|
||||||
setPayStatus("success")
|
setPayStatus("success")
|
||||||
|
tenantNeedsPaymentSetup().then(needs => setShowSetup(needs)).catch(() => {})
|
||||||
} else {
|
} else {
|
||||||
setPayStatus("error")
|
setPayStatus("error")
|
||||||
setPayError("Payment not yet confirmed. Please try again after sending.")
|
setPayError("Payment not yet confirmed. Please try again after sending.")
|
||||||
@@ -48,18 +62,16 @@ export default function PaymentDialog(props: PaymentDialogProps) {
|
|||||||
function handleClose() {
|
function handleClose() {
|
||||||
setPayStatus("idle")
|
setPayStatus("idle")
|
||||||
setPayError("")
|
setPayError("")
|
||||||
|
setBolt11("")
|
||||||
|
setQrDataUrl("")
|
||||||
|
setShowSetup(false)
|
||||||
props.onClose()
|
props.onClose()
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalSats = () => props.invoice.items.reduce((sum, item) => sum + item.sats, 0)
|
const amountLabel = () => `$${(props.invoice.amount_due / 100).toFixed(2)}`
|
||||||
|
|
||||||
const periodLabel = () => {
|
|
||||||
const start = new Date(props.invoice.period_start * 1000)
|
|
||||||
const end = new Date(props.invoice.period_end * 1000)
|
|
||||||
return `${start.toLocaleDateString()} – ${end.toLocaleDateString()}`
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<>
|
||||||
<Modal
|
<Modal
|
||||||
open={props.open}
|
open={props.open}
|
||||||
onClose={handleClose}
|
onClose={handleClose}
|
||||||
@@ -71,14 +83,7 @@ export default function PaymentDialog(props: PaymentDialogProps) {
|
|||||||
<div class="flex items-start justify-between gap-3">
|
<div class="flex items-start justify-between gap-3">
|
||||||
<div>
|
<div>
|
||||||
<h2 class="text-lg font-semibold text-gray-900">Pay Invoice</h2>
|
<h2 class="text-lg font-semibold text-gray-900">Pay Invoice</h2>
|
||||||
<Show when={totalSats() > 0}>
|
<p class="text-2xl font-bold text-gray-900 mt-1">{amountLabel()}</p>
|
||||||
<p class="text-2xl font-bold text-gray-900 mt-1">
|
|
||||||
{totalSats().toLocaleString()} <span class="text-base font-normal text-gray-500">sats</span>
|
|
||||||
</p>
|
|
||||||
</Show>
|
|
||||||
<Show when={props.invoice.period_start && props.invoice.period_end}>
|
|
||||||
<p class="text-xs text-gray-500 mt-0.5">{periodLabel()}</p>
|
|
||||||
</Show>
|
|
||||||
</div>
|
</div>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
@@ -93,45 +98,24 @@ export default function PaymentDialog(props: PaymentDialogProps) {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Pay with label + Tabs */}
|
{/* Content */}
|
||||||
<div class="px-6 pt-4">
|
|
||||||
<p class="text-xs font-medium text-gray-500 uppercase tracking-wide mb-2">Pay with</p>
|
|
||||||
<div class="flex gap-2 border border-gray-200 rounded-lg p-1">
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
class={`flex-1 rounded-md px-3 py-2 text-sm transition-colors ${tab() === "bitcoin" ? "bg-gray-900 text-white" : "text-gray-700 hover:bg-gray-50"}`}
|
|
||||||
onClick={() => setTab("bitcoin")}
|
|
||||||
>
|
|
||||||
Bitcoin
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
class={`flex-1 rounded-md px-3 py-2 text-sm transition-colors ${tab() === "card" ? "bg-gray-900 text-white" : "text-gray-700 hover:bg-gray-50"}`}
|
|
||||||
onClick={() => setTab("card")}
|
|
||||||
>
|
|
||||||
Card
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Tab content */}
|
|
||||||
<div class="px-6 py-4 min-h-[240px] flex flex-col items-center justify-center">
|
<div class="px-6 py-4 min-h-[240px] flex flex-col items-center justify-center">
|
||||||
<Show when={tab() === "bitcoin"}>
|
<Show
|
||||||
<Show
|
when={payStatus() === "success"}
|
||||||
when={payStatus() === "success"}
|
fallback={
|
||||||
fallback={
|
<div class="w-full space-y-3">
|
||||||
<div class="w-full space-y-3">
|
<Show
|
||||||
<Show
|
when={qrDataUrl()}
|
||||||
when={qrDataUrl()}
|
fallback={<div class="flex items-center justify-center py-12 text-sm text-gray-400">Generating invoice...</div>}
|
||||||
fallback={<div class="flex items-center justify-center py-12 text-sm text-gray-400">Generating invoice...</div>}
|
>
|
||||||
>
|
<img src={qrDataUrl()} alt="Lightning invoice QR code" class="mx-auto rounded-lg" />
|
||||||
<img src={qrDataUrl()} alt="Lightning invoice QR code" class="mx-auto rounded-lg" />
|
</Show>
|
||||||
</Show>
|
<Show when={bolt11()}>
|
||||||
<div class="flex rounded-lg border border-gray-300">
|
<div class="flex rounded-lg border border-gray-300">
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
readOnly
|
readOnly
|
||||||
value={props.invoice.bolt11}
|
value={bolt11()}
|
||||||
class="min-w-0 flex-1 rounded-l-lg border-0 px-3 py-2 text-xs text-gray-500 bg-transparent focus:outline-none"
|
class="min-w-0 flex-1 rounded-l-lg border-0 px-3 py-2 text-xs text-gray-500 bg-transparent focus:outline-none"
|
||||||
/>
|
/>
|
||||||
<button
|
<button
|
||||||
@@ -146,31 +130,27 @@ export default function PaymentDialog(props: PaymentDialogProps) {
|
|||||||
</svg>
|
</svg>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</Show>
|
||||||
}
|
|
||||||
>
|
|
||||||
<div class="text-center">
|
|
||||||
<div class="mx-auto mb-3 flex h-12 w-12 items-center justify-center rounded-full bg-green-100">
|
|
||||||
<svg class="w-6 h-6 text-green-600" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
||||||
<polyline points="20 6 9 17 4 12" />
|
|
||||||
</svg>
|
|
||||||
</div>
|
|
||||||
<p class="text-sm font-medium text-gray-900">Payment confirmed!</p>
|
|
||||||
<p class="text-xs text-gray-500 mt-1">Thank you. Your relay is now active.</p>
|
|
||||||
</div>
|
</div>
|
||||||
</Show>
|
}
|
||||||
</Show>
|
>
|
||||||
|
<div class="text-center space-y-3">
|
||||||
<Show when={tab() === "card"}>
|
<div class="mx-auto mb-3 flex h-12 w-12 items-center justify-center rounded-full bg-green-100">
|
||||||
<div class="text-center">
|
<svg class="w-6 h-6 text-green-600" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||||
<div class="mx-auto mb-3 flex h-12 w-12 items-center justify-center rounded-full bg-gray-100">
|
<polyline points="20 6 9 17 4 12" />
|
||||||
<svg class="w-6 h-6 text-gray-400" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
||||||
<rect x="1" y="4" width="22" height="16" rx="2" ry="2" />
|
|
||||||
<line x1="1" y1="10" x2="23" y2="10" />
|
|
||||||
</svg>
|
</svg>
|
||||||
</div>
|
</div>
|
||||||
<p class="text-sm font-medium text-gray-700">Coming soon</p>
|
<p class="text-sm font-medium text-gray-900">Payment confirmed!</p>
|
||||||
<p class="text-xs text-gray-500 mt-1">Card payments are not yet available.</p>
|
<p class="text-xs text-gray-500">Thank you. Your account is up to date.</p>
|
||||||
|
<Show when={showSetup()}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setShowPaymentSetup(true)}
|
||||||
|
class="mt-2 text-sm font-medium text-blue-600 hover:text-blue-700"
|
||||||
|
>
|
||||||
|
Set up automatic payments
|
||||||
|
</button>
|
||||||
|
</Show>
|
||||||
</div>
|
</div>
|
||||||
</Show>
|
</Show>
|
||||||
</div>
|
</div>
|
||||||
@@ -216,5 +196,10 @@ export default function PaymentDialog(props: PaymentDialogProps) {
|
|||||||
</Show>
|
</Show>
|
||||||
</div>
|
</div>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
<PaymentSetup
|
||||||
|
open={showPaymentSetup()}
|
||||||
|
onClose={() => setShowPaymentSetup(false)}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,190 @@
|
|||||||
|
import { createSignal, Show } from "solid-js"
|
||||||
|
import Modal from "@/components/Modal"
|
||||||
|
import { updateActiveTenant } from "@/lib/hooks"
|
||||||
|
import { createPortalSession } from "@/lib/api"
|
||||||
|
import { account } from "@/lib/state"
|
||||||
|
|
||||||
|
type Tab = "nwc" | "card"
|
||||||
|
|
||||||
|
type PaymentSetupProps = {
|
||||||
|
open: boolean
|
||||||
|
onClose: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function PaymentSetup(props: PaymentSetupProps) {
|
||||||
|
const [tab, setTab] = createSignal<Tab>("nwc")
|
||||||
|
const [nwcUrl, setNwcUrl] = createSignal("")
|
||||||
|
const [saving, setSaving] = createSignal(false)
|
||||||
|
const [saved, setSaved] = createSignal(false)
|
||||||
|
const [error, setError] = createSignal("")
|
||||||
|
const [redirecting, setRedirecting] = createSignal(false)
|
||||||
|
|
||||||
|
async function saveNwc() {
|
||||||
|
const url = nwcUrl().trim()
|
||||||
|
if (!url) return
|
||||||
|
setSaving(true)
|
||||||
|
setError("")
|
||||||
|
try {
|
||||||
|
await updateActiveTenant({ nwc_url: url })
|
||||||
|
setSaved(true)
|
||||||
|
} catch (e) {
|
||||||
|
setError(e instanceof Error ? e.message : "Failed to save wallet connection")
|
||||||
|
} finally {
|
||||||
|
setSaving(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function openPortal() {
|
||||||
|
setRedirecting(true)
|
||||||
|
setError("")
|
||||||
|
try {
|
||||||
|
const { url } = await createPortalSession(account()!.pubkey)
|
||||||
|
window.location.href = url
|
||||||
|
} catch (e) {
|
||||||
|
setError(e instanceof Error ? e.message : "Failed to open billing portal")
|
||||||
|
setRedirecting(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleClose() {
|
||||||
|
setNwcUrl("")
|
||||||
|
setSaved(false)
|
||||||
|
setError("")
|
||||||
|
props.onClose()
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal
|
||||||
|
open={props.open}
|
||||||
|
onClose={handleClose}
|
||||||
|
wrapperClass="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4"
|
||||||
|
panelClass="w-full max-w-md rounded-2xl bg-white shadow-xl overflow-hidden"
|
||||||
|
>
|
||||||
|
<div class="px-6 pt-6 pb-4 border-b border-gray-100">
|
||||||
|
<div class="flex items-start justify-between gap-3">
|
||||||
|
<div>
|
||||||
|
<h2 class="text-lg font-semibold text-gray-900">Set Up Payments</h2>
|
||||||
|
<p class="text-sm text-gray-500 mt-1">Choose how you'd like to pay for your relay.</p>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleClose}
|
||||||
|
class="text-gray-400 hover:text-gray-700 rounded p-1 hover:bg-gray-100 flex-shrink-0"
|
||||||
|
aria-label="Close"
|
||||||
|
>
|
||||||
|
<svg class="w-5 h-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<path d="M18 6L6 18M6 6l12 12" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="px-6 pt-4">
|
||||||
|
<p class="text-xs font-medium text-gray-500 uppercase tracking-wide mb-2">Pay with</p>
|
||||||
|
<div class="flex gap-2 border border-gray-200 rounded-lg p-1">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class={`flex-1 rounded-md px-3 py-2 text-sm transition-colors ${tab() === "nwc" ? "bg-gray-900 text-white" : "text-gray-700 hover:bg-gray-50"}`}
|
||||||
|
onClick={() => setTab("nwc")}
|
||||||
|
>
|
||||||
|
Lightning (NWC)
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class={`flex-1 rounded-md px-3 py-2 text-sm transition-colors ${tab() === "card" ? "bg-gray-900 text-white" : "text-gray-700 hover:bg-gray-50"}`}
|
||||||
|
onClick={() => setTab("card")}
|
||||||
|
>
|
||||||
|
Card
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="px-6 py-4 min-h-[180px] flex flex-col justify-center">
|
||||||
|
<Show when={tab() === "nwc"}>
|
||||||
|
<Show
|
||||||
|
when={!saved()}
|
||||||
|
fallback={
|
||||||
|
<div class="text-center">
|
||||||
|
<div class="mx-auto mb-3 flex h-12 w-12 items-center justify-center rounded-full bg-green-100">
|
||||||
|
<svg class="w-6 h-6 text-green-600" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<polyline points="20 6 9 17 4 12" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<p class="text-sm font-medium text-gray-900">Wallet connected!</p>
|
||||||
|
<p class="text-xs text-gray-500 mt-1">Automatic payments are now enabled.</p>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<div class="space-y-3">
|
||||||
|
<label class="block text-sm font-medium text-gray-700">Nostr Wallet Connect URL</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={nwcUrl()}
|
||||||
|
onInput={(e) => setNwcUrl(e.currentTarget.value)}
|
||||||
|
placeholder="nostr+walletconnect://..."
|
||||||
|
class="w-full border border-gray-300 rounded-lg px-3 py-2"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={saveNwc}
|
||||||
|
disabled={saving() || !nwcUrl().trim()}
|
||||||
|
class="w-full py-2 px-4 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 disabled:opacity-50 transition-colors"
|
||||||
|
>
|
||||||
|
{saving() ? "Saving..." : "Save"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</Show>
|
||||||
|
</Show>
|
||||||
|
|
||||||
|
<Show when={tab() === "card"}>
|
||||||
|
<div class="text-center space-y-4">
|
||||||
|
<div class="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-gray-100">
|
||||||
|
<svg class="w-6 h-6 text-gray-400" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
<rect x="1" y="4" width="22" height="16" rx="2" ry="2" />
|
||||||
|
<line x1="1" y1="10" x2="23" y2="10" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<p class="text-sm text-gray-600">Add a payment card via Stripe to enable automatic billing.</p>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={openPortal}
|
||||||
|
disabled={redirecting()}
|
||||||
|
class="w-full py-2 px-4 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 disabled:opacity-50 transition-colors"
|
||||||
|
>
|
||||||
|
{redirecting() ? "Redirecting..." : "Add a payment card"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</Show>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Show when={error()}>
|
||||||
|
<div class="px-6 pb-2">
|
||||||
|
<p class="text-xs text-red-600">{error()}</p>
|
||||||
|
</div>
|
||||||
|
</Show>
|
||||||
|
|
||||||
|
<div class="px-6 py-4 border-t border-gray-100">
|
||||||
|
<Show when={saved()}>
|
||||||
|
<div class="flex justify-end">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleClose}
|
||||||
|
class="py-2 px-4 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700"
|
||||||
|
>
|
||||||
|
Done
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</Show>
|
||||||
|
<Show when={!saved()}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleClose}
|
||||||
|
class="py-2 px-4 border border-gray-300 text-gray-700 text-sm rounded-lg hover:bg-gray-50 transition-colors"
|
||||||
|
>
|
||||||
|
Set up later
|
||||||
|
</button>
|
||||||
|
</Show>
|
||||||
|
</div>
|
||||||
|
</Modal>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -20,10 +20,9 @@ function XIcon() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function priceLabel(sats: number) {
|
function priceLabel(amount: number) {
|
||||||
if (sats === 0) return "0"
|
if (amount === 0) return "Free"
|
||||||
if (sats >= 1000) return `${(sats / 1000).toLocaleString()}K`
|
return `$${amount / 100}`
|
||||||
return sats.toLocaleString()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function memberLabel(members: number | null) {
|
function memberLabel(members: number | null) {
|
||||||
@@ -55,8 +54,8 @@ export default function PricingTable(props: PricingTableProps) {
|
|||||||
)}
|
)}
|
||||||
<h3 class="text-lg font-bold text-gray-900 mb-1">{plan.name}</h3>
|
<h3 class="text-lg font-bold text-gray-900 mb-1">{plan.name}</h3>
|
||||||
<div class="mb-8">
|
<div class="mb-8">
|
||||||
<span class="text-4xl font-extrabold text-gray-900">{priceLabel(plan.sats)}</span>
|
<span class="text-4xl font-extrabold text-gray-900">{priceLabel(plan.amount)}</span>
|
||||||
<span class="text-sm text-gray-400 ml-1">sats / mo</span>
|
<span class="text-sm text-gray-400 ml-1">/ mo</span>
|
||||||
</div>
|
</div>
|
||||||
<ul class="mb-8 text-sm text-gray-600 space-y-3">
|
<ul class="mb-8 text-sm text-gray-600 space-y-3">
|
||||||
<li class="flex items-start gap-2"><CheckIcon />{memberLabel(plan.members)}</li>
|
<li class="flex items-start gap-2"><CheckIcon />{memberLabel(plan.members)}</li>
|
||||||
|
|||||||
@@ -10,12 +10,8 @@ import { setToastMessage } from "@/components/Toast"
|
|||||||
import { plans } from "@/lib/state"
|
import { plans } from "@/lib/state"
|
||||||
|
|
||||||
const STATUS_STYLES: Record<string, string> = {
|
const STATUS_STYLES: Record<string, string> = {
|
||||||
active: "bg-green-50 text-green-700 border-green-200",
|
active: "bg-green-50 text-green-700 border-green-200",
|
||||||
new: "bg-blue-50 text-blue-700 border-blue-200",
|
inactive: "bg-gray-100 text-gray-500 border-gray-200",
|
||||||
pending: "bg-yellow-50 text-yellow-700 border-yellow-200",
|
|
||||||
provisioning_failed: "bg-red-50 text-red-700 border-red-200",
|
|
||||||
deactivated: "bg-gray-100 text-gray-500 border-gray-200",
|
|
||||||
suspended: "bg-orange-50 text-orange-700 border-orange-200",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function StatusBadge(props: { status: string }) {
|
function StatusBadge(props: { status: string }) {
|
||||||
@@ -56,7 +52,9 @@ type RelayDetailCardProps = {
|
|||||||
showTenant?: boolean
|
showTenant?: boolean
|
||||||
editHref?: string
|
editHref?: string
|
||||||
onDeactivate?: () => void
|
onDeactivate?: () => void
|
||||||
|
onReactivate?: () => void
|
||||||
deactivating?: boolean
|
deactivating?: boolean
|
||||||
|
reactivating?: boolean
|
||||||
onTogglePublicJoin?: () => void
|
onTogglePublicJoin?: () => void
|
||||||
onToggleStripSignatures?: () => void
|
onToggleStripSignatures?: () => void
|
||||||
onToggleGroups?: () => void
|
onToggleGroups?: () => void
|
||||||
@@ -149,7 +147,7 @@ export default function RelayDetailCard(props: RelayDetailCardProps) {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Show when={props.editHref && props.onDeactivate}>
|
<Show when={props.editHref && (props.onDeactivate || props.onReactivate)}>
|
||||||
<div class="relative flex-shrink-0" ref={menuContainerRef}>
|
<div class="relative flex-shrink-0" ref={menuContainerRef}>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
@@ -174,17 +172,32 @@ export default function RelayDetailCard(props: RelayDetailCardProps) {
|
|||||||
>
|
>
|
||||||
Edit Details
|
Edit Details
|
||||||
</A>
|
</A>
|
||||||
<button
|
<Show when={r().status === "active" && props.onDeactivate}>
|
||||||
type="button"
|
<button
|
||||||
class="block w-full text-left px-3 py-2 text-sm text-red-600 hover:bg-red-50 disabled:opacity-50"
|
type="button"
|
||||||
onClick={() => {
|
class="block w-full text-left px-3 py-2 text-sm text-red-600 hover:bg-red-50 disabled:opacity-50"
|
||||||
setMenuOpen(false)
|
onClick={() => {
|
||||||
props.onDeactivate?.()
|
setMenuOpen(false)
|
||||||
}}
|
props.onDeactivate?.()
|
||||||
disabled={props.deactivating}
|
}}
|
||||||
>
|
disabled={props.deactivating}
|
||||||
{props.deactivating ? "Deactivating..." : "Deactivate"}
|
>
|
||||||
</button>
|
{props.deactivating ? "Deactivating..." : "Deactivate"}
|
||||||
|
</button>
|
||||||
|
</Show>
|
||||||
|
<Show when={r().status === "inactive" && props.onReactivate}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="block w-full text-left px-3 py-2 text-sm text-blue-600 hover:bg-blue-50 disabled:opacity-50"
|
||||||
|
onClick={() => {
|
||||||
|
setMenuOpen(false)
|
||||||
|
props.onReactivate?.()
|
||||||
|
}}
|
||||||
|
disabled={props.reactivating}
|
||||||
|
>
|
||||||
|
{props.reactivating ? "Reactivating..." : "Reactivate"}
|
||||||
|
</button>
|
||||||
|
</Show>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</Show>
|
</Show>
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ export default function RelayForm(props: RelayFormProps) {
|
|||||||
>
|
>
|
||||||
<div class="font-bold text-gray-900">{p.name}</div>
|
<div class="font-bold text-gray-900">{p.name}</div>
|
||||||
<div class="text-sm text-gray-500 mt-1">
|
<div class="text-sm text-gray-500 mt-1">
|
||||||
{p.sats === 0 ? "Free" : `${p.sats.toLocaleString()} sats/mo`}
|
{p.amount === 0 ? "Free" : `$${p.amount / 100}/mo`}
|
||||||
</div>
|
</div>
|
||||||
<div class="text-xs text-gray-500 mt-2">
|
<div class="text-xs text-gray-500 mt-2">
|
||||||
{p.members === null ? "Unlimited members" : `Up to ${p.members} members`}
|
{p.members === null ? "Unlimited members" : `Up to ${p.members} members`}
|
||||||
|
|||||||
+28
-31
@@ -11,8 +11,8 @@ type ApiOk<T> = {
|
|||||||
code: string
|
code: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type BillingInput = {
|
type UpdateTenantInput = {
|
||||||
nwc_url: string
|
nwc_url?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthCache = {
|
type AuthCache = {
|
||||||
@@ -34,7 +34,8 @@ export class ApiError extends Error {
|
|||||||
export type Plan = {
|
export type Plan = {
|
||||||
id: string
|
id: string
|
||||||
name: string
|
name: string
|
||||||
sats: number
|
amount: number
|
||||||
|
stripe_price_id: string | null
|
||||||
members: number | null
|
members: number | null
|
||||||
blossom: boolean
|
blossom: boolean
|
||||||
livekit: boolean
|
livekit: boolean
|
||||||
@@ -50,6 +51,8 @@ export type Relay = {
|
|||||||
plan: PlanId
|
plan: PlanId
|
||||||
status: string
|
status: string
|
||||||
sync_error: string
|
sync_error: string
|
||||||
|
stripe_subscription_item_id: string | null
|
||||||
|
synced: number
|
||||||
info_name: string
|
info_name: string
|
||||||
info_icon: string
|
info_icon: string
|
||||||
info_description: string
|
info_description: string
|
||||||
@@ -97,28 +100,18 @@ export type Tenant = {
|
|||||||
pubkey: string
|
pubkey: string
|
||||||
nwc_url: string
|
nwc_url: string
|
||||||
created_at: number
|
created_at: number
|
||||||
billing_anchor: number
|
stripe_customer_id: string
|
||||||
}
|
stripe_subscription_id: string | null
|
||||||
|
past_due_at: number | null
|
||||||
export type InvoiceItem = {
|
nwc_error: string | null
|
||||||
id: string
|
|
||||||
invoice: string
|
|
||||||
relay: string
|
|
||||||
sats: number
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Invoice = {
|
export type Invoice = {
|
||||||
id: string
|
id: string
|
||||||
tenant: string
|
|
||||||
status: string
|
status: string
|
||||||
items: InvoiceItem[]
|
amount_due: number
|
||||||
created_at: number
|
currency: string
|
||||||
attempted_at: number
|
hosted_invoice_url: string
|
||||||
error: string
|
|
||||||
closed_at: number
|
|
||||||
sent_at: number
|
|
||||||
paid_at: number
|
|
||||||
bolt11: string
|
|
||||||
period_start: number
|
period_start: number
|
||||||
period_end: number
|
period_end: number
|
||||||
}
|
}
|
||||||
@@ -222,10 +215,6 @@ export function getTenant(pubkey: string) {
|
|||||||
return callApi<undefined, Tenant>("GET", `/tenants/${pubkey}`)
|
return callApi<undefined, Tenant>("GET", `/tenants/${pubkey}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createTenant() {
|
|
||||||
return callApi<undefined, Tenant>("POST", "/tenants")
|
|
||||||
}
|
|
||||||
|
|
||||||
export function listTenantRelays(pubkey: string) {
|
export function listTenantRelays(pubkey: string) {
|
||||||
return callApi<undefined, Relay[]>("GET", `/tenants/${pubkey}/relays`)
|
return callApi<undefined, Relay[]>("GET", `/tenants/${pubkey}/relays`)
|
||||||
}
|
}
|
||||||
@@ -234,8 +223,8 @@ export function listTenantInvoices(pubkey: string) {
|
|||||||
return callApi<undefined, Invoice[]>("GET", `/tenants/${pubkey}/invoices`)
|
return callApi<undefined, Invoice[]>("GET", `/tenants/${pubkey}/invoices`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateTenantBilling(pubkey: string, billing: BillingInput) {
|
export function updateTenant(pubkey: string, input: UpdateTenantInput) {
|
||||||
return callApi<BillingInput, BillingInput>("PUT", `/tenants/${pubkey}/billing`, billing)
|
return callApi<UpdateTenantInput, Tenant>("PUT", `/tenants/${pubkey}`, input)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function listRelays() {
|
export function listRelays() {
|
||||||
@@ -247,7 +236,19 @@ export function getRelay(id: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function listRelayActivity(id: string) {
|
export function listRelayActivity(id: string) {
|
||||||
return callApi<undefined, Activity[]>("GET", `/relays/${id}/activity`)
|
return callApi<undefined, { activity: Activity[] }>("GET", `/relays/${id}/activity`)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function reactivateRelay(id: string) {
|
||||||
|
return callApi<undefined, void>("POST", `/relays/${id}/reactivate`)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createPortalSession(pubkey: string) {
|
||||||
|
return callApi<undefined, { url: string }>("GET", `/tenants/${pubkey}/stripe/session`)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getInvoiceBolt11(invoiceId: string) {
|
||||||
|
return callApi<undefined, { bolt11: string }>("GET", `/invoices/${invoiceId}/bolt11`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createRelay(input: CreateRelayInput) {
|
export function createRelay(input: CreateRelayInput) {
|
||||||
@@ -262,10 +263,6 @@ export function deactivateRelay(id: string) {
|
|||||||
return callApi<undefined, void>("POST", `/relays/${id}/deactivate`)
|
return callApi<undefined, void>("POST", `/relays/${id}/deactivate`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function listInvoices() {
|
|
||||||
return callApi<undefined, Invoice[]>("GET", "/invoices")
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getInvoice(id: string) {
|
export function getInvoice(id: string) {
|
||||||
return callApi<undefined, Invoice>("GET", `/invoices/${id}`)
|
return callApi<undefined, Invoice>("GET", `/invoices/${id}`)
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-11
@@ -7,18 +7,17 @@ import { map, of } from "rxjs"
|
|||||||
import {
|
import {
|
||||||
createRelay,
|
createRelay,
|
||||||
deactivateRelay,
|
deactivateRelay,
|
||||||
|
reactivateRelay,
|
||||||
getRelay,
|
getRelay,
|
||||||
getTenant,
|
getTenant,
|
||||||
listRelayActivity,
|
listRelayActivity,
|
||||||
listRelays,
|
listRelays,
|
||||||
listTenantInvoices,
|
|
||||||
listTenantRelays,
|
listTenantRelays,
|
||||||
listTenants,
|
listTenants,
|
||||||
updateRelay,
|
updateRelay,
|
||||||
updateTenantBilling,
|
updateTenant,
|
||||||
type Activity,
|
type Activity,
|
||||||
type CreateRelayInput,
|
type CreateRelayInput,
|
||||||
type Invoice,
|
|
||||||
type Relay,
|
type Relay,
|
||||||
type Tenant,
|
type Tenant,
|
||||||
type UpdateRelayInput,
|
type UpdateRelayInput,
|
||||||
@@ -87,11 +86,12 @@ export const useTenant = () => createResource(() => getTenant(account()!.pubkey)
|
|||||||
|
|
||||||
export const useTenantRelays = () => createResource(() => listTenantRelays(account()!.pubkey))
|
export const useTenantRelays = () => createResource(() => listTenantRelays(account()!.pubkey))
|
||||||
|
|
||||||
export const useTenantInvoices = () => createResource(() => listTenantInvoices(account()!.pubkey))
|
|
||||||
|
|
||||||
export const useRelay = (relayId: () => string) => createResource(relayId, getRelay)
|
export const useRelay = (relayId: () => string) => createResource(relayId, getRelay)
|
||||||
|
|
||||||
export const useRelayActivity = (relayId: () => string) => createResource(relayId, listRelayActivity)
|
export const useRelayActivity = (relayId: () => string) => createResource(relayId, async (id) => {
|
||||||
|
const result = await listRelayActivity(id)
|
||||||
|
return result.activity
|
||||||
|
})
|
||||||
|
|
||||||
export const useAdminTenants = () => createResource(listTenants)
|
export const useAdminTenants = () => createResource(listTenants)
|
||||||
|
|
||||||
@@ -122,7 +122,7 @@ export const createRelayForActiveTenant = (input: CreateRelayInput) => {
|
|||||||
return createRelay({...defaults, ...input, ...overrides})
|
return createRelay({...defaults, ...input, ...overrides})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const updateActiveTenantBilling = (nwc_url: string) => updateTenantBilling(account()!.pubkey, { nwc_url })
|
export const updateActiveTenant = (input: { nwc_url?: string }) => updateTenant(account()!.pubkey, input)
|
||||||
|
|
||||||
export const updateRelayById = (id: string, input: UpdateRelayInput) => updateRelay(id, input)
|
export const updateRelayById = (id: string, input: UpdateRelayInput) => updateRelay(id, input)
|
||||||
|
|
||||||
@@ -130,9 +130,11 @@ export const updateRelayPlanById = (id: string, plan: string) => updateRelay(id,
|
|||||||
|
|
||||||
export const deactivateRelayById = (id: string) => deactivateRelay(id)
|
export const deactivateRelayById = (id: string) => deactivateRelay(id)
|
||||||
|
|
||||||
export async function checkPendingInvoice(): Promise<Invoice | undefined> {
|
export const reactivateRelayById = (id: string) => reactivateRelay(id)
|
||||||
const invoices = await listTenantInvoices(account()!.pubkey)
|
|
||||||
return invoices.find(inv => inv.status === "pending")
|
export async function tenantNeedsPaymentSetup(): Promise<boolean> {
|
||||||
|
const tenant = await getTenant(account()!.pubkey)
|
||||||
|
return !tenant.nwc_url && !tenant.stripe_subscription_id
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getRelayMembers(url: string) {
|
export async function getRelayMembers(url: string) {
|
||||||
@@ -145,4 +147,4 @@ export async function getRelayMembers(url: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type { Activity, Invoice, Relay, Tenant }
|
export type { Activity, Relay, Tenant }
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { createSignal } from "solid-js"
|
import { createSignal } from "solid-js"
|
||||||
import { updateRelayById, deactivateRelayById, checkPendingInvoice, type Invoice, type Relay } from "@/lib/hooks"
|
import { updateRelayById, deactivateRelayById, reactivateRelayById, tenantNeedsPaymentSetup, type Relay } from "@/lib/hooks"
|
||||||
import { setToastMessage } from "@/components/Toast"
|
import { setToastMessage } from "@/components/Toast"
|
||||||
import type { PlanId } from "@/lib/api"
|
import type { PlanId } from "@/lib/api"
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ export default function useRelayToggles(
|
|||||||
{ refetch, mutate }: RelayActions,
|
{ refetch, mutate }: RelayActions,
|
||||||
) {
|
) {
|
||||||
const [busy, setBusy] = createSignal(false)
|
const [busy, setBusy] = createSignal(false)
|
||||||
const [pendingInvoice, setPendingInvoice] = createSignal<Invoice | undefined>()
|
const [needsPaymentSetup, setNeedsPaymentSetup] = createSignal(false)
|
||||||
|
|
||||||
async function updateRelay(next: Relay, previous: Relay) {
|
async function updateRelay(next: Relay, previous: Relay) {
|
||||||
mutate(next)
|
mutate(next)
|
||||||
@@ -63,6 +63,19 @@ export default function useRelayToggles(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleReactivate() {
|
||||||
|
if (busy()) return
|
||||||
|
setBusy(true)
|
||||||
|
try {
|
||||||
|
await reactivateRelayById(relayId())
|
||||||
|
await refetch()
|
||||||
|
} catch (e) {
|
||||||
|
setToastMessage(e instanceof Error ? e.message : "Failed to reactivate relay")
|
||||||
|
} finally {
|
||||||
|
setBusy(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function handleUpdatePlan(plan: PlanId) {
|
async function handleUpdatePlan(plan: PlanId) {
|
||||||
const current = relay()
|
const current = relay()
|
||||||
if (!current) return
|
if (!current) return
|
||||||
@@ -88,8 +101,8 @@ export default function useRelayToggles(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (plan !== "free") {
|
if (plan !== "free") {
|
||||||
const invoice = await checkPendingInvoice()
|
const needs = await tenantNeedsPaymentSetup()
|
||||||
if (invoice) setPendingInvoice(invoice)
|
if (needs) setNeedsPaymentSetup(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,5 +116,5 @@ export default function useRelayToggles(
|
|||||||
onToggleLivekitSupport: () => toggle("livekit_enabled", relay()?.plan !== "free"),
|
onToggleLivekitSupport: () => toggle("livekit_enabled", relay()?.plan !== "free"),
|
||||||
}
|
}
|
||||||
|
|
||||||
return { busy, handleDeactivate, handleUpdatePlan, pendingInvoice, clearPendingInvoice: () => setPendingInvoice(undefined), toggles }
|
return { busy, handleDeactivate, handleReactivate, handleUpdatePlan, needsPaymentSetup, clearNeedsPaymentSetup: () => setNeedsPaymentSetup(false), toggles }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,20 @@
|
|||||||
import { createEffect, createMemo, createSignal, For, Show } from "solid-js"
|
import { createEffect, createMemo, createResource, createSignal, For, Show } from "solid-js"
|
||||||
import PageContainer from "@/components/PageContainer"
|
import PageContainer from "@/components/PageContainer"
|
||||||
import LoadingState from "@/components/LoadingState"
|
import LoadingState from "@/components/LoadingState"
|
||||||
import PaymentDialog from "@/components/PaymentDialog"
|
import PaymentDialog from "@/components/PaymentDialog"
|
||||||
import useMinLoading from "@/components/useMinLoading"
|
import useMinLoading from "@/components/useMinLoading"
|
||||||
import { updateActiveTenantBilling, useTenant, useTenantInvoices, type Invoice } from "@/lib/hooks"
|
import { updateActiveTenant, useTenant } from "@/lib/hooks"
|
||||||
|
import { createPortalSession, listTenantInvoices, type Invoice } from "@/lib/api"
|
||||||
|
import { account } from "@/lib/state"
|
||||||
|
|
||||||
export default function Account() {
|
export default function Account() {
|
||||||
const [tenant, { refetch: refetchTenant }] = useTenant()
|
const [tenant, { refetch: refetchTenant }] = useTenant()
|
||||||
const [invoices, { refetch: refetchInvoices }] = useTenantInvoices()
|
const [invoices, { refetch: refetchInvoices }] = createResource(() => listTenantInvoices(account()!.pubkey))
|
||||||
const [nwcUrl, setNwcUrl] = createSignal("")
|
const [nwcUrl, setNwcUrl] = createSignal("")
|
||||||
const [saving, setSaving] = createSignal(false)
|
const [saving, setSaving] = createSignal(false)
|
||||||
const [error, setError] = createSignal("")
|
const [error, setError] = createSignal("")
|
||||||
const [selectedInvoice, setSelectedInvoice] = createSignal<Invoice | undefined>()
|
const [selectedInvoice, setSelectedInvoice] = createSignal<Invoice | undefined>()
|
||||||
|
const [portalLoading, setPortalLoading] = createSignal(false)
|
||||||
const invoicesLoading = useMinLoading(() => invoices.loading)
|
const invoicesLoading = useMinLoading(() => invoices.loading)
|
||||||
|
|
||||||
const hasBillingChanges = createMemo(() => {
|
const hasBillingChanges = createMemo(() => {
|
||||||
@@ -29,7 +32,7 @@ export default function Account() {
|
|||||||
setSaving(true)
|
setSaving(true)
|
||||||
try {
|
try {
|
||||||
const next = nwcUrl().trim()
|
const next = nwcUrl().trim()
|
||||||
await updateActiveTenantBilling(next)
|
await updateActiveTenant({ nwc_url: next })
|
||||||
await refetchTenant()
|
await refetchTenant()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setError(e instanceof Error ? e.message : "Failed to update billing")
|
setError(e instanceof Error ? e.message : "Failed to update billing")
|
||||||
@@ -43,15 +46,29 @@ export default function Account() {
|
|||||||
void refetchInvoices()
|
void refetchInvoices()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function openPortal() {
|
||||||
|
setPortalLoading(true)
|
||||||
|
try {
|
||||||
|
const { url } = await createPortalSession(account()!.pubkey)
|
||||||
|
window.location.href = url
|
||||||
|
} catch (e) {
|
||||||
|
setError(e instanceof Error ? e.message : "Failed to open billing portal")
|
||||||
|
} finally {
|
||||||
|
setPortalLoading(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
localStorage.clear()
|
localStorage.clear()
|
||||||
window.location.href = "/"
|
window.location.href = "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
const invoiceStatusStyles: Record<string, string> = {
|
const invoiceStatusStyles: Record<string, string> = {
|
||||||
pending: "bg-yellow-50 text-yellow-700 border-yellow-200",
|
draft: "bg-gray-100 text-gray-500 border-gray-200",
|
||||||
|
open: "bg-yellow-50 text-yellow-700 border-yellow-200",
|
||||||
paid: "bg-green-50 text-green-700 border-green-200",
|
paid: "bg-green-50 text-green-700 border-green-200",
|
||||||
closed: "bg-gray-100 text-gray-500 border-gray-200",
|
void: "bg-gray-100 text-gray-500 border-gray-200",
|
||||||
|
uncollectible: "bg-red-50 text-red-700 border-red-200",
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -80,7 +97,17 @@ export default function Account() {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section class="bg-white border border-gray-200 rounded-xl p-6">
|
<section class="bg-white border border-gray-200 rounded-xl p-6">
|
||||||
<h2 class="text-lg font-semibold text-gray-900 mb-4">Recurring Billing</h2>
|
<div class="flex items-center justify-between gap-3 mb-4">
|
||||||
|
<h2 class="text-lg font-semibold text-gray-900">Recurring Billing</h2>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={openPortal}
|
||||||
|
disabled={portalLoading()}
|
||||||
|
class="text-sm font-medium text-blue-600 hover:text-blue-700 disabled:opacity-50"
|
||||||
|
>
|
||||||
|
{portalLoading() ? "Loading..." : "Manage Billing"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
<p class="text-sm text-gray-600 mb-4">
|
<p class="text-sm text-gray-600 mb-4">
|
||||||
Enable automatic payments by providing your Nostr Wallet Connect URL.
|
Enable automatic payments by providing your Nostr Wallet Connect URL.
|
||||||
</p>
|
</p>
|
||||||
@@ -101,6 +128,9 @@ export default function Account() {
|
|||||||
{saving() ? "Saving..." : "Save"}
|
{saving() ? "Saving..." : "Save"}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
<Show when={tenant()?.nwc_error}>
|
||||||
|
<p class="mt-3 text-sm text-red-600">{tenant()!.nwc_error}</p>
|
||||||
|
</Show>
|
||||||
<Show when={error()}>
|
<Show when={error()}>
|
||||||
<p class="mt-3 text-sm text-red-600">{error()}</p>
|
<p class="mt-3 text-sm text-red-600">{error()}</p>
|
||||||
</Show>
|
</Show>
|
||||||
@@ -116,7 +146,7 @@ export default function Account() {
|
|||||||
<ul class="space-y-3">
|
<ul class="space-y-3">
|
||||||
<For each={invoices()}>
|
<For each={invoices()}>
|
||||||
{(invoice) => {
|
{(invoice) => {
|
||||||
const isPending = () => invoice.status === "pending"
|
const isOpen = () => invoice.status === "open"
|
||||||
const statusStyle = () => invoiceStatusStyles[invoice.status] ?? "bg-gray-100 text-gray-500 border-gray-200"
|
const statusStyle = () => invoiceStatusStyles[invoice.status] ?? "bg-gray-100 text-gray-500 border-gray-200"
|
||||||
const periodLabel = () => {
|
const periodLabel = () => {
|
||||||
const start = new Date(invoice.period_start * 1000)
|
const start = new Date(invoice.period_start * 1000)
|
||||||
@@ -126,33 +156,28 @@ export default function Account() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<li
|
<li
|
||||||
class={`rounded-lg border border-gray-200 p-4 text-sm ${isPending() ? "cursor-pointer hover:border-blue-300 hover:bg-blue-50 transition-colors" : ""}`}
|
class={`rounded-lg border border-gray-200 p-4 text-sm ${isOpen() ? "cursor-pointer hover:border-blue-300 hover:bg-blue-50 transition-colors" : ""}`}
|
||||||
onClick={() => isPending() && setSelectedInvoice(invoice)}
|
onClick={() => isOpen() && setSelectedInvoice(invoice)}
|
||||||
title={isPending() ? "Click to pay this invoice" : undefined}
|
title={isOpen() ? "Click to pay this invoice" : undefined}
|
||||||
>
|
>
|
||||||
<div class="flex items-center justify-between gap-3">
|
<div class="flex items-center justify-between gap-3">
|
||||||
<div>
|
<div>
|
||||||
<span class="font-medium text-gray-900">
|
<span class="font-medium text-gray-900">
|
||||||
{invoice.items.length > 0
|
${(invoice.amount_due / 100).toFixed(2)}
|
||||||
? `${invoice.items.reduce((sum, item) => sum + item.sats, 0).toLocaleString()} sats`
|
|
||||||
: "—"}
|
|
||||||
</span>
|
</span>
|
||||||
<Show when={invoice.period_start && invoice.period_end}>
|
<Show when={invoice.period_start && invoice.period_end}>
|
||||||
<p class="text-xs text-gray-500 mt-0.5">{periodLabel()}</p>
|
<p class="text-xs text-gray-500 mt-0.5">{periodLabel()}</p>
|
||||||
</Show>
|
</Show>
|
||||||
</div>
|
</div>
|
||||||
<div class="flex items-center gap-2 flex-shrink-0">
|
<div class="flex items-center gap-2 flex-shrink-0">
|
||||||
<Show when={isPending()}>
|
<Show when={isOpen()}>
|
||||||
<span class="text-xs text-blue-600 font-medium">Pay now →</span>
|
<span class="text-xs text-blue-600 font-medium">Pay now</span>
|
||||||
</Show>
|
</Show>
|
||||||
<span class={`inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-medium capitalize ${statusStyle()}`}>
|
<span class={`inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-medium capitalize ${statusStyle()}`}>
|
||||||
{invoice.status}
|
{invoice.status}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<Show when={invoice.error}>
|
|
||||||
<p class="text-xs text-red-500 mt-2">{invoice.error}</p>
|
|
||||||
</Show>
|
|
||||||
</li>
|
</li>
|
||||||
)
|
)
|
||||||
}}
|
}}
|
||||||
|
|||||||
@@ -187,8 +187,8 @@ export default function Home() {
|
|||||||
<path d="M13 2L3 14h9l-1 8 10-12h-9l1-8z" />
|
<path d="M13 2L3 14h9l-1 8 10-12h-9l1-8z" />
|
||||||
</svg>
|
</svg>
|
||||||
),
|
),
|
||||||
title: "Pay with sats",
|
title: "Flexible payments",
|
||||||
body: "Lightning-native billing. No credit cards, no bank accounts — just sats, straight from your wallet.",
|
body: "Pay with Bitcoin/Lightning or with a card.",
|
||||||
},
|
},
|
||||||
].map(({ icon, title, body }) => (
|
].map(({ icon, title, body }) => (
|
||||||
<div class="bg-white rounded-2xl border border-gray-200 p-6 hover:border-blue-200 hover:shadow-sm transition-all">
|
<div class="bg-white rounded-2xl border border-gray-200 p-6 hover:border-blue-200 hover:shadow-sm transition-all">
|
||||||
@@ -301,7 +301,7 @@ export default function Home() {
|
|||||||
<div class="max-w-5xl mx-auto px-6 py-20">
|
<div class="max-w-5xl mx-auto px-6 py-20">
|
||||||
<h2 class="text-3xl font-bold text-center text-gray-900 mb-4">Simple pricing</h2>
|
<h2 class="text-3xl font-bold text-center text-gray-900 mb-4">Simple pricing</h2>
|
||||||
<p class="text-center text-gray-500 mb-14 max-w-lg mx-auto">
|
<p class="text-center text-gray-500 mb-14 max-w-lg mx-auto">
|
||||||
Pay in sats. Upgrade or cancel any time.
|
Upgrade or cancel any time.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<PricingTable onCta={openRelayModal} />
|
<PricingTable onCta={openRelayModal} />
|
||||||
@@ -314,7 +314,7 @@ export default function Home() {
|
|||||||
<div class="relative max-w-5xl mx-auto px-6 py-28 text-center">
|
<div class="relative max-w-5xl mx-auto px-6 py-28 text-center">
|
||||||
<h2 class="text-4xl font-extrabold text-gray-900 mb-4">Ready to launch your relay?</h2>
|
<h2 class="text-4xl font-extrabold text-gray-900 mb-4">Ready to launch your relay?</h2>
|
||||||
<p class="text-gray-500 mb-10 max-w-lg mx-auto text-lg">
|
<p class="text-gray-500 mb-10 max-w-lg mx-auto text-lg">
|
||||||
Join communities already running on Caravel. Set up in minutes, pay in sats.
|
Join communities already running on Caravel. Set up in minutes.
|
||||||
</p>
|
</p>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ export default function AdminRelayDetail() {
|
|||||||
const [relay, { refetch, mutate }] = useRelay(relayId)
|
const [relay, { refetch, mutate }] = useRelay(relayId)
|
||||||
const loading = useMinLoading(() => relay.loading && !relay())
|
const loading = useMinLoading(() => relay.loading && !relay())
|
||||||
const [activity] = useRelayActivity(relayId)
|
const [activity] = useRelayActivity(relayId)
|
||||||
const { busy, handleDeactivate, toggles } = useRelayToggles(relayId, relay, { refetch, mutate })
|
const { busy, handleDeactivate, handleReactivate, toggles } = useRelayToggles(relayId, relay, { refetch, mutate })
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PageContainer>
|
<PageContainer>
|
||||||
@@ -29,7 +29,9 @@ export default function AdminRelayDetail() {
|
|||||||
showTenant
|
showTenant
|
||||||
editHref={`/admin/relays/${params.id}/edit`}
|
editHref={`/admin/relays/${params.id}/edit`}
|
||||||
onDeactivate={handleDeactivate}
|
onDeactivate={handleDeactivate}
|
||||||
|
onReactivate={handleReactivate}
|
||||||
deactivating={busy()}
|
deactivating={busy()}
|
||||||
|
reactivating={busy()}
|
||||||
enforcePlanLimits={false}
|
enforcePlanLimits={false}
|
||||||
showPlanActions={false}
|
showPlanActions={false}
|
||||||
{...toggles}
|
{...toggles}
|
||||||
|
|||||||
@@ -14,6 +14,12 @@ export default function AdminTenantDetail() {
|
|||||||
const [relays] = useAdminTenantRelays(tenantId)
|
const [relays] = useAdminTenantRelays(tenantId)
|
||||||
const loading = useMinLoading(() => tenant.loading || relays.loading)
|
const loading = useMinLoading(() => tenant.loading || relays.loading)
|
||||||
|
|
||||||
|
const pastDueLabel = () => {
|
||||||
|
const ts = tenant()?.past_due_at
|
||||||
|
if (!ts) return null
|
||||||
|
return new Date(ts * 1000).toLocaleString()
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PageContainer>
|
<PageContainer>
|
||||||
<BackLink href="/admin/tenants" label="Tenants" />
|
<BackLink href="/admin/tenants" label="Tenants" />
|
||||||
@@ -24,7 +30,32 @@ export default function AdminTenantDetail() {
|
|||||||
<section class="bg-white border border-gray-200 rounded-xl p-6">
|
<section class="bg-white border border-gray-200 rounded-xl p-6">
|
||||||
<h2 class="text-lg font-semibold mb-4">Status</h2>
|
<h2 class="text-lg font-semibold mb-4">Status</h2>
|
||||||
<Show when={tenant()}>
|
<Show when={tenant()}>
|
||||||
<p class="text-sm text-gray-700">Current: <span class="font-medium uppercase tracking-wide">tenant</span></p>
|
{(t) => (
|
||||||
|
<dl class="grid gap-y-3 text-sm">
|
||||||
|
<div class="flex gap-2">
|
||||||
|
<dt class="text-gray-500">Status:</dt>
|
||||||
|
<dd class="font-medium uppercase tracking-wide">{t().past_due_at ? "past due" : "active"}</dd>
|
||||||
|
</div>
|
||||||
|
<Show when={t().stripe_customer_id}>
|
||||||
|
<div class="flex gap-2">
|
||||||
|
<dt class="text-gray-500">Stripe Customer:</dt>
|
||||||
|
<dd class="font-mono text-xs">{t().stripe_customer_id}</dd>
|
||||||
|
</div>
|
||||||
|
</Show>
|
||||||
|
<Show when={pastDueLabel()}>
|
||||||
|
<div class="flex gap-2">
|
||||||
|
<dt class="text-gray-500">Past Due Since:</dt>
|
||||||
|
<dd class="text-red-600 font-medium">{pastDueLabel()}</dd>
|
||||||
|
</div>
|
||||||
|
</Show>
|
||||||
|
<Show when={t().nwc_error}>
|
||||||
|
<div class="flex gap-2">
|
||||||
|
<dt class="text-gray-500">NWC Error:</dt>
|
||||||
|
<dd class="text-red-600">{t().nwc_error}</dd>
|
||||||
|
</div>
|
||||||
|
</Show>
|
||||||
|
</dl>
|
||||||
|
)}
|
||||||
</Show>
|
</Show>
|
||||||
</section>
|
</section>
|
||||||
<section class="bg-white border border-gray-200 rounded-xl p-6">
|
<section class="bg-white border border-gray-200 rounded-xl p-6">
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { useParams } from "@solidjs/router"
|
|||||||
import { createMemo, createResource, Show } from "solid-js"
|
import { createMemo, createResource, Show } from "solid-js"
|
||||||
import BackLink from "@/components/BackLink"
|
import BackLink from "@/components/BackLink"
|
||||||
import PageContainer from "@/components/PageContainer"
|
import PageContainer from "@/components/PageContainer"
|
||||||
import PaymentDialog from "@/components/PaymentDialog"
|
import PaymentSetup from "@/components/PaymentSetup"
|
||||||
import RelayDetailCard from "@/components/RelayDetailCard"
|
import RelayDetailCard from "@/components/RelayDetailCard"
|
||||||
import ResourceState from "@/components/ResourceState"
|
import ResourceState from "@/components/ResourceState"
|
||||||
import useMinLoading from "@/components/useMinLoading"
|
import useMinLoading from "@/components/useMinLoading"
|
||||||
@@ -21,7 +21,7 @@ export default function RelayDetail() {
|
|||||||
const [members] = createResource(relayUrl, getRelayMembers)
|
const [members] = createResource(relayUrl, getRelayMembers)
|
||||||
const loading = useMinLoading(() => relay.loading && !relay())
|
const loading = useMinLoading(() => relay.loading && !relay())
|
||||||
const [activity] = useRelayActivity(relayId)
|
const [activity] = useRelayActivity(relayId)
|
||||||
const { busy, handleDeactivate, handleUpdatePlan, pendingInvoice, clearPendingInvoice, toggles } = useRelayToggles(relayId, relay, { refetch, mutate })
|
const { busy, handleDeactivate, handleReactivate, handleUpdatePlan, needsPaymentSetup, clearNeedsPaymentSetup, toggles } = useRelayToggles(relayId, relay, { refetch, mutate })
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PageContainer>
|
<PageContainer>
|
||||||
@@ -35,7 +35,9 @@ export default function RelayDetail() {
|
|||||||
currentMembers={members.length}
|
currentMembers={members.length}
|
||||||
editHref={`/relays/${params.id}/edit`}
|
editHref={`/relays/${params.id}/edit`}
|
||||||
onDeactivate={handleDeactivate}
|
onDeactivate={handleDeactivate}
|
||||||
|
onReactivate={handleReactivate}
|
||||||
deactivating={busy()}
|
deactivating={busy()}
|
||||||
|
reactivating={busy()}
|
||||||
onUpdatePlan={handleUpdatePlan}
|
onUpdatePlan={handleUpdatePlan}
|
||||||
{...toggles}
|
{...toggles}
|
||||||
/>
|
/>
|
||||||
@@ -43,15 +45,10 @@ export default function RelayDetail() {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</Show>
|
</Show>
|
||||||
<Show when={pendingInvoice()}>
|
<PaymentSetup
|
||||||
{(invoice) => (
|
open={needsPaymentSetup()}
|
||||||
<PaymentDialog
|
onClose={clearNeedsPaymentSetup}
|
||||||
invoice={invoice()}
|
/>
|
||||||
open={true}
|
|
||||||
onClose={clearPendingInvoice}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</Show>
|
|
||||||
</PageContainer>
|
</PageContainer>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,10 +34,7 @@ export default function RelayList() {
|
|||||||
<select value={status()} onChange={e => setStatus(e.currentTarget.value)} class="border border-gray-300 rounded-lg px-3 py-2 bg-white">
|
<select value={status()} onChange={e => setStatus(e.currentTarget.value)} class="border border-gray-300 rounded-lg px-3 py-2 bg-white">
|
||||||
<option value="all">All statuses</option>
|
<option value="all">All statuses</option>
|
||||||
<option value="active">Active</option>
|
<option value="active">Active</option>
|
||||||
<option value="pending">Pending</option>
|
<option value="inactive">Inactive</option>
|
||||||
<option value="deactivated">Deactivated</option>
|
|
||||||
<option value="provisioning_failed">Provisioning failed</option>
|
|
||||||
<option value="suspended">Suspended</option>
|
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<ResourceState loading={loading()} error={relays.error} loadingText="Loading relays..." errorText="Failed to load relays." />
|
<ResourceState loading={loading()} error={relays.error} loadingText="Loading relays..." errorText="Failed to load relays." />
|
||||||
|
|||||||
@@ -2,13 +2,13 @@ import { createSignal } from "solid-js"
|
|||||||
import { useNavigate } from "@solidjs/router"
|
import { useNavigate } from "@solidjs/router"
|
||||||
import BackLink from "@/components/BackLink"
|
import BackLink from "@/components/BackLink"
|
||||||
import PageContainer from "@/components/PageContainer"
|
import PageContainer from "@/components/PageContainer"
|
||||||
import PaymentDialog from "@/components/PaymentDialog"
|
import PaymentSetup from "@/components/PaymentSetup"
|
||||||
import RelayForm, { type RelayFormValues } from "@/components/RelayForm"
|
import RelayForm, { type RelayFormValues } from "@/components/RelayForm"
|
||||||
import { checkPendingInvoice, createRelayForActiveTenant, type Invoice } from "@/lib/hooks"
|
import { createRelayForActiveTenant, tenantNeedsPaymentSetup } from "@/lib/hooks"
|
||||||
|
|
||||||
export default function RelayNew() {
|
export default function RelayNew() {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const [pendingInvoice, setPendingInvoice] = createSignal<Invoice | undefined>()
|
const [showPaymentSetup, setShowPaymentSetup] = createSignal(false)
|
||||||
let createdRelayId = ""
|
let createdRelayId = ""
|
||||||
|
|
||||||
async function handleSubmit(values: RelayFormValues) {
|
async function handleSubmit(values: RelayFormValues) {
|
||||||
@@ -16,9 +16,9 @@ export default function RelayNew() {
|
|||||||
createdRelayId = relay.id
|
createdRelayId = relay.id
|
||||||
|
|
||||||
if (values.plan !== "free") {
|
if (values.plan !== "free") {
|
||||||
const invoice = await checkPendingInvoice()
|
const needs = await tenantNeedsPaymentSetup()
|
||||||
if (invoice) {
|
if (needs) {
|
||||||
setPendingInvoice(invoice)
|
setShowPaymentSetup(true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,7 +27,7 @@ export default function RelayNew() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleDialogClose() {
|
function handleDialogClose() {
|
||||||
setPendingInvoice(undefined)
|
setShowPaymentSetup(false)
|
||||||
navigate(`/relays/${createdRelayId}`)
|
navigate(`/relays/${createdRelayId}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,13 +41,10 @@ export default function RelayNew() {
|
|||||||
submitLabel="Create Relay"
|
submitLabel="Create Relay"
|
||||||
submittingLabel="Creating..."
|
submittingLabel="Creating..."
|
||||||
/>
|
/>
|
||||||
{pendingInvoice() && (
|
<PaymentSetup
|
||||||
<PaymentDialog
|
open={showPaymentSetup()}
|
||||||
invoice={pendingInvoice()!}
|
onClose={handleDialogClose}
|
||||||
open={!!pendingInvoice()}
|
/>
|
||||||
onClose={handleDialogClose}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</PageContainer>
|
</PageContainer>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user