Skip to main content

stripe_shared/
invoice_line_item.rs

1/// Invoice Line Items represent the individual lines within an [invoice](https://docs.stripe.com/api/invoices) and only exist within the context of an invoice.
2///
3/// Each line item is backed by either an [invoice item](https://docs.stripe.com/api/invoiceitems) or a [subscription item](https://docs.stripe.com/api/subscription_items).
4#[derive(Clone)]
5#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
6#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
7pub struct InvoiceLineItem {
8    /// The amount, in cents (or local equivalent).
9    pub amount: i64,
10    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
11    /// Must be a [supported currency](https://stripe.com/docs/currencies).
12    pub currency: stripe_types::Currency,
13    /// An arbitrary string attached to the object. Often useful for displaying to users.
14    pub description: Option<String>,
15    /// The amount of discount calculated per discount for this line item.
16    pub discount_amounts: Option<Vec<stripe_shared::DiscountsResourceDiscountAmount>>,
17    /// If true, discounts will apply to this line item. Always false for prorations.
18    pub discountable: bool,
19    /// The discounts applied to the invoice line item.
20    /// Line item discounts are applied before invoice 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::InvoiceLineItemId,
25    /// The ID of the invoice that contains this line item.
26    pub invoice: Option<String>,
27    /// If the object exists in live mode, the value is `true`.
28    /// If the object exists in test mode, the value is `false`.
29    pub livemode: bool,
30    /// Set of [key-value pairs](https://docs.stripe.com/api/metadata) that you can attach to an object.
31    /// This can be useful for storing additional information about the object in a structured format.
32    /// Note that for line items with `type=subscription`, `metadata` reflects the current metadata from the subscription associated with the line item, unless the invoice line was directly updated with different metadata after creation.
33    pub metadata: std::collections::HashMap<String, String>,
34    /// The parent that generated this line item.
35    pub parent:
36        Option<stripe_shared::BillingBillResourceInvoicingLinesParentsInvoiceLineItemParent>,
37    pub period: stripe_shared::InvoiceLineItemPeriod,
38    /// Contains pretax credit amounts (ex: discount, credit grants, etc) that apply to this line item.
39    pub pretax_credit_amounts: Option<Vec<stripe_shared::InvoicesResourcePretaxCreditAmount>>,
40    /// The pricing information of the line item.
41    pub pricing: Option<stripe_shared::BillingBillResourceInvoicingPricingPricing>,
42    /// Quantity of units for the invoice line item in integer format, with any decimal precision truncated.
43    /// For the line item's full-precision decimal quantity, use `quantity_decimal`.
44    /// This field will be deprecated in favor of `quantity_decimal` in a future version.
45    /// If the line item is a proration or subscription, the quantity of the subscription that the proration was computed for.
46    pub quantity: Option<u64>,
47    /// Non-negative decimal with at most 12 decimal places. The quantity of units for the line item.
48    pub quantity_decimal: Option<String>,
49    pub subscription: Option<stripe_types::Expandable<stripe_shared::Subscription>>,
50    /// The subtotal of the line item, in cents (or local equivalent), before any discounts or taxes.
51    pub subtotal: i64,
52    /// The tax information of the line item.
53    pub taxes: Option<Vec<stripe_shared::BillingBillResourceInvoicingTaxesTax>>,
54}
55#[cfg(feature = "redact-generated-debug")]
56impl std::fmt::Debug for InvoiceLineItem {
57    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
58        f.debug_struct("InvoiceLineItem").finish_non_exhaustive()
59    }
60}
61#[doc(hidden)]
62pub struct InvoiceLineItemBuilder {
63    amount: Option<i64>,
64    currency: Option<stripe_types::Currency>,
65    description: Option<Option<String>>,
66    discount_amounts: Option<Option<Vec<stripe_shared::DiscountsResourceDiscountAmount>>>,
67    discountable: Option<bool>,
68    discounts: Option<Vec<stripe_types::Expandable<stripe_shared::Discount>>>,
69    id: Option<stripe_shared::InvoiceLineItemId>,
70    invoice: Option<Option<String>>,
71    livemode: Option<bool>,
72    metadata: Option<std::collections::HashMap<String, String>>,
73    parent: Option<
74        Option<stripe_shared::BillingBillResourceInvoicingLinesParentsInvoiceLineItemParent>,
75    >,
76    period: Option<stripe_shared::InvoiceLineItemPeriod>,
77    pretax_credit_amounts: Option<Option<Vec<stripe_shared::InvoicesResourcePretaxCreditAmount>>>,
78    pricing: Option<Option<stripe_shared::BillingBillResourceInvoicingPricingPricing>>,
79    quantity: Option<Option<u64>>,
80    quantity_decimal: Option<Option<String>>,
81    subscription: Option<Option<stripe_types::Expandable<stripe_shared::Subscription>>>,
82    subtotal: Option<i64>,
83    taxes: Option<Option<Vec<stripe_shared::BillingBillResourceInvoicingTaxesTax>>>,
84}
85
86#[allow(
87    unused_variables,
88    irrefutable_let_patterns,
89    clippy::let_unit_value,
90    clippy::match_single_binding,
91    clippy::single_match
92)]
93const _: () = {
94    use miniserde::de::{Map, Visitor};
95    use miniserde::json::Value;
96    use miniserde::{Deserialize, Result, make_place};
97    use stripe_types::miniserde_helpers::FromValueOpt;
98    use stripe_types::{MapBuilder, ObjectDeser};
99
100    make_place!(Place);
101
102    impl Deserialize for InvoiceLineItem {
103        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
104            Place::new(out)
105        }
106    }
107
108    struct Builder<'a> {
109        out: &'a mut Option<InvoiceLineItem>,
110        builder: InvoiceLineItemBuilder,
111    }
112
113    impl Visitor for Place<InvoiceLineItem> {
114        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
115            Ok(Box::new(Builder {
116                out: &mut self.out,
117                builder: InvoiceLineItemBuilder::deser_default(),
118            }))
119        }
120    }
121
122    impl MapBuilder for InvoiceLineItemBuilder {
123        type Out = InvoiceLineItem;
124        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
125            Ok(match k {
126                "amount" => Deserialize::begin(&mut self.amount),
127                "currency" => Deserialize::begin(&mut self.currency),
128                "description" => Deserialize::begin(&mut self.description),
129                "discount_amounts" => Deserialize::begin(&mut self.discount_amounts),
130                "discountable" => Deserialize::begin(&mut self.discountable),
131                "discounts" => Deserialize::begin(&mut self.discounts),
132                "id" => Deserialize::begin(&mut self.id),
133                "invoice" => Deserialize::begin(&mut self.invoice),
134                "livemode" => Deserialize::begin(&mut self.livemode),
135                "metadata" => Deserialize::begin(&mut self.metadata),
136                "parent" => Deserialize::begin(&mut self.parent),
137                "period" => Deserialize::begin(&mut self.period),
138                "pretax_credit_amounts" => Deserialize::begin(&mut self.pretax_credit_amounts),
139                "pricing" => Deserialize::begin(&mut self.pricing),
140                "quantity" => Deserialize::begin(&mut self.quantity),
141                "quantity_decimal" => Deserialize::begin(&mut self.quantity_decimal),
142                "subscription" => Deserialize::begin(&mut self.subscription),
143                "subtotal" => Deserialize::begin(&mut self.subtotal),
144                "taxes" => Deserialize::begin(&mut self.taxes),
145                _ => <dyn Visitor>::ignore(),
146            })
147        }
148
149        fn deser_default() -> Self {
150            Self {
151                amount: None,
152                currency: None,
153                description: Some(None),
154                discount_amounts: Some(None),
155                discountable: None,
156                discounts: None,
157                id: None,
158                invoice: Some(None),
159                livemode: None,
160                metadata: None,
161                parent: Some(None),
162                period: None,
163                pretax_credit_amounts: Some(None),
164                pricing: Some(None),
165                quantity: Some(None),
166                quantity_decimal: Some(None),
167                subscription: Some(None),
168                subtotal: None,
169                taxes: Some(None),
170            }
171        }
172
173        fn take_out(&mut self) -> Option<Self::Out> {
174            let (
175                Some(amount),
176                Some(currency),
177                Some(description),
178                Some(discount_amounts),
179                Some(discountable),
180                Some(discounts),
181                Some(id),
182                Some(invoice),
183                Some(livemode),
184                Some(metadata),
185                Some(parent),
186                Some(period),
187                Some(pretax_credit_amounts),
188                Some(pricing),
189                Some(quantity),
190                Some(quantity_decimal),
191                Some(subscription),
192                Some(subtotal),
193                Some(taxes),
194            ) = (
195                self.amount,
196                self.currency.take(),
197                self.description.take(),
198                self.discount_amounts.take(),
199                self.discountable,
200                self.discounts.take(),
201                self.id.take(),
202                self.invoice.take(),
203                self.livemode,
204                self.metadata.take(),
205                self.parent.take(),
206                self.period,
207                self.pretax_credit_amounts.take(),
208                self.pricing.take(),
209                self.quantity,
210                self.quantity_decimal.take(),
211                self.subscription.take(),
212                self.subtotal,
213                self.taxes.take(),
214            )
215            else {
216                return None;
217            };
218            Some(Self::Out {
219                amount,
220                currency,
221                description,
222                discount_amounts,
223                discountable,
224                discounts,
225                id,
226                invoice,
227                livemode,
228                metadata,
229                parent,
230                period,
231                pretax_credit_amounts,
232                pricing,
233                quantity,
234                quantity_decimal,
235                subscription,
236                subtotal,
237                taxes,
238            })
239        }
240    }
241
242    impl Map for Builder<'_> {
243        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
244            self.builder.key(k)
245        }
246
247        fn finish(&mut self) -> Result<()> {
248            *self.out = self.builder.take_out();
249            Ok(())
250        }
251    }
252
253    impl ObjectDeser for InvoiceLineItem {
254        type Builder = InvoiceLineItemBuilder;
255    }
256
257    impl FromValueOpt for InvoiceLineItem {
258        fn from_value(v: Value) -> Option<Self> {
259            let Value::Object(obj) = v else {
260                return None;
261            };
262            let mut b = InvoiceLineItemBuilder::deser_default();
263            for (k, v) in obj {
264                match k.as_str() {
265                    "amount" => b.amount = FromValueOpt::from_value(v),
266                    "currency" => b.currency = FromValueOpt::from_value(v),
267                    "description" => b.description = FromValueOpt::from_value(v),
268                    "discount_amounts" => b.discount_amounts = FromValueOpt::from_value(v),
269                    "discountable" => b.discountable = FromValueOpt::from_value(v),
270                    "discounts" => b.discounts = FromValueOpt::from_value(v),
271                    "id" => b.id = FromValueOpt::from_value(v),
272                    "invoice" => b.invoice = FromValueOpt::from_value(v),
273                    "livemode" => b.livemode = FromValueOpt::from_value(v),
274                    "metadata" => b.metadata = FromValueOpt::from_value(v),
275                    "parent" => b.parent = FromValueOpt::from_value(v),
276                    "period" => b.period = FromValueOpt::from_value(v),
277                    "pretax_credit_amounts" => {
278                        b.pretax_credit_amounts = FromValueOpt::from_value(v)
279                    }
280                    "pricing" => b.pricing = FromValueOpt::from_value(v),
281                    "quantity" => b.quantity = FromValueOpt::from_value(v),
282                    "quantity_decimal" => b.quantity_decimal = FromValueOpt::from_value(v),
283                    "subscription" => b.subscription = FromValueOpt::from_value(v),
284                    "subtotal" => b.subtotal = FromValueOpt::from_value(v),
285                    "taxes" => b.taxes = FromValueOpt::from_value(v),
286                    _ => {}
287                }
288            }
289            b.take_out()
290        }
291    }
292};
293#[cfg(feature = "serialize")]
294impl serde::Serialize for InvoiceLineItem {
295    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
296        use serde::ser::SerializeStruct;
297        let mut s = s.serialize_struct("InvoiceLineItem", 20)?;
298        s.serialize_field("amount", &self.amount)?;
299        s.serialize_field("currency", &self.currency)?;
300        s.serialize_field("description", &self.description)?;
301        s.serialize_field("discount_amounts", &self.discount_amounts)?;
302        s.serialize_field("discountable", &self.discountable)?;
303        s.serialize_field("discounts", &self.discounts)?;
304        s.serialize_field("id", &self.id)?;
305        s.serialize_field("invoice", &self.invoice)?;
306        s.serialize_field("livemode", &self.livemode)?;
307        s.serialize_field("metadata", &self.metadata)?;
308        s.serialize_field("parent", &self.parent)?;
309        s.serialize_field("period", &self.period)?;
310        s.serialize_field("pretax_credit_amounts", &self.pretax_credit_amounts)?;
311        s.serialize_field("pricing", &self.pricing)?;
312        s.serialize_field("quantity", &self.quantity)?;
313        s.serialize_field("quantity_decimal", &self.quantity_decimal)?;
314        s.serialize_field("subscription", &self.subscription)?;
315        s.serialize_field("subtotal", &self.subtotal)?;
316        s.serialize_field("taxes", &self.taxes)?;
317
318        s.serialize_field("object", "line_item")?;
319        s.end()
320    }
321}
322impl stripe_types::Object for InvoiceLineItem {
323    type Id = stripe_shared::InvoiceLineItemId;
324    fn id(&self) -> &Self::Id {
325        &self.id
326    }
327
328    fn into_id(self) -> Self::Id {
329        self.id
330    }
331}
332stripe_types::def_id!(InvoiceLineItemId);