rust_woocommerce/controllers/
refunds.rs

1use serde::{Deserialize, Serialize};
2use serde_with::skip_serializing_none;
3
4use crate::MetaData;
5
6#[skip_serializing_none]
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct RefundCreate {
9    amount: Option<String>,
10    reason: Option<String>,
11    refunded_by: Option<i32>,
12    meta_data: Option<Vec<MetaData>>,
13    line_items: Option<Vec<OrderRefundLineItemCreate>>,
14    api_refund: Option<bool>,
15    api_restock: Option<bool>,
16}
17impl RefundCreate {
18    pub fn builder() -> RefundCreateBuilder<NoAmount, NoItems> {
19        RefundCreateBuilder {
20            amount: NoAmount,
21            line_items: NoItems,
22            ..Default::default()
23        }
24    }
25}
26#[skip_serializing_none]
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct OrderRefundLineItemCreate {
29    id: i32,
30    name: Option<String>,
31    product_id: Option<i32>,
32    variation_id: Option<i32>,
33    quantity: i32,
34    total: Option<String>,
35    total_tax: Option<String>,
36    meta_data: Option<Vec<MetaData>>,
37    refund_total: Option<f64>,
38}
39impl OrderRefundLineItemCreate {
40    pub fn builder() -> OrderRefundLineItemCreateBuilder<NoId, NoQuantity> {
41        OrderRefundLineItemCreateBuilder {
42            id: NoId,
43            quantity: NoQuantity,
44            ..Default::default()
45        }
46    }
47}
48#[derive(Default, Clone)]
49pub struct RefundCreateBuilder<A, I> {
50    amount: A,
51    reason: Option<String>,
52    refunded_by: Option<i32>,
53    meta_data: Option<Vec<MetaData>>,
54    line_items: I,
55    api_refund: Option<bool>,
56    api_restock: Option<bool>,
57}
58#[derive(Default, Clone)]
59pub struct NoAmount;
60#[derive(Default, Clone)]
61pub struct NoItems;
62#[derive(Default, Clone)]
63pub struct WithAmount(String);
64#[derive(Default, Clone)]
65pub struct WithItems(Vec<OrderRefundLineItemCreate>);
66#[derive(Default, Clone)]
67pub struct NoId;
68#[derive(Default, Clone)]
69pub struct NoQuantity;
70#[derive(Default, Clone)]
71pub struct WithId(i32);
72#[derive(Default, Clone)]
73pub struct WithQuantity(i32);
74impl<A, I> RefundCreateBuilder<A, I> {
75    /// Total refund amount. Optional. If this parameter is provided, it will take precedence over line item totals, even when total of line items does not matches with this amount.
76    pub fn amount(self, amount: impl Into<String>) -> RefundCreateBuilder<WithAmount, I> {
77        RefundCreateBuilder {
78            amount: WithAmount(amount.into()),
79            reason: self.reason,
80            refunded_by: self.refunded_by,
81            meta_data: self.meta_data,
82            line_items: self.line_items,
83            api_refund: self.api_refund,
84            api_restock: self.api_restock,
85        }
86    }
87    /// Reason for refund.
88    pub fn reason(mut self, reason: impl Into<String>) -> Self {
89        let _ = self.reason.insert(reason.into());
90        self
91    }
92    /// User ID of user who created the refund.    
93    pub fn refunded_by(mut self, refunded_by: i32) -> Self {
94        let _ = self.refunded_by.insert(refunded_by);
95        self
96    }
97    /// Meta data.    
98    pub fn meta_data(mut self, key: impl Into<String>, value: impl Serialize) -> Self {
99        self.meta_data.get_or_insert(vec![]).push(MetaData {
100            id: None,
101            key: key.into(),
102            value: serde_json::json!(value),
103        });
104        self
105    }
106    /// When true, the payment gateway API is used to generate the refund. Default is true.
107    /// This method set false
108    pub fn api_refund(mut self) -> Self {
109        let _ = self.api_refund.insert(false);
110        self
111    }
112    /// When true, the selected line items are restocked Default is true.
113    /// This method set false
114    pub fn api_restock(mut self) -> Self {
115        let _ = self.api_restock.insert(false);
116        self
117    }
118}
119impl<A> RefundCreateBuilder<A, NoItems> {
120    /// Line item data.
121    pub fn line_item(self, line_item_id: i32, quatnity: i32) -> RefundCreateBuilder<A, WithItems> {
122        let l = OrderRefundLineItemCreate::builder()
123            .id(line_item_id)
124            .quantity(quatnity)
125            .build();
126        RefundCreateBuilder {
127            amount: self.amount,
128            reason: self.reason,
129            refunded_by: self.refunded_by,
130            meta_data: self.meta_data,
131            line_items: WithItems(vec![l]),
132            api_refund: self.api_refund,
133            api_restock: self.api_restock,
134        }
135    }
136}
137impl<A> RefundCreateBuilder<A, WithItems> {
138    /// Line item data.
139    pub fn line_item(mut self, line_item_id: i32, quatnity: i32) -> Self {
140        let line_item = OrderRefundLineItemCreate::builder()
141            .id(line_item_id)
142            .quantity(quatnity)
143            .build();
144        self.line_items.0.push(line_item);
145        self
146    }
147}
148impl RefundCreateBuilder<NoAmount, WithItems> {
149    pub fn build(self) -> RefundCreate {
150        RefundCreate {
151            amount: None,
152            reason: self.reason,
153            refunded_by: self.refunded_by,
154            meta_data: self.meta_data,
155            line_items: Some(self.line_items.0),
156            api_refund: self.api_refund,
157            api_restock: self.api_restock,
158        }
159    }
160}
161impl RefundCreateBuilder<WithAmount, WithItems> {
162    pub fn build(self) -> RefundCreate {
163        RefundCreate {
164            amount: Some(self.amount.0),
165            reason: self.reason,
166            refunded_by: self.refunded_by,
167            meta_data: self.meta_data,
168            line_items: Some(self.line_items.0),
169            api_refund: self.api_refund,
170            api_restock: self.api_restock,
171        }
172    }
173}
174impl RefundCreateBuilder<WithAmount, NoItems> {
175    pub fn build(self) -> RefundCreate {
176        RefundCreate {
177            amount: Some(self.amount.0),
178            reason: self.reason,
179            refunded_by: self.refunded_by,
180            meta_data: self.meta_data,
181            line_items: None,
182            api_refund: self.api_refund,
183            api_restock: self.api_restock,
184        }
185    }
186}
187#[derive(Default, Clone)]
188pub struct OrderRefundLineItemCreateBuilder<I, Q> {
189    /// Item ID
190    pub id: I,
191    /// Product name.
192    pub name: Option<String>,
193    /// Product ID.
194    pub product_id: Option<i32>,
195    /// Variation ID, if applicable.    
196    pub variation_id: Option<i32>,
197    /// Quantity ordered.
198    pub quantity: Q,
199    /// Line total (after discounts).    
200    pub total: Option<String>,
201    /// Line total tax (after discounts).
202    pub total_tax: Option<String>,
203    /// Meta data.
204    pub meta_data: Option<Vec<MetaData>>,
205    // The amount to refund for this line item, excluding taxes.
206    pub refund_total: Option<f64>,
207}
208impl<I, Q> OrderRefundLineItemCreateBuilder<I, Q> {
209    /// Item ID
210    pub fn id(self, id: i32) -> OrderRefundLineItemCreateBuilder<WithId, Q> {
211        OrderRefundLineItemCreateBuilder {
212            id: WithId(id),
213            name: self.name,
214            product_id: self.product_id,
215            variation_id: self.variation_id,
216            quantity: self.quantity,
217            total: self.total,
218            total_tax: self.total_tax,
219            meta_data: self.meta_data,
220            refund_total: self.refund_total,
221        }
222    }
223    /// Product name.
224    pub fn name(mut self, name: impl Into<String>) -> Self {
225        let _ = self.name.insert(name.into());
226        self
227    }
228    /// Product ID.
229    pub fn product_id(mut self, product_id: i32) -> Self {
230        let _ = self.product_id.insert(product_id);
231        self
232    }
233    /// Variation ID, if applicable.    
234    pub fn variation_id(mut self, variation_id: i32) -> Self {
235        let _ = self.variation_id.insert(variation_id);
236        self
237    }
238    /// Quantity ordered.
239    pub fn quantity(self, quantity: i32) -> OrderRefundLineItemCreateBuilder<I, WithQuantity> {
240        OrderRefundLineItemCreateBuilder {
241            id: self.id,
242            name: self.name,
243            product_id: self.product_id,
244            variation_id: self.variation_id,
245            quantity: WithQuantity(quantity),
246            total: self.total,
247            total_tax: self.total_tax,
248            meta_data: self.meta_data,
249            refund_total: self.refund_total,
250        }
251    }
252    /// Line total (after discounts).    
253    pub fn total(mut self, total: impl Into<String>) -> Self {
254        let _ = self.total.insert(total.into());
255        self
256    }
257    /// Line total tax (after discounts).
258    pub fn total_tax(mut self, total_tax: impl Into<String>) -> Self {
259        let _ = self.total_tax.insert(total_tax.into());
260        self
261    }
262    /// Meta data.
263    pub fn meta_data(mut self, key: impl Into<String>, value: impl Serialize) -> Self {
264        self.meta_data.get_or_insert(vec![]).push(MetaData {
265            id: None,
266            key: key.into(),
267            value: serde_json::json!(value),
268        });
269        self
270    }
271    // The amount to refund for this line item, excluding taxes.
272    pub fn refund_total(mut self, refund_total: f64) -> Self {
273        let _ = self.refund_total.insert(refund_total);
274        self
275    }
276}
277impl OrderRefundLineItemCreateBuilder<WithId, WithQuantity> {
278    pub fn build(self) -> OrderRefundLineItemCreate {
279        OrderRefundLineItemCreate {
280            id: self.id.0,
281            name: self.name,
282            product_id: self.product_id,
283            variation_id: self.variation_id,
284            quantity: self.quantity.0,
285            total: self.total,
286            total_tax: self.total_tax,
287            meta_data: self.meta_data,
288            refund_total: self.refund_total,
289        }
290    }
291}