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
135                _ => <dyn Visitor>::ignore(),
136            })
137        }
138
139        fn deser_default() -> Self {
140            Self {
141                active: Deserialize::default(),
142                country: Deserialize::default(),
143                created: Deserialize::default(),
144                description: Deserialize::default(),
145                display_name: Deserialize::default(),
146                effective_percentage: Deserialize::default(),
147                flat_amount: Deserialize::default(),
148                id: Deserialize::default(),
149                inclusive: Deserialize::default(),
150                jurisdiction: Deserialize::default(),
151                jurisdiction_level: Deserialize::default(),
152                livemode: Deserialize::default(),
153                metadata: Deserialize::default(),
154                percentage: Deserialize::default(),
155                rate_type: Deserialize::default(),
156                state: Deserialize::default(),
157                tax_type: Deserialize::default(),
158            }
159        }
160
161        fn take_out(&mut self) -> Option<Self::Out> {
162            let (
163                Some(active),
164                Some(country),
165                Some(created),
166                Some(description),
167                Some(display_name),
168                Some(effective_percentage),
169                Some(flat_amount),
170                Some(id),
171                Some(inclusive),
172                Some(jurisdiction),
173                Some(jurisdiction_level),
174                Some(livemode),
175                Some(metadata),
176                Some(percentage),
177                Some(rate_type),
178                Some(state),
179                Some(tax_type),
180            ) = (
181                self.active,
182                self.country.take(),
183                self.created,
184                self.description.take(),
185                self.display_name.take(),
186                self.effective_percentage,
187                self.flat_amount.take(),
188                self.id.take(),
189                self.inclusive,
190                self.jurisdiction.take(),
191                self.jurisdiction_level,
192                self.livemode,
193                self.metadata.take(),
194                self.percentage,
195                self.rate_type,
196                self.state.take(),
197                self.tax_type.take(),
198            )
199            else {
200                return None;
201            };
202            Some(Self::Out {
203                active,
204                country,
205                created,
206                description,
207                display_name,
208                effective_percentage,
209                flat_amount,
210                id,
211                inclusive,
212                jurisdiction,
213                jurisdiction_level,
214                livemode,
215                metadata,
216                percentage,
217                rate_type,
218                state,
219                tax_type,
220            })
221        }
222    }
223
224    impl Map for Builder<'_> {
225        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
226            self.builder.key(k)
227        }
228
229        fn finish(&mut self) -> Result<()> {
230            *self.out = self.builder.take_out();
231            Ok(())
232        }
233    }
234
235    impl ObjectDeser for TaxRate {
236        type Builder = TaxRateBuilder;
237    }
238
239    impl FromValueOpt for TaxRate {
240        fn from_value(v: Value) -> Option<Self> {
241            let Value::Object(obj) = v else {
242                return None;
243            };
244            let mut b = TaxRateBuilder::deser_default();
245            for (k, v) in obj {
246                match k.as_str() {
247                    "active" => b.active = FromValueOpt::from_value(v),
248                    "country" => b.country = FromValueOpt::from_value(v),
249                    "created" => b.created = FromValueOpt::from_value(v),
250                    "description" => b.description = FromValueOpt::from_value(v),
251                    "display_name" => b.display_name = FromValueOpt::from_value(v),
252                    "effective_percentage" => b.effective_percentage = FromValueOpt::from_value(v),
253                    "flat_amount" => b.flat_amount = FromValueOpt::from_value(v),
254                    "id" => b.id = FromValueOpt::from_value(v),
255                    "inclusive" => b.inclusive = FromValueOpt::from_value(v),
256                    "jurisdiction" => b.jurisdiction = FromValueOpt::from_value(v),
257                    "jurisdiction_level" => b.jurisdiction_level = FromValueOpt::from_value(v),
258                    "livemode" => b.livemode = FromValueOpt::from_value(v),
259                    "metadata" => b.metadata = FromValueOpt::from_value(v),
260                    "percentage" => b.percentage = FromValueOpt::from_value(v),
261                    "rate_type" => b.rate_type = FromValueOpt::from_value(v),
262                    "state" => b.state = FromValueOpt::from_value(v),
263                    "tax_type" => b.tax_type = FromValueOpt::from_value(v),
264
265                    _ => {}
266                }
267            }
268            b.take_out()
269        }
270    }
271};
272#[cfg(feature = "serialize")]
273impl serde::Serialize for TaxRate {
274    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
275        use serde::ser::SerializeStruct;
276        let mut s = s.serialize_struct("TaxRate", 18)?;
277        s.serialize_field("active", &self.active)?;
278        s.serialize_field("country", &self.country)?;
279        s.serialize_field("created", &self.created)?;
280        s.serialize_field("description", &self.description)?;
281        s.serialize_field("display_name", &self.display_name)?;
282        s.serialize_field("effective_percentage", &self.effective_percentage)?;
283        s.serialize_field("flat_amount", &self.flat_amount)?;
284        s.serialize_field("id", &self.id)?;
285        s.serialize_field("inclusive", &self.inclusive)?;
286        s.serialize_field("jurisdiction", &self.jurisdiction)?;
287        s.serialize_field("jurisdiction_level", &self.jurisdiction_level)?;
288        s.serialize_field("livemode", &self.livemode)?;
289        s.serialize_field("metadata", &self.metadata)?;
290        s.serialize_field("percentage", &self.percentage)?;
291        s.serialize_field("rate_type", &self.rate_type)?;
292        s.serialize_field("state", &self.state)?;
293        s.serialize_field("tax_type", &self.tax_type)?;
294
295        s.serialize_field("object", "tax_rate")?;
296        s.end()
297    }
298}
299/// The level of the jurisdiction that imposes this tax rate.
300/// Will be `null` for manually defined tax rates.
301#[derive(Copy, Clone, Eq, PartialEq)]
302pub enum TaxRateJurisdictionLevel {
303    City,
304    Country,
305    County,
306    District,
307    Multiple,
308    State,
309}
310impl TaxRateJurisdictionLevel {
311    pub fn as_str(self) -> &'static str {
312        use TaxRateJurisdictionLevel::*;
313        match self {
314            City => "city",
315            Country => "country",
316            County => "county",
317            District => "district",
318            Multiple => "multiple",
319            State => "state",
320        }
321    }
322}
323
324impl std::str::FromStr for TaxRateJurisdictionLevel {
325    type Err = stripe_types::StripeParseError;
326    fn from_str(s: &str) -> Result<Self, Self::Err> {
327        use TaxRateJurisdictionLevel::*;
328        match s {
329            "city" => Ok(City),
330            "country" => Ok(Country),
331            "county" => Ok(County),
332            "district" => Ok(District),
333            "multiple" => Ok(Multiple),
334            "state" => Ok(State),
335            _ => Err(stripe_types::StripeParseError),
336        }
337    }
338}
339impl std::fmt::Display for TaxRateJurisdictionLevel {
340    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
341        f.write_str(self.as_str())
342    }
343}
344
345impl std::fmt::Debug for TaxRateJurisdictionLevel {
346    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
347        f.write_str(self.as_str())
348    }
349}
350#[cfg(feature = "serialize")]
351impl serde::Serialize for TaxRateJurisdictionLevel {
352    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
353    where
354        S: serde::Serializer,
355    {
356        serializer.serialize_str(self.as_str())
357    }
358}
359impl miniserde::Deserialize for TaxRateJurisdictionLevel {
360    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
361        crate::Place::new(out)
362    }
363}
364
365impl miniserde::de::Visitor for crate::Place<TaxRateJurisdictionLevel> {
366    fn string(&mut self, s: &str) -> miniserde::Result<()> {
367        use std::str::FromStr;
368        self.out = Some(TaxRateJurisdictionLevel::from_str(s).map_err(|_| miniserde::Error)?);
369        Ok(())
370    }
371}
372
373stripe_types::impl_from_val_with_from_str!(TaxRateJurisdictionLevel);
374#[cfg(feature = "deserialize")]
375impl<'de> serde::Deserialize<'de> for TaxRateJurisdictionLevel {
376    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
377        use std::str::FromStr;
378        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
379        Self::from_str(&s)
380            .map_err(|_| serde::de::Error::custom("Unknown value for TaxRateJurisdictionLevel"))
381    }
382}
383/// Indicates the type of tax rate applied to the taxable amount.
384/// This value can be `null` when no tax applies to the location.
385/// This field is only present for TaxRates created by Stripe Tax.
386#[derive(Copy, Clone, Eq, PartialEq)]
387pub enum TaxRateRateType {
388    FlatAmount,
389    Percentage,
390}
391impl TaxRateRateType {
392    pub fn as_str(self) -> &'static str {
393        use TaxRateRateType::*;
394        match self {
395            FlatAmount => "flat_amount",
396            Percentage => "percentage",
397        }
398    }
399}
400
401impl std::str::FromStr for TaxRateRateType {
402    type Err = stripe_types::StripeParseError;
403    fn from_str(s: &str) -> Result<Self, Self::Err> {
404        use TaxRateRateType::*;
405        match s {
406            "flat_amount" => Ok(FlatAmount),
407            "percentage" => Ok(Percentage),
408            _ => Err(stripe_types::StripeParseError),
409        }
410    }
411}
412impl std::fmt::Display for TaxRateRateType {
413    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
414        f.write_str(self.as_str())
415    }
416}
417
418impl std::fmt::Debug for TaxRateRateType {
419    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
420        f.write_str(self.as_str())
421    }
422}
423#[cfg(feature = "serialize")]
424impl serde::Serialize for TaxRateRateType {
425    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
426    where
427        S: serde::Serializer,
428    {
429        serializer.serialize_str(self.as_str())
430    }
431}
432impl miniserde::Deserialize for TaxRateRateType {
433    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
434        crate::Place::new(out)
435    }
436}
437
438impl miniserde::de::Visitor for crate::Place<TaxRateRateType> {
439    fn string(&mut self, s: &str) -> miniserde::Result<()> {
440        use std::str::FromStr;
441        self.out = Some(TaxRateRateType::from_str(s).map_err(|_| miniserde::Error)?);
442        Ok(())
443    }
444}
445
446stripe_types::impl_from_val_with_from_str!(TaxRateRateType);
447#[cfg(feature = "deserialize")]
448impl<'de> serde::Deserialize<'de> for TaxRateRateType {
449    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
450        use std::str::FromStr;
451        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
452        Self::from_str(&s)
453            .map_err(|_| serde::de::Error::custom("Unknown value for TaxRateRateType"))
454    }
455}
456impl stripe_types::Object for TaxRate {
457    type Id = stripe_shared::TaxRateId;
458    fn id(&self) -> &Self::Id {
459        &self.id
460    }
461
462    fn into_id(self) -> Self::Id {
463        self.id
464    }
465}
466stripe_types::def_id!(TaxRateId);
467#[derive(Clone, Eq, PartialEq)]
468#[non_exhaustive]
469pub enum TaxRateTaxType {
470    AmusementTax,
471    CommunicationsTax,
472    Gst,
473    Hst,
474    Igst,
475    Jct,
476    LeaseTax,
477    Pst,
478    Qst,
479    RetailDeliveryFee,
480    Rst,
481    SalesTax,
482    ServiceTax,
483    Vat,
484    /// An unrecognized value from Stripe. Should not be used as a request parameter.
485    Unknown(String),
486}
487impl TaxRateTaxType {
488    pub fn as_str(&self) -> &str {
489        use TaxRateTaxType::*;
490        match self {
491            AmusementTax => "amusement_tax",
492            CommunicationsTax => "communications_tax",
493            Gst => "gst",
494            Hst => "hst",
495            Igst => "igst",
496            Jct => "jct",
497            LeaseTax => "lease_tax",
498            Pst => "pst",
499            Qst => "qst",
500            RetailDeliveryFee => "retail_delivery_fee",
501            Rst => "rst",
502            SalesTax => "sales_tax",
503            ServiceTax => "service_tax",
504            Vat => "vat",
505            Unknown(v) => v,
506        }
507    }
508}
509
510impl std::str::FromStr for TaxRateTaxType {
511    type Err = std::convert::Infallible;
512    fn from_str(s: &str) -> Result<Self, Self::Err> {
513        use TaxRateTaxType::*;
514        match s {
515            "amusement_tax" => Ok(AmusementTax),
516            "communications_tax" => Ok(CommunicationsTax),
517            "gst" => Ok(Gst),
518            "hst" => Ok(Hst),
519            "igst" => Ok(Igst),
520            "jct" => Ok(Jct),
521            "lease_tax" => Ok(LeaseTax),
522            "pst" => Ok(Pst),
523            "qst" => Ok(Qst),
524            "retail_delivery_fee" => Ok(RetailDeliveryFee),
525            "rst" => Ok(Rst),
526            "sales_tax" => Ok(SalesTax),
527            "service_tax" => Ok(ServiceTax),
528            "vat" => Ok(Vat),
529            v => Ok(Unknown(v.to_owned())),
530        }
531    }
532}
533impl std::fmt::Display for TaxRateTaxType {
534    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
535        f.write_str(self.as_str())
536    }
537}
538
539impl std::fmt::Debug for TaxRateTaxType {
540    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
541        f.write_str(self.as_str())
542    }
543}
544impl serde::Serialize for TaxRateTaxType {
545    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
546    where
547        S: serde::Serializer,
548    {
549        serializer.serialize_str(self.as_str())
550    }
551}
552impl miniserde::Deserialize for TaxRateTaxType {
553    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
554        crate::Place::new(out)
555    }
556}
557
558impl miniserde::de::Visitor for crate::Place<TaxRateTaxType> {
559    fn string(&mut self, s: &str) -> miniserde::Result<()> {
560        use std::str::FromStr;
561        self.out = Some(TaxRateTaxType::from_str(s).unwrap());
562        Ok(())
563    }
564}
565
566stripe_types::impl_from_val_with_from_str!(TaxRateTaxType);
567#[cfg(feature = "deserialize")]
568impl<'de> serde::Deserialize<'de> for TaxRateTaxType {
569    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
570        use std::str::FromStr;
571        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
572        Ok(Self::from_str(&s).unwrap())
573    }
574}