stripe_shared/
customer.rs

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