stripe_shared/
tax_rate.rs

1/// Tax rates can be applied to [invoices](/invoicing/taxes/tax-rates), [subscriptions](/billing/taxes/tax-rates) and [Checkout Sessions](/payments/checkout/use-manual-tax-rates) to collect tax.
2///
3/// Related guide: [Tax rates](/billing/taxes/tax-rates)
4///
5/// For more details see <<https://stripe.com/docs/api/tax_rates/object>>.
6#[derive(Clone, Debug)]
7#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
8pub struct TaxRate {
9    /// Defaults to `true`.
10    /// When set to `false`, this tax rate cannot be used with new applications or Checkout Sessions, but will still work for subscriptions and invoices that already have it set.
11    pub active: bool,
12    /// Two-letter country code ([ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)).
13    pub country: Option<String>,
14    /// Time at which the object was created. Measured in seconds since the Unix epoch.
15    pub created: stripe_types::Timestamp,
16    /// An arbitrary string attached to the tax rate for your internal use only.
17    /// It will not be visible to your customers.
18    pub description: Option<String>,
19    /// The display name of the tax rates as it will appear to your customer on their receipt email, PDF, and the hosted invoice page.
20    pub display_name: String,
21    /// Actual/effective tax rate percentage out of 100.
22    /// For tax calculations with automatic_tax[enabled]=true,.
23    /// this percentage reflects the rate actually used to calculate tax based on the product's taxability
24    /// and whether the user is registered to collect taxes in the corresponding jurisdiction.
25    pub effective_percentage: Option<f64>,
26    /// The amount of the tax rate when the `rate_type` is `flat_amount`.
27    /// Tax rates with `rate_type` `percentage` can vary based on the transaction, resulting in this field being `null`.
28    /// This field exposes the amount and currency of the flat tax rate.
29    pub flat_amount: Option<stripe_shared::TaxRateFlatAmount>,
30    /// Unique identifier for the object.
31    pub id: stripe_shared::TaxRateId,
32    /// This specifies if the tax rate is inclusive or exclusive.
33    pub inclusive: bool,
34    /// The jurisdiction for the tax rate.
35    /// You can use this label field for tax reporting purposes.
36    /// It also appears on your customer’s invoice.
37    pub jurisdiction: Option<String>,
38    /// The level of the jurisdiction that imposes this tax rate.
39    /// Will be `null` for manually defined tax rates.
40    pub jurisdiction_level: Option<TaxRateJurisdictionLevel>,
41    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
42    pub livemode: bool,
43    /// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object.
44    /// This can be useful for storing additional information about the object in a structured format.
45    pub metadata: Option<std::collections::HashMap<String, String>>,
46    /// Tax rate percentage out of 100.
47    /// For tax calculations with automatic_tax[enabled]=true, this percentage includes the statutory tax rate of non-taxable jurisdictions.
48    pub percentage: f64,
49    /// Indicates the type of tax rate applied to the taxable amount.
50    /// This value can be `null` when no tax applies to the location.
51    /// This field is only present for TaxRates created by Stripe Tax.
52    pub rate_type: Option<TaxRateRateType>,
53    /// [ISO 3166-2 subdivision code](https://en.wikipedia.org/wiki/ISO_3166-2), without country prefix.
54    /// For example, "NY" for New York, United States.
55    pub state: Option<String>,
56    /// The high-level tax type, such as `vat` or `sales_tax`.
57    pub tax_type: Option<stripe_shared::TaxRateTaxType>,
58}
59#[doc(hidden)]
60pub struct TaxRateBuilder {
61    active: Option<bool>,
62    country: Option<Option<String>>,
63    created: Option<stripe_types::Timestamp>,
64    description: Option<Option<String>>,
65    display_name: Option<String>,
66    effective_percentage: Option<Option<f64>>,
67    flat_amount: Option<Option<stripe_shared::TaxRateFlatAmount>>,
68    id: Option<stripe_shared::TaxRateId>,
69    inclusive: Option<bool>,
70    jurisdiction: Option<Option<String>>,
71    jurisdiction_level: Option<Option<TaxRateJurisdictionLevel>>,
72    livemode: Option<bool>,
73    metadata: Option<Option<std::collections::HashMap<String, String>>>,
74    percentage: Option<f64>,
75    rate_type: Option<Option<TaxRateRateType>>,
76    state: Option<Option<String>>,
77    tax_type: Option<Option<stripe_shared::TaxRateTaxType>>,
78}
79
80#[allow(
81    unused_variables,
82    irrefutable_let_patterns,
83    clippy::let_unit_value,
84    clippy::match_single_binding,
85    clippy::single_match
86)]
87const _: () = {
88    use miniserde::de::{Map, Visitor};
89    use miniserde::json::Value;
90    use miniserde::{Deserialize, Result, make_place};
91    use stripe_types::miniserde_helpers::FromValueOpt;
92    use stripe_types::{MapBuilder, ObjectDeser};
93
94    make_place!(Place);
95
96    impl Deserialize for TaxRate {
97        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
98            Place::new(out)
99        }
100    }
101
102    struct Builder<'a> {
103        out: &'a mut Option<TaxRate>,
104        builder: TaxRateBuilder,
105    }
106
107    impl Visitor for Place<TaxRate> {
108        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
109            Ok(Box::new(Builder { out: &mut self.out, builder: TaxRateBuilder::deser_default() }))
110        }
111    }
112
113    impl MapBuilder for TaxRateBuilder {
114        type Out = TaxRate;
115        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
116            Ok(match k {
117                "active" => Deserialize::begin(&mut self.active),
118                "country" => Deserialize::begin(&mut self.country),
119                "created" => Deserialize::begin(&mut self.created),
120                "description" => Deserialize::begin(&mut self.description),
121                "display_name" => Deserialize::begin(&mut self.display_name),
122                "effective_percentage" => Deserialize::begin(&mut self.effective_percentage),
123                "flat_amount" => Deserialize::begin(&mut self.flat_amount),
124                "id" => Deserialize::begin(&mut self.id),
125                "inclusive" => Deserialize::begin(&mut self.inclusive),
126                "jurisdiction" => Deserialize::begin(&mut self.jurisdiction),
127                "jurisdiction_level" => Deserialize::begin(&mut self.jurisdiction_level),
128                "livemode" => Deserialize::begin(&mut self.livemode),
129                "metadata" => Deserialize::begin(&mut self.metadata),
130                "percentage" => Deserialize::begin(&mut self.percentage),
131                "rate_type" => Deserialize::begin(&mut self.rate_type),
132                "state" => Deserialize::begin(&mut self.state),
133                "tax_type" => Deserialize::begin(&mut self.tax_type),
134                _ => <dyn Visitor>::ignore(),
135            })
136        }
137
138        fn deser_default() -> Self {
139            Self {
140                active: Deserialize::default(),
141                country: Deserialize::default(),
142                created: Deserialize::default(),
143                description: Deserialize::default(),
144                display_name: Deserialize::default(),
145                effective_percentage: Deserialize::default(),
146                flat_amount: Deserialize::default(),
147                id: Deserialize::default(),
148                inclusive: Deserialize::default(),
149                jurisdiction: Deserialize::default(),
150                jurisdiction_level: Deserialize::default(),
151                livemode: Deserialize::default(),
152                metadata: Deserialize::default(),
153                percentage: Deserialize::default(),
154                rate_type: Deserialize::default(),
155                state: Deserialize::default(),
156                tax_type: Deserialize::default(),
157            }
158        }
159
160        fn take_out(&mut self) -> Option<Self::Out> {
161            let (
162                Some(active),
163                Some(country),
164                Some(created),
165                Some(description),
166                Some(display_name),
167                Some(effective_percentage),
168                Some(flat_amount),
169                Some(id),
170                Some(inclusive),
171                Some(jurisdiction),
172                Some(jurisdiction_level),
173                Some(livemode),
174                Some(metadata),
175                Some(percentage),
176                Some(rate_type),
177                Some(state),
178                Some(tax_type),
179            ) = (
180                self.active,
181                self.country.take(),
182                self.created,
183                self.description.take(),
184                self.display_name.take(),
185                self.effective_percentage,
186                self.flat_amount.take(),
187                self.id.take(),
188                self.inclusive,
189                self.jurisdiction.take(),
190                self.jurisdiction_level,
191                self.livemode,
192                self.metadata.take(),
193                self.percentage,
194                self.rate_type,
195                self.state.take(),
196                self.tax_type.take(),
197            )
198            else {
199                return None;
200            };
201            Some(Self::Out {
202                active,
203                country,
204                created,
205                description,
206                display_name,
207                effective_percentage,
208                flat_amount,
209                id,
210                inclusive,
211                jurisdiction,
212                jurisdiction_level,
213                livemode,
214                metadata,
215                percentage,
216                rate_type,
217                state,
218                tax_type,
219            })
220        }
221    }
222
223    impl Map for Builder<'_> {
224        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
225            self.builder.key(k)
226        }
227
228        fn finish(&mut self) -> Result<()> {
229            *self.out = self.builder.take_out();
230            Ok(())
231        }
232    }
233
234    impl ObjectDeser for TaxRate {
235        type Builder = TaxRateBuilder;
236    }
237
238    impl FromValueOpt for TaxRate {
239        fn from_value(v: Value) -> Option<Self> {
240            let Value::Object(obj) = v else {
241                return None;
242            };
243            let mut b = TaxRateBuilder::deser_default();
244            for (k, v) in obj {
245                match k.as_str() {
246                    "active" => b.active = FromValueOpt::from_value(v),
247                    "country" => b.country = FromValueOpt::from_value(v),
248                    "created" => b.created = FromValueOpt::from_value(v),
249                    "description" => b.description = FromValueOpt::from_value(v),
250                    "display_name" => b.display_name = FromValueOpt::from_value(v),
251                    "effective_percentage" => b.effective_percentage = FromValueOpt::from_value(v),
252                    "flat_amount" => b.flat_amount = FromValueOpt::from_value(v),
253                    "id" => b.id = FromValueOpt::from_value(v),
254                    "inclusive" => b.inclusive = FromValueOpt::from_value(v),
255                    "jurisdiction" => b.jurisdiction = FromValueOpt::from_value(v),
256                    "jurisdiction_level" => b.jurisdiction_level = FromValueOpt::from_value(v),
257                    "livemode" => b.livemode = FromValueOpt::from_value(v),
258                    "metadata" => b.metadata = FromValueOpt::from_value(v),
259                    "percentage" => b.percentage = FromValueOpt::from_value(v),
260                    "rate_type" => b.rate_type = FromValueOpt::from_value(v),
261                    "state" => b.state = FromValueOpt::from_value(v),
262                    "tax_type" => b.tax_type = FromValueOpt::from_value(v),
263                    _ => {}
264                }
265            }
266            b.take_out()
267        }
268    }
269};
270#[cfg(feature = "serialize")]
271impl serde::Serialize for TaxRate {
272    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
273        use serde::ser::SerializeStruct;
274        let mut s = s.serialize_struct("TaxRate", 18)?;
275        s.serialize_field("active", &self.active)?;
276        s.serialize_field("country", &self.country)?;
277        s.serialize_field("created", &self.created)?;
278        s.serialize_field("description", &self.description)?;
279        s.serialize_field("display_name", &self.display_name)?;
280        s.serialize_field("effective_percentage", &self.effective_percentage)?;
281        s.serialize_field("flat_amount", &self.flat_amount)?;
282        s.serialize_field("id", &self.id)?;
283        s.serialize_field("inclusive", &self.inclusive)?;
284        s.serialize_field("jurisdiction", &self.jurisdiction)?;
285        s.serialize_field("jurisdiction_level", &self.jurisdiction_level)?;
286        s.serialize_field("livemode", &self.livemode)?;
287        s.serialize_field("metadata", &self.metadata)?;
288        s.serialize_field("percentage", &self.percentage)?;
289        s.serialize_field("rate_type", &self.rate_type)?;
290        s.serialize_field("state", &self.state)?;
291        s.serialize_field("tax_type", &self.tax_type)?;
292
293        s.serialize_field("object", "tax_rate")?;
294        s.end()
295    }
296}
297/// The level of the jurisdiction that imposes this tax rate.
298/// Will be `null` for manually defined tax rates.
299#[derive(Copy, Clone, Eq, PartialEq)]
300pub enum TaxRateJurisdictionLevel {
301    City,
302    Country,
303    County,
304    District,
305    Multiple,
306    State,
307}
308impl TaxRateJurisdictionLevel {
309    pub fn as_str(self) -> &'static str {
310        use TaxRateJurisdictionLevel::*;
311        match self {
312            City => "city",
313            Country => "country",
314            County => "county",
315            District => "district",
316            Multiple => "multiple",
317            State => "state",
318        }
319    }
320}
321
322impl std::str::FromStr for TaxRateJurisdictionLevel {
323    type Err = stripe_types::StripeParseError;
324    fn from_str(s: &str) -> Result<Self, Self::Err> {
325        use TaxRateJurisdictionLevel::*;
326        match s {
327            "city" => Ok(City),
328            "country" => Ok(Country),
329            "county" => Ok(County),
330            "district" => Ok(District),
331            "multiple" => Ok(Multiple),
332            "state" => Ok(State),
333            _ => Err(stripe_types::StripeParseError),
334        }
335    }
336}
337impl std::fmt::Display for TaxRateJurisdictionLevel {
338    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
339        f.write_str(self.as_str())
340    }
341}
342
343impl std::fmt::Debug for TaxRateJurisdictionLevel {
344    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
345        f.write_str(self.as_str())
346    }
347}
348#[cfg(feature = "serialize")]
349impl serde::Serialize for TaxRateJurisdictionLevel {
350    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
351    where
352        S: serde::Serializer,
353    {
354        serializer.serialize_str(self.as_str())
355    }
356}
357impl miniserde::Deserialize for TaxRateJurisdictionLevel {
358    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
359        crate::Place::new(out)
360    }
361}
362
363impl miniserde::de::Visitor for crate::Place<TaxRateJurisdictionLevel> {
364    fn string(&mut self, s: &str) -> miniserde::Result<()> {
365        use std::str::FromStr;
366        self.out = Some(TaxRateJurisdictionLevel::from_str(s).map_err(|_| miniserde::Error)?);
367        Ok(())
368    }
369}
370
371stripe_types::impl_from_val_with_from_str!(TaxRateJurisdictionLevel);
372#[cfg(feature = "deserialize")]
373impl<'de> serde::Deserialize<'de> for TaxRateJurisdictionLevel {
374    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
375        use std::str::FromStr;
376        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
377        Self::from_str(&s)
378            .map_err(|_| serde::de::Error::custom("Unknown value for TaxRateJurisdictionLevel"))
379    }
380}
381/// Indicates the type of tax rate applied to the taxable amount.
382/// This value can be `null` when no tax applies to the location.
383/// This field is only present for TaxRates created by Stripe Tax.
384#[derive(Copy, Clone, Eq, PartialEq)]
385pub enum TaxRateRateType {
386    FlatAmount,
387    Percentage,
388}
389impl TaxRateRateType {
390    pub fn as_str(self) -> &'static str {
391        use TaxRateRateType::*;
392        match self {
393            FlatAmount => "flat_amount",
394            Percentage => "percentage",
395        }
396    }
397}
398
399impl std::str::FromStr for TaxRateRateType {
400    type Err = stripe_types::StripeParseError;
401    fn from_str(s: &str) -> Result<Self, Self::Err> {
402        use TaxRateRateType::*;
403        match s {
404            "flat_amount" => Ok(FlatAmount),
405            "percentage" => Ok(Percentage),
406            _ => Err(stripe_types::StripeParseError),
407        }
408    }
409}
410impl std::fmt::Display for TaxRateRateType {
411    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
412        f.write_str(self.as_str())
413    }
414}
415
416impl std::fmt::Debug for TaxRateRateType {
417    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
418        f.write_str(self.as_str())
419    }
420}
421#[cfg(feature = "serialize")]
422impl serde::Serialize for TaxRateRateType {
423    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
424    where
425        S: serde::Serializer,
426    {
427        serializer.serialize_str(self.as_str())
428    }
429}
430impl miniserde::Deserialize for TaxRateRateType {
431    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
432        crate::Place::new(out)
433    }
434}
435
436impl miniserde::de::Visitor for crate::Place<TaxRateRateType> {
437    fn string(&mut self, s: &str) -> miniserde::Result<()> {
438        use std::str::FromStr;
439        self.out = Some(TaxRateRateType::from_str(s).map_err(|_| miniserde::Error)?);
440        Ok(())
441    }
442}
443
444stripe_types::impl_from_val_with_from_str!(TaxRateRateType);
445#[cfg(feature = "deserialize")]
446impl<'de> serde::Deserialize<'de> for TaxRateRateType {
447    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
448        use std::str::FromStr;
449        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
450        Self::from_str(&s)
451            .map_err(|_| serde::de::Error::custom("Unknown value for TaxRateRateType"))
452    }
453}
454impl stripe_types::Object for TaxRate {
455    type Id = stripe_shared::TaxRateId;
456    fn id(&self) -> &Self::Id {
457        &self.id
458    }
459
460    fn into_id(self) -> Self::Id {
461        self.id
462    }
463}
464stripe_types::def_id!(TaxRateId);
465#[derive(Clone, Eq, PartialEq)]
466#[non_exhaustive]
467pub enum TaxRateTaxType {
468    AmusementTax,
469    CommunicationsTax,
470    Gst,
471    Hst,
472    Igst,
473    Jct,
474    LeaseTax,
475    Pst,
476    Qst,
477    RetailDeliveryFee,
478    Rst,
479    SalesTax,
480    ServiceTax,
481    Vat,
482    /// An unrecognized value from Stripe. Should not be used as a request parameter.
483    Unknown(String),
484}
485impl TaxRateTaxType {
486    pub fn as_str(&self) -> &str {
487        use TaxRateTaxType::*;
488        match self {
489            AmusementTax => "amusement_tax",
490            CommunicationsTax => "communications_tax",
491            Gst => "gst",
492            Hst => "hst",
493            Igst => "igst",
494            Jct => "jct",
495            LeaseTax => "lease_tax",
496            Pst => "pst",
497            Qst => "qst",
498            RetailDeliveryFee => "retail_delivery_fee",
499            Rst => "rst",
500            SalesTax => "sales_tax",
501            ServiceTax => "service_tax",
502            Vat => "vat",
503            Unknown(v) => v,
504        }
505    }
506}
507
508impl std::str::FromStr for TaxRateTaxType {
509    type Err = std::convert::Infallible;
510    fn from_str(s: &str) -> Result<Self, Self::Err> {
511        use TaxRateTaxType::*;
512        match s {
513            "amusement_tax" => Ok(AmusementTax),
514            "communications_tax" => Ok(CommunicationsTax),
515            "gst" => Ok(Gst),
516            "hst" => Ok(Hst),
517            "igst" => Ok(Igst),
518            "jct" => Ok(Jct),
519            "lease_tax" => Ok(LeaseTax),
520            "pst" => Ok(Pst),
521            "qst" => Ok(Qst),
522            "retail_delivery_fee" => Ok(RetailDeliveryFee),
523            "rst" => Ok(Rst),
524            "sales_tax" => Ok(SalesTax),
525            "service_tax" => Ok(ServiceTax),
526            "vat" => Ok(Vat),
527            v => Ok(Unknown(v.to_owned())),
528        }
529    }
530}
531impl std::fmt::Display for TaxRateTaxType {
532    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
533        f.write_str(self.as_str())
534    }
535}
536
537impl std::fmt::Debug for TaxRateTaxType {
538    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
539        f.write_str(self.as_str())
540    }
541}
542impl serde::Serialize for TaxRateTaxType {
543    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
544    where
545        S: serde::Serializer,
546    {
547        serializer.serialize_str(self.as_str())
548    }
549}
550impl miniserde::Deserialize for TaxRateTaxType {
551    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
552        crate::Place::new(out)
553    }
554}
555
556impl miniserde::de::Visitor for crate::Place<TaxRateTaxType> {
557    fn string(&mut self, s: &str) -> miniserde::Result<()> {
558        use std::str::FromStr;
559        self.out = Some(TaxRateTaxType::from_str(s).unwrap());
560        Ok(())
561    }
562}
563
564stripe_types::impl_from_val_with_from_str!(TaxRateTaxType);
565#[cfg(feature = "deserialize")]
566impl<'de> serde::Deserialize<'de> for TaxRateTaxType {
567    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
568        use std::str::FromStr;
569        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
570        Ok(Self::from_str(&s).unwrap())
571    }
572}