stripe_shared/
checkout_session_item.rs

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