square_rust/api/models/request/create_customer/versions/
v20230925.rs

1//! Models for the CreateCustomerRequestV20230925 endpoint.
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::api::models::objects::address::Address;
7use crate::api::models::objects::customer_tax_ids::CustomerTaxIds;
8
9/// Creates a new customer for a business. For api version 2023-09-25
10
11/// You must provide at least one of the following values in your request to this endpoint:
12
13/// - given_name
14/// - family_name
15/// - company_name
16/// - email_address
17/// - phone_number
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct CreateCustomerRequestV20230925 {
20    /// The idempotency key for the request. For more information, see [Idempotency](https://developer.squareup.com/docs/build-basics/common-api-patterns/idempotency).
21    pub idempotency_key: Option<String>,
22    /// The given name (that is, the first name) associated with the customer profile.
23    /// The maximum length for this value is 300 characters.
24    pub given_name: Option<String>,
25    /// The family name (that is, the last name) associated with the customer profile.
26    /// The maximum length for this value is 300 characters.
27    pub family_name: Option<String>,
28    /// A business name associated with the customer profile.
29    /// The maximum length for this value is 500 characters.
30    pub company_name: Option<String>,
31    /// A nickname for the customer profile.
32    /// The maximum length for this value is 100 characters.
33    pub nickname: Option<String>,
34    /// The email address associated with the customer profile.
35    /// The maximum length for this value is 254 characters.
36    pub email_address: Option<String>,
37    /// The physical address associated with the customer profile. For maximum length constraints, see
38    /// [Customer addresses](https://developer.squareup.com/docs/customers-api/use-the-api/keep-records#address). The first_name and last_name fields are ignored if they are present in the request.
39    pub address: Option<Address>,
40    /// The phone number associated with the customer profile. The phone number must be valid and can contain 9–16 digits,
41    ///  with an optional + prefix and country code. For more information, see [Customer phone numbers](https://developer.squareup.com/docs/customers-api/use-the-api/keep-records#phone-number).
42    pub phone_number: Option<String>,
43    /// An optional second ID used to associate the customer profile with an entity in another system.
44    /// The maximum length for this value is 100 characters.
45    pub reference_id: Option<String>,
46    /// A custom note associated with the customer profile.
47    pub note: Option<String>,
48    /// The birthday associated with the customer profile, in YYYY-MM-DD or MM-DD format. For example, specify
49    /// 1998-09-21 for September 21, 1998, or 09-21 for September 21. Birthdays are returned in YYYY-MM-DD
50    ///  format, where YYYY is the specified birth year or 0000 if a birth year is not specified.
51    pub birthday: Option<String>,
52    /// The tax ID associated with the customer profile. This field is available only for customers of sellers in EU countries
53    ///  or the United Kingdom. In other countries, this field is ignored when included in a CreateCustomer request. For
54    /// more information, see [Customer tax IDs](https://developer.squareup.com/docs/customers-api/what-it-does#customer-tax-ids).
55    pub tax_ids: Option<Vec<CustomerTaxIds>>,
56}
57
58impl CreateCustomerRequestV20230925 {
59    #[allow(clippy::too_many_arguments)]
60    pub fn new(
61        idempotency_key: Option<String>,
62        given_name: Option<String>,
63        family_name: Option<String>,
64        company_name: Option<String>,
65        nickname: Option<String>,
66        email_address: Option<String>,
67        address: Option<Address>,
68        phone_number: Option<String>,
69        reference_id: Option<String>,
70        note: Option<String>,
71        birthday: Option<String>,
72        tax_ids: Option<Vec<CustomerTaxIds>>,
73    ) -> CreateCustomerRequestV20230925 {
74        CreateCustomerRequestV20230925::validate_required_keys(
75            given_name.clone(),
76            family_name.clone(),
77            company_name.clone(),
78            email_address.clone(),
79            phone_number.clone(),
80        );
81        CreateCustomerRequestV20230925 {
82            idempotency_key,
83            given_name,
84            family_name,
85            company_name,
86            nickname,
87            email_address,
88            address,
89            phone_number,
90            reference_id,
91            note,
92            birthday,
93            tax_ids,
94        }
95    }
96
97    fn validate_required_keys(
98        given_name: Option<String>,
99        family_name: Option<String>,
100        company_name: Option<String>,
101        email_address: Option<String>,
102        phone_number: Option<String>,
103    ) {
104        if given_name.is_none()
105            && family_name.is_none()
106            && company_name.is_none()
107            && email_address.is_none()
108            && phone_number.is_none()
109        {
110            panic!("At least one of the following values must be provided: given_name, family_name, company_name, email_address, phone_number");
111        }
112    }
113
114    /// Converts the CreateCustomerRequestV20230925 into a JSON string.
115    pub fn to_body_string(&self) -> String {
116        let mut json_value: Value = serde_json::to_value(self).unwrap();
117        CreateCustomerRequestV20230925::remove_nulls(&mut json_value);
118        json_value.to_string()
119    }
120
121    fn remove_nulls(value: &mut Value) {
122        match value {
123            Value::Object(map) => {
124                map.retain(|_, v| !v.is_null());
125                for v in map.values_mut() {
126                    CreateCustomerRequestV20230925::remove_nulls(v);
127                }
128            }
129            Value::Array(arr) => {
130                for v in arr.iter_mut() {
131                    CreateCustomerRequestV20230925::remove_nulls(v);
132                }
133            }
134            _ => {}
135        }
136    }
137}
138
139#[cfg(test)]
140mod tests {
141    use serde_json::json;
142
143    use super::*;
144    use crate::api::models::enums::country::Country;
145
146    #[test]
147    fn test_full_serialization() {
148        let request = CreateCustomerRequestV20230925 {
149            idempotency_key: Some("key".to_string()),
150            given_name: Some("John".to_string()),
151            family_name: Some("Doe".to_string()),
152            company_name: Some("Company".to_string()),
153            nickname: Some("JD".to_string()),
154            email_address: Some("john.doe@example.com".to_string()),
155            address: Some(Address {
156                address_line_1: Some("123 Main St".to_string()),
157                address_line_2: Some("Apt 4".to_string()),
158                address_line_3: None,
159                locality: Some("Smalltown".to_string()),
160                sublocality: None,
161                sublocality_2: None,
162                sublocality_3: None,
163                administrative_district_level_1: Some("CA".to_string()),
164                administrative_district_level_2: None,
165                administrative_district_level_3: None,
166                postal_code: Some("12345".to_string()),
167                country: Some(Country::Us),
168                first_name: Some("John".to_string()),
169                last_name: Some("Doe".to_string()),
170            }),
171            phone_number: Some("1234567890".to_string()),
172            reference_id: Some("ref123".to_string()),
173            note: Some("This is a note.".to_string()),
174            birthday: Some("1990-01-01".to_string()),
175            tax_ids: Some(vec![CustomerTaxIds {
176                eu_vat: Some("EU123456".to_string()),
177            }]),
178        };
179
180        let serialized = request.to_body_string();
181        let expected = json!({
182            "idempotency_key": "key",
183            "given_name": "John",
184            "family_name": "Doe",
185            "company_name": "Company",
186            "nickname": "JD",
187            "email_address": "john.doe@example.com",
188            "address": {
189                "address_line_1": "123 Main St",
190                "address_line_2": "Apt 4",
191                "locality": "Smalltown",
192                "administrative_district_level_1": "CA",
193                "postal_code": "12345",
194                "country": "US",
195                "first_name": "John",
196                "last_name": "Doe"
197            },
198            "phone_number": "1234567890",
199            "reference_id": "ref123",
200            "note": "This is a note.",
201            "birthday": "1990-01-01",
202            "tax_ids": [
203                {
204                    "eu_vat": "EU123456"
205                }
206            ]
207        })
208        .to_string();
209
210        assert_eq!(serialized, expected);
211    }
212}