paddle_rust_sdk/
businesses.rs

1//! Builders for making requests to the Paddle API for customer businesses.
2//!
3//! See the [Paddle API](https://developer.paddle.com/api-reference/businesses/overview) documentation for more information.
4
5use std::collections::HashMap;
6
7use reqwest::Method;
8use serde::Serialize;
9use serde_with::skip_serializing_none;
10
11use crate::entities::{Business, Contact};
12use crate::enums::Status;
13use crate::ids::{BusinessID, CustomerID};
14use crate::paginated::Paginated;
15use crate::{Paddle, Result};
16
17/// Request builder for fetching businesses from Paddle API.
18#[skip_serializing_none]
19#[derive(Serialize)]
20pub struct BusinessesList<'a> {
21    #[serde(skip)]
22    client: &'a Paddle,
23    #[serde(skip)]
24    customer_id: CustomerID,
25    after: Option<BusinessID>,
26    #[serde(serialize_with = "crate::comma_separated")]
27    id: Option<Vec<BusinessID>>,
28    order_by: Option<String>,
29    per_page: Option<usize>,
30    search: Option<String>,
31    status: Option<Status>,
32}
33
34impl<'a> BusinessesList<'a> {
35    pub fn new(client: &'a Paddle, customer_id: impl Into<CustomerID>) -> Self {
36        Self {
37            client,
38            customer_id: customer_id.into(),
39            after: None,
40            id: None,
41            order_by: None,
42            per_page: None,
43            search: None,
44            status: None,
45        }
46    }
47
48    /// Return entities after the specified Paddle ID when working with paginated endpoints. Used in the `meta.pagination.next` URL in responses for list operations.
49    pub fn after(&mut self, business_id: impl Into<BusinessID>) -> &mut Self {
50        self.after = Some(business_id.into());
51        self
52    }
53
54    /// Return only the IDs specified.
55    pub fn ids(
56        &mut self,
57        business_ids: impl IntoIterator<Item = impl Into<BusinessID>>,
58    ) -> &mut Self {
59        self.id = Some(business_ids.into_iter().map(Into::into).collect());
60        self
61    }
62
63    /// Order returned entities by the specified field. Valid fields for ordering: id
64    pub fn order_by_asc(&mut self, field: &str) -> &mut Self {
65        self.order_by = Some(format!("{}[ASC]", field));
66        self
67    }
68
69    /// Order returned entities by the specified field. Valid fields for ordering: id
70    pub fn order_by_desc(&mut self, field: &str) -> &mut Self {
71        self.order_by = Some(format!("{}[DESC]", field));
72        self
73    }
74
75    /// Set how many entities are returned per page. Paddle returns the maximum number of results if a number greater than the maximum is requested.
76    /// Check `meta.pagination.per_page` in the response to see how many were returned.
77    ///
78    /// Default: `50`; Maximum: `200`.
79    pub fn per_page(&mut self, entities_per_page: usize) -> &mut Self {
80        self.per_page = Some(entities_per_page);
81        self
82    }
83
84    /// Return entities that match a search query. Searches `status`, `created_at`, and `updated_at`.
85    pub fn search(&mut self, term: impl Into<String>) -> &mut Self {
86        self.search = Some(term.into());
87        self
88    }
89
90    /// Return only prices with the specified status.
91    pub fn status(&mut self, status: Status) -> &mut Self {
92        self.status = Some(status);
93        self
94    }
95
96    /// Send the request to Paddle and return the response.
97    pub fn send(&self) -> Paginated<'_, Vec<Business>> {
98        let url = format!("/customers/{}/businesses", self.customer_id.as_ref());
99
100        Paginated::new(self.client, &url, self)
101    }
102}
103
104/// Request builder for creating customer businesses in Paddle API.
105#[skip_serializing_none]
106#[derive(Serialize)]
107pub struct BusinessCreate<'a> {
108    #[serde(skip)]
109    client: &'a Paddle,
110    #[serde(skip)]
111    customer_id: CustomerID,
112    name: String,
113    company_number: Option<String>,
114    tax_identifier: Option<String>,
115    contacts: Option<Vec<Contact>>,
116    custom_data: Option<HashMap<String, String>>,
117}
118
119impl<'a> BusinessCreate<'a> {
120    pub fn new(
121        client: &'a Paddle,
122        customer_id: impl Into<CustomerID>,
123        name: impl Into<String>,
124    ) -> Self {
125        Self {
126            client,
127            customer_id: customer_id.into(),
128            name: name.into(),
129            company_number: None,
130            tax_identifier: None,
131            contacts: None,
132            custom_data: None,
133        }
134    }
135
136    /// Company number for this business.
137    pub fn company_number(&mut self, company_number: impl Into<String>) -> &mut Self {
138        self.company_number = Some(company_number.into());
139        self
140    }
141
142    /// Tax identifier for this business.
143    pub fn tax_identifier(&mut self, tax_identifier: impl Into<String>) -> &mut Self {
144        self.tax_identifier = Some(tax_identifier.into());
145        self
146    }
147
148    /// Contact information for this business.
149    pub fn contacts(&mut self, contacts: impl IntoIterator<Item = Contact>) -> &mut Self {
150        self.contacts = Some(contacts.into_iter().collect());
151        self
152    }
153
154    /// Custom data for this business.
155    pub fn custom_data(&mut self, custom_data: HashMap<String, String>) -> &mut Self {
156        self.custom_data = Some(custom_data);
157        self
158    }
159
160    /// Send the request to Paddle and return the response.
161    pub async fn send(&self) -> Result<Business> {
162        self.client
163            .send(
164                self,
165                Method::POST,
166                &format!("/customers/{}/businesses", self.customer_id.as_ref()),
167            )
168            .await
169    }
170}
171
172/// Request builder for fetching a single business from Paddle API.
173#[skip_serializing_none]
174#[derive(Serialize)]
175pub struct BusinessGet<'a> {
176    #[serde(skip)]
177    client: &'a Paddle,
178    #[serde(skip)]
179    customer_id: CustomerID,
180    #[serde(skip)]
181    business_id: BusinessID,
182}
183
184impl<'a> BusinessGet<'a> {
185    pub fn new(
186        client: &'a Paddle,
187        customer_id: impl Into<CustomerID>,
188        business_id: impl Into<BusinessID>,
189    ) -> Self {
190        Self {
191            client,
192            customer_id: customer_id.into(),
193            business_id: business_id.into(),
194        }
195    }
196
197    /// Send the request to Paddle and return the response.
198    pub async fn send(&self) -> Result<Business> {
199        self.client
200            .send(
201                self,
202                Method::GET,
203                &format!(
204                    "/customers/{}/businesses/{}",
205                    self.customer_id.as_ref(),
206                    self.business_id.as_ref()
207                ),
208            )
209            .await
210    }
211}
212
213/// Request builder for updating a business in Paddle API.
214#[skip_serializing_none]
215#[derive(Serialize)]
216pub struct BusinessUpdate<'a> {
217    #[serde(skip)]
218    client: &'a Paddle,
219    #[serde(skip)]
220    customer_id: CustomerID,
221    #[serde(skip)]
222    business_id: BusinessID,
223    name: Option<String>,
224    company_number: Option<String>,
225    tax_identifier: Option<String>,
226    contacts: Option<Vec<Contact>>,
227    custom_data: Option<HashMap<String, String>>,
228}
229
230impl<'a> BusinessUpdate<'a> {
231    pub fn new(
232        client: &'a Paddle,
233        customer_id: impl Into<CustomerID>,
234        business_id: impl Into<BusinessID>,
235    ) -> Self {
236        Self {
237            client,
238            customer_id: customer_id.into(),
239            business_id: business_id.into(),
240            name: None,
241            company_number: None,
242            tax_identifier: None,
243            contacts: None,
244            custom_data: None,
245        }
246    }
247
248    /// Name of this business.
249    pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
250        self.name = Some(name.into());
251        self
252    }
253
254    /// Company number for this business.
255    pub fn company_number(&mut self, company_number: impl Into<String>) -> &mut Self {
256        self.company_number = Some(company_number.into());
257        self
258    }
259
260    /// Tax identifier for this business.
261    pub fn tax_identifier(&mut self, tax_identifier: impl Into<String>) -> &mut Self {
262        self.tax_identifier = Some(tax_identifier.into());
263        self
264    }
265
266    /// Contact information for this business.
267    pub fn contacts(&mut self, contacts: impl IntoIterator<Item = Contact>) -> &mut Self {
268        self.contacts = Some(contacts.into_iter().collect());
269        self
270    }
271
272    /// Custom data for this business.
273    pub fn custom_data(&mut self, custom_data: HashMap<String, String>) -> &mut Self {
274        self.custom_data = Some(custom_data);
275        self
276    }
277
278    /// Send the request to Paddle and return the response.
279    pub async fn send(&self) -> Result<Business> {
280        self.client
281            .send(
282                self,
283                Method::PATCH,
284                &format!(
285                    "/customers/{}/businesses/{}",
286                    self.customer_id.as_ref(),
287                    self.business_id.as_ref()
288                ),
289            )
290            .await
291    }
292}