Skip to main content

stripe_shared/
customer_cash_balance_transaction.rs

1/// Customers with certain payments enabled have a cash balance, representing funds that were paid
2/// by the customer to a merchant, but have not yet been allocated to a payment.
3/// Cash Balance Transactions.
4/// represent when funds are moved into or out of this balance.
5/// This includes funding by the customer, allocation.
6/// to payments, and refunds to the customer.
7///
8/// For more details see <<https://stripe.com/docs/api/cash_balance_transactions/object>>.
9#[derive(Clone)]
10#[cfg_attr(not(feature = "redact-generated-debug"), derive(Debug))]
11#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
12pub struct CustomerCashBalanceTransaction {
13pub adjusted_for_overdraft: Option<stripe_shared::CustomerBalanceResourceCashBalanceTransactionResourceAdjustedForOverdraft>,
14pub applied_to_payment: Option<stripe_shared::CustomerBalanceResourceCashBalanceTransactionResourceAppliedToPaymentTransaction>,
15    /// Time at which the object was created. Measured in seconds since the Unix epoch.
16pub created: stripe_types::Timestamp,
17        /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
18    /// Must be a [supported currency](https://stripe.com/docs/currencies).
19pub currency: stripe_types::Currency,
20    /// The customer whose available cash balance changed as a result of this transaction.
21pub customer: stripe_types::Expandable<stripe_shared::Customer>,
22        /// The ID of an Account representing a customer whose available cash balance changed as a result of this transaction.
23pub customer_account: Option<String>,
24        /// The total available cash balance for the specified currency after this transaction was applied.
25    /// Represented in the [smallest currency unit](https://docs.stripe.com/currencies#zero-decimal).
26pub ending_balance: i64,
27pub funded: Option<stripe_shared::CustomerBalanceResourceCashBalanceTransactionResourceFundedTransaction>,
28    /// Unique identifier for the object.
29pub id: stripe_shared::CustomerCashBalanceTransactionId,
30        /// If the object exists in live mode, the value is `true`.
31    /// If the object exists in test mode, the value is `false`.
32pub livemode: bool,
33        /// The amount by which the cash balance changed, represented in the [smallest currency unit](https://docs.stripe.com/currencies#zero-decimal).
34    /// A positive value represents funds being added to the cash balance, a negative value represents funds being removed from the cash balance.
35pub net_amount: i64,
36pub refunded_from_payment: Option<stripe_shared::CustomerBalanceResourceCashBalanceTransactionResourceRefundedFromPaymentTransaction>,
37pub transferred_to_balance: Option<stripe_shared::CustomerBalanceResourceCashBalanceTransactionResourceTransferredToBalance>,
38        /// The type of the cash balance transaction.
39    /// New types may be added in future.
40    /// See [Customer Balance](https://docs.stripe.com/payments/customer-balance#types) to learn more about these types.
41#[cfg_attr(feature = "deserialize", serde(rename = "type"))]
42pub type_: CustomerCashBalanceTransactionType,
43pub unapplied_from_payment: Option<stripe_shared::CustomerBalanceResourceCashBalanceTransactionResourceUnappliedFromPaymentTransaction>,
44
45}
46#[cfg(feature = "redact-generated-debug")]
47impl std::fmt::Debug for CustomerCashBalanceTransaction {
48    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
49        f.debug_struct("CustomerCashBalanceTransaction").finish_non_exhaustive()
50    }
51}
52#[doc(hidden)]
53pub struct CustomerCashBalanceTransactionBuilder {
54    adjusted_for_overdraft: Option<Option<stripe_shared::CustomerBalanceResourceCashBalanceTransactionResourceAdjustedForOverdraft>>,
55applied_to_payment: Option<Option<stripe_shared::CustomerBalanceResourceCashBalanceTransactionResourceAppliedToPaymentTransaction>>,
56created: Option<stripe_types::Timestamp>,
57currency: Option<stripe_types::Currency>,
58customer: Option<stripe_types::Expandable<stripe_shared::Customer>>,
59customer_account: Option<Option<String>>,
60ending_balance: Option<i64>,
61funded: Option<Option<stripe_shared::CustomerBalanceResourceCashBalanceTransactionResourceFundedTransaction>>,
62id: Option<stripe_shared::CustomerCashBalanceTransactionId>,
63livemode: Option<bool>,
64net_amount: Option<i64>,
65refunded_from_payment: Option<Option<stripe_shared::CustomerBalanceResourceCashBalanceTransactionResourceRefundedFromPaymentTransaction>>,
66transferred_to_balance: Option<Option<stripe_shared::CustomerBalanceResourceCashBalanceTransactionResourceTransferredToBalance>>,
67type_: Option<CustomerCashBalanceTransactionType>,
68unapplied_from_payment: Option<Option<stripe_shared::CustomerBalanceResourceCashBalanceTransactionResourceUnappliedFromPaymentTransaction>>,
69
70}
71
72#[allow(
73    unused_variables,
74    irrefutable_let_patterns,
75    clippy::let_unit_value,
76    clippy::match_single_binding,
77    clippy::single_match
78)]
79const _: () = {
80    use miniserde::de::{Map, Visitor};
81    use miniserde::json::Value;
82    use miniserde::{Deserialize, Result, make_place};
83    use stripe_types::miniserde_helpers::FromValueOpt;
84    use stripe_types::{MapBuilder, ObjectDeser};
85
86    make_place!(Place);
87
88    impl Deserialize for CustomerCashBalanceTransaction {
89        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
90            Place::new(out)
91        }
92    }
93
94    struct Builder<'a> {
95        out: &'a mut Option<CustomerCashBalanceTransaction>,
96        builder: CustomerCashBalanceTransactionBuilder,
97    }
98
99    impl Visitor for Place<CustomerCashBalanceTransaction> {
100        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
101            Ok(Box::new(Builder {
102                out: &mut self.out,
103                builder: CustomerCashBalanceTransactionBuilder::deser_default(),
104            }))
105        }
106    }
107
108    impl MapBuilder for CustomerCashBalanceTransactionBuilder {
109        type Out = CustomerCashBalanceTransaction;
110        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
111            Ok(match k {
112                "adjusted_for_overdraft" => Deserialize::begin(&mut self.adjusted_for_overdraft),
113                "applied_to_payment" => Deserialize::begin(&mut self.applied_to_payment),
114                "created" => Deserialize::begin(&mut self.created),
115                "currency" => Deserialize::begin(&mut self.currency),
116                "customer" => Deserialize::begin(&mut self.customer),
117                "customer_account" => Deserialize::begin(&mut self.customer_account),
118                "ending_balance" => Deserialize::begin(&mut self.ending_balance),
119                "funded" => Deserialize::begin(&mut self.funded),
120                "id" => Deserialize::begin(&mut self.id),
121                "livemode" => Deserialize::begin(&mut self.livemode),
122                "net_amount" => Deserialize::begin(&mut self.net_amount),
123                "refunded_from_payment" => Deserialize::begin(&mut self.refunded_from_payment),
124                "transferred_to_balance" => Deserialize::begin(&mut self.transferred_to_balance),
125                "type" => Deserialize::begin(&mut self.type_),
126                "unapplied_from_payment" => Deserialize::begin(&mut self.unapplied_from_payment),
127                _ => <dyn Visitor>::ignore(),
128            })
129        }
130
131        fn deser_default() -> Self {
132            Self {
133                adjusted_for_overdraft: Some(None),
134                applied_to_payment: Some(None),
135                created: None,
136                currency: None,
137                customer: None,
138                customer_account: Some(None),
139                ending_balance: None,
140                funded: Some(None),
141                id: None,
142                livemode: None,
143                net_amount: None,
144                refunded_from_payment: Some(None),
145                transferred_to_balance: Some(None),
146                type_: None,
147                unapplied_from_payment: Some(None),
148            }
149        }
150
151        fn take_out(&mut self) -> Option<Self::Out> {
152            let (
153                Some(adjusted_for_overdraft),
154                Some(applied_to_payment),
155                Some(created),
156                Some(currency),
157                Some(customer),
158                Some(customer_account),
159                Some(ending_balance),
160                Some(funded),
161                Some(id),
162                Some(livemode),
163                Some(net_amount),
164                Some(refunded_from_payment),
165                Some(transferred_to_balance),
166                Some(type_),
167                Some(unapplied_from_payment),
168            ) = (
169                self.adjusted_for_overdraft.take(),
170                self.applied_to_payment.take(),
171                self.created,
172                self.currency.take(),
173                self.customer.take(),
174                self.customer_account.take(),
175                self.ending_balance,
176                self.funded.take(),
177                self.id.take(),
178                self.livemode,
179                self.net_amount,
180                self.refunded_from_payment.take(),
181                self.transferred_to_balance.take(),
182                self.type_.take(),
183                self.unapplied_from_payment.take(),
184            )
185            else {
186                return None;
187            };
188            Some(Self::Out {
189                adjusted_for_overdraft,
190                applied_to_payment,
191                created,
192                currency,
193                customer,
194                customer_account,
195                ending_balance,
196                funded,
197                id,
198                livemode,
199                net_amount,
200                refunded_from_payment,
201                transferred_to_balance,
202                type_,
203                unapplied_from_payment,
204            })
205        }
206    }
207
208    impl Map for Builder<'_> {
209        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
210            self.builder.key(k)
211        }
212
213        fn finish(&mut self) -> Result<()> {
214            *self.out = self.builder.take_out();
215            Ok(())
216        }
217    }
218
219    impl ObjectDeser for CustomerCashBalanceTransaction {
220        type Builder = CustomerCashBalanceTransactionBuilder;
221    }
222
223    impl FromValueOpt for CustomerCashBalanceTransaction {
224        fn from_value(v: Value) -> Option<Self> {
225            let Value::Object(obj) = v else {
226                return None;
227            };
228            let mut b = CustomerCashBalanceTransactionBuilder::deser_default();
229            for (k, v) in obj {
230                match k.as_str() {
231                    "adjusted_for_overdraft" => {
232                        b.adjusted_for_overdraft = FromValueOpt::from_value(v)
233                    }
234                    "applied_to_payment" => b.applied_to_payment = FromValueOpt::from_value(v),
235                    "created" => b.created = FromValueOpt::from_value(v),
236                    "currency" => b.currency = FromValueOpt::from_value(v),
237                    "customer" => b.customer = FromValueOpt::from_value(v),
238                    "customer_account" => b.customer_account = FromValueOpt::from_value(v),
239                    "ending_balance" => b.ending_balance = FromValueOpt::from_value(v),
240                    "funded" => b.funded = FromValueOpt::from_value(v),
241                    "id" => b.id = FromValueOpt::from_value(v),
242                    "livemode" => b.livemode = FromValueOpt::from_value(v),
243                    "net_amount" => b.net_amount = FromValueOpt::from_value(v),
244                    "refunded_from_payment" => {
245                        b.refunded_from_payment = FromValueOpt::from_value(v)
246                    }
247                    "transferred_to_balance" => {
248                        b.transferred_to_balance = FromValueOpt::from_value(v)
249                    }
250                    "type" => b.type_ = FromValueOpt::from_value(v),
251                    "unapplied_from_payment" => {
252                        b.unapplied_from_payment = FromValueOpt::from_value(v)
253                    }
254                    _ => {}
255                }
256            }
257            b.take_out()
258        }
259    }
260};
261#[cfg(feature = "serialize")]
262impl serde::Serialize for CustomerCashBalanceTransaction {
263    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
264        use serde::ser::SerializeStruct;
265        let mut s = s.serialize_struct("CustomerCashBalanceTransaction", 16)?;
266        s.serialize_field("adjusted_for_overdraft", &self.adjusted_for_overdraft)?;
267        s.serialize_field("applied_to_payment", &self.applied_to_payment)?;
268        s.serialize_field("created", &self.created)?;
269        s.serialize_field("currency", &self.currency)?;
270        s.serialize_field("customer", &self.customer)?;
271        s.serialize_field("customer_account", &self.customer_account)?;
272        s.serialize_field("ending_balance", &self.ending_balance)?;
273        s.serialize_field("funded", &self.funded)?;
274        s.serialize_field("id", &self.id)?;
275        s.serialize_field("livemode", &self.livemode)?;
276        s.serialize_field("net_amount", &self.net_amount)?;
277        s.serialize_field("refunded_from_payment", &self.refunded_from_payment)?;
278        s.serialize_field("transferred_to_balance", &self.transferred_to_balance)?;
279        s.serialize_field("type", &self.type_)?;
280        s.serialize_field("unapplied_from_payment", &self.unapplied_from_payment)?;
281
282        s.serialize_field("object", "customer_cash_balance_transaction")?;
283        s.end()
284    }
285}
286/// The type of the cash balance transaction.
287/// New types may be added in future.
288/// See [Customer Balance](https://docs.stripe.com/payments/customer-balance#types) to learn more about these types.
289#[derive(Clone, Eq, PartialEq)]
290#[non_exhaustive]
291pub enum CustomerCashBalanceTransactionType {
292    AdjustedForOverdraft,
293    AppliedToPayment,
294    Funded,
295    FundingReversed,
296    RefundedFromPayment,
297    ReturnCanceled,
298    ReturnInitiated,
299    TransferredToBalance,
300    UnappliedFromPayment,
301    /// An unrecognized value from Stripe. Should not be used as a request parameter.
302    Unknown(String),
303}
304impl CustomerCashBalanceTransactionType {
305    pub fn as_str(&self) -> &str {
306        use CustomerCashBalanceTransactionType::*;
307        match self {
308            AdjustedForOverdraft => "adjusted_for_overdraft",
309            AppliedToPayment => "applied_to_payment",
310            Funded => "funded",
311            FundingReversed => "funding_reversed",
312            RefundedFromPayment => "refunded_from_payment",
313            ReturnCanceled => "return_canceled",
314            ReturnInitiated => "return_initiated",
315            TransferredToBalance => "transferred_to_balance",
316            UnappliedFromPayment => "unapplied_from_payment",
317            Unknown(v) => v,
318        }
319    }
320}
321
322impl std::str::FromStr for CustomerCashBalanceTransactionType {
323    type Err = std::convert::Infallible;
324    fn from_str(s: &str) -> Result<Self, Self::Err> {
325        use CustomerCashBalanceTransactionType::*;
326        match s {
327            "adjusted_for_overdraft" => Ok(AdjustedForOverdraft),
328            "applied_to_payment" => Ok(AppliedToPayment),
329            "funded" => Ok(Funded),
330            "funding_reversed" => Ok(FundingReversed),
331            "refunded_from_payment" => Ok(RefundedFromPayment),
332            "return_canceled" => Ok(ReturnCanceled),
333            "return_initiated" => Ok(ReturnInitiated),
334            "transferred_to_balance" => Ok(TransferredToBalance),
335            "unapplied_from_payment" => Ok(UnappliedFromPayment),
336            v => {
337                tracing::warn!(
338                    "Unknown value '{}' for enum '{}'",
339                    v,
340                    "CustomerCashBalanceTransactionType"
341                );
342                Ok(Unknown(v.to_owned()))
343            }
344        }
345    }
346}
347impl std::fmt::Display for CustomerCashBalanceTransactionType {
348    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
349        f.write_str(self.as_str())
350    }
351}
352
353#[cfg(not(feature = "redact-generated-debug"))]
354impl std::fmt::Debug for CustomerCashBalanceTransactionType {
355    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
356        f.write_str(self.as_str())
357    }
358}
359#[cfg(feature = "redact-generated-debug")]
360impl std::fmt::Debug for CustomerCashBalanceTransactionType {
361    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
362        f.debug_struct(stringify!(CustomerCashBalanceTransactionType)).finish_non_exhaustive()
363    }
364}
365#[cfg(feature = "serialize")]
366impl serde::Serialize for CustomerCashBalanceTransactionType {
367    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
368    where
369        S: serde::Serializer,
370    {
371        serializer.serialize_str(self.as_str())
372    }
373}
374impl miniserde::Deserialize for CustomerCashBalanceTransactionType {
375    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
376        crate::Place::new(out)
377    }
378}
379
380impl miniserde::de::Visitor for crate::Place<CustomerCashBalanceTransactionType> {
381    fn string(&mut self, s: &str) -> miniserde::Result<()> {
382        use std::str::FromStr;
383        self.out = Some(CustomerCashBalanceTransactionType::from_str(s).expect("infallible"));
384        Ok(())
385    }
386}
387
388stripe_types::impl_from_val_with_from_str!(CustomerCashBalanceTransactionType);
389#[cfg(feature = "deserialize")]
390impl<'de> serde::Deserialize<'de> for CustomerCashBalanceTransactionType {
391    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
392        use std::str::FromStr;
393        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
394        Ok(Self::from_str(&s).expect("infallible"))
395    }
396}
397impl stripe_types::Object for CustomerCashBalanceTransaction {
398    type Id = stripe_shared::CustomerCashBalanceTransactionId;
399    fn id(&self) -> &Self::Id {
400        &self.id
401    }
402
403    fn into_id(self) -> Self::Id {
404        self.id
405    }
406}
407stripe_types::def_id!(CustomerCashBalanceTransactionId);