stripe_shared/
customer.rs

1/// This object represents a customer of your business.
2/// Use it to [create recurring charges](https://stripe.com/docs/invoicing/customer), [save payment](https://stripe.com/docs/payments/save-during-payment) and contact information,.
3/// and track payments that belong to the same customer.
4///
5/// For more details see <<https://stripe.com/docs/api/customers/object>>.
6#[derive(Clone, Debug)]
7#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
8pub struct Customer {
9    /// The customer's address.
10    pub address: Option<stripe_shared::Address>,
11    /// The current balance, if any, that's stored on the customer in their default currency.
12    /// If negative, the customer has credit to apply to their next invoice.
13    /// If positive, the customer has an amount owed that's added to their next invoice.
14    /// The balance only considers amounts that Stripe hasn't successfully applied to any invoice.
15    /// It doesn't reflect unpaid invoices.
16    /// This balance is only taken into account after invoices finalize.
17    /// For multi-currency balances, see [invoice_credit_balance](https://stripe.com/docs/api/customers/object#customer_object-invoice_credit_balance).
18    pub balance: Option<i64>,
19    /// The customer's business name.
20    pub business_name: Option<String>,
21    /// The current funds being held by Stripe on behalf of the customer.
22    /// You can apply these funds towards payment intents when the source is "cash_balance".
23    /// The `settings[reconciliation_mode]` field describes if these funds apply to these payment intents manually or automatically.
24    pub cash_balance: Option<stripe_shared::CashBalance>,
25    /// Time at which the object was created. Measured in seconds since the Unix epoch.
26    pub created: stripe_types::Timestamp,
27    /// Three-letter [ISO code for the currency](https://stripe.com/docs/currencies) the customer can be charged in for recurring billing purposes.
28    pub currency: Option<stripe_types::Currency>,
29    /// ID of the default payment source for the customer.
30    ///
31    /// If you use payment methods created through the PaymentMethods API, see the [invoice_settings.default_payment_method](https://stripe.com/docs/api/customers/object#customer_object-invoice_settings-default_payment_method) field instead.
32    pub default_source: Option<stripe_types::Expandable<stripe_shared::PaymentSource>>,
33    /// Tracks the most recent state change on any invoice belonging to the customer.
34    /// Paying an invoice or marking it uncollectible via the API will set this field to false.
35    /// An automatic payment failure or passing the `invoice.due_date` will set this field to `true`.
36    ///
37    /// If an invoice becomes uncollectible by [dunning](https://stripe.com/docs/billing/automatic-collection), `delinquent` doesn't reset to `false`.
38    ///
39    /// If you care whether the customer has paid their most recent subscription invoice, use `subscription.status` instead.
40    /// Paying or marking uncollectible any customer invoice regardless of whether it is the latest invoice for a subscription will always set this field to `false`.
41    pub delinquent: Option<bool>,
42    /// An arbitrary string attached to the object. Often useful for displaying to users.
43    pub description: Option<String>,
44    /// Describes the current discount active on the customer, if there is one.
45    pub discount: Option<stripe_shared::Discount>,
46    /// The customer's email address.
47    pub email: Option<String>,
48    /// Unique identifier for the object.
49    pub id: stripe_shared::CustomerId,
50    /// The customer's individual name.
51    pub individual_name: Option<String>,
52    /// The current multi-currency balances, if any, that's stored on the customer.
53    /// If positive in a currency, the customer has a credit to apply to their next invoice denominated in that currency.
54    /// If negative, the customer has an amount owed that's added to their next invoice denominated in that currency.
55    /// These balances don't apply to unpaid invoices.
56    /// They solely track amounts that Stripe hasn't successfully applied to any invoice.
57    /// Stripe only applies a balance in a specific currency to an invoice after that invoice (which is in the same currency) finalizes.
58    pub invoice_credit_balance: Option<std::collections::HashMap<String, i64>>,
59    /// The prefix for the customer used to generate unique invoice numbers.
60    pub invoice_prefix: Option<String>,
61    pub invoice_settings: Option<stripe_shared::InvoiceSettingCustomerSetting>,
62    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
63    pub livemode: bool,
64    /// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object.
65    /// This can be useful for storing additional information about the object in a structured format.
66    pub metadata: Option<std::collections::HashMap<String, String>>,
67    /// The customer's full name or business name.
68    pub name: Option<String>,
69    /// The suffix of the customer's next invoice number (for example, 0001).
70    /// When the account uses account level sequencing, this parameter is ignored in API requests and the field omitted in API responses.
71    pub next_invoice_sequence: Option<i64>,
72    /// The customer's phone number.
73    pub phone: Option<String>,
74    /// The customer's preferred locales (languages), ordered by preference.
75    pub preferred_locales: Option<Vec<String>>,
76    /// Mailing and shipping address for the customer. Appears on invoices emailed to this customer.
77    pub shipping: Option<stripe_shared::Shipping>,
78    /// The customer's payment sources, if any.
79    pub sources: Option<stripe_types::List<stripe_shared::PaymentSource>>,
80    /// The customer's current subscriptions, if any.
81    pub subscriptions: Option<stripe_types::List<stripe_shared::Subscription>>,
82    pub tax: Option<stripe_shared::CustomerTax>,
83    /// Describes the customer's tax exemption status, which is `none`, `exempt`, or `reverse`.
84    /// When set to `reverse`, invoice and receipt PDFs include the following text: **"Reverse charge"**.
85    pub tax_exempt: Option<stripe_shared::CustomerTaxExempt>,
86    /// The customer's tax IDs.
87    pub tax_ids: Option<stripe_types::List<stripe_shared::TaxId>>,
88    /// ID of the test clock that this customer belongs to.
89    pub test_clock: Option<stripe_types::Expandable<stripe_shared::TestHelpersTestClock>>,
90}
91#[doc(hidden)]
92pub struct CustomerBuilder {
93    address: Option<Option<stripe_shared::Address>>,
94    balance: Option<Option<i64>>,
95    business_name: Option<Option<String>>,
96    cash_balance: Option<Option<stripe_shared::CashBalance>>,
97    created: Option<stripe_types::Timestamp>,
98    currency: Option<Option<stripe_types::Currency>>,
99    default_source: Option<Option<stripe_types::Expandable<stripe_shared::PaymentSource>>>,
100    delinquent: Option<Option<bool>>,
101    description: Option<Option<String>>,
102    discount: Option<Option<stripe_shared::Discount>>,
103    email: Option<Option<String>>,
104    id: Option<stripe_shared::CustomerId>,
105    individual_name: Option<Option<String>>,
106    invoice_credit_balance: Option<Option<std::collections::HashMap<String, i64>>>,
107    invoice_prefix: Option<Option<String>>,
108    invoice_settings: Option<Option<stripe_shared::InvoiceSettingCustomerSetting>>,
109    livemode: Option<bool>,
110    metadata: Option<Option<std::collections::HashMap<String, String>>>,
111    name: Option<Option<String>>,
112    next_invoice_sequence: Option<Option<i64>>,
113    phone: Option<Option<String>>,
114    preferred_locales: Option<Option<Vec<String>>>,
115    shipping: Option<Option<stripe_shared::Shipping>>,
116    sources: Option<Option<stripe_types::List<stripe_shared::PaymentSource>>>,
117    subscriptions: Option<Option<stripe_types::List<stripe_shared::Subscription>>>,
118    tax: Option<Option<stripe_shared::CustomerTax>>,
119    tax_exempt: Option<Option<stripe_shared::CustomerTaxExempt>>,
120    tax_ids: Option<Option<stripe_types::List<stripe_shared::TaxId>>>,
121    test_clock: Option<Option<stripe_types::Expandable<stripe_shared::TestHelpersTestClock>>>,
122}
123
124#[allow(
125    unused_variables,
126    irrefutable_let_patterns,
127    clippy::let_unit_value,
128    clippy::match_single_binding,
129    clippy::single_match
130)]
131const _: () = {
132    use miniserde::de::{Map, Visitor};
133    use miniserde::json::Value;
134    use miniserde::{Deserialize, Result, make_place};
135    use stripe_types::miniserde_helpers::FromValueOpt;
136    use stripe_types::{MapBuilder, ObjectDeser};
137
138    make_place!(Place);
139
140    impl Deserialize for Customer {
141        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
142            Place::new(out)
143        }
144    }
145
146    struct Builder<'a> {
147        out: &'a mut Option<Customer>,
148        builder: CustomerBuilder,
149    }
150
151    impl Visitor for Place<Customer> {
152        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
153            Ok(Box::new(Builder { out: &mut self.out, builder: CustomerBuilder::deser_default() }))
154        }
155    }
156
157    impl MapBuilder for CustomerBuilder {
158        type Out = Customer;
159        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
160            Ok(match k {
161                "address" => Deserialize::begin(&mut self.address),
162                "balance" => Deserialize::begin(&mut self.balance),
163                "business_name" => Deserialize::begin(&mut self.business_name),
164                "cash_balance" => Deserialize::begin(&mut self.cash_balance),
165                "created" => Deserialize::begin(&mut self.created),
166                "currency" => Deserialize::begin(&mut self.currency),
167                "default_source" => Deserialize::begin(&mut self.default_source),
168                "delinquent" => Deserialize::begin(&mut self.delinquent),
169                "description" => Deserialize::begin(&mut self.description),
170                "discount" => Deserialize::begin(&mut self.discount),
171                "email" => Deserialize::begin(&mut self.email),
172                "id" => Deserialize::begin(&mut self.id),
173                "individual_name" => Deserialize::begin(&mut self.individual_name),
174                "invoice_credit_balance" => Deserialize::begin(&mut self.invoice_credit_balance),
175                "invoice_prefix" => Deserialize::begin(&mut self.invoice_prefix),
176                "invoice_settings" => Deserialize::begin(&mut self.invoice_settings),
177                "livemode" => Deserialize::begin(&mut self.livemode),
178                "metadata" => Deserialize::begin(&mut self.metadata),
179                "name" => Deserialize::begin(&mut self.name),
180                "next_invoice_sequence" => Deserialize::begin(&mut self.next_invoice_sequence),
181                "phone" => Deserialize::begin(&mut self.phone),
182                "preferred_locales" => Deserialize::begin(&mut self.preferred_locales),
183                "shipping" => Deserialize::begin(&mut self.shipping),
184                "sources" => Deserialize::begin(&mut self.sources),
185                "subscriptions" => Deserialize::begin(&mut self.subscriptions),
186                "tax" => Deserialize::begin(&mut self.tax),
187                "tax_exempt" => Deserialize::begin(&mut self.tax_exempt),
188                "tax_ids" => Deserialize::begin(&mut self.tax_ids),
189                "test_clock" => Deserialize::begin(&mut self.test_clock),
190                _ => <dyn Visitor>::ignore(),
191            })
192        }
193
194        fn deser_default() -> Self {
195            Self {
196                address: Deserialize::default(),
197                balance: Deserialize::default(),
198                business_name: Deserialize::default(),
199                cash_balance: Deserialize::default(),
200                created: Deserialize::default(),
201                currency: Deserialize::default(),
202                default_source: Deserialize::default(),
203                delinquent: Deserialize::default(),
204                description: Deserialize::default(),
205                discount: Deserialize::default(),
206                email: Deserialize::default(),
207                id: Deserialize::default(),
208                individual_name: Deserialize::default(),
209                invoice_credit_balance: Deserialize::default(),
210                invoice_prefix: Deserialize::default(),
211                invoice_settings: Deserialize::default(),
212                livemode: Deserialize::default(),
213                metadata: Deserialize::default(),
214                name: Deserialize::default(),
215                next_invoice_sequence: Deserialize::default(),
216                phone: Deserialize::default(),
217                preferred_locales: Deserialize::default(),
218                shipping: Deserialize::default(),
219                sources: Deserialize::default(),
220                subscriptions: Deserialize::default(),
221                tax: Deserialize::default(),
222                tax_exempt: Deserialize::default(),
223                tax_ids: Deserialize::default(),
224                test_clock: Deserialize::default(),
225            }
226        }
227
228        fn take_out(&mut self) -> Option<Self::Out> {
229            let (
230                Some(address),
231                Some(balance),
232                Some(business_name),
233                Some(cash_balance),
234                Some(created),
235                Some(currency),
236                Some(default_source),
237                Some(delinquent),
238                Some(description),
239                Some(discount),
240                Some(email),
241                Some(id),
242                Some(individual_name),
243                Some(invoice_credit_balance),
244                Some(invoice_prefix),
245                Some(invoice_settings),
246                Some(livemode),
247                Some(metadata),
248                Some(name),
249                Some(next_invoice_sequence),
250                Some(phone),
251                Some(preferred_locales),
252                Some(shipping),
253                Some(sources),
254                Some(subscriptions),
255                Some(tax),
256                Some(tax_exempt),
257                Some(tax_ids),
258                Some(test_clock),
259            ) = (
260                self.address.take(),
261                self.balance,
262                self.business_name.take(),
263                self.cash_balance.take(),
264                self.created,
265                self.currency.take(),
266                self.default_source.take(),
267                self.delinquent,
268                self.description.take(),
269                self.discount.take(),
270                self.email.take(),
271                self.id.take(),
272                self.individual_name.take(),
273                self.invoice_credit_balance.take(),
274                self.invoice_prefix.take(),
275                self.invoice_settings.take(),
276                self.livemode,
277                self.metadata.take(),
278                self.name.take(),
279                self.next_invoice_sequence,
280                self.phone.take(),
281                self.preferred_locales.take(),
282                self.shipping.take(),
283                self.sources.take(),
284                self.subscriptions.take(),
285                self.tax.take(),
286                self.tax_exempt,
287                self.tax_ids.take(),
288                self.test_clock.take(),
289            )
290            else {
291                return None;
292            };
293            Some(Self::Out {
294                address,
295                balance,
296                business_name,
297                cash_balance,
298                created,
299                currency,
300                default_source,
301                delinquent,
302                description,
303                discount,
304                email,
305                id,
306                individual_name,
307                invoice_credit_balance,
308                invoice_prefix,
309                invoice_settings,
310                livemode,
311                metadata,
312                name,
313                next_invoice_sequence,
314                phone,
315                preferred_locales,
316                shipping,
317                sources,
318                subscriptions,
319                tax,
320                tax_exempt,
321                tax_ids,
322                test_clock,
323            })
324        }
325    }
326
327    impl Map for Builder<'_> {
328        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
329            self.builder.key(k)
330        }
331
332        fn finish(&mut self) -> Result<()> {
333            *self.out = self.builder.take_out();
334            Ok(())
335        }
336    }
337
338    impl ObjectDeser for Customer {
339        type Builder = CustomerBuilder;
340    }
341
342    impl FromValueOpt for Customer {
343        fn from_value(v: Value) -> Option<Self> {
344            let Value::Object(obj) = v else {
345                return None;
346            };
347            let mut b = CustomerBuilder::deser_default();
348            for (k, v) in obj {
349                match k.as_str() {
350                    "address" => b.address = FromValueOpt::from_value(v),
351                    "balance" => b.balance = FromValueOpt::from_value(v),
352                    "business_name" => b.business_name = FromValueOpt::from_value(v),
353                    "cash_balance" => b.cash_balance = FromValueOpt::from_value(v),
354                    "created" => b.created = FromValueOpt::from_value(v),
355                    "currency" => b.currency = FromValueOpt::from_value(v),
356                    "default_source" => b.default_source = FromValueOpt::from_value(v),
357                    "delinquent" => b.delinquent = FromValueOpt::from_value(v),
358                    "description" => b.description = FromValueOpt::from_value(v),
359                    "discount" => b.discount = FromValueOpt::from_value(v),
360                    "email" => b.email = FromValueOpt::from_value(v),
361                    "id" => b.id = FromValueOpt::from_value(v),
362                    "individual_name" => b.individual_name = FromValueOpt::from_value(v),
363                    "invoice_credit_balance" => {
364                        b.invoice_credit_balance = FromValueOpt::from_value(v)
365                    }
366                    "invoice_prefix" => b.invoice_prefix = FromValueOpt::from_value(v),
367                    "invoice_settings" => b.invoice_settings = FromValueOpt::from_value(v),
368                    "livemode" => b.livemode = FromValueOpt::from_value(v),
369                    "metadata" => b.metadata = FromValueOpt::from_value(v),
370                    "name" => b.name = FromValueOpt::from_value(v),
371                    "next_invoice_sequence" => {
372                        b.next_invoice_sequence = FromValueOpt::from_value(v)
373                    }
374                    "phone" => b.phone = FromValueOpt::from_value(v),
375                    "preferred_locales" => b.preferred_locales = FromValueOpt::from_value(v),
376                    "shipping" => b.shipping = FromValueOpt::from_value(v),
377                    "sources" => b.sources = FromValueOpt::from_value(v),
378                    "subscriptions" => b.subscriptions = FromValueOpt::from_value(v),
379                    "tax" => b.tax = FromValueOpt::from_value(v),
380                    "tax_exempt" => b.tax_exempt = FromValueOpt::from_value(v),
381                    "tax_ids" => b.tax_ids = FromValueOpt::from_value(v),
382                    "test_clock" => b.test_clock = FromValueOpt::from_value(v),
383                    _ => {}
384                }
385            }
386            b.take_out()
387        }
388    }
389};
390#[cfg(feature = "serialize")]
391impl serde::Serialize for Customer {
392    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
393        use serde::ser::SerializeStruct;
394        let mut s = s.serialize_struct("Customer", 30)?;
395        s.serialize_field("address", &self.address)?;
396        s.serialize_field("balance", &self.balance)?;
397        s.serialize_field("business_name", &self.business_name)?;
398        s.serialize_field("cash_balance", &self.cash_balance)?;
399        s.serialize_field("created", &self.created)?;
400        s.serialize_field("currency", &self.currency)?;
401        s.serialize_field("default_source", &self.default_source)?;
402        s.serialize_field("delinquent", &self.delinquent)?;
403        s.serialize_field("description", &self.description)?;
404        s.serialize_field("discount", &self.discount)?;
405        s.serialize_field("email", &self.email)?;
406        s.serialize_field("id", &self.id)?;
407        s.serialize_field("individual_name", &self.individual_name)?;
408        s.serialize_field("invoice_credit_balance", &self.invoice_credit_balance)?;
409        s.serialize_field("invoice_prefix", &self.invoice_prefix)?;
410        s.serialize_field("invoice_settings", &self.invoice_settings)?;
411        s.serialize_field("livemode", &self.livemode)?;
412        s.serialize_field("metadata", &self.metadata)?;
413        s.serialize_field("name", &self.name)?;
414        s.serialize_field("next_invoice_sequence", &self.next_invoice_sequence)?;
415        s.serialize_field("phone", &self.phone)?;
416        s.serialize_field("preferred_locales", &self.preferred_locales)?;
417        s.serialize_field("shipping", &self.shipping)?;
418        s.serialize_field("sources", &self.sources)?;
419        s.serialize_field("subscriptions", &self.subscriptions)?;
420        s.serialize_field("tax", &self.tax)?;
421        s.serialize_field("tax_exempt", &self.tax_exempt)?;
422        s.serialize_field("tax_ids", &self.tax_ids)?;
423        s.serialize_field("test_clock", &self.test_clock)?;
424
425        s.serialize_field("object", "customer")?;
426        s.end()
427    }
428}
429impl stripe_types::Object for Customer {
430    type Id = stripe_shared::CustomerId;
431    fn id(&self) -> &Self::Id {
432        &self.id
433    }
434
435    fn into_id(self) -> Self::Id {
436        self.id
437    }
438}
439stripe_types::def_id!(CustomerId);
440#[derive(Copy, Clone, Eq, PartialEq)]
441pub enum CustomerTaxExempt {
442    Exempt,
443    None,
444    Reverse,
445}
446impl CustomerTaxExempt {
447    pub fn as_str(self) -> &'static str {
448        use CustomerTaxExempt::*;
449        match self {
450            Exempt => "exempt",
451            None => "none",
452            Reverse => "reverse",
453        }
454    }
455}
456
457impl std::str::FromStr for CustomerTaxExempt {
458    type Err = stripe_types::StripeParseError;
459    fn from_str(s: &str) -> Result<Self, Self::Err> {
460        use CustomerTaxExempt::*;
461        match s {
462            "exempt" => Ok(Exempt),
463            "none" => Ok(None),
464            "reverse" => Ok(Reverse),
465            _ => Err(stripe_types::StripeParseError),
466        }
467    }
468}
469impl std::fmt::Display for CustomerTaxExempt {
470    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
471        f.write_str(self.as_str())
472    }
473}
474
475impl std::fmt::Debug for CustomerTaxExempt {
476    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
477        f.write_str(self.as_str())
478    }
479}
480impl serde::Serialize for CustomerTaxExempt {
481    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
482    where
483        S: serde::Serializer,
484    {
485        serializer.serialize_str(self.as_str())
486    }
487}
488impl miniserde::Deserialize for CustomerTaxExempt {
489    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
490        crate::Place::new(out)
491    }
492}
493
494impl miniserde::de::Visitor for crate::Place<CustomerTaxExempt> {
495    fn string(&mut self, s: &str) -> miniserde::Result<()> {
496        use std::str::FromStr;
497        self.out = Some(CustomerTaxExempt::from_str(s).map_err(|_| miniserde::Error)?);
498        Ok(())
499    }
500}
501
502stripe_types::impl_from_val_with_from_str!(CustomerTaxExempt);
503#[cfg(feature = "deserialize")]
504impl<'de> serde::Deserialize<'de> for CustomerTaxExempt {
505    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
506        use std::str::FromStr;
507        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
508        Self::from_str(&s)
509            .map_err(|_| serde::de::Error::custom("Unknown value for CustomerTaxExempt"))
510    }
511}