Skip to main content

stripe_shared/
checkout_session_item.rs

1/// A line item.
2#[derive(Clone)]
3#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
4#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
5pub struct CheckoutSessionItem {
6    pub adjustable_quantity: Option<stripe_shared::LineItemsAdjustableQuantity>,
7    /// Total discount amount applied. If no discounts were applied, defaults to 0.
8    pub amount_discount: i64,
9    /// Total before any discounts or taxes are applied.
10    pub amount_subtotal: i64,
11    /// Total tax amount applied. If no tax was applied, defaults to 0.
12    pub amount_tax: i64,
13    /// Total after discounts and taxes.
14    pub amount_total: i64,
15    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
16    /// Must be a [supported currency](https://stripe.com/docs/currencies).
17    pub currency: stripe_types::Currency,
18    /// An arbitrary string attached to the object.
19    /// Often useful for displaying to users.
20    /// Defaults to product name.
21    pub description: Option<String>,
22    /// The discounts applied to the line item.
23    pub discounts: Option<Vec<stripe_shared::LineItemsDiscountAmount>>,
24    /// Unique identifier for the object.
25    pub id: stripe_shared::CheckoutSessionItemId,
26    /// Set of [key-value pairs](https://docs.stripe.com/api/metadata) that you can attach to an object.
27    /// This can be useful for storing additional information about the object in a structured format.
28    pub metadata: Option<std::collections::HashMap<String, String>>,
29    /// The price used to generate the line item.
30    pub price: Option<stripe_shared::Price>,
31    /// The quantity of products being purchased.
32    pub quantity: Option<u64>,
33    /// The taxes applied to the line item.
34    pub taxes: Option<Vec<stripe_shared::LineItemsTaxAmount>>,
35}
36#[cfg(feature = "redact-generated-debug")]
37impl std::fmt::Debug for CheckoutSessionItem {
38    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
39        f.debug_struct("CheckoutSessionItem").finish_non_exhaustive()
40    }
41}
42#[doc(hidden)]
43pub struct CheckoutSessionItemBuilder {
44    adjustable_quantity: Option<Option<stripe_shared::LineItemsAdjustableQuantity>>,
45    amount_discount: Option<i64>,
46    amount_subtotal: Option<i64>,
47    amount_tax: Option<i64>,
48    amount_total: Option<i64>,
49    currency: Option<stripe_types::Currency>,
50    description: Option<Option<String>>,
51    discounts: Option<Option<Vec<stripe_shared::LineItemsDiscountAmount>>>,
52    id: Option<stripe_shared::CheckoutSessionItemId>,
53    metadata: Option<Option<std::collections::HashMap<String, String>>>,
54    price: Option<Option<stripe_shared::Price>>,
55    quantity: Option<Option<u64>>,
56    taxes: Option<Option<Vec<stripe_shared::LineItemsTaxAmount>>>,
57}
58
59#[allow(
60    unused_variables,
61    irrefutable_let_patterns,
62    clippy::let_unit_value,
63    clippy::match_single_binding,
64    clippy::single_match
65)]
66const _: () = {
67    use miniserde::de::{Map, Visitor};
68    use miniserde::json::Value;
69    use miniserde::{Deserialize, Result, make_place};
70    use stripe_types::miniserde_helpers::FromValueOpt;
71    use stripe_types::{MapBuilder, ObjectDeser};
72
73    make_place!(Place);
74
75    impl Deserialize for CheckoutSessionItem {
76        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
77            Place::new(out)
78        }
79    }
80
81    struct Builder<'a> {
82        out: &'a mut Option<CheckoutSessionItem>,
83        builder: CheckoutSessionItemBuilder,
84    }
85
86    impl Visitor for Place<CheckoutSessionItem> {
87        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
88            Ok(Box::new(Builder {
89                out: &mut self.out,
90                builder: CheckoutSessionItemBuilder::deser_default(),
91            }))
92        }
93    }
94
95    impl MapBuilder for CheckoutSessionItemBuilder {
96        type Out = CheckoutSessionItem;
97        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
98            Ok(match k {
99                "adjustable_quantity" => Deserialize::begin(&mut self.adjustable_quantity),
100                "amount_discount" => Deserialize::begin(&mut self.amount_discount),
101                "amount_subtotal" => Deserialize::begin(&mut self.amount_subtotal),
102                "amount_tax" => Deserialize::begin(&mut self.amount_tax),
103                "amount_total" => Deserialize::begin(&mut self.amount_total),
104                "currency" => Deserialize::begin(&mut self.currency),
105                "description" => Deserialize::begin(&mut self.description),
106                "discounts" => Deserialize::begin(&mut self.discounts),
107                "id" => Deserialize::begin(&mut self.id),
108                "metadata" => Deserialize::begin(&mut self.metadata),
109                "price" => Deserialize::begin(&mut self.price),
110                "quantity" => Deserialize::begin(&mut self.quantity),
111                "taxes" => Deserialize::begin(&mut self.taxes),
112                _ => <dyn Visitor>::ignore(),
113            })
114        }
115
116        fn deser_default() -> Self {
117            Self {
118                adjustable_quantity: Some(None),
119                amount_discount: None,
120                amount_subtotal: None,
121                amount_tax: None,
122                amount_total: None,
123                currency: None,
124                description: Some(None),
125                discounts: Some(None),
126                id: None,
127                metadata: Some(None),
128                price: Some(None),
129                quantity: Some(None),
130                taxes: Some(None),
131            }
132        }
133
134        fn take_out(&mut self) -> Option<Self::Out> {
135            let (
136                Some(adjustable_quantity),
137                Some(amount_discount),
138                Some(amount_subtotal),
139                Some(amount_tax),
140                Some(amount_total),
141                Some(currency),
142                Some(description),
143                Some(discounts),
144                Some(id),
145                Some(metadata),
146                Some(price),
147                Some(quantity),
148                Some(taxes),
149            ) = (
150                self.adjustable_quantity,
151                self.amount_discount,
152                self.amount_subtotal,
153                self.amount_tax,
154                self.amount_total,
155                self.currency.take(),
156                self.description.take(),
157                self.discounts.take(),
158                self.id.take(),
159                self.metadata.take(),
160                self.price.take(),
161                self.quantity,
162                self.taxes.take(),
163            )
164            else {
165                return None;
166            };
167            Some(Self::Out {
168                adjustable_quantity,
169                amount_discount,
170                amount_subtotal,
171                amount_tax,
172                amount_total,
173                currency,
174                description,
175                discounts,
176                id,
177                metadata,
178                price,
179                quantity,
180                taxes,
181            })
182        }
183    }
184
185    impl Map for Builder<'_> {
186        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
187            self.builder.key(k)
188        }
189
190        fn finish(&mut self) -> Result<()> {
191            *self.out = self.builder.take_out();
192            Ok(())
193        }
194    }
195
196    impl ObjectDeser for CheckoutSessionItem {
197        type Builder = CheckoutSessionItemBuilder;
198    }
199
200    impl FromValueOpt for CheckoutSessionItem {
201        fn from_value(v: Value) -> Option<Self> {
202            let Value::Object(obj) = v else {
203                return None;
204            };
205            let mut b = CheckoutSessionItemBuilder::deser_default();
206            for (k, v) in obj {
207                match k.as_str() {
208                    "adjustable_quantity" => b.adjustable_quantity = FromValueOpt::from_value(v),
209                    "amount_discount" => b.amount_discount = FromValueOpt::from_value(v),
210                    "amount_subtotal" => b.amount_subtotal = FromValueOpt::from_value(v),
211                    "amount_tax" => b.amount_tax = FromValueOpt::from_value(v),
212                    "amount_total" => b.amount_total = FromValueOpt::from_value(v),
213                    "currency" => b.currency = FromValueOpt::from_value(v),
214                    "description" => b.description = 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                    "price" => b.price = FromValueOpt::from_value(v),
219                    "quantity" => b.quantity = FromValueOpt::from_value(v),
220                    "taxes" => b.taxes = FromValueOpt::from_value(v),
221                    _ => {}
222                }
223            }
224            b.take_out()
225        }
226    }
227};
228#[cfg(feature = "serialize")]
229impl serde::Serialize for CheckoutSessionItem {
230    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
231        use serde::ser::SerializeStruct;
232        let mut s = s.serialize_struct("CheckoutSessionItem", 14)?;
233        s.serialize_field("adjustable_quantity", &self.adjustable_quantity)?;
234        s.serialize_field("amount_discount", &self.amount_discount)?;
235        s.serialize_field("amount_subtotal", &self.amount_subtotal)?;
236        s.serialize_field("amount_tax", &self.amount_tax)?;
237        s.serialize_field("amount_total", &self.amount_total)?;
238        s.serialize_field("currency", &self.currency)?;
239        s.serialize_field("description", &self.description)?;
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("price", &self.price)?;
244        s.serialize_field("quantity", &self.quantity)?;
245        s.serialize_field("taxes", &self.taxes)?;
246
247        s.serialize_field("object", "item")?;
248        s.end()
249    }
250}
251impl stripe_types::Object for CheckoutSessionItem {
252    type Id = stripe_shared::CheckoutSessionItemId;
253    fn id(&self) -> &Self::Id {
254        &self.id
255    }
256
257    fn into_id(self) -> Self::Id {
258        self.id
259    }
260}
261stripe_types::def_id!(CheckoutSessionItemId);