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::{make_place, Deserialize, Result};
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
130                _ => <dyn Visitor>::ignore(),
131            })
132        }
133
134        fn deser_default() -> Self {
135            Self {
136                amount: Deserialize::default(),
137                amount_reversed: Deserialize::default(),
138                balance_transaction: Deserialize::default(),
139                created: Deserialize::default(),
140                currency: Deserialize::default(),
141                description: Deserialize::default(),
142                destination: Deserialize::default(),
143                destination_payment: Deserialize::default(),
144                id: Deserialize::default(),
145                livemode: Deserialize::default(),
146                metadata: Deserialize::default(),
147                reversals: Deserialize::default(),
148                reversed: Deserialize::default(),
149                source_transaction: Deserialize::default(),
150                source_type: Deserialize::default(),
151                transfer_group: Deserialize::default(),
152            }
153        }
154
155        fn take_out(&mut self) -> Option<Self::Out> {
156            let (
157                Some(amount),
158                Some(amount_reversed),
159                Some(balance_transaction),
160                Some(created),
161                Some(currency),
162                Some(description),
163                Some(destination),
164                Some(destination_payment),
165                Some(id),
166                Some(livemode),
167                Some(metadata),
168                Some(reversals),
169                Some(reversed),
170                Some(source_transaction),
171                Some(source_type),
172                Some(transfer_group),
173            ) = (
174                self.amount,
175                self.amount_reversed,
176                self.balance_transaction.take(),
177                self.created,
178                self.currency,
179                self.description.take(),
180                self.destination.take(),
181                self.destination_payment.take(),
182                self.id.take(),
183                self.livemode,
184                self.metadata.take(),
185                self.reversals.take(),
186                self.reversed,
187                self.source_transaction.take(),
188                self.source_type.take(),
189                self.transfer_group.take(),
190            )
191            else {
192                return None;
193            };
194            Some(Self::Out {
195                amount,
196                amount_reversed,
197                balance_transaction,
198                created,
199                currency,
200                description,
201                destination,
202                destination_payment,
203                id,
204                livemode,
205                metadata,
206                reversals,
207                reversed,
208                source_transaction,
209                source_type,
210                transfer_group,
211            })
212        }
213    }
214
215    impl<'a> Map for Builder<'a> {
216        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
217            self.builder.key(k)
218        }
219
220        fn finish(&mut self) -> Result<()> {
221            *self.out = self.builder.take_out();
222            Ok(())
223        }
224    }
225
226    impl ObjectDeser for Transfer {
227        type Builder = TransferBuilder;
228    }
229
230    impl FromValueOpt for Transfer {
231        fn from_value(v: Value) -> Option<Self> {
232            let Value::Object(obj) = v else {
233                return None;
234            };
235            let mut b = TransferBuilder::deser_default();
236            for (k, v) in obj {
237                match k.as_str() {
238                    "amount" => b.amount = FromValueOpt::from_value(v),
239                    "amount_reversed" => b.amount_reversed = FromValueOpt::from_value(v),
240                    "balance_transaction" => b.balance_transaction = FromValueOpt::from_value(v),
241                    "created" => b.created = FromValueOpt::from_value(v),
242                    "currency" => b.currency = FromValueOpt::from_value(v),
243                    "description" => b.description = FromValueOpt::from_value(v),
244                    "destination" => b.destination = FromValueOpt::from_value(v),
245                    "destination_payment" => b.destination_payment = FromValueOpt::from_value(v),
246                    "id" => b.id = FromValueOpt::from_value(v),
247                    "livemode" => b.livemode = FromValueOpt::from_value(v),
248                    "metadata" => b.metadata = FromValueOpt::from_value(v),
249                    "reversals" => b.reversals = FromValueOpt::from_value(v),
250                    "reversed" => b.reversed = FromValueOpt::from_value(v),
251                    "source_transaction" => b.source_transaction = FromValueOpt::from_value(v),
252                    "source_type" => b.source_type = FromValueOpt::from_value(v),
253                    "transfer_group" => b.transfer_group = FromValueOpt::from_value(v),
254
255                    _ => {}
256                }
257            }
258            b.take_out()
259        }
260    }
261};
262#[cfg(feature = "serialize")]
263impl serde::Serialize for Transfer {
264    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
265        use serde::ser::SerializeStruct;
266        let mut s = s.serialize_struct("Transfer", 17)?;
267        s.serialize_field("amount", &self.amount)?;
268        s.serialize_field("amount_reversed", &self.amount_reversed)?;
269        s.serialize_field("balance_transaction", &self.balance_transaction)?;
270        s.serialize_field("created", &self.created)?;
271        s.serialize_field("currency", &self.currency)?;
272        s.serialize_field("description", &self.description)?;
273        s.serialize_field("destination", &self.destination)?;
274        s.serialize_field("destination_payment", &self.destination_payment)?;
275        s.serialize_field("id", &self.id)?;
276        s.serialize_field("livemode", &self.livemode)?;
277        s.serialize_field("metadata", &self.metadata)?;
278        s.serialize_field("reversals", &self.reversals)?;
279        s.serialize_field("reversed", &self.reversed)?;
280        s.serialize_field("source_transaction", &self.source_transaction)?;
281        s.serialize_field("source_type", &self.source_type)?;
282        s.serialize_field("transfer_group", &self.transfer_group)?;
283
284        s.serialize_field("object", "transfer")?;
285        s.end()
286    }
287}
288impl stripe_types::Object for Transfer {
289    type Id = stripe_shared::TransferId;
290    fn id(&self) -> &Self::Id {
291        &self.id
292    }
293
294    fn into_id(self) -> Self::Id {
295        self.id
296    }
297}
298stripe_types::def_id!(TransferId);