stripe_shared/
customer_balance_transaction.rs

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