lago_types/requests/
credit_note.rs

1use serde::{Deserialize, Serialize};
2
3use crate::filters::common::ListFilters;
4use crate::filters::credit_note::CreditNoteFilter;
5use crate::models::{CreditNoteReason, CreditNoteRefundStatus, PaginationParams};
6
7/// Request parameters for listing credit notes.
8#[derive(Debug, Clone)]
9pub struct ListCreditNotesRequest {
10    pub pagination: PaginationParams,
11    pub filters: CreditNoteFilter,
12}
13
14impl ListCreditNotesRequest {
15    /// Creates a new empty list credit notes request.
16    pub fn new() -> Self {
17        Self {
18            pagination: PaginationParams::default(),
19            filters: CreditNoteFilter::default(),
20        }
21    }
22
23    /// Sets the pagination parameters for the request.
24    pub fn with_pagination(mut self, pagination: PaginationParams) -> Self {
25        self.pagination = pagination;
26        self
27    }
28
29    /// Sets the credit note filters for the request.
30    pub fn with_filters(mut self, filters: CreditNoteFilter) -> Self {
31        self.filters = filters;
32        self
33    }
34
35    /// Converts the request parameters into HTTP query parameters.
36    pub fn to_query_params(&self) -> Vec<(&str, String)> {
37        let mut params = self.pagination.to_query_params();
38        params.extend(self.filters.to_query_params());
39        params
40    }
41}
42
43impl Default for ListCreditNotesRequest {
44    fn default() -> Self {
45        Self::new()
46    }
47}
48
49/// Request parameters for retrieving a specific credit note.
50#[derive(Debug, Clone)]
51pub struct GetCreditNoteRequest {
52    /// The Lago ID of the credit note to retrieve
53    pub lago_id: String,
54}
55
56impl GetCreditNoteRequest {
57    /// Creates a new get credit note request.
58    pub fn new(lago_id: String) -> Self {
59        Self { lago_id }
60    }
61}
62
63/// Item input for creating a credit note.
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct CreateCreditNoteItemInput {
66    /// The Lago ID of the fee to credit
67    pub fee_id: String,
68    /// The amount to credit for this fee in cents
69    pub amount_cents: i64,
70}
71
72impl CreateCreditNoteItemInput {
73    /// Creates a new credit note item input.
74    pub fn new(fee_id: String, amount_cents: i64) -> Self {
75        Self {
76            fee_id,
77            amount_cents,
78        }
79    }
80}
81
82/// Input parameters for creating a credit note.
83#[derive(Debug, Clone, Serialize, Deserialize)]
84pub struct CreateCreditNoteInput {
85    /// The Lago ID of the invoice to credit
86    pub invoice_id: String,
87    /// The reason for the credit note
88    pub reason: CreditNoteReason,
89    /// Optional description
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub description: Option<String>,
92    /// The amount to be credited in cents
93    pub credit_amount_cents: i64,
94    /// The amount to be refunded in cents
95    pub refund_amount_cents: i64,
96    /// The line items for the credit note
97    pub items: Vec<CreateCreditNoteItemInput>,
98}
99
100impl CreateCreditNoteInput {
101    /// Creates a new credit note input.
102    ///
103    /// # Arguments
104    /// * `invoice_id` - The Lago ID of the invoice to credit
105    /// * `reason` - The reason for the credit note
106    /// * `credit_amount_cents` - The amount to be credited in cents
107    /// * `refund_amount_cents` - The amount to be refunded in cents
108    /// * `items` - The line items for the credit note
109    pub fn new(
110        invoice_id: String,
111        reason: CreditNoteReason,
112        credit_amount_cents: i64,
113        refund_amount_cents: i64,
114        items: Vec<CreateCreditNoteItemInput>,
115    ) -> Self {
116        Self {
117            invoice_id,
118            reason,
119            description: None,
120            credit_amount_cents,
121            refund_amount_cents,
122            items,
123        }
124    }
125
126    /// Sets the description for the credit note.
127    pub fn with_description(mut self, description: String) -> Self {
128        self.description = Some(description);
129        self
130    }
131}
132
133/// Request wrapper for creating a credit note.
134#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct CreateCreditNoteRequest {
136    pub credit_note: CreateCreditNoteInput,
137}
138
139impl CreateCreditNoteRequest {
140    /// Creates a new create credit note request.
141    pub fn new(credit_note: CreateCreditNoteInput) -> Self {
142        Self { credit_note }
143    }
144}
145
146/// Input parameters for updating a credit note.
147#[derive(Debug, Clone, Serialize, Deserialize)]
148pub struct UpdateCreditNoteInput {
149    /// The refund status to update
150    #[serde(skip_serializing_if = "Option::is_none")]
151    pub refund_status: Option<CreditNoteRefundStatus>,
152}
153
154impl UpdateCreditNoteInput {
155    /// Creates a new empty update credit note input.
156    pub fn new() -> Self {
157        Self {
158            refund_status: None,
159        }
160    }
161
162    /// Sets the refund status.
163    pub fn with_refund_status(mut self, status: CreditNoteRefundStatus) -> Self {
164        self.refund_status = Some(status);
165        self
166    }
167}
168
169impl Default for UpdateCreditNoteInput {
170    fn default() -> Self {
171        Self::new()
172    }
173}
174
175/// Request wrapper for updating a credit note.
176#[derive(Debug, Clone, Serialize, Deserialize)]
177pub struct UpdateCreditNoteRequest {
178    /// The Lago ID of the credit note to update
179    #[serde(skip)]
180    pub lago_id: String,
181    /// The update input
182    pub credit_note: UpdateCreditNoteInput,
183}
184
185impl UpdateCreditNoteRequest {
186    /// Creates a new update credit note request.
187    pub fn new(lago_id: String, input: UpdateCreditNoteInput) -> Self {
188        Self {
189            lago_id,
190            credit_note: input,
191        }
192    }
193}