1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4use time::OffsetDateTime;
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub struct Subscription {
8 pub id: String,
9 pub plan: String,
10 #[serde(rename = "referenceId")]
11 pub reference_id: String,
12 #[serde(default, rename = "stripeCustomerId")]
13 pub stripe_customer_id: Option<String>,
14 #[serde(default, rename = "stripeSubscriptionId")]
15 pub stripe_subscription_id: Option<String>,
16 pub status: String,
17 #[serde(default, rename = "periodStart", with = "time::serde::rfc3339::option")]
18 pub period_start: Option<OffsetDateTime>,
19 #[serde(default, rename = "periodEnd", with = "time::serde::rfc3339::option")]
20 pub period_end: Option<OffsetDateTime>,
21 #[serde(default, rename = "trialStart", with = "time::serde::rfc3339::option")]
22 pub trial_start: Option<OffsetDateTime>,
23 #[serde(default, rename = "trialEnd", with = "time::serde::rfc3339::option")]
24 pub trial_end: Option<OffsetDateTime>,
25 #[serde(default, rename = "cancelAtPeriodEnd")]
26 pub cancel_at_period_end: bool,
27 #[serde(default, rename = "cancelAt", with = "time::serde::rfc3339::option")]
28 pub cancel_at: Option<OffsetDateTime>,
29 #[serde(default, rename = "canceledAt", with = "time::serde::rfc3339::option")]
30 pub canceled_at: Option<OffsetDateTime>,
31 #[serde(default, rename = "endedAt", with = "time::serde::rfc3339::option")]
32 pub ended_at: Option<OffsetDateTime>,
33 #[serde(default)]
34 pub seats: Option<i64>,
35 #[serde(default, rename = "billingInterval")]
36 pub billing_interval: Option<String>,
37 #[serde(default, rename = "stripeScheduleId")]
38 pub stripe_schedule_id: Option<String>,
39}
40
41#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
42pub struct StripePrice {
43 pub id: String,
44 #[serde(default)]
45 pub lookup_key: Option<String>,
46 #[serde(default)]
47 pub recurring: Option<StripeRecurring>,
48}
49
50impl StripePrice {
51 pub fn new(id: impl Into<String>) -> Self {
52 Self {
53 id: id.into(),
54 lookup_key: None,
55 recurring: None,
56 }
57 }
58
59 pub fn lookup_key(mut self, lookup_key: impl Into<String>) -> Self {
60 self.lookup_key = Some(lookup_key.into());
61 self
62 }
63
64 pub fn recurring(mut self, interval: impl Into<String>, usage_type: impl Into<String>) -> Self {
65 self.recurring = Some(StripeRecurring {
66 interval: interval.into(),
67 usage_type: Some(usage_type.into()),
68 });
69 self
70 }
71}
72
73#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
74pub struct StripeRecurring {
75 pub interval: String,
76 #[serde(default)]
77 pub usage_type: Option<String>,
78}
79
80#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
81pub struct StripeSubscriptionItem {
82 pub id: String,
83 pub price: StripePrice,
84 #[serde(default)]
85 pub quantity: Option<i64>,
86 #[serde(default)]
87 pub current_period_start: Option<i64>,
88 #[serde(default)]
89 pub current_period_end: Option<i64>,
90}
91
92impl StripeSubscriptionItem {
93 pub fn new(id: impl Into<String>, price: StripePrice) -> Self {
94 Self {
95 id: id.into(),
96 price,
97 quantity: None,
98 current_period_start: None,
99 current_period_end: None,
100 }
101 }
102
103 pub fn quantity(mut self, quantity: i64) -> Self {
104 self.quantity = Some(quantity);
105 self
106 }
107}
108
109#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
110pub struct StripeList<T> {
111 pub data: Vec<T>,
112}
113
114#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
115pub struct StripeSubscription {
116 pub id: String,
117 #[serde(default)]
118 pub customer: Option<serde_json::Value>,
119 pub status: String,
120 pub items: StripeList<StripeSubscriptionItem>,
121 #[serde(default)]
122 pub metadata: BTreeMap<String, String>,
123 #[serde(default)]
124 pub cancel_at_period_end: bool,
125 #[serde(default)]
126 pub cancel_at: Option<i64>,
127 #[serde(default)]
128 pub canceled_at: Option<i64>,
129 #[serde(default)]
130 pub ended_at: Option<i64>,
131 #[serde(default)]
132 pub trial_start: Option<i64>,
133 #[serde(default)]
134 pub trial_end: Option<i64>,
135 #[serde(default)]
136 pub schedule: Option<serde_json::Value>,
137}
138
139#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
140pub struct StripeCheckoutSession {
141 pub id: String,
142 #[serde(default)]
143 pub mode: Option<String>,
144 #[serde(default)]
145 pub subscription: Option<serde_json::Value>,
146 #[serde(default)]
147 pub client_reference_id: Option<String>,
148 #[serde(default)]
149 pub metadata: BTreeMap<String, String>,
150 #[serde(default)]
151 pub url: Option<String>,
152}
153
154#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
155pub struct StripeEvent {
156 pub id: String,
157 #[serde(rename = "type")]
158 pub event_type: String,
159 pub data: StripeEventData,
160}
161
162#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
163pub struct StripeEventData {
164 pub object: serde_json::Value,
165}