forked from coracle/caravel
fix(billing): ensure all tenants have valid Stripe customer IDs
This commit is contained in:
@@ -12,7 +12,7 @@ CREATE TABLE IF NOT EXISTS tenant (
|
|||||||
nwc_url TEXT NOT NULL DEFAULT '',
|
nwc_url TEXT NOT NULL DEFAULT '',
|
||||||
nwc_error TEXT,
|
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,
|
||||||
stripe_subscription_id TEXT,
|
stripe_subscription_id TEXT,
|
||||||
past_due_at INTEGER
|
past_due_at INTEGER
|
||||||
);
|
);
|
||||||
|
|||||||
+42
-24
@@ -370,32 +370,50 @@ async fn get_identity(
|
|||||||
let pubkey = state.api.extract_auth_pubkey(&headers)?;
|
let pubkey = state.api.extract_auth_pubkey(&headers)?;
|
||||||
let is_admin = state.api.admins.iter().any(|a| a == &pubkey);
|
let is_admin = state.api.admins.iter().any(|a| a == &pubkey);
|
||||||
|
|
||||||
// Only create if tenant doesn't exist yet
|
// Ensure tenant exists.
|
||||||
if let Ok(None) = state.api.query.get_tenant(&pubkey).await {
|
match state.api.query.get_tenant(&pubkey).await {
|
||||||
// TODO: Call Stripe API to create a new customer
|
Ok(Some(_)) => {}
|
||||||
let stripe_customer_id = String::new();
|
Ok(None) => {
|
||||||
|
let stripe_customer_id = match state.api.billing.stripe_create_customer(&pubkey).await {
|
||||||
|
Ok(id) => id,
|
||||||
|
Err(e) => {
|
||||||
|
return Ok(err(
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"stripe-customer-create-failed",
|
||||||
|
&e.to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let tenant = Tenant {
|
let tenant = Tenant {
|
||||||
pubkey: pubkey.clone(),
|
pubkey: pubkey.clone(),
|
||||||
nwc_url: String::new(),
|
nwc_url: String::new(),
|
||||||
nwc_error: None,
|
nwc_error: None,
|
||||||
created_at: now_ts(),
|
created_at: now_ts(),
|
||||||
stripe_customer_id,
|
stripe_customer_id,
|
||||||
stripe_subscription_id: None,
|
stripe_subscription_id: None,
|
||||||
past_due_at: None,
|
past_due_at: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
match state.api.command.create_tenant(&tenant).await {
|
match state.api.command.create_tenant(&tenant).await {
|
||||||
Ok(()) => {}
|
Ok(()) => {}
|
||||||
Err(e) if matches!(map_unique_error(&e), Some("pubkey-exists")) => {}
|
Err(e) if matches!(map_unique_error(&e), Some("pubkey-exists")) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return Ok(err(
|
return Ok(err(
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
"internal",
|
"internal",
|
||||||
&e.to_string(),
|
&e.to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
return Ok(err(
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"internal",
|
||||||
|
&e.to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(ok(StatusCode::OK, IdentityResponse { pubkey, is_admin }))
|
Ok(ok(StatusCode::OK, IdentityResponse { pubkey, is_admin }))
|
||||||
|
|||||||
@@ -56,6 +56,9 @@ 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_secret_key = std::env::var("STRIPE_SECRET_KEY").unwrap_or_default();
|
||||||
|
if stripe_secret_key.trim().is_empty() {
|
||||||
|
panic!("missing STRIPE_SECRET_KEY environment variable");
|
||||||
|
}
|
||||||
let stripe_webhook_secret = std::env::var("STRIPE_WEBHOOK_SECRET").unwrap_or_default();
|
let stripe_webhook_secret = std::env::var("STRIPE_WEBHOOK_SECRET").unwrap_or_default();
|
||||||
let btc_quote_api_base =
|
let btc_quote_api_base =
|
||||||
std::env::var("BTC_PRICE_API_BASE").unwrap_or_else(|_| COINBASE_SPOT_API.to_string());
|
std::env::var("BTC_PRICE_API_BASE").unwrap_or_else(|_| COINBASE_SPOT_API.to_string());
|
||||||
@@ -472,6 +475,33 @@ impl Billing {
|
|||||||
Ok((invoice, tenant))
|
Ok((invoice, tenant))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn stripe_create_customer(&self, tenant_pubkey: &str) -> Result<String> {
|
||||||
|
let short_pubkey: String = tenant_pubkey.chars().take(12).collect();
|
||||||
|
let display_name = format!("Caravel tenant {short_pubkey}");
|
||||||
|
|
||||||
|
let resp = self
|
||||||
|
.http
|
||||||
|
.post(format!("{STRIPE_API}/customers"))
|
||||||
|
.bearer_auth(&self.stripe_secret_key)
|
||||||
|
.form(&[
|
||||||
|
("name", display_name.as_str()),
|
||||||
|
("metadata[tenant_pubkey]", tenant_pubkey),
|
||||||
|
])
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let body: serde_json::Value = resp.error_for_status()?.json().await?;
|
||||||
|
let customer_id = body["id"]
|
||||||
|
.as_str()
|
||||||
|
.ok_or_else(|| anyhow!("missing customer id"))?;
|
||||||
|
|
||||||
|
if !customer_id.starts_with("cus_") {
|
||||||
|
return Err(anyhow!("unexpected customer id format"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(customer_id.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn stripe_list_invoices(&self, customer_id: &str) -> Result<serde_json::Value> {
|
pub async fn stripe_list_invoices(&self, customer_id: &str) -> Result<serde_json::Value> {
|
||||||
let resp = self
|
let resp = self
|
||||||
.http
|
.http
|
||||||
|
|||||||
@@ -66,6 +66,10 @@ impl Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_tenant(&self, tenant: &Tenant) -> Result<()> {
|
pub async fn create_tenant(&self, tenant: &Tenant) -> Result<()> {
|
||||||
|
if tenant.stripe_customer_id.trim().is_empty() {
|
||||||
|
anyhow::bail!("stripe_customer_id is required");
|
||||||
|
}
|
||||||
|
|
||||||
let mut tx = self.pool.begin().await?;
|
let mut tx = self.pool.begin().await?;
|
||||||
|
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
@@ -205,7 +209,8 @@ impl Command {
|
|||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let activity = Self::insert_activity(&mut tx, activity_type, "relay", relay_id).await?;
|
let activity =
|
||||||
|
Self::insert_activity(&mut tx, "deactivate_relay", "relay", &relay_id).await?;
|
||||||
|
|
||||||
tx.commit().await?;
|
tx.commit().await?;
|
||||||
self.emit(activity);
|
self.emit(activity);
|
||||||
|
|||||||
Reference in New Issue
Block a user