sumup_rs/
customers.rs

1use crate::{
2    CreateCustomerRequest, Customer, PaymentInstrument, Result, SumUpClient, UpdateCustomerRequest,
3};
4
5impl SumUpClient {
6    /// Creates a new saved customer resource.
7    ///
8    /// # Arguments
9    /// * `body` - The request body containing the customer_id and personal details.
10    pub async fn create_customer(&self, body: &CreateCustomerRequest) -> Result<Customer> {
11        let url = self.build_url("/v0.1/customers")?;
12
13        let response = self
14            .http_client
15            .post(url)
16            .bearer_auth(&self.api_key)
17            .json(body)
18            .send()
19            .await?;
20
21        if response.status().is_success() {
22            let customer = response.json::<Customer>().await?;
23            Ok(customer)
24        } else {
25            self.handle_error(response).await
26        }
27    }
28
29    /// Retrieves an identified saved customer resource.
30    ///
31    /// # Arguments
32    /// * `customer_id` - The unique ID of the customer.
33    pub async fn retrieve_customer(&self, customer_id: &str) -> Result<Customer> {
34        let url = self.build_url(&format!("/v0.1/customers/{}", customer_id))?;
35
36        let response = self
37            .http_client
38            .get(url)
39            .bearer_auth(&self.api_key)
40            .send()
41            .await?;
42
43        if response.status().is_success() {
44            let customer = response.json::<Customer>().await?;
45            Ok(customer)
46        } else {
47            self.handle_error(response).await
48        }
49    }
50
51    /// Updates an identified saved customer resource's personal details.
52    ///
53    /// # Arguments
54    /// * `customer_id` - The unique ID of the customer.
55    /// * `body` - The customer details to update.
56    pub async fn update_customer(
57        &self,
58        customer_id: &str,
59        body: &UpdateCustomerRequest,
60    ) -> Result<Customer> {
61        let url = self.build_url(&format!("/v0.1/customers/{}", customer_id))?;
62
63        let response = self
64            .http_client
65            .put(url)
66            .bearer_auth(&self.api_key)
67            .json(body)
68            .send()
69            .await?;
70
71        if response.status().is_success() {
72            let customer = response.json::<Customer>().await?;
73            Ok(customer)
74        } else {
75            self.handle_error(response).await
76        }
77    }
78
79    /// Lists all payment instrument resources that are saved for an identified customer.
80    ///
81    /// # Arguments
82    /// * `customer_id` - The unique ID of the customer.
83    pub async fn list_customer_payment_instruments(
84        &self,
85        customer_id: &str,
86    ) -> Result<Vec<PaymentInstrument>> {
87        let url = self.build_url(&format!(
88            "/v0.1/customers/{}/payment-instruments",
89            customer_id
90        ))?;
91
92        let response = self
93            .http_client
94            .get(url)
95            .bearer_auth(&self.api_key)
96            .send()
97            .await?;
98
99        if response.status().is_success() {
100            let instruments = response.json::<Vec<PaymentInstrument>>().await?;
101            Ok(instruments)
102        } else {
103            self.handle_error(response).await
104        }
105    }
106
107    /// Deactivates an identified card payment instrument resource for a customer.
108    /// A successful deactivation returns a 204 No Content response.
109    ///
110    /// # Arguments
111    /// * `customer_id` - The unique ID of the customer.
112    /// * `token` - The token of the payment instrument to deactivate.
113    pub async fn deactivate_customer_payment_instrument(
114        &self,
115        customer_id: &str,
116        token: &str,
117    ) -> Result<()> {
118        let url = self.build_url(&format!(
119            "/v0.1/customers/{}/payment-instruments/{}",
120            customer_id, token
121        ))?;
122
123        let response = self
124            .http_client
125            .delete(url)
126            .bearer_auth(&self.api_key)
127            .send()
128            .await?;
129
130        if response.status().is_success() {
131            Ok(()) // Successful deletion often returns 204 No Content
132        } else {
133            self.handle_error(response).await
134        }
135    }
136}