use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct Balance {
pub app_id: String,
pub user_id: String,
pub subscription: u32,
pub purchased: u32,
pub team_id: Option<String>,
#[serde(default)]
pub team_pool: u32,
pub version: u64,
}
impl Balance {
pub fn total(&self) -> u64 {
self.subscription as u64 + self.purchased as u64 + self.team_pool as u64
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct BillingStatus {
pub app_id: String,
pub user_id: String,
pub tier: String,
pub subscription_status: String,
pub balance: Balance,
}
impl BillingStatus {
pub fn free_fallback(user_id: impl Into<String>) -> Self {
let user_id = user_id.into();
Self {
app_id: String::new(),
user_id: user_id.clone(),
tier: "free".into(),
subscription_status: "degraded".into(),
balance: Balance {
user_id,
..Balance::default()
},
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LedgerEntry {
pub tx_id: String,
pub app_id: String,
pub owner_id: String,
pub tx_type: String,
pub amount: i32,
pub from_subscription: i32,
pub from_purchased: i32,
pub from_team_pool: i32,
pub related_id: Option<String>,
pub description: String,
pub created_at: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DebitRequest {
pub user_id: String,
pub amount: u32,
pub related_id: String,
pub description: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DebitResult {
pub tx_id: String,
pub amount: u32,
pub from_subscription: u32,
pub from_purchased: u32,
pub from_team_pool: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefundRequest {
pub user_id: String,
pub debit_tx_id: String,
pub reason: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Purchase {
Subscription {
tier: String,
},
CreditPack {
pack_id: String,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckoutRequest {
pub user_id: String,
pub email: String,
pub purchase: Purchase,
pub success_url: String,
pub cancel_url: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckoutInput {
pub purchase: Purchase,
pub success_url: String,
pub cancel_url: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckoutResponse {
pub session_id: String,
pub checkout_url: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PortalInput {
pub return_url: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PortalResponse {
pub portal_url: String,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum SnapshotSource {
Fresh,
Cache,
Stale,
FreeFallback,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BillingSnapshot {
pub status: BillingStatus,
pub source: SnapshotSource,
pub degraded: bool,
}
#[derive(Debug, Clone)]
pub struct TierPolicy {
order: Vec<String>,
}
impl TierPolicy {
pub fn new(order: impl IntoIterator<Item = impl Into<String>>) -> Self {
Self {
order: order.into_iter().map(Into::into).collect(),
}
}
pub fn allows(&self, actual: &str, required: &str) -> bool {
let rank = |tier: &str| self.order.iter().position(|value| value == tier);
matches!((rank(actual), rank(required)), (Some(actual), Some(required)) if actual >= required)
}
}
impl Default for TierPolicy {
fn default() -> Self {
Self::new(["free", "pro", "team"])
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_tier_policy_is_monotonic() {
let policy = TierPolicy::default();
assert!(policy.allows("team", "pro"));
assert!(policy.allows("pro", "pro"));
assert!(!policy.allows("free", "pro"));
assert!(!policy.allows("unknown", "free"));
}
#[test]
fn cross_language_status_fixture_is_forward_compatible() {
let status: BillingStatus =
serde_json::from_str(include_str!("../../../sdk-conformance/billing-status.json"))
.unwrap();
assert_eq!(status.tier, "pro");
assert_eq!(status.balance.total(), 160);
}
}