Skip to main content

stripe_shared/
subscription_item.rs

1/// Subscription items allow you to create customer subscriptions with more than
2/// one plan, making it easy to represent complex billing relationships.
3///
4/// For more details see <<https://stripe.com/docs/api/subscription_items/object>>.
5#[derive(Clone)]
6#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
7#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
8pub struct SubscriptionItem {
9    /// The time period the subscription item has been billed for.
10    pub billed_until: Option<stripe_types::Timestamp>,
11    /// Define thresholds at which an invoice will be sent, and the related subscription advanced to a new billing period.
12    pub billing_thresholds: Option<stripe_shared::SubscriptionItemBillingThresholds>,
13    /// Time at which the object was created. Measured in seconds since the Unix epoch.
14    pub created: i64,
15    /// The end time of this subscription item's current billing period.
16    pub current_period_end: stripe_types::Timestamp,
17    /// The start time of this subscription item's current billing period.
18    pub current_period_start: stripe_types::Timestamp,
19    /// The discounts applied to the subscription item.
20    /// Subscription item discounts are applied before subscription discounts.
21    /// Use `expand[]=discounts` to expand each discount.
22    pub discounts: Vec<stripe_types::Expandable<stripe_shared::Discount>>,
23    /// Unique identifier for the object.
24    pub id: stripe_shared::SubscriptionItemId,
25    /// Set of [key-value pairs](https://docs.stripe.com/api/metadata) that you can attach to an object.
26    /// This can be useful for storing additional information about the object in a structured format.
27    pub metadata: std::collections::HashMap<String, String>,
28    pub plan: stripe_shared::Plan,
29    pub price: stripe_shared::Price,
30    /// The [quantity](https://docs.stripe.com/subscriptions/quantities) of the plan to which the customer should be subscribed.
31    pub quantity: Option<u64>,
32    /// The `subscription` this `subscription_item` belongs to.
33    pub subscription: String,
34    /// The tax rates which apply to this `subscription_item`.
35    /// When set, the `default_tax_rates` on the subscription do not apply to this `subscription_item`.
36    pub tax_rates: Option<Vec<stripe_shared::TaxRate>>,
37}
38#[cfg(feature = "redact-generated-debug")]
39impl std::fmt::Debug for SubscriptionItem {
40    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
41        f.debug_struct("SubscriptionItem").finish_non_exhaustive()
42    }
43}
44#[doc(hidden)]
45pub struct SubscriptionItemBuilder {
46    billed_until: Option<Option<stripe_types::Timestamp>>,
47    billing_thresholds: Option<Option<stripe_shared::SubscriptionItemBillingThresholds>>,
48    created: Option<i64>,
49    current_period_end: Option<stripe_types::Timestamp>,
50    current_period_start: Option<stripe_types::Timestamp>,
51    discounts: Option<Vec<stripe_types::Expandable<stripe_shared::Discount>>>,
52    id: Option<stripe_shared::SubscriptionItemId>,
53    metadata: Option<std::collections::HashMap<String, String>>,
54    plan: Option<stripe_shared::Plan>,
55    price: Option<stripe_shared::Price>,
56    quantity: Option<Option<u64>>,
57    subscription: Option<String>,
58    tax_rates: Option<Option<Vec<stripe_shared::TaxRate>>>,
59}
60
61#[allow(
62    unused_variables,
63    irrefutable_let_patterns,
64    clippy::let_unit_value,
65    clippy::match_single_binding,
66    clippy::single_match
67)]
68const _: () = {
69    use miniserde::de::{Map, Visitor};
70    use miniserde::json::Value;
71    use miniserde::{Deserialize, Result, make_place};
72    use stripe_types::miniserde_helpers::FromValueOpt;
73    use stripe_types::{MapBuilder, ObjectDeser};
74
75    make_place!(Place);
76
77    impl Deserialize for SubscriptionItem {
78        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
79            Place::new(out)
80        }
81    }
82
83    struct Builder<'a> {
84        out: &'a mut Option<SubscriptionItem>,
85        builder: SubscriptionItemBuilder,
86    }
87
88    impl Visitor for Place<SubscriptionItem> {
89        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
90            Ok(Box::new(Builder {
91                out: &mut self.out,
92                builder: SubscriptionItemBuilder::deser_default(),
93            }))
94        }
95    }
96
97    impl MapBuilder for SubscriptionItemBuilder {
98        type Out = SubscriptionItem;
99        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
100            Ok(match k {
101                "billed_until" => Deserialize::begin(&mut self.billed_until),
102                "billing_thresholds" => Deserialize::begin(&mut self.billing_thresholds),
103                "created" => Deserialize::begin(&mut self.created),
104                "current_period_end" => Deserialize::begin(&mut self.current_period_end),
105                "current_period_start" => Deserialize::begin(&mut self.current_period_start),
106                "discounts" => Deserialize::begin(&mut self.discounts),
107                "id" => Deserialize::begin(&mut self.id),
108                "metadata" => Deserialize::begin(&mut self.metadata),
109                "plan" => Deserialize::begin(&mut self.plan),
110                "price" => Deserialize::begin(&mut self.price),
111                "quantity" => Deserialize::begin(&mut self.quantity),
112                "subscription" => Deserialize::begin(&mut self.subscription),
113                "tax_rates" => Deserialize::begin(&mut self.tax_rates),
114                _ => <dyn Visitor>::ignore(),
115            })
116        }
117
118        fn deser_default() -> Self {
119            Self {
120                billed_until: Some(None),
121                billing_thresholds: Some(None),
122                created: None,
123                current_period_end: None,
124                current_period_start: None,
125                discounts: None,
126                id: None,
127                metadata: None,
128                plan: None,
129                price: None,
130                quantity: Some(None),
131                subscription: None,
132                tax_rates: Some(None),
133            }
134        }
135
136        fn take_out(&mut self) -> Option<Self::Out> {
137            let (
138                Some(billed_until),
139                Some(billing_thresholds),
140                Some(created),
141                Some(current_period_end),
142                Some(current_period_start),
143                Some(discounts),
144                Some(id),
145                Some(metadata),
146                Some(plan),
147                Some(price),
148                Some(quantity),
149                Some(subscription),
150                Some(tax_rates),
151            ) = (
152                self.billed_until,
153                self.billing_thresholds,
154                self.created,
155                self.current_period_end,
156                self.current_period_start,
157                self.discounts.take(),
158                self.id.take(),
159                self.metadata.take(),
160                self.plan.take(),
161                self.price.take(),
162                self.quantity,
163                self.subscription.take(),
164                self.tax_rates.take(),
165            )
166            else {
167                return None;
168            };
169            Some(Self::Out {
170                billed_until,
171                billing_thresholds,
172                created,
173                current_period_end,
174                current_period_start,
175                discounts,
176                id,
177                metadata,
178                plan,
179                price,
180                quantity,
181                subscription,
182                tax_rates,
183            })
184        }
185    }
186
187    impl Map for Builder<'_> {
188        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
189            self.builder.key(k)
190        }
191
192        fn finish(&mut self) -> Result<()> {
193            *self.out = self.builder.take_out();
194            Ok(())
195        }
196    }
197
198    impl ObjectDeser for SubscriptionItem {
199        type Builder = SubscriptionItemBuilder;
200    }
201
202    impl FromValueOpt for SubscriptionItem {
203        fn from_value(v: Value) -> Option<Self> {
204            let Value::Object(obj) = v else {
205                return None;
206            };
207            let mut b = SubscriptionItemBuilder::deser_default();
208            for (k, v) in obj {
209                match k.as_str() {
210                    "billed_until" => b.billed_until = FromValueOpt::from_value(v),
211                    "billing_thresholds" => b.billing_thresholds = FromValueOpt::from_value(v),
212                    "created" => b.created = FromValueOpt::from_value(v),
213                    "current_period_end" => b.current_period_end = FromValueOpt::from_value(v),
214                    "current_period_start" => b.current_period_start = FromValueOpt::from_value(v),
215                    "discounts" => b.discounts = FromValueOpt::from_value(v),
216                    "id" => b.id = FromValueOpt::from_value(v),
217                    "metadata" => b.metadata = FromValueOpt::from_value(v),
218                    "plan" => b.plan = FromValueOpt::from_value(v),
219                    "price" => b.price = FromValueOpt::from_value(v),
220                    "quantity" => b.quantity = FromValueOpt::from_value(v),
221                    "subscription" => b.subscription = FromValueOpt::from_value(v),
222                    "tax_rates" => b.tax_rates = FromValueOpt::from_value(v),
223                    _ => {}
224                }
225            }
226            b.take_out()
227        }
228    }
229};
230#[cfg(feature = "serialize")]
231impl serde::Serialize for SubscriptionItem {
232    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
233        use serde::ser::SerializeStruct;
234        let mut s = s.serialize_struct("SubscriptionItem", 14)?;
235        s.serialize_field("billed_until", &self.billed_until)?;
236        s.serialize_field("billing_thresholds", &self.billing_thresholds)?;
237        s.serialize_field("created", &self.created)?;
238        s.serialize_field("current_period_end", &self.current_period_end)?;
239        s.serialize_field("current_period_start", &self.current_period_start)?;
240        s.serialize_field("discounts", &self.discounts)?;
241        s.serialize_field("id", &self.id)?;
242        s.serialize_field("metadata", &self.metadata)?;
243        s.serialize_field("plan", &self.plan)?;
244        s.serialize_field("price", &self.price)?;
245        s.serialize_field("quantity", &self.quantity)?;
246        s.serialize_field("subscription", &self.subscription)?;
247        s.serialize_field("tax_rates", &self.tax_rates)?;
248
249        s.serialize_field("object", "subscription_item")?;
250        s.end()
251    }
252}
253impl stripe_types::Object for SubscriptionItem {
254    type Id = stripe_shared::SubscriptionItemId;
255    fn id(&self) -> &Self::Id {
256        &self.id
257    }
258
259    fn into_id(self) -> Self::Id {
260        self.id
261    }
262}
263stripe_types::def_id!(SubscriptionItemId);