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