Skip to main content

payrail_core/
customer.rs

1use crate::{CountryCode, PhoneNumber};
2
3/// Optional customer details attached to a payment request.
4#[derive(Debug, Clone, Default, PartialEq, Eq)]
5pub struct Customer {
6    email: Option<String>,
7    phone: Option<PhoneNumber>,
8    country: Option<CountryCode>,
9    name: Option<String>,
10}
11
12impl Customer {
13    /// Creates an empty customer.
14    #[inline]
15    #[must_use]
16    pub fn new() -> Self {
17        Self::default()
18    }
19
20    /// Sets the email address.
21    #[must_use]
22    pub fn with_email(mut self, email: impl Into<String>) -> Self {
23        self.email = Some(email.into());
24        self
25    }
26
27    /// Sets the phone number.
28    #[must_use]
29    pub fn with_phone(mut self, phone: PhoneNumber) -> Self {
30        self.phone = Some(phone);
31        self
32    }
33
34    /// Sets the country.
35    #[must_use]
36    pub fn with_country(mut self, country: CountryCode) -> Self {
37        self.country = Some(country);
38        self
39    }
40
41    /// Sets the customer name.
42    #[must_use]
43    pub fn with_name(mut self, name: impl Into<String>) -> Self {
44        self.name = Some(name.into());
45        self
46    }
47
48    /// Returns the email address.
49    #[inline]
50    #[must_use]
51    pub fn email(&self) -> Option<&str> {
52        self.email.as_deref()
53    }
54
55    /// Returns the phone number.
56    #[inline]
57    #[must_use]
58    pub const fn phone(&self) -> Option<&PhoneNumber> {
59        self.phone.as_ref()
60    }
61
62    /// Returns the country.
63    #[inline]
64    #[must_use]
65    pub const fn country(&self) -> Option<&CountryCode> {
66        self.country.as_ref()
67    }
68
69    /// Returns the customer name.
70    #[inline]
71    #[must_use]
72    pub fn name(&self) -> Option<&str> {
73        self.name.as_deref()
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn builder_methods_set_customer_fields() {
83        let phone = PhoneNumber::new("260971234567").expect("phone should be valid");
84        let country = CountryCode::new("ZM").expect("country should be valid");
85
86        let customer = Customer::new()
87            .with_email("customer@example.com")
88            .with_phone(phone)
89            .with_country(country)
90            .with_name("Ada Buyer");
91
92        assert_eq!(customer.email(), Some("customer@example.com"));
93        assert_eq!(
94            customer.phone().expect("phone should be present").as_e164(),
95            "+260971234567"
96        );
97        assert_eq!(
98            customer
99                .country()
100                .expect("country should be present")
101                .as_str(),
102            "ZM"
103        );
104        assert_eq!(customer.name(), Some("Ada Buyer"));
105    }
106}