stripe_misc/tax_calculation/
types.rs

1/// A Tax Calculation allows you to calculate the tax to collect from your customer.
2///
3/// Related guide: [Calculate tax in your custom payment flow](https://stripe.com/docs/tax/custom)
4///
5/// For more details see <<https://stripe.com/docs/api/tax/calculations/object>>.
6#[derive(Clone, Debug)]
7#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
8pub struct TaxCalculation {
9    /// Total amount after taxes in the [smallest currency unit](https://stripe.com/docs/currencies#zero-decimal).
10    pub amount_total: i64,
11    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
12    /// Must be a [supported currency](https://stripe.com/docs/currencies).
13    pub currency: stripe_types::Currency,
14    /// The ID of an existing [Customer](https://stripe.com/docs/api/customers/object) used for the resource.
15    pub customer: Option<String>,
16    pub customer_details: stripe_misc::TaxProductResourceCustomerDetails,
17    /// Timestamp of date at which the tax calculation will expire.
18    pub expires_at: Option<stripe_types::Timestamp>,
19    /// Unique identifier for the calculation.
20    pub id: Option<stripe_misc::TaxCalculationId>,
21    /// The list of items the customer is purchasing.
22    pub line_items: Option<stripe_types::List<stripe_misc::TaxCalculationLineItem>>,
23    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
24    pub livemode: bool,
25    /// The details of the ship from location, such as the address.
26    pub ship_from_details: Option<stripe_misc::TaxProductResourceShipFromDetails>,
27    /// The shipping cost details for the calculation.
28    pub shipping_cost: Option<stripe_misc::TaxProductResourceTaxCalculationShippingCost>,
29    /// The amount of tax to be collected on top of the line item prices.
30    pub tax_amount_exclusive: i64,
31    /// The amount of tax already included in the line item prices.
32    pub tax_amount_inclusive: i64,
33    /// Breakdown of individual tax amounts that add up to the total.
34    pub tax_breakdown: Vec<stripe_misc::TaxProductResourceTaxBreakdown>,
35    /// Timestamp of date at which the tax rules and rates in effect applies for the calculation.
36    pub tax_date: stripe_types::Timestamp,
37}
38#[doc(hidden)]
39pub struct TaxCalculationBuilder {
40    amount_total: Option<i64>,
41    currency: Option<stripe_types::Currency>,
42    customer: Option<Option<String>>,
43    customer_details: Option<stripe_misc::TaxProductResourceCustomerDetails>,
44    expires_at: Option<Option<stripe_types::Timestamp>>,
45    id: Option<Option<stripe_misc::TaxCalculationId>>,
46    line_items: Option<Option<stripe_types::List<stripe_misc::TaxCalculationLineItem>>>,
47    livemode: Option<bool>,
48    ship_from_details: Option<Option<stripe_misc::TaxProductResourceShipFromDetails>>,
49    shipping_cost: Option<Option<stripe_misc::TaxProductResourceTaxCalculationShippingCost>>,
50    tax_amount_exclusive: Option<i64>,
51    tax_amount_inclusive: Option<i64>,
52    tax_breakdown: Option<Vec<stripe_misc::TaxProductResourceTaxBreakdown>>,
53    tax_date: Option<stripe_types::Timestamp>,
54}
55
56#[allow(
57    unused_variables,
58    irrefutable_let_patterns,
59    clippy::let_unit_value,
60    clippy::match_single_binding,
61    clippy::single_match
62)]
63const _: () = {
64    use miniserde::de::{Map, Visitor};
65    use miniserde::json::Value;
66    use miniserde::{Deserialize, Result, make_place};
67    use stripe_types::miniserde_helpers::FromValueOpt;
68    use stripe_types::{MapBuilder, ObjectDeser};
69
70    make_place!(Place);
71
72    impl Deserialize for TaxCalculation {
73        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
74            Place::new(out)
75        }
76    }
77
78    struct Builder<'a> {
79        out: &'a mut Option<TaxCalculation>,
80        builder: TaxCalculationBuilder,
81    }
82
83    impl Visitor for Place<TaxCalculation> {
84        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
85            Ok(Box::new(Builder {
86                out: &mut self.out,
87                builder: TaxCalculationBuilder::deser_default(),
88            }))
89        }
90    }
91
92    impl MapBuilder for TaxCalculationBuilder {
93        type Out = TaxCalculation;
94        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
95            Ok(match k {
96                "amount_total" => Deserialize::begin(&mut self.amount_total),
97                "currency" => Deserialize::begin(&mut self.currency),
98                "customer" => Deserialize::begin(&mut self.customer),
99                "customer_details" => Deserialize::begin(&mut self.customer_details),
100                "expires_at" => Deserialize::begin(&mut self.expires_at),
101                "id" => Deserialize::begin(&mut self.id),
102                "line_items" => Deserialize::begin(&mut self.line_items),
103                "livemode" => Deserialize::begin(&mut self.livemode),
104                "ship_from_details" => Deserialize::begin(&mut self.ship_from_details),
105                "shipping_cost" => Deserialize::begin(&mut self.shipping_cost),
106                "tax_amount_exclusive" => Deserialize::begin(&mut self.tax_amount_exclusive),
107                "tax_amount_inclusive" => Deserialize::begin(&mut self.tax_amount_inclusive),
108                "tax_breakdown" => Deserialize::begin(&mut self.tax_breakdown),
109                "tax_date" => Deserialize::begin(&mut self.tax_date),
110
111                _ => <dyn Visitor>::ignore(),
112            })
113        }
114
115        fn deser_default() -> Self {
116            Self {
117                amount_total: Deserialize::default(),
118                currency: Deserialize::default(),
119                customer: Deserialize::default(),
120                customer_details: Deserialize::default(),
121                expires_at: Deserialize::default(),
122                id: Deserialize::default(),
123                line_items: Deserialize::default(),
124                livemode: Deserialize::default(),
125                ship_from_details: Deserialize::default(),
126                shipping_cost: Deserialize::default(),
127                tax_amount_exclusive: Deserialize::default(),
128                tax_amount_inclusive: Deserialize::default(),
129                tax_breakdown: Deserialize::default(),
130                tax_date: Deserialize::default(),
131            }
132        }
133
134        fn take_out(&mut self) -> Option<Self::Out> {
135            let (
136                Some(amount_total),
137                Some(currency),
138                Some(customer),
139                Some(customer_details),
140                Some(expires_at),
141                Some(id),
142                Some(line_items),
143                Some(livemode),
144                Some(ship_from_details),
145                Some(shipping_cost),
146                Some(tax_amount_exclusive),
147                Some(tax_amount_inclusive),
148                Some(tax_breakdown),
149                Some(tax_date),
150            ) = (
151                self.amount_total,
152                self.currency.take(),
153                self.customer.take(),
154                self.customer_details.take(),
155                self.expires_at,
156                self.id.take(),
157                self.line_items.take(),
158                self.livemode,
159                self.ship_from_details.take(),
160                self.shipping_cost.take(),
161                self.tax_amount_exclusive,
162                self.tax_amount_inclusive,
163                self.tax_breakdown.take(),
164                self.tax_date,
165            )
166            else {
167                return None;
168            };
169            Some(Self::Out {
170                amount_total,
171                currency,
172                customer,
173                customer_details,
174                expires_at,
175                id,
176                line_items,
177                livemode,
178                ship_from_details,
179                shipping_cost,
180                tax_amount_exclusive,
181                tax_amount_inclusive,
182                tax_breakdown,
183                tax_date,
184            })
185        }
186    }
187
188    impl Map for Builder<'_> {
189        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
190            self.builder.key(k)
191        }
192
193        fn finish(&mut self) -> Result<()> {
194            *self.out = self.builder.take_out();
195            Ok(())
196        }
197    }
198
199    impl ObjectDeser for TaxCalculation {
200        type Builder = TaxCalculationBuilder;
201    }
202
203    impl FromValueOpt for TaxCalculation {
204        fn from_value(v: Value) -> Option<Self> {
205            let Value::Object(obj) = v else {
206                return None;
207            };
208            let mut b = TaxCalculationBuilder::deser_default();
209            for (k, v) in obj {
210                match k.as_str() {
211                    "amount_total" => b.amount_total = FromValueOpt::from_value(v),
212                    "currency" => b.currency = FromValueOpt::from_value(v),
213                    "customer" => b.customer = FromValueOpt::from_value(v),
214                    "customer_details" => b.customer_details = FromValueOpt::from_value(v),
215                    "expires_at" => b.expires_at = FromValueOpt::from_value(v),
216                    "id" => b.id = FromValueOpt::from_value(v),
217                    "line_items" => b.line_items = FromValueOpt::from_value(v),
218                    "livemode" => b.livemode = FromValueOpt::from_value(v),
219                    "ship_from_details" => b.ship_from_details = FromValueOpt::from_value(v),
220                    "shipping_cost" => b.shipping_cost = FromValueOpt::from_value(v),
221                    "tax_amount_exclusive" => b.tax_amount_exclusive = FromValueOpt::from_value(v),
222                    "tax_amount_inclusive" => b.tax_amount_inclusive = FromValueOpt::from_value(v),
223                    "tax_breakdown" => b.tax_breakdown = FromValueOpt::from_value(v),
224                    "tax_date" => b.tax_date = FromValueOpt::from_value(v),
225
226                    _ => {}
227                }
228            }
229            b.take_out()
230        }
231    }
232};
233#[cfg(feature = "serialize")]
234impl serde::Serialize for TaxCalculation {
235    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
236        use serde::ser::SerializeStruct;
237        let mut s = s.serialize_struct("TaxCalculation", 15)?;
238        s.serialize_field("amount_total", &self.amount_total)?;
239        s.serialize_field("currency", &self.currency)?;
240        s.serialize_field("customer", &self.customer)?;
241        s.serialize_field("customer_details", &self.customer_details)?;
242        s.serialize_field("expires_at", &self.expires_at)?;
243        s.serialize_field("id", &self.id)?;
244        s.serialize_field("line_items", &self.line_items)?;
245        s.serialize_field("livemode", &self.livemode)?;
246        s.serialize_field("ship_from_details", &self.ship_from_details)?;
247        s.serialize_field("shipping_cost", &self.shipping_cost)?;
248        s.serialize_field("tax_amount_exclusive", &self.tax_amount_exclusive)?;
249        s.serialize_field("tax_amount_inclusive", &self.tax_amount_inclusive)?;
250        s.serialize_field("tax_breakdown", &self.tax_breakdown)?;
251        s.serialize_field("tax_date", &self.tax_date)?;
252
253        s.serialize_field("object", "tax.calculation")?;
254        s.end()
255    }
256}
257impl stripe_types::Object for TaxCalculation {
258    type Id = Option<stripe_misc::TaxCalculationId>;
259    fn id(&self) -> &Self::Id {
260        &self.id
261    }
262
263    fn into_id(self) -> Self::Id {
264        self.id
265    }
266}
267stripe_types::def_id!(TaxCalculationId);