stripe_misc/tax_transaction/
types.rs

1/// A Tax Transaction records the tax collected from or refunded to your customer.
2///
3/// Related guide: [Calculate tax in your custom payment flow](https://stripe.com/docs/tax/custom#tax-transaction).
4///
5/// For more details see <<https://stripe.com/docs/api/tax/transactions/object>>.
6#[derive(Clone, Debug)]
7#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
8pub struct TaxTransaction {
9    /// Time at which the object was created. Measured in seconds since the Unix epoch.
10    pub created: stripe_types::Timestamp,
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    /// Unique identifier for the transaction.
18    pub id: stripe_misc::TaxTransactionId,
19    /// The tax collected or refunded, by line item.
20    pub line_items: Option<stripe_types::List<stripe_misc::TaxTransactionLineItem>>,
21    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
22    pub livemode: bool,
23    /// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object.
24    /// This can be useful for storing additional information about the object in a structured format.
25    pub metadata: Option<std::collections::HashMap<String, String>>,
26    /// The Unix timestamp representing when the tax liability is assumed or reduced.
27    pub posted_at: stripe_types::Timestamp,
28    /// A custom unique identifier, such as 'myOrder_123'.
29    pub reference: String,
30    /// If `type=reversal`, contains information about what was reversed.
31    pub reversal: Option<stripe_misc::TaxProductResourceTaxTransactionResourceReversal>,
32    /// The details of the ship from location, such as the address.
33    pub ship_from_details: Option<stripe_misc::TaxProductResourceShipFromDetails>,
34    /// The shipping cost details for the transaction.
35    pub shipping_cost: Option<stripe_misc::TaxProductResourceTaxTransactionShippingCost>,
36    /// Timestamp of date at which the tax rules and rates in effect applies for the calculation.
37    pub tax_date: stripe_types::Timestamp,
38    /// If `reversal`, this transaction reverses an earlier transaction.
39    #[cfg_attr(feature = "deserialize", serde(rename = "type"))]
40    pub type_: TaxTransactionType,
41}
42#[doc(hidden)]
43pub struct TaxTransactionBuilder {
44    created: Option<stripe_types::Timestamp>,
45    currency: Option<stripe_types::Currency>,
46    customer: Option<Option<String>>,
47    customer_details: Option<stripe_misc::TaxProductResourceCustomerDetails>,
48    id: Option<stripe_misc::TaxTransactionId>,
49    line_items: Option<Option<stripe_types::List<stripe_misc::TaxTransactionLineItem>>>,
50    livemode: Option<bool>,
51    metadata: Option<Option<std::collections::HashMap<String, String>>>,
52    posted_at: Option<stripe_types::Timestamp>,
53    reference: Option<String>,
54    reversal: Option<Option<stripe_misc::TaxProductResourceTaxTransactionResourceReversal>>,
55    ship_from_details: Option<Option<stripe_misc::TaxProductResourceShipFromDetails>>,
56    shipping_cost: Option<Option<stripe_misc::TaxProductResourceTaxTransactionShippingCost>>,
57    tax_date: Option<stripe_types::Timestamp>,
58    type_: Option<TaxTransactionType>,
59}
60
61#[allow(
62    unused_variables,
63    irrefutable_let_patterns,
64    clippy::let_unit_value,
65    clippy::match_single_binding,
66    clippy::single_match
67)]
68const _: () = {
69    use miniserde::de::{Map, Visitor};
70    use miniserde::json::Value;
71    use miniserde::{Deserialize, Result, make_place};
72    use stripe_types::miniserde_helpers::FromValueOpt;
73    use stripe_types::{MapBuilder, ObjectDeser};
74
75    make_place!(Place);
76
77    impl Deserialize for TaxTransaction {
78        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
79            Place::new(out)
80        }
81    }
82
83    struct Builder<'a> {
84        out: &'a mut Option<TaxTransaction>,
85        builder: TaxTransactionBuilder,
86    }
87
88    impl Visitor for Place<TaxTransaction> {
89        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
90            Ok(Box::new(Builder {
91                out: &mut self.out,
92                builder: TaxTransactionBuilder::deser_default(),
93            }))
94        }
95    }
96
97    impl MapBuilder for TaxTransactionBuilder {
98        type Out = TaxTransaction;
99        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
100            Ok(match k {
101                "created" => Deserialize::begin(&mut self.created),
102                "currency" => Deserialize::begin(&mut self.currency),
103                "customer" => Deserialize::begin(&mut self.customer),
104                "customer_details" => Deserialize::begin(&mut self.customer_details),
105                "id" => Deserialize::begin(&mut self.id),
106                "line_items" => Deserialize::begin(&mut self.line_items),
107                "livemode" => Deserialize::begin(&mut self.livemode),
108                "metadata" => Deserialize::begin(&mut self.metadata),
109                "posted_at" => Deserialize::begin(&mut self.posted_at),
110                "reference" => Deserialize::begin(&mut self.reference),
111                "reversal" => Deserialize::begin(&mut self.reversal),
112                "ship_from_details" => Deserialize::begin(&mut self.ship_from_details),
113                "shipping_cost" => Deserialize::begin(&mut self.shipping_cost),
114                "tax_date" => Deserialize::begin(&mut self.tax_date),
115                "type" => Deserialize::begin(&mut self.type_),
116
117                _ => <dyn Visitor>::ignore(),
118            })
119        }
120
121        fn deser_default() -> Self {
122            Self {
123                created: Deserialize::default(),
124                currency: Deserialize::default(),
125                customer: Deserialize::default(),
126                customer_details: Deserialize::default(),
127                id: Deserialize::default(),
128                line_items: Deserialize::default(),
129                livemode: Deserialize::default(),
130                metadata: Deserialize::default(),
131                posted_at: Deserialize::default(),
132                reference: Deserialize::default(),
133                reversal: Deserialize::default(),
134                ship_from_details: Deserialize::default(),
135                shipping_cost: Deserialize::default(),
136                tax_date: Deserialize::default(),
137                type_: Deserialize::default(),
138            }
139        }
140
141        fn take_out(&mut self) -> Option<Self::Out> {
142            let (
143                Some(created),
144                Some(currency),
145                Some(customer),
146                Some(customer_details),
147                Some(id),
148                Some(line_items),
149                Some(livemode),
150                Some(metadata),
151                Some(posted_at),
152                Some(reference),
153                Some(reversal),
154                Some(ship_from_details),
155                Some(shipping_cost),
156                Some(tax_date),
157                Some(type_),
158            ) = (
159                self.created,
160                self.currency.take(),
161                self.customer.take(),
162                self.customer_details.take(),
163                self.id.take(),
164                self.line_items.take(),
165                self.livemode,
166                self.metadata.take(),
167                self.posted_at,
168                self.reference.take(),
169                self.reversal.take(),
170                self.ship_from_details.take(),
171                self.shipping_cost.take(),
172                self.tax_date,
173                self.type_,
174            )
175            else {
176                return None;
177            };
178            Some(Self::Out {
179                created,
180                currency,
181                customer,
182                customer_details,
183                id,
184                line_items,
185                livemode,
186                metadata,
187                posted_at,
188                reference,
189                reversal,
190                ship_from_details,
191                shipping_cost,
192                tax_date,
193                type_,
194            })
195        }
196    }
197
198    impl Map for Builder<'_> {
199        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
200            self.builder.key(k)
201        }
202
203        fn finish(&mut self) -> Result<()> {
204            *self.out = self.builder.take_out();
205            Ok(())
206        }
207    }
208
209    impl ObjectDeser for TaxTransaction {
210        type Builder = TaxTransactionBuilder;
211    }
212
213    impl FromValueOpt for TaxTransaction {
214        fn from_value(v: Value) -> Option<Self> {
215            let Value::Object(obj) = v else {
216                return None;
217            };
218            let mut b = TaxTransactionBuilder::deser_default();
219            for (k, v) in obj {
220                match k.as_str() {
221                    "created" => b.created = FromValueOpt::from_value(v),
222                    "currency" => b.currency = FromValueOpt::from_value(v),
223                    "customer" => b.customer = FromValueOpt::from_value(v),
224                    "customer_details" => b.customer_details = FromValueOpt::from_value(v),
225                    "id" => b.id = FromValueOpt::from_value(v),
226                    "line_items" => b.line_items = FromValueOpt::from_value(v),
227                    "livemode" => b.livemode = FromValueOpt::from_value(v),
228                    "metadata" => b.metadata = FromValueOpt::from_value(v),
229                    "posted_at" => b.posted_at = FromValueOpt::from_value(v),
230                    "reference" => b.reference = FromValueOpt::from_value(v),
231                    "reversal" => b.reversal = FromValueOpt::from_value(v),
232                    "ship_from_details" => b.ship_from_details = FromValueOpt::from_value(v),
233                    "shipping_cost" => b.shipping_cost = FromValueOpt::from_value(v),
234                    "tax_date" => b.tax_date = FromValueOpt::from_value(v),
235                    "type" => b.type_ = FromValueOpt::from_value(v),
236
237                    _ => {}
238                }
239            }
240            b.take_out()
241        }
242    }
243};
244#[cfg(feature = "serialize")]
245impl serde::Serialize for TaxTransaction {
246    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
247        use serde::ser::SerializeStruct;
248        let mut s = s.serialize_struct("TaxTransaction", 16)?;
249        s.serialize_field("created", &self.created)?;
250        s.serialize_field("currency", &self.currency)?;
251        s.serialize_field("customer", &self.customer)?;
252        s.serialize_field("customer_details", &self.customer_details)?;
253        s.serialize_field("id", &self.id)?;
254        s.serialize_field("line_items", &self.line_items)?;
255        s.serialize_field("livemode", &self.livemode)?;
256        s.serialize_field("metadata", &self.metadata)?;
257        s.serialize_field("posted_at", &self.posted_at)?;
258        s.serialize_field("reference", &self.reference)?;
259        s.serialize_field("reversal", &self.reversal)?;
260        s.serialize_field("ship_from_details", &self.ship_from_details)?;
261        s.serialize_field("shipping_cost", &self.shipping_cost)?;
262        s.serialize_field("tax_date", &self.tax_date)?;
263        s.serialize_field("type", &self.type_)?;
264
265        s.serialize_field("object", "tax.transaction")?;
266        s.end()
267    }
268}
269/// If `reversal`, this transaction reverses an earlier transaction.
270#[derive(Copy, Clone, Eq, PartialEq)]
271pub enum TaxTransactionType {
272    Reversal,
273    Transaction,
274}
275impl TaxTransactionType {
276    pub fn as_str(self) -> &'static str {
277        use TaxTransactionType::*;
278        match self {
279            Reversal => "reversal",
280            Transaction => "transaction",
281        }
282    }
283}
284
285impl std::str::FromStr for TaxTransactionType {
286    type Err = stripe_types::StripeParseError;
287    fn from_str(s: &str) -> Result<Self, Self::Err> {
288        use TaxTransactionType::*;
289        match s {
290            "reversal" => Ok(Reversal),
291            "transaction" => Ok(Transaction),
292            _ => Err(stripe_types::StripeParseError),
293        }
294    }
295}
296impl std::fmt::Display for TaxTransactionType {
297    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
298        f.write_str(self.as_str())
299    }
300}
301
302impl std::fmt::Debug for TaxTransactionType {
303    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
304        f.write_str(self.as_str())
305    }
306}
307#[cfg(feature = "serialize")]
308impl serde::Serialize for TaxTransactionType {
309    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
310    where
311        S: serde::Serializer,
312    {
313        serializer.serialize_str(self.as_str())
314    }
315}
316impl miniserde::Deserialize for TaxTransactionType {
317    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
318        crate::Place::new(out)
319    }
320}
321
322impl miniserde::de::Visitor for crate::Place<TaxTransactionType> {
323    fn string(&mut self, s: &str) -> miniserde::Result<()> {
324        use std::str::FromStr;
325        self.out = Some(TaxTransactionType::from_str(s).map_err(|_| miniserde::Error)?);
326        Ok(())
327    }
328}
329
330stripe_types::impl_from_val_with_from_str!(TaxTransactionType);
331#[cfg(feature = "deserialize")]
332impl<'de> serde::Deserialize<'de> for TaxTransactionType {
333    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
334        use std::str::FromStr;
335        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
336        Self::from_str(&s)
337            .map_err(|_| serde::de::Error::custom("Unknown value for TaxTransactionType"))
338    }
339}
340impl stripe_types::Object for TaxTransaction {
341    type Id = stripe_misc::TaxTransactionId;
342    fn id(&self) -> &Self::Id {
343        &self.id
344    }
345
346    fn into_id(self) -> Self::Id {
347        self.id
348    }
349}
350stripe_types::def_id!(TaxTransactionId);