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