1use serde::{Deserialize, Serialize};
2
3use crate::models::PaginationParams;
4
5use crate::filters::{common::ListFilters, invoice::InvoiceFilters};
6
7#[derive(Debug, Clone)]
12pub struct ListInvoicesRequest {
13 pub pagination: PaginationParams,
14 pub filters: InvoiceFilters,
15}
16
17impl ListInvoicesRequest {
18 pub fn new() -> Self {
23 Self {
24 pagination: PaginationParams::default(),
25 filters: InvoiceFilters::default(),
26 }
27 }
28
29 pub fn with_pagination(mut self, pagination: PaginationParams) -> Self {
37 self.pagination = pagination;
38 self
39 }
40
41 pub fn with_filters(mut self, filters: InvoiceFilters) -> Self {
49 self.filters = filters;
50 self
51 }
52
53 pub fn to_query_params(&self) -> Vec<(&str, String)> {
58 let mut params = self.pagination.to_query_params();
59 params.extend(self.filters.to_query_params());
60 params
61 }
62}
63
64impl Default for ListInvoicesRequest {
65 fn default() -> Self {
66 Self::new()
67 }
68}
69
70#[derive(Debug, Clone)]
75pub struct GetInvoiceRequest {
76 pub invoice_id: String,
77}
78
79impl GetInvoiceRequest {
80 pub fn new(invoice_id: String) -> Self {
88 Self { invoice_id }
89 }
90}
91
92#[derive(Debug, Clone, Serialize, Deserialize)]
94#[serde(rename_all = "snake_case")]
95pub enum BillingTime {
96 Anniversary,
98 Calendar,
100}
101
102#[derive(Debug, Clone, Default, Serialize, Deserialize)]
107pub struct InvoicePreviewCustomer {
108 #[serde(skip_serializing_if = "Option::is_none")]
109 pub external_id: Option<String>,
110 #[serde(skip_serializing_if = "Option::is_none")]
111 pub name: Option<String>,
112 #[serde(skip_serializing_if = "Option::is_none")]
113 pub address_line1: Option<String>,
114 #[serde(skip_serializing_if = "Option::is_none")]
115 pub address_line2: Option<String>,
116 #[serde(skip_serializing_if = "Option::is_none")]
117 pub city: Option<String>,
118 #[serde(skip_serializing_if = "Option::is_none")]
119 pub state: Option<String>,
120 #[serde(skip_serializing_if = "Option::is_none")]
121 pub country: Option<String>,
122 #[serde(skip_serializing_if = "Option::is_none")]
123 pub currency: Option<String>,
124 #[serde(skip_serializing_if = "Option::is_none")]
125 pub timezone: Option<String>,
126 #[serde(skip_serializing_if = "Option::is_none")]
127 pub tax_identification_number: Option<String>,
128 #[serde(skip_serializing_if = "Option::is_none")]
129 pub shipping_address: Option<InvoicePreviewAddress>,
130 #[serde(skip_serializing_if = "Option::is_none")]
131 pub integration_customers: Option<Vec<InvoicePreviewIntegrationCustomer>>,
132}
133
134impl InvoicePreviewCustomer {
135 pub fn with_external_id(external_id: String) -> Self {
143 Self {
144 external_id: Some(external_id),
145 ..Default::default()
146 }
147 }
148
149 pub fn new() -> Self {
154 Self::default()
155 }
156
157 pub fn with_name(mut self, name: String) -> Self {
158 self.name = Some(name);
159 self
160 }
161
162 pub fn with_address(
163 mut self,
164 address_line1: String,
165 address_line2: Option<String>,
166 city: Option<String>,
167 state: Option<String>,
168 country: Option<String>,
169 ) -> Self {
170 self.address_line1 = Some(address_line1);
171 self.address_line2 = address_line2;
172 self.city = city;
173 self.state = state;
174 self.country = country;
175 self
176 }
177
178 pub fn with_currency(mut self, currency: String) -> Self {
179 self.currency = Some(currency);
180 self
181 }
182
183 pub fn with_timezone(mut self, timezone: String) -> Self {
184 self.timezone = Some(timezone);
185 self
186 }
187
188 pub fn with_tax_identification_number(mut self, tax_id: String) -> Self {
189 self.tax_identification_number = Some(tax_id);
190 self
191 }
192
193 pub fn with_shipping_address(mut self, address: InvoicePreviewAddress) -> Self {
194 self.shipping_address = Some(address);
195 self
196 }
197
198 pub fn with_integration_customers(
199 mut self,
200 integrations: Vec<InvoicePreviewIntegrationCustomer>,
201 ) -> Self {
202 self.integration_customers = Some(integrations);
203 self
204 }
205}
206
207#[derive(Debug, Clone, Default, Serialize, Deserialize)]
209pub struct InvoicePreviewAddress {
210 #[serde(skip_serializing_if = "Option::is_none")]
211 pub address_line1: Option<String>,
212 #[serde(skip_serializing_if = "Option::is_none")]
213 pub address_line2: Option<String>,
214 #[serde(skip_serializing_if = "Option::is_none")]
215 pub city: Option<String>,
216 #[serde(skip_serializing_if = "Option::is_none")]
217 pub state: Option<String>,
218 #[serde(skip_serializing_if = "Option::is_none")]
219 pub country: Option<String>,
220 #[serde(skip_serializing_if = "Option::is_none")]
221 pub zipcode: Option<String>,
222}
223
224#[derive(Debug, Clone, Serialize, Deserialize)]
226pub struct InvoicePreviewIntegrationCustomer {
227 pub integration_type: String,
228 pub integration_code: String,
229}
230
231#[derive(Debug, Clone, Serialize, Deserialize)]
233pub struct InvoicePreviewCoupon {
234 pub code: String,
235 #[serde(skip_serializing_if = "Option::is_none")]
236 pub name: Option<String>,
237 #[serde(skip_serializing_if = "Option::is_none")]
238 pub coupon_type: Option<InvoicePreviewCouponType>,
239 #[serde(skip_serializing_if = "Option::is_none")]
240 pub amount_cents: Option<i64>,
241 #[serde(skip_serializing_if = "Option::is_none")]
242 pub amount_currency: Option<String>,
243 #[serde(skip_serializing_if = "Option::is_none")]
244 pub percentage_rate: Option<String>,
245 #[serde(skip_serializing_if = "Option::is_none")]
246 pub frequency_duration: Option<i32>,
247}
248
249impl InvoicePreviewCoupon {
250 pub fn new(code: String) -> Self {
258 Self {
259 code,
260 name: None,
261 coupon_type: None,
262 amount_cents: None,
263 amount_currency: None,
264 percentage_rate: None,
265 frequency_duration: None,
266 }
267 }
268
269 pub fn with_name(mut self, name: String) -> Self {
270 self.name = Some(name);
271 self
272 }
273
274 pub fn with_fixed_amount(mut self, amount_cents: i64, currency: String) -> Self {
275 self.coupon_type = Some(InvoicePreviewCouponType::FixedAmount);
276 self.amount_cents = Some(amount_cents);
277 self.amount_currency = Some(currency);
278 self
279 }
280
281 pub fn with_percentage(mut self, percentage_rate: String) -> Self {
282 self.coupon_type = Some(InvoicePreviewCouponType::Percentage);
283 self.percentage_rate = Some(percentage_rate);
284 self
285 }
286
287 pub fn with_frequency_duration(mut self, duration: i32) -> Self {
288 self.frequency_duration = Some(duration);
289 self
290 }
291}
292
293#[derive(Debug, Clone, Serialize, Deserialize)]
295#[serde(rename_all = "snake_case")]
296pub enum InvoicePreviewCouponType {
297 FixedAmount,
298 Percentage,
299}
300
301#[derive(Debug, Clone, Serialize, Deserialize)]
306pub struct InvoicePreviewSubscriptions {
307 pub external_ids: Vec<String>,
308 #[serde(skip_serializing_if = "Option::is_none")]
309 pub plan_code: Option<String>,
310 #[serde(skip_serializing_if = "Option::is_none")]
311 pub terminated_at: Option<String>,
312}
313
314impl InvoicePreviewSubscriptions {
315 pub fn new(external_ids: Vec<String>) -> Self {
323 Self {
324 external_ids,
325 plan_code: None,
326 terminated_at: None,
327 }
328 }
329
330 pub fn with_plan_code(mut self, plan_code: String) -> Self {
331 self.plan_code = Some(plan_code);
332 self
333 }
334
335 pub fn with_terminated_at(mut self, terminated_at: String) -> Self {
336 self.terminated_at = Some(terminated_at);
337 self
338 }
339}
340
341#[derive(Debug, Clone, Default, Serialize, Deserialize)]
346pub struct InvoicePreviewInput {
347 pub customer: InvoicePreviewCustomer,
348 #[serde(skip_serializing_if = "Option::is_none")]
349 pub plan_code: Option<String>,
350 #[serde(skip_serializing_if = "Option::is_none")]
351 pub subscription_at: Option<String>,
352 #[serde(skip_serializing_if = "Option::is_none")]
353 pub billing_time: Option<BillingTime>,
354 #[serde(skip_serializing_if = "Option::is_none")]
355 pub coupons: Option<Vec<InvoicePreviewCoupon>>,
356 #[serde(skip_serializing_if = "Option::is_none")]
357 pub subscriptions: Option<InvoicePreviewSubscriptions>,
358 #[serde(skip_serializing_if = "Option::is_none")]
359 pub billing_entity_code: Option<String>,
360}
361
362impl InvoicePreviewInput {
363 pub fn new(customer: InvoicePreviewCustomer) -> Self {
371 Self {
372 customer,
373 ..Default::default()
374 }
375 }
376
377 pub fn for_customer(external_id: String) -> Self {
385 Self::new(InvoicePreviewCustomer::with_external_id(external_id))
386 }
387
388 pub fn with_plan_code(mut self, plan_code: String) -> Self {
389 self.plan_code = Some(plan_code);
390 self
391 }
392
393 pub fn with_subscription_at(mut self, subscription_at: String) -> Self {
394 self.subscription_at = Some(subscription_at);
395 self
396 }
397
398 pub fn with_billing_time(mut self, billing_time: BillingTime) -> Self {
399 self.billing_time = Some(billing_time);
400 self
401 }
402
403 pub fn with_coupons(mut self, coupons: Vec<InvoicePreviewCoupon>) -> Self {
404 self.coupons = Some(coupons);
405 self
406 }
407
408 pub fn with_subscriptions(mut self, subscriptions: InvoicePreviewSubscriptions) -> Self {
409 self.subscriptions = Some(subscriptions);
410 self
411 }
412
413 pub fn with_billing_entity_code(mut self, code: String) -> Self {
414 self.billing_entity_code = Some(code);
415 self
416 }
417}
418
419#[derive(Debug, Clone, Serialize, Deserialize)]
423pub struct InvoicePreviewRequest {
424 #[serde(flatten)]
425 pub input: InvoicePreviewInput,
426}
427
428impl InvoicePreviewRequest {
429 pub fn new(input: InvoicePreviewInput) -> Self {
437 Self { input }
438 }
439}