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_.take(),
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(Clone, Eq, PartialEq)]
258#[non_exhaustive]
259pub enum CustomerBalanceTransactionType {
260    Adjustment,
261    AppliedToInvoice,
262    CheckoutSessionSubscriptionPayment,
263    CheckoutSessionSubscriptionPaymentCanceled,
264    CreditNote,
265    Initial,
266    InvoiceOverpaid,
267    InvoiceTooLarge,
268    InvoiceTooSmall,
269    Migration,
270    UnappliedFromInvoice,
271    UnspentReceiverCredit,
272    /// An unrecognized value from Stripe. Should not be used as a request parameter.
273    Unknown(String),
274}
275impl CustomerBalanceTransactionType {
276    pub fn as_str(&self) -> &str {
277        use CustomerBalanceTransactionType::*;
278        match self {
279            Adjustment => "adjustment",
280            AppliedToInvoice => "applied_to_invoice",
281            CheckoutSessionSubscriptionPayment => "checkout_session_subscription_payment",
282            CheckoutSessionSubscriptionPaymentCanceled => {
283                "checkout_session_subscription_payment_canceled"
284            }
285            CreditNote => "credit_note",
286            Initial => "initial",
287            InvoiceOverpaid => "invoice_overpaid",
288            InvoiceTooLarge => "invoice_too_large",
289            InvoiceTooSmall => "invoice_too_small",
290            Migration => "migration",
291            UnappliedFromInvoice => "unapplied_from_invoice",
292            UnspentReceiverCredit => "unspent_receiver_credit",
293            Unknown(v) => v,
294        }
295    }
296}
297
298impl std::str::FromStr for CustomerBalanceTransactionType {
299    type Err = std::convert::Infallible;
300    fn from_str(s: &str) -> Result<Self, Self::Err> {
301        use CustomerBalanceTransactionType::*;
302        match s {
303            "adjustment" => Ok(Adjustment),
304            "applied_to_invoice" => Ok(AppliedToInvoice),
305            "checkout_session_subscription_payment" => Ok(CheckoutSessionSubscriptionPayment),
306            "checkout_session_subscription_payment_canceled" => {
307                Ok(CheckoutSessionSubscriptionPaymentCanceled)
308            }
309            "credit_note" => Ok(CreditNote),
310            "initial" => Ok(Initial),
311            "invoice_overpaid" => Ok(InvoiceOverpaid),
312            "invoice_too_large" => Ok(InvoiceTooLarge),
313            "invoice_too_small" => Ok(InvoiceTooSmall),
314            "migration" => Ok(Migration),
315            "unapplied_from_invoice" => Ok(UnappliedFromInvoice),
316            "unspent_receiver_credit" => Ok(UnspentReceiverCredit),
317            v => {
318                tracing::warn!(
319                    "Unknown value '{}' for enum '{}'",
320                    v,
321                    "CustomerBalanceTransactionType"
322                );
323                Ok(Unknown(v.to_owned()))
324            }
325        }
326    }
327}
328impl std::fmt::Display for CustomerBalanceTransactionType {
329    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
330        f.write_str(self.as_str())
331    }
332}
333
334impl std::fmt::Debug for CustomerBalanceTransactionType {
335    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
336        f.write_str(self.as_str())
337    }
338}
339#[cfg(feature = "serialize")]
340impl serde::Serialize for CustomerBalanceTransactionType {
341    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
342    where
343        S: serde::Serializer,
344    {
345        serializer.serialize_str(self.as_str())
346    }
347}
348impl miniserde::Deserialize for CustomerBalanceTransactionType {
349    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
350        crate::Place::new(out)
351    }
352}
353
354impl miniserde::de::Visitor for crate::Place<CustomerBalanceTransactionType> {
355    fn string(&mut self, s: &str) -> miniserde::Result<()> {
356        use std::str::FromStr;
357        self.out = Some(CustomerBalanceTransactionType::from_str(s).expect("infallible"));
358        Ok(())
359    }
360}
361
362stripe_types::impl_from_val_with_from_str!(CustomerBalanceTransactionType);
363#[cfg(feature = "deserialize")]
364impl<'de> serde::Deserialize<'de> for CustomerBalanceTransactionType {
365    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
366        use std::str::FromStr;
367        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
368        Ok(Self::from_str(&s).expect("infallible"))
369    }
370}
371impl stripe_types::Object for CustomerBalanceTransaction {
372    type Id = stripe_shared::CustomerBalanceTransactionId;
373    fn id(&self) -> &Self::Id {
374        &self.id
375    }
376
377    fn into_id(self) -> Self::Id {
378        self.id
379    }
380}
381stripe_types::def_id!(CustomerBalanceTransactionId);