Skip to main content

lago_types/requests/
payment.rs

1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3
4use crate::models::PaginationParams;
5
6/// Request parameters for listing payments.
7///
8/// This struct combines pagination parameters and optional filters
9/// to build a comprehensive request for retrieving payment lists.
10#[derive(Debug, Clone, Default)]
11pub struct ListPaymentsRequest {
12    pub pagination: PaginationParams,
13    pub external_customer_id: Option<String>,
14    pub invoice_id: Option<Uuid>,
15}
16
17impl ListPaymentsRequest {
18    /// Creates a new empty list payments request.
19    ///
20    /// # Returns
21    /// A new `ListPaymentsRequest` instance with default pagination and no filters.
22    pub fn new() -> Self {
23        Self::default()
24    }
25
26    /// Sets the pagination parameters for the request.
27    ///
28    /// # Arguments
29    /// * `pagination` - The pagination parameters to use
30    ///
31    /// # Returns
32    /// The modified request instance for method chaining.
33    pub fn with_pagination(mut self, pagination: PaginationParams) -> Self {
34        self.pagination = pagination;
35        self
36    }
37
38    /// Sets the external customer ID filter.
39    ///
40    /// # Arguments
41    /// * `external_customer_id` - The external customer ID to filter by
42    ///
43    /// # Returns
44    /// The modified request instance for method chaining.
45    pub fn with_external_customer_id(mut self, external_customer_id: String) -> Self {
46        self.external_customer_id = Some(external_customer_id);
47        self
48    }
49
50    /// Sets the invoice ID filter.
51    ///
52    /// # Arguments
53    /// * `invoice_id` - The invoice ID to filter by
54    ///
55    /// # Returns
56    /// The modified request instance for method chaining.
57    pub fn with_invoice_id(mut self, invoice_id: Uuid) -> Self {
58        self.invoice_id = Some(invoice_id);
59        self
60    }
61
62    /// Converts the request parameters into HTTP query parameters.
63    ///
64    /// # Returns
65    /// A vector of query parameter tuples containing both pagination and filter criteria.
66    pub fn to_query_params(&self) -> Vec<(&str, String)> {
67        let mut params = self.pagination.to_query_params();
68
69        if let Some(external_customer_id) = &self.external_customer_id {
70            params.push(("external_customer_id", external_customer_id.clone()));
71        }
72
73        if let Some(invoice_id) = &self.invoice_id {
74            params.push(("invoice_id", invoice_id.to_string()));
75        }
76
77        params
78    }
79}
80
81/// Request parameters for retrieving a specific payment.
82///
83/// This struct contains the identifier needed to fetch a single payment
84/// from the API.
85#[derive(Debug, Clone)]
86pub struct GetPaymentRequest {
87    /// The Lago ID of the payment to retrieve.
88    pub lago_id: Uuid,
89}
90
91impl GetPaymentRequest {
92    /// Creates a new get payment request.
93    ///
94    /// # Arguments
95    /// * `lago_id` - The unique Lago identifier of the payment to retrieve
96    ///
97    /// # Returns
98    /// A new `GetPaymentRequest` instance with the specified payment ID.
99    pub fn new(lago_id: Uuid) -> Self {
100        Self { lago_id }
101    }
102}
103
104/// Request parameters for listing a customer's payments.
105///
106/// This struct combines the customer identifier with pagination parameters
107/// to retrieve payments for a specific customer.
108#[derive(Debug, Clone)]
109pub struct ListCustomerPaymentsRequest {
110    /// The external customer ID.
111    pub external_customer_id: String,
112    /// Pagination parameters.
113    pub pagination: PaginationParams,
114    /// Optional invoice ID filter.
115    pub invoice_id: Option<Uuid>,
116}
117
118impl ListCustomerPaymentsRequest {
119    /// Creates a new list customer payments request.
120    ///
121    /// # Arguments
122    /// * `external_customer_id` - The external customer ID
123    ///
124    /// # Returns
125    /// A new `ListCustomerPaymentsRequest` instance.
126    pub fn new(external_customer_id: String) -> Self {
127        Self {
128            external_customer_id,
129            pagination: PaginationParams::default(),
130            invoice_id: None,
131        }
132    }
133
134    /// Sets the pagination parameters for the request.
135    ///
136    /// # Arguments
137    /// * `pagination` - The pagination parameters to use
138    ///
139    /// # Returns
140    /// The modified request instance for method chaining.
141    pub fn with_pagination(mut self, pagination: PaginationParams) -> Self {
142        self.pagination = pagination;
143        self
144    }
145
146    /// Sets the invoice ID filter.
147    ///
148    /// # Arguments
149    /// * `invoice_id` - The invoice ID to filter by
150    ///
151    /// # Returns
152    /// The modified request instance for method chaining.
153    pub fn with_invoice_id(mut self, invoice_id: Uuid) -> Self {
154        self.invoice_id = Some(invoice_id);
155        self
156    }
157
158    /// Converts the request parameters into HTTP query parameters.
159    ///
160    /// # Returns
161    /// A vector of query parameter tuples.
162    pub fn to_query_params(&self) -> Vec<(&str, String)> {
163        let mut params = self.pagination.to_query_params();
164
165        if let Some(invoice_id) = &self.invoice_id {
166            params.push(("invoice_id", invoice_id.to_string()));
167        }
168
169        params
170    }
171}
172
173/// Input for creating a payment.
174#[derive(Debug, Clone, Default, Serialize, Deserialize)]
175pub struct CreatePaymentInput {
176    /// Unique identifier assigned to the invoice.
177    pub invoice_id: String,
178    /// The payment amount in cents.
179    pub amount_cents: i64,
180    /// Reference for the payment.
181    pub reference: String,
182    /// The date the payment was made.
183    #[serde(skip_serializing_if = "Option::is_none")]
184    pub paid_at: Option<String>,
185}
186
187impl CreatePaymentInput {
188    /// Creates a new payment input.
189    ///
190    /// # Arguments
191    /// * `invoice_id` - The invoice ID to associate with the payment
192    /// * `amount_cents` - The payment amount in cents
193    /// * `reference` - A reference for the payment
194    ///
195    /// # Returns
196    /// A new `CreatePaymentInput` instance.
197    pub fn new(invoice_id: String, amount_cents: i64, reference: String) -> Self {
198        Self {
199            invoice_id,
200            amount_cents,
201            reference,
202            paid_at: None,
203        }
204    }
205
206    /// Sets the date the payment was made.
207    ///
208    /// # Arguments
209    /// * `paid_at` - The payment date (YYYY-MM-DD format)
210    ///
211    /// # Returns
212    /// The modified input instance for method chaining.
213    pub fn with_paid_at(mut self, paid_at: String) -> Self {
214        self.paid_at = Some(paid_at);
215        self
216    }
217}
218
219/// Request for creating a payment.
220#[derive(Debug, Clone, Serialize, Deserialize)]
221pub struct CreatePaymentRequest {
222    /// The payment input data.
223    pub payment: CreatePaymentInput,
224}
225
226impl CreatePaymentRequest {
227    /// Creates a new create payment request.
228    ///
229    /// # Arguments
230    /// * `input` - The payment input data
231    ///
232    /// # Returns
233    /// A new `CreatePaymentRequest` instance.
234    pub fn new(input: CreatePaymentInput) -> Self {
235        Self { payment: input }
236    }
237}