stripe_shared/
customer_balance_transaction.rs

1/// Each customer has a [Balance](https://docs.stripe.com/api/customers/object#customer_object-balance) value,.
2/// which denotes a debit or credit that's automatically applied to their next invoice upon finalization.
3/// You may modify the value directly by using the [update customer API](https://docs.stripe.com/api/customers/update),.
4/// or by creating a Customer Balance Transaction, which increments or decrements the customer's `balance` by the specified `amount`.
5///
6/// Related guide: [Customer balance](https://docs.stripe.com/billing/customer/balance)
7///
8/// For more details see <<https://stripe.com/docs/api/customer_balance_transactions/object>>.
9#[derive(Clone, Debug)]
10#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
11pub struct CustomerBalanceTransaction {
12    /// The amount of the transaction.
13    /// A negative value is a credit for the customer's balance, and a positive value is a debit to the customer's `balance`.
14    pub amount: i64,
15    /// The ID of the checkout session (if any) that created the transaction.
16    pub checkout_session: Option<stripe_types::Expandable<stripe_shared::CheckoutSession>>,
17    /// Time at which the object was created. Measured in seconds since the Unix epoch.
18    pub created: stripe_types::Timestamp,
19    /// The ID of the credit note (if any) related to the transaction.
20    pub credit_note: Option<stripe_types::Expandable<stripe_shared::CreditNote>>,
21    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
22    /// Must be a [supported currency](https://stripe.com/docs/currencies).
23    pub currency: stripe_types::Currency,
24    /// The ID of the customer the transaction belongs to.
25    pub customer: stripe_types::Expandable<stripe_shared::Customer>,
26    /// The ID of an Account representing a customer that the transaction belongs to.
27    pub customer_account: Option<String>,
28    /// An arbitrary string attached to the object. Often useful for displaying to users.
29    pub description: Option<String>,
30    /// The customer's `balance` after the transaction was applied.
31    /// A negative value decreases the amount due on the customer's next invoice.
32    /// A positive value increases the amount due on the customer's next invoice.
33    pub ending_balance: i64,
34    /// Unique identifier for the object.
35    pub id: stripe_shared::CustomerBalanceTransactionId,
36    /// The ID of the invoice (if any) related to the transaction.
37    pub invoice: Option<stripe_types::Expandable<stripe_shared::Invoice>>,
38    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
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: Option<std::collections::HashMap<String, String>>,
43    /// Transaction type: `adjustment`, `applied_to_invoice`, `credit_note`, `initial`, `invoice_overpaid`, `invoice_too_large`, `invoice_too_small`, `unspent_receiver_credit`, `unapplied_from_invoice`, `checkout_session_subscription_payment`, or `checkout_session_subscription_payment_canceled`.
44    /// See the [Customer Balance page](https://docs.stripe.com/billing/customer/balance#types) to learn more about transaction types.
45    #[cfg_attr(feature = "deserialize", serde(rename = "type"))]
46    pub type_: CustomerBalanceTransactionType,
47}
48#[doc(hidden)]
49pub struct CustomerBalanceTransactionBuilder {
50    amount: Option<i64>,
51    checkout_session: Option<Option<stripe_types::Expandable<stripe_shared::CheckoutSession>>>,
52    created: Option<stripe_types::Timestamp>,
53    credit_note: Option<Option<stripe_types::Expandable<stripe_shared::CreditNote>>>,
54    currency: Option<stripe_types::Currency>,
55    customer: Option<stripe_types::Expandable<stripe_shared::Customer>>,
56    customer_account: Option<Option<String>>,
57    description: Option<Option<String>>,
58    ending_balance: Option<i64>,
59    id: Option<stripe_shared::CustomerBalanceTransactionId>,
60    invoice: Option<Option<stripe_types::Expandable<stripe_shared::Invoice>>>,
61    livemode: Option<bool>,
62    metadata: Option<Option<std::collections::HashMap<String, String>>>,
63    type_: Option<CustomerBalanceTransactionType>,
64}
65
66#[allow(
67    unused_variables,
68    irrefutable_let_patterns,
69    clippy::let_unit_value,
70    clippy::match_single_binding,
71    clippy::single_match
72)]
73const _: () = {
74    use miniserde::de::{Map, Visitor};
75    use miniserde::json::Value;
76    use miniserde::{Deserialize, Result, make_place};
77    use stripe_types::miniserde_helpers::FromValueOpt;
78    use stripe_types::{MapBuilder, ObjectDeser};
79
80    make_place!(Place);
81
82    impl Deserialize for CustomerBalanceTransaction {
83        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
84            Place::new(out)
85        }
86    }
87
88    struct Builder<'a> {
89        out: &'a mut Option<CustomerBalanceTransaction>,
90        builder: CustomerBalanceTransactionBuilder,
91    }
92
93    impl Visitor for Place<CustomerBalanceTransaction> {
94        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
95            Ok(Box::new(Builder {
96                out: &mut self.out,
97                builder: CustomerBalanceTransactionBuilder::deser_default(),
98            }))
99        }
100    }
101
102    impl MapBuilder for CustomerBalanceTransactionBuilder {
103        type Out = CustomerBalanceTransaction;
104        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
105            Ok(match k {
106                "amount" => Deserialize::begin(&mut self.amount),
107                "checkout_session" => Deserialize::begin(&mut self.checkout_session),
108                "created" => Deserialize::begin(&mut self.created),
109                "credit_note" => Deserialize::begin(&mut self.credit_note),
110                "currency" => Deserialize::begin(&mut self.currency),
111                "customer" => Deserialize::begin(&mut self.customer),
112                "customer_account" => Deserialize::begin(&mut self.customer_account),
113                "description" => Deserialize::begin(&mut self.description),
114                "ending_balance" => Deserialize::begin(&mut self.ending_balance),
115                "id" => Deserialize::begin(&mut self.id),
116                "invoice" => Deserialize::begin(&mut self.invoice),
117                "livemode" => Deserialize::begin(&mut self.livemode),
118                "metadata" => Deserialize::begin(&mut self.metadata),
119                "type" => Deserialize::begin(&mut self.type_),
120                _ => <dyn Visitor>::ignore(),
121            })
122        }
123
124        fn deser_default() -> Self {
125            Self {
126                amount: Deserialize::default(),
127                checkout_session: Deserialize::default(),
128                created: Deserialize::default(),
129                credit_note: Deserialize::default(),
130                currency: Deserialize::default(),
131                customer: Deserialize::default(),
132                customer_account: Deserialize::default(),
133                description: Deserialize::default(),
134                ending_balance: Deserialize::default(),
135                id: Deserialize::default(),
136                invoice: Deserialize::default(),
137                livemode: Deserialize::default(),
138                metadata: Deserialize::default(),
139                type_: Deserialize::default(),
140            }
141        }
142
143        fn take_out(&mut self) -> Option<Self::Out> {
144            let (
145                Some(amount),
146                Some(checkout_session),
147                Some(created),
148                Some(credit_note),
149                Some(currency),
150                Some(customer),
151                Some(customer_account),
152                Some(description),
153                Some(ending_balance),
154                Some(id),
155                Some(invoice),
156                Some(livemode),
157                Some(metadata),
158                Some(type_),
159            ) = (
160                self.amount,
161                self.checkout_session.take(),
162                self.created,
163                self.credit_note.take(),
164                self.currency.take(),
165                self.customer.take(),
166                self.customer_account.take(),
167                self.description.take(),
168                self.ending_balance,
169                self.id.take(),
170                self.invoice.take(),
171                self.livemode,
172                self.metadata.take(),
173                self.type_.take(),
174            )
175            else {
176                return None;
177            };
178            Some(Self::Out {
179                amount,
180                checkout_session,
181                created,
182                credit_note,
183                currency,
184                customer,
185                customer_account,
186                description,
187                ending_balance,
188                id,
189                invoice,
190                livemode,
191                metadata,
192                type_,
193            })
194        }
195    }
196
197    impl Map for Builder<'_> {
198        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
199            self.builder.key(k)
200        }
201
202        fn finish(&mut self) -> Result<()> {
203            *self.out = self.builder.take_out();
204            Ok(())
205        }
206    }
207
208    impl ObjectDeser for CustomerBalanceTransaction {
209        type Builder = CustomerBalanceTransactionBuilder;
210    }
211
212    impl FromValueOpt for CustomerBalanceTransaction {
213        fn from_value(v: Value) -> Option<Self> {
214            let Value::Object(obj) = v else {
215                return None;
216            };
217            let mut b = CustomerBalanceTransactionBuilder::deser_default();
218            for (k, v) in obj {
219                match k.as_str() {
220                    "amount" => b.amount = FromValueOpt::from_value(v),
221                    "checkout_session" => b.checkout_session = FromValueOpt::from_value(v),
222                    "created" => b.created = FromValueOpt::from_value(v),
223                    "credit_note" => b.credit_note = FromValueOpt::from_value(v),
224                    "currency" => b.currency = FromValueOpt::from_value(v),
225                    "customer" => b.customer = FromValueOpt::from_value(v),
226                    "customer_account" => b.customer_account = FromValueOpt::from_value(v),
227                    "description" => b.description = FromValueOpt::from_value(v),
228                    "ending_balance" => b.ending_balance = FromValueOpt::from_value(v),
229                    "id" => b.id = FromValueOpt::from_value(v),
230                    "invoice" => b.invoice = FromValueOpt::from_value(v),
231                    "livemode" => b.livemode = FromValueOpt::from_value(v),
232                    "metadata" => b.metadata = FromValueOpt::from_value(v),
233                    "type" => b.type_ = FromValueOpt::from_value(v),
234                    _ => {}
235                }
236            }
237            b.take_out()
238        }
239    }
240};
241#[cfg(feature = "serialize")]
242impl serde::Serialize for CustomerBalanceTransaction {
243    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
244        use serde::ser::SerializeStruct;
245        let mut s = s.serialize_struct("CustomerBalanceTransaction", 15)?;
246        s.serialize_field("amount", &self.amount)?;
247        s.serialize_field("checkout_session", &self.checkout_session)?;
248        s.serialize_field("created", &self.created)?;
249        s.serialize_field("credit_note", &self.credit_note)?;
250        s.serialize_field("currency", &self.currency)?;
251        s.serialize_field("customer", &self.customer)?;
252        s.serialize_field("customer_account", &self.customer_account)?;
253        s.serialize_field("description", &self.description)?;
254        s.serialize_field("ending_balance", &self.ending_balance)?;
255        s.serialize_field("id", &self.id)?;
256        s.serialize_field("invoice", &self.invoice)?;
257        s.serialize_field("livemode", &self.livemode)?;
258        s.serialize_field("metadata", &self.metadata)?;
259        s.serialize_field("type", &self.type_)?;
260
261        s.serialize_field("object", "customer_balance_transaction")?;
262        s.end()
263    }
264}
265/// Transaction type: `adjustment`, `applied_to_invoice`, `credit_note`, `initial`, `invoice_overpaid`, `invoice_too_large`, `invoice_too_small`, `unspent_receiver_credit`, `unapplied_from_invoice`, `checkout_session_subscription_payment`, or `checkout_session_subscription_payment_canceled`.
266/// See the [Customer Balance page](https://docs.stripe.com/billing/customer/balance#types) to learn more about transaction types.
267#[derive(Clone, Eq, PartialEq)]
268#[non_exhaustive]
269pub enum CustomerBalanceTransactionType {
270    Adjustment,
271    AppliedToInvoice,
272    CheckoutSessionSubscriptionPayment,
273    CheckoutSessionSubscriptionPaymentCanceled,
274    CreditNote,
275    Initial,
276    InvoiceOverpaid,
277    InvoiceTooLarge,
278    InvoiceTooSmall,
279    Migration,
280    UnappliedFromInvoice,
281    UnspentReceiverCredit,
282    /// An unrecognized value from Stripe. Should not be used as a request parameter.
283    Unknown(String),
284}
285impl CustomerBalanceTransactionType {
286    pub fn as_str(&self) -> &str {
287        use CustomerBalanceTransactionType::*;
288        match self {
289            Adjustment => "adjustment",
290            AppliedToInvoice => "applied_to_invoice",
291            CheckoutSessionSubscriptionPayment => "checkout_session_subscription_payment",
292            CheckoutSessionSubscriptionPaymentCanceled => {
293                "checkout_session_subscription_payment_canceled"
294            }
295            CreditNote => "credit_note",
296            Initial => "initial",
297            InvoiceOverpaid => "invoice_overpaid",
298            InvoiceTooLarge => "invoice_too_large",
299            InvoiceTooSmall => "invoice_too_small",
300            Migration => "migration",
301            UnappliedFromInvoice => "unapplied_from_invoice",
302            UnspentReceiverCredit => "unspent_receiver_credit",
303            Unknown(v) => v,
304        }
305    }
306}
307
308impl std::str::FromStr for CustomerBalanceTransactionType {
309    type Err = std::convert::Infallible;
310    fn from_str(s: &str) -> Result<Self, Self::Err> {
311        use CustomerBalanceTransactionType::*;
312        match s {
313            "adjustment" => Ok(Adjustment),
314            "applied_to_invoice" => Ok(AppliedToInvoice),
315            "checkout_session_subscription_payment" => Ok(CheckoutSessionSubscriptionPayment),
316            "checkout_session_subscription_payment_canceled" => {
317                Ok(CheckoutSessionSubscriptionPaymentCanceled)
318            }
319            "credit_note" => Ok(CreditNote),
320            "initial" => Ok(Initial),
321            "invoice_overpaid" => Ok(InvoiceOverpaid),
322            "invoice_too_large" => Ok(InvoiceTooLarge),
323            "invoice_too_small" => Ok(InvoiceTooSmall),
324            "migration" => Ok(Migration),
325            "unapplied_from_invoice" => Ok(UnappliedFromInvoice),
326            "unspent_receiver_credit" => Ok(UnspentReceiverCredit),
327            v => {
328                tracing::warn!(
329                    "Unknown value '{}' for enum '{}'",
330                    v,
331                    "CustomerBalanceTransactionType"
332                );
333                Ok(Unknown(v.to_owned()))
334            }
335        }
336    }
337}
338impl std::fmt::Display for CustomerBalanceTransactionType {
339    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
340        f.write_str(self.as_str())
341    }
342}
343
344impl std::fmt::Debug for CustomerBalanceTransactionType {
345    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
346        f.write_str(self.as_str())
347    }
348}
349#[cfg(feature = "serialize")]
350impl serde::Serialize for CustomerBalanceTransactionType {
351    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
352    where
353        S: serde::Serializer,
354    {
355        serializer.serialize_str(self.as_str())
356    }
357}
358impl miniserde::Deserialize for CustomerBalanceTransactionType {
359    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
360        crate::Place::new(out)
361    }
362}
363
364impl miniserde::de::Visitor for crate::Place<CustomerBalanceTransactionType> {
365    fn string(&mut self, s: &str) -> miniserde::Result<()> {
366        use std::str::FromStr;
367        self.out = Some(CustomerBalanceTransactionType::from_str(s).expect("infallible"));
368        Ok(())
369    }
370}
371
372stripe_types::impl_from_val_with_from_str!(CustomerBalanceTransactionType);
373#[cfg(feature = "deserialize")]
374impl<'de> serde::Deserialize<'de> for CustomerBalanceTransactionType {
375    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
376        use std::str::FromStr;
377        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
378        Ok(Self::from_str(&s).expect("infallible"))
379    }
380}
381impl stripe_types::Object for CustomerBalanceTransaction {
382    type Id = stripe_shared::CustomerBalanceTransactionId;
383    fn id(&self) -> &Self::Id {
384        &self.id
385    }
386
387    fn into_id(self) -> Self::Id {
388        self.id
389    }
390}
391stripe_types::def_id!(CustomerBalanceTransactionId);