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