Skip to main content

stripe_shared/
transfer_data.rs

1#[derive(Clone)]
2#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
3#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
4#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
5pub struct TransferData {
6    /// The amount transferred to the destination account.
7    /// This transfer will occur automatically after the payment succeeds.
8    /// If no amount is specified, by default the entire payment amount is transferred to the destination account.
9    /// The amount must be less than or equal to the [amount](https://docs.stripe.com/api/payment_intents/object#payment_intent_object-amount), and must be a positive integer.
10    ///  representing how much to transfer in the smallest currency unit (e.g., 100 cents to charge $1.00).
11    pub amount: Option<i64>,
12    /// An arbitrary string attached to the transfer. Often useful for displaying to users.
13    pub description: Option<String>,
14    /// The account (if any) that the payment is attributed to for tax reporting, and where funds from the payment are transferred to after payment success.
15    pub destination: stripe_types::Expandable<stripe_shared::Account>,
16    /// Set of [key-value pairs](https://docs.stripe.com/api/metadata) that you can attach to an object.
17    /// This can be useful for storing additional information about the object in a structured format.
18    pub metadata: Option<std::collections::HashMap<String, String>>,
19    pub payment_data: Option<stripe_shared::PaymentData>,
20}
21#[cfg(feature = "redact-generated-debug")]
22impl std::fmt::Debug for TransferData {
23    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
24        f.debug_struct("TransferData").finish_non_exhaustive()
25    }
26}
27#[doc(hidden)]
28pub struct TransferDataBuilder {
29    amount: Option<Option<i64>>,
30    description: Option<Option<String>>,
31    destination: Option<stripe_types::Expandable<stripe_shared::Account>>,
32    metadata: Option<Option<std::collections::HashMap<String, String>>>,
33    payment_data: Option<Option<stripe_shared::PaymentData>>,
34}
35
36#[allow(
37    unused_variables,
38    irrefutable_let_patterns,
39    clippy::let_unit_value,
40    clippy::match_single_binding,
41    clippy::single_match
42)]
43const _: () = {
44    use miniserde::de::{Map, Visitor};
45    use miniserde::json::Value;
46    use miniserde::{Deserialize, Result, make_place};
47    use stripe_types::miniserde_helpers::FromValueOpt;
48    use stripe_types::{MapBuilder, ObjectDeser};
49
50    make_place!(Place);
51
52    impl Deserialize for TransferData {
53        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
54            Place::new(out)
55        }
56    }
57
58    struct Builder<'a> {
59        out: &'a mut Option<TransferData>,
60        builder: TransferDataBuilder,
61    }
62
63    impl Visitor for Place<TransferData> {
64        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
65            Ok(Box::new(Builder {
66                out: &mut self.out,
67                builder: TransferDataBuilder::deser_default(),
68            }))
69        }
70    }
71
72    impl MapBuilder for TransferDataBuilder {
73        type Out = TransferData;
74        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
75            Ok(match k {
76                "amount" => Deserialize::begin(&mut self.amount),
77                "description" => Deserialize::begin(&mut self.description),
78                "destination" => Deserialize::begin(&mut self.destination),
79                "metadata" => Deserialize::begin(&mut self.metadata),
80                "payment_data" => Deserialize::begin(&mut self.payment_data),
81                _ => <dyn Visitor>::ignore(),
82            })
83        }
84
85        fn deser_default() -> Self {
86            Self {
87                amount: Some(None),
88                description: Some(None),
89                destination: None,
90                metadata: Some(None),
91                payment_data: Some(None),
92            }
93        }
94
95        fn take_out(&mut self) -> Option<Self::Out> {
96            let (
97                Some(amount),
98                Some(description),
99                Some(destination),
100                Some(metadata),
101                Some(payment_data),
102            ) = (
103                self.amount,
104                self.description.take(),
105                self.destination.take(),
106                self.metadata.take(),
107                self.payment_data.take(),
108            )
109            else {
110                return None;
111            };
112            Some(Self::Out { amount, description, destination, metadata, payment_data })
113        }
114    }
115
116    impl Map for Builder<'_> {
117        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
118            self.builder.key(k)
119        }
120
121        fn finish(&mut self) -> Result<()> {
122            *self.out = self.builder.take_out();
123            Ok(())
124        }
125    }
126
127    impl ObjectDeser for TransferData {
128        type Builder = TransferDataBuilder;
129    }
130
131    impl FromValueOpt for TransferData {
132        fn from_value(v: Value) -> Option<Self> {
133            let Value::Object(obj) = v else {
134                return None;
135            };
136            let mut b = TransferDataBuilder::deser_default();
137            for (k, v) in obj {
138                match k.as_str() {
139                    "amount" => b.amount = FromValueOpt::from_value(v),
140                    "description" => b.description = FromValueOpt::from_value(v),
141                    "destination" => b.destination = FromValueOpt::from_value(v),
142                    "metadata" => b.metadata = FromValueOpt::from_value(v),
143                    "payment_data" => b.payment_data = FromValueOpt::from_value(v),
144                    _ => {}
145                }
146            }
147            b.take_out()
148        }
149    }
150};