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                _ => <dyn Visitor>::ignore(),
117            })
118        }
119
120        fn deser_default() -> Self {
121            Self {
122                created: Deserialize::default(),
123                currency: Deserialize::default(),
124                customer: Deserialize::default(),
125                customer_details: Deserialize::default(),
126                id: Deserialize::default(),
127                line_items: Deserialize::default(),
128                livemode: Deserialize::default(),
129                metadata: Deserialize::default(),
130                posted_at: Deserialize::default(),
131                reference: Deserialize::default(),
132                reversal: Deserialize::default(),
133                ship_from_details: Deserialize::default(),
134                shipping_cost: Deserialize::default(),
135                tax_date: Deserialize::default(),
136                type_: Deserialize::default(),
137            }
138        }
139
140        fn take_out(&mut self) -> Option<Self::Out> {
141            let (
142                Some(created),
143                Some(currency),
144                Some(customer),
145                Some(customer_details),
146                Some(id),
147                Some(line_items),
148                Some(livemode),
149                Some(metadata),
150                Some(posted_at),
151                Some(reference),
152                Some(reversal),
153                Some(ship_from_details),
154                Some(shipping_cost),
155                Some(tax_date),
156                Some(type_),
157            ) = (
158                self.created,
159                self.currency.take(),
160                self.customer.take(),
161                self.customer_details.take(),
162                self.id.take(),
163                self.line_items.take(),
164                self.livemode,
165                self.metadata.take(),
166                self.posted_at,
167                self.reference.take(),
168                self.reversal.take(),
169                self.ship_from_details.take(),
170                self.shipping_cost.take(),
171                self.tax_date,
172                self.type_,
173            )
174            else {
175                return None;
176            };
177            Some(Self::Out {
178                created,
179                currency,
180                customer,
181                customer_details,
182                id,
183                line_items,
184                livemode,
185                metadata,
186                posted_at,
187                reference,
188                reversal,
189                ship_from_details,
190                shipping_cost,
191                tax_date,
192                type_,
193            })
194        }
195    }
196
197    impl Map for Builder<'_> {
198        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
199            self.builder.key(k)
200        }
201
202        fn finish(&mut self) -> Result<()> {
203            *self.out = self.builder.take_out();
204            Ok(())
205        }
206    }
207
208    impl ObjectDeser for TaxTransaction {
209        type Builder = TaxTransactionBuilder;
210    }
211
212    impl FromValueOpt for TaxTransaction {
213        fn from_value(v: Value) -> Option<Self> {
214            let Value::Object(obj) = v else {
215                return None;
216            };
217            let mut b = TaxTransactionBuilder::deser_default();
218            for (k, v) in obj {
219                match k.as_str() {
220                    "created" => b.created = FromValueOpt::from_value(v),
221                    "currency" => b.currency = FromValueOpt::from_value(v),
222                    "customer" => b.customer = FromValueOpt::from_value(v),
223                    "customer_details" => b.customer_details = FromValueOpt::from_value(v),
224                    "id" => b.id = FromValueOpt::from_value(v),
225                    "line_items" => b.line_items = FromValueOpt::from_value(v),
226                    "livemode" => b.livemode = FromValueOpt::from_value(v),
227                    "metadata" => b.metadata = FromValueOpt::from_value(v),
228                    "posted_at" => b.posted_at = FromValueOpt::from_value(v),
229                    "reference" => b.reference = FromValueOpt::from_value(v),
230                    "reversal" => b.reversal = FromValueOpt::from_value(v),
231                    "ship_from_details" => b.ship_from_details = FromValueOpt::from_value(v),
232                    "shipping_cost" => b.shipping_cost = FromValueOpt::from_value(v),
233                    "tax_date" => b.tax_date = FromValueOpt::from_value(v),
234                    "type" => b.type_ = FromValueOpt::from_value(v),
235                    _ => {}
236                }
237            }
238            b.take_out()
239        }
240    }
241};
242#[cfg(feature = "serialize")]
243impl serde::Serialize for TaxTransaction {
244    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
245        use serde::ser::SerializeStruct;
246        let mut s = s.serialize_struct("TaxTransaction", 16)?;
247        s.serialize_field("created", &self.created)?;
248        s.serialize_field("currency", &self.currency)?;
249        s.serialize_field("customer", &self.customer)?;
250        s.serialize_field("customer_details", &self.customer_details)?;
251        s.serialize_field("id", &self.id)?;
252        s.serialize_field("line_items", &self.line_items)?;
253        s.serialize_field("livemode", &self.livemode)?;
254        s.serialize_field("metadata", &self.metadata)?;
255        s.serialize_field("posted_at", &self.posted_at)?;
256        s.serialize_field("reference", &self.reference)?;
257        s.serialize_field("reversal", &self.reversal)?;
258        s.serialize_field("ship_from_details", &self.ship_from_details)?;
259        s.serialize_field("shipping_cost", &self.shipping_cost)?;
260        s.serialize_field("tax_date", &self.tax_date)?;
261        s.serialize_field("type", &self.type_)?;
262
263        s.serialize_field("object", "tax.transaction")?;
264        s.end()
265    }
266}
267/// If `reversal`, this transaction reverses an earlier transaction.
268#[derive(Copy, Clone, Eq, PartialEq)]
269pub enum TaxTransactionType {
270    Reversal,
271    Transaction,
272}
273impl TaxTransactionType {
274    pub fn as_str(self) -> &'static str {
275        use TaxTransactionType::*;
276        match self {
277            Reversal => "reversal",
278            Transaction => "transaction",
279        }
280    }
281}
282
283impl std::str::FromStr for TaxTransactionType {
284    type Err = stripe_types::StripeParseError;
285    fn from_str(s: &str) -> Result<Self, Self::Err> {
286        use TaxTransactionType::*;
287        match s {
288            "reversal" => Ok(Reversal),
289            "transaction" => Ok(Transaction),
290            _ => Err(stripe_types::StripeParseError),
291        }
292    }
293}
294impl std::fmt::Display for TaxTransactionType {
295    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
296        f.write_str(self.as_str())
297    }
298}
299
300impl std::fmt::Debug for TaxTransactionType {
301    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
302        f.write_str(self.as_str())
303    }
304}
305#[cfg(feature = "serialize")]
306impl serde::Serialize for TaxTransactionType {
307    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
308    where
309        S: serde::Serializer,
310    {
311        serializer.serialize_str(self.as_str())
312    }
313}
314impl miniserde::Deserialize for TaxTransactionType {
315    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
316        crate::Place::new(out)
317    }
318}
319
320impl miniserde::de::Visitor for crate::Place<TaxTransactionType> {
321    fn string(&mut self, s: &str) -> miniserde::Result<()> {
322        use std::str::FromStr;
323        self.out = Some(TaxTransactionType::from_str(s).map_err(|_| miniserde::Error)?);
324        Ok(())
325    }
326}
327
328stripe_types::impl_from_val_with_from_str!(TaxTransactionType);
329#[cfg(feature = "deserialize")]
330impl<'de> serde::Deserialize<'de> for TaxTransactionType {
331    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
332        use std::str::FromStr;
333        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
334        Self::from_str(&s)
335            .map_err(|_| serde::de::Error::custom("Unknown value for TaxTransactionType"))
336    }
337}
338impl stripe_types::Object for TaxTransaction {
339    type Id = stripe_misc::TaxTransactionId;
340    fn id(&self) -> &Self::Id {
341        &self.id
342    }
343
344    fn into_id(self) -> Self::Id {
345        self.id
346    }
347}
348stripe_types::def_id!(TaxTransactionId);