Skip to main content

stripe_shared/
transfer.rs

1/// A `Transfer` object is created when you move funds between Stripe accounts as
2/// part of Connect.
3///
4/// Before April 6, 2017, transfers also represented movement of funds from a
5/// Stripe account to a card or bank account. This behavior has since been split
6/// out into a [Payout](https://api.stripe.com#payout_object) object, with corresponding payout endpoints.
7/// For more.
8/// information, read about the
9/// [transfer/payout split](https://docs.stripe.com/transfer-payout-split).
10///
11/// Related guide: [Creating separate charges and transfers](https://docs.stripe.com/connect/separate-charges-and-transfers).
12///
13/// For more details see <<https://stripe.com/docs/api/transfers/object>>.
14#[derive(Clone)]
15#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
16#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
17pub struct Transfer {
18    /// Amount in cents (or local equivalent) to be transferred.
19    pub amount: i64,
20    /// Amount in cents (or local equivalent) reversed (can be less than the amount attribute on the transfer if a partial reversal was issued).
21    pub amount_reversed: i64,
22    /// Balance transaction that describes the impact of this transfer on your account balance.
23    pub balance_transaction: Option<stripe_types::Expandable<stripe_shared::BalanceTransaction>>,
24    /// Time that this record of the transfer was first created.
25    pub created: stripe_types::Timestamp,
26    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
27    /// Must be a [supported currency](https://stripe.com/docs/currencies).
28    pub currency: stripe_types::Currency,
29    /// An arbitrary string attached to the object. Often useful for displaying to users.
30    pub description: Option<String>,
31    /// ID of the Stripe account the transfer was sent to.
32    pub destination: Option<stripe_types::Expandable<stripe_shared::Account>>,
33    /// If the destination is a Stripe account, this will be the ID of the payment that the destination account received for the transfer.
34    pub destination_payment: Option<stripe_types::Expandable<stripe_shared::Charge>>,
35    /// Unique identifier for the object.
36    pub id: stripe_shared::TransferId,
37    /// If the object exists in live mode, the value is `true`.
38    /// If the object exists in test mode, the value is `false`.
39    pub livemode: bool,
40    /// Set of [key-value pairs](https://docs.stripe.com/api/metadata) that you can attach to an object.
41    /// This can be useful for storing additional information about the object in a structured format.
42    pub metadata: std::collections::HashMap<String, String>,
43    /// A list of reversals that have been applied to the transfer.
44    pub reversals: stripe_types::List<stripe_shared::TransferReversal>,
45    /// Whether the transfer has been fully reversed.
46    /// If the transfer is only partially reversed, this attribute will still be false.
47    pub reversed: bool,
48    /// ID of the charge that was used to fund the transfer.
49    /// If null, the transfer was funded from the available balance.
50    pub source_transaction: Option<stripe_types::Expandable<stripe_shared::Charge>>,
51    /// The source balance this transfer came from. One of `card`, `fpx`, or `bank_account`.
52    pub source_type: Option<String>,
53    /// A string that identifies this transaction as part of a group.
54    /// See the [Connect documentation](https://docs.stripe.com/connect/separate-charges-and-transfers#transfer-options) for details.
55    pub transfer_group: Option<String>,
56}
57#[cfg(feature = "redact-generated-debug")]
58impl std::fmt::Debug for Transfer {
59    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
60        f.debug_struct("Transfer").finish_non_exhaustive()
61    }
62}
63#[doc(hidden)]
64pub struct TransferBuilder {
65    amount: Option<i64>,
66    amount_reversed: Option<i64>,
67    balance_transaction:
68        Option<Option<stripe_types::Expandable<stripe_shared::BalanceTransaction>>>,
69    created: Option<stripe_types::Timestamp>,
70    currency: Option<stripe_types::Currency>,
71    description: Option<Option<String>>,
72    destination: Option<Option<stripe_types::Expandable<stripe_shared::Account>>>,
73    destination_payment: Option<Option<stripe_types::Expandable<stripe_shared::Charge>>>,
74    id: Option<stripe_shared::TransferId>,
75    livemode: Option<bool>,
76    metadata: Option<std::collections::HashMap<String, String>>,
77    reversals: Option<stripe_types::List<stripe_shared::TransferReversal>>,
78    reversed: Option<bool>,
79    source_transaction: Option<Option<stripe_types::Expandable<stripe_shared::Charge>>>,
80    source_type: Option<Option<String>>,
81    transfer_group: Option<Option<String>>,
82}
83
84#[allow(
85    unused_variables,
86    irrefutable_let_patterns,
87    clippy::let_unit_value,
88    clippy::match_single_binding,
89    clippy::single_match
90)]
91const _: () = {
92    use miniserde::de::{Map, Visitor};
93    use miniserde::json::Value;
94    use miniserde::{Deserialize, Result, make_place};
95    use stripe_types::miniserde_helpers::FromValueOpt;
96    use stripe_types::{MapBuilder, ObjectDeser};
97
98    make_place!(Place);
99
100    impl Deserialize for Transfer {
101        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
102            Place::new(out)
103        }
104    }
105
106    struct Builder<'a> {
107        out: &'a mut Option<Transfer>,
108        builder: TransferBuilder,
109    }
110
111    impl Visitor for Place<Transfer> {
112        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
113            Ok(Box::new(Builder { out: &mut self.out, builder: TransferBuilder::deser_default() }))
114        }
115    }
116
117    impl MapBuilder for TransferBuilder {
118        type Out = Transfer;
119        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
120            Ok(match k {
121                "amount" => Deserialize::begin(&mut self.amount),
122                "amount_reversed" => Deserialize::begin(&mut self.amount_reversed),
123                "balance_transaction" => Deserialize::begin(&mut self.balance_transaction),
124                "created" => Deserialize::begin(&mut self.created),
125                "currency" => Deserialize::begin(&mut self.currency),
126                "description" => Deserialize::begin(&mut self.description),
127                "destination" => Deserialize::begin(&mut self.destination),
128                "destination_payment" => Deserialize::begin(&mut self.destination_payment),
129                "id" => Deserialize::begin(&mut self.id),
130                "livemode" => Deserialize::begin(&mut self.livemode),
131                "metadata" => Deserialize::begin(&mut self.metadata),
132                "reversals" => Deserialize::begin(&mut self.reversals),
133                "reversed" => Deserialize::begin(&mut self.reversed),
134                "source_transaction" => Deserialize::begin(&mut self.source_transaction),
135                "source_type" => Deserialize::begin(&mut self.source_type),
136                "transfer_group" => Deserialize::begin(&mut self.transfer_group),
137                _ => <dyn Visitor>::ignore(),
138            })
139        }
140
141        fn deser_default() -> Self {
142            Self {
143                amount: None,
144                amount_reversed: None,
145                balance_transaction: Some(None),
146                created: None,
147                currency: None,
148                description: Some(None),
149                destination: Some(None),
150                destination_payment: Some(None),
151                id: None,
152                livemode: None,
153                metadata: None,
154                reversals: None,
155                reversed: None,
156                source_transaction: Some(None),
157                source_type: Some(None),
158                transfer_group: Some(None),
159            }
160        }
161
162        fn take_out(&mut self) -> Option<Self::Out> {
163            let (
164                Some(amount),
165                Some(amount_reversed),
166                Some(balance_transaction),
167                Some(created),
168                Some(currency),
169                Some(description),
170                Some(destination),
171                Some(destination_payment),
172                Some(id),
173                Some(livemode),
174                Some(metadata),
175                Some(reversals),
176                Some(reversed),
177                Some(source_transaction),
178                Some(source_type),
179                Some(transfer_group),
180            ) = (
181                self.amount,
182                self.amount_reversed,
183                self.balance_transaction.take(),
184                self.created,
185                self.currency.take(),
186                self.description.take(),
187                self.destination.take(),
188                self.destination_payment.take(),
189                self.id.take(),
190                self.livemode,
191                self.metadata.take(),
192                self.reversals.take(),
193                self.reversed,
194                self.source_transaction.take(),
195                self.source_type.take(),
196                self.transfer_group.take(),
197            )
198            else {
199                return None;
200            };
201            Some(Self::Out {
202                amount,
203                amount_reversed,
204                balance_transaction,
205                created,
206                currency,
207                description,
208                destination,
209                destination_payment,
210                id,
211                livemode,
212                metadata,
213                reversals,
214                reversed,
215                source_transaction,
216                source_type,
217                transfer_group,
218            })
219        }
220    }
221
222    impl Map for Builder<'_> {
223        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
224            self.builder.key(k)
225        }
226
227        fn finish(&mut self) -> Result<()> {
228            *self.out = self.builder.take_out();
229            Ok(())
230        }
231    }
232
233    impl ObjectDeser for Transfer {
234        type Builder = TransferBuilder;
235    }
236
237    impl FromValueOpt for Transfer {
238        fn from_value(v: Value) -> Option<Self> {
239            let Value::Object(obj) = v else {
240                return None;
241            };
242            let mut b = TransferBuilder::deser_default();
243            for (k, v) in obj {
244                match k.as_str() {
245                    "amount" => b.amount = FromValueOpt::from_value(v),
246                    "amount_reversed" => b.amount_reversed = FromValueOpt::from_value(v),
247                    "balance_transaction" => b.balance_transaction = FromValueOpt::from_value(v),
248                    "created" => b.created = FromValueOpt::from_value(v),
249                    "currency" => b.currency = FromValueOpt::from_value(v),
250                    "description" => b.description = FromValueOpt::from_value(v),
251                    "destination" => b.destination = FromValueOpt::from_value(v),
252                    "destination_payment" => b.destination_payment = FromValueOpt::from_value(v),
253                    "id" => b.id = FromValueOpt::from_value(v),
254                    "livemode" => b.livemode = FromValueOpt::from_value(v),
255                    "metadata" => b.metadata = FromValueOpt::from_value(v),
256                    "reversals" => b.reversals = FromValueOpt::from_value(v),
257                    "reversed" => b.reversed = FromValueOpt::from_value(v),
258                    "source_transaction" => b.source_transaction = FromValueOpt::from_value(v),
259                    "source_type" => b.source_type = FromValueOpt::from_value(v),
260                    "transfer_group" => b.transfer_group = FromValueOpt::from_value(v),
261                    _ => {}
262                }
263            }
264            b.take_out()
265        }
266    }
267};
268#[cfg(feature = "serialize")]
269impl serde::Serialize for Transfer {
270    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
271        use serde::ser::SerializeStruct;
272        let mut s = s.serialize_struct("Transfer", 17)?;
273        s.serialize_field("amount", &self.amount)?;
274        s.serialize_field("amount_reversed", &self.amount_reversed)?;
275        s.serialize_field("balance_transaction", &self.balance_transaction)?;
276        s.serialize_field("created", &self.created)?;
277        s.serialize_field("currency", &self.currency)?;
278        s.serialize_field("description", &self.description)?;
279        s.serialize_field("destination", &self.destination)?;
280        s.serialize_field("destination_payment", &self.destination_payment)?;
281        s.serialize_field("id", &self.id)?;
282        s.serialize_field("livemode", &self.livemode)?;
283        s.serialize_field("metadata", &self.metadata)?;
284        s.serialize_field("reversals", &self.reversals)?;
285        s.serialize_field("reversed", &self.reversed)?;
286        s.serialize_field("source_transaction", &self.source_transaction)?;
287        s.serialize_field("source_type", &self.source_type)?;
288        s.serialize_field("transfer_group", &self.transfer_group)?;
289
290        s.serialize_field("object", "transfer")?;
291        s.end()
292    }
293}
294impl stripe_types::Object for Transfer {
295    type Id = stripe_shared::TransferId;
296    fn id(&self) -> &Self::Id {
297        &self.id
298    }
299
300    fn into_id(self) -> Self::Id {
301        self.id
302    }
303}
304stripe_types::def_id!(TransferId);