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                _ => <dyn Visitor>::ignore(),
111            })
112        }
113
114        fn deser_default() -> Self {
115            Self {
116                amount_total: Deserialize::default(),
117                currency: Deserialize::default(),
118                customer: Deserialize::default(),
119                customer_details: Deserialize::default(),
120                expires_at: Deserialize::default(),
121                id: Deserialize::default(),
122                line_items: Deserialize::default(),
123                livemode: Deserialize::default(),
124                ship_from_details: Deserialize::default(),
125                shipping_cost: Deserialize::default(),
126                tax_amount_exclusive: Deserialize::default(),
127                tax_amount_inclusive: Deserialize::default(),
128                tax_breakdown: Deserialize::default(),
129                tax_date: Deserialize::default(),
130            }
131        }
132
133        fn take_out(&mut self) -> Option<Self::Out> {
134            let (
135                Some(amount_total),
136                Some(currency),
137                Some(customer),
138                Some(customer_details),
139                Some(expires_at),
140                Some(id),
141                Some(line_items),
142                Some(livemode),
143                Some(ship_from_details),
144                Some(shipping_cost),
145                Some(tax_amount_exclusive),
146                Some(tax_amount_inclusive),
147                Some(tax_breakdown),
148                Some(tax_date),
149            ) = (
150                self.amount_total,
151                self.currency.take(),
152                self.customer.take(),
153                self.customer_details.take(),
154                self.expires_at,
155                self.id.take(),
156                self.line_items.take(),
157                self.livemode,
158                self.ship_from_details.take(),
159                self.shipping_cost.take(),
160                self.tax_amount_exclusive,
161                self.tax_amount_inclusive,
162                self.tax_breakdown.take(),
163                self.tax_date,
164            )
165            else {
166                return None;
167            };
168            Some(Self::Out {
169                amount_total,
170                currency,
171                customer,
172                customer_details,
173                expires_at,
174                id,
175                line_items,
176                livemode,
177                ship_from_details,
178                shipping_cost,
179                tax_amount_exclusive,
180                tax_amount_inclusive,
181                tax_breakdown,
182                tax_date,
183            })
184        }
185    }
186
187    impl Map for Builder<'_> {
188        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
189            self.builder.key(k)
190        }
191
192        fn finish(&mut self) -> Result<()> {
193            *self.out = self.builder.take_out();
194            Ok(())
195        }
196    }
197
198    impl ObjectDeser for TaxCalculation {
199        type Builder = TaxCalculationBuilder;
200    }
201
202    impl FromValueOpt for TaxCalculation {
203        fn from_value(v: Value) -> Option<Self> {
204            let Value::Object(obj) = v else {
205                return None;
206            };
207            let mut b = TaxCalculationBuilder::deser_default();
208            for (k, v) in obj {
209                match k.as_str() {
210                    "amount_total" => b.amount_total = FromValueOpt::from_value(v),
211                    "currency" => b.currency = FromValueOpt::from_value(v),
212                    "customer" => b.customer = FromValueOpt::from_value(v),
213                    "customer_details" => b.customer_details = FromValueOpt::from_value(v),
214                    "expires_at" => b.expires_at = FromValueOpt::from_value(v),
215                    "id" => b.id = FromValueOpt::from_value(v),
216                    "line_items" => b.line_items = FromValueOpt::from_value(v),
217                    "livemode" => b.livemode = FromValueOpt::from_value(v),
218                    "ship_from_details" => b.ship_from_details = FromValueOpt::from_value(v),
219                    "shipping_cost" => b.shipping_cost = FromValueOpt::from_value(v),
220                    "tax_amount_exclusive" => b.tax_amount_exclusive = FromValueOpt::from_value(v),
221                    "tax_amount_inclusive" => b.tax_amount_inclusive = FromValueOpt::from_value(v),
222                    "tax_breakdown" => b.tax_breakdown = FromValueOpt::from_value(v),
223                    "tax_date" => b.tax_date = FromValueOpt::from_value(v),
224                    _ => {}
225                }
226            }
227            b.take_out()
228        }
229    }
230};
231#[cfg(feature = "serialize")]
232impl serde::Serialize for TaxCalculation {
233    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
234        use serde::ser::SerializeStruct;
235        let mut s = s.serialize_struct("TaxCalculation", 15)?;
236        s.serialize_field("amount_total", &self.amount_total)?;
237        s.serialize_field("currency", &self.currency)?;
238        s.serialize_field("customer", &self.customer)?;
239        s.serialize_field("customer_details", &self.customer_details)?;
240        s.serialize_field("expires_at", &self.expires_at)?;
241        s.serialize_field("id", &self.id)?;
242        s.serialize_field("line_items", &self.line_items)?;
243        s.serialize_field("livemode", &self.livemode)?;
244        s.serialize_field("ship_from_details", &self.ship_from_details)?;
245        s.serialize_field("shipping_cost", &self.shipping_cost)?;
246        s.serialize_field("tax_amount_exclusive", &self.tax_amount_exclusive)?;
247        s.serialize_field("tax_amount_inclusive", &self.tax_amount_inclusive)?;
248        s.serialize_field("tax_breakdown", &self.tax_breakdown)?;
249        s.serialize_field("tax_date", &self.tax_date)?;
250
251        s.serialize_field("object", "tax.calculation")?;
252        s.end()
253    }
254}
255impl stripe_types::Object for TaxCalculation {
256    type Id = Option<stripe_misc::TaxCalculationId>;
257    fn id(&self) -> &Self::Id {
258        &self.id
259    }
260
261    fn into_id(self) -> Self::Id {
262        self.id
263    }
264}
265stripe_types::def_id!(TaxCalculationId);