rust_woocommerce/controllers/
orders.rs

1use crate::{Billing, CurrencyISO, MetaData, OrderStatus, Shipping, TaxStatus};
2use anyhow::{anyhow, Result};
3use serde::{Deserialize, Serialize};
4use serde_with::skip_serializing_none;
5#[skip_serializing_none]
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct CreateOrder {
8    parent_id: Option<i32>,
9    status: OrderStatus,
10    currency: CurrencyISO,
11    customer_id: i32,
12    customer_note: Option<String>,
13    billing: Billing,
14    shipping: Shipping,
15    payment_method: String,
16    payment_method_title: String,
17    transaction_id: Option<String>,
18    meta_data: Vec<MetaData>,
19    line_items: Vec<OrderLineItemCreate>,
20    shipping_lines: Vec<ShippingLineCreate>,
21    fee_lines: Vec<OrderFeeLineCreate>,
22    coupon_lines: Vec<OrderCouponLineCreate>,
23    set_paid: bool,
24}
25#[skip_serializing_none]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct UpdateOrder {
28    id: Option<i32>,
29    parent_id: Option<i32>,
30    status: Option<OrderStatus>,
31    currency: Option<CurrencyISO>,
32    customer_id: Option<i32>,
33    customer_note: Option<String>,
34    billing: Option<Billing>,
35    shipping: Option<Shipping>,
36    payment_method: Option<String>,
37    payment_method_title: Option<String>,
38    transaction_id: Option<String>,
39    meta_data: Option<Vec<MetaData>>,
40    line_items: Option<Vec<OrderLineItemCreate>>,
41    shipping_lines: Option<Vec<ShippingLineCreate>>,
42    fee_lines: Option<Vec<OrderFeeLineCreate>>,
43    coupon_lines: Option<Vec<OrderCouponLineCreate>>,
44    set_paid: Option<bool>,
45}
46#[skip_serializing_none]
47#[derive(Debug, Clone, Serialize, Deserialize, Default)]
48pub struct OrderLineItemCreate {
49    product_id: i32,
50    variation_id: Option<i32>,
51    quantity: i32,
52    tax_class: Option<String>,
53    subtotal: Option<String>,
54    total: Option<String>,
55    meta_data: Option<Vec<MetaData>>,
56    price: Option<f64>,
57}
58impl OrderLineItemCreate {
59    /// new product with id and quantity
60    pub fn new() -> Self {
61        OrderLineItemCreate::default()
62    }
63    /// Product ID.
64    pub fn product_id(mut self, product_id: i32) -> Self {
65        self.product_id = product_id;
66        self
67    }
68    /// Variation ID, if applicable.
69    pub fn variation_id(mut self, variation_id: i32) -> Self {
70        let _ = self.variation_id.insert(variation_id);
71        self
72    }
73    /// Quantity ordered.
74    pub fn quantity(mut self, quantity: i32) -> Self {
75        self.quantity = quantity;
76        self
77    }
78    /// Slug of the tax class of product.
79    pub fn tax_class(mut self, tax_class: impl Into<String>) -> Self {
80        let _ = self.tax_class.insert(tax_class.into());
81        self
82    }
83    /// Line subtotal (before discounts).
84    pub fn subtotal(mut self, subtotal: impl Into<String>) -> Self {
85        let _ = self.subtotal.insert(subtotal.into());
86        self
87    }
88    /// Line total (after discounts).
89    pub fn total(mut self, total: impl Into<String>) -> Self {
90        let _ = self.total.insert(total.into());
91        self
92    }
93    /// Meta data.
94    pub fn meta_data(mut self, key: impl Into<String>, value: impl serde::Serialize) -> Self {
95        self.meta_data.get_or_insert(vec![]).push(MetaData {
96            id: None,
97            key: key.into(),
98            value: serde_json::json!(value),
99        });
100        self
101    }
102    /// Product price.
103    pub fn price(mut self, price: f64) -> Self {
104        let _ = self.price.insert(price);
105        self
106    }
107}
108#[skip_serializing_none]
109#[derive(Debug, Clone, Serialize, Deserialize, Default)]
110pub struct ShippingLineCreate {
111    /// Shipping method name.
112    method_title: String,
113    /// Shipping method ID.
114    method_id: String,
115    /// Line total (after discounts).
116    total: String,
117    /// Meta data.
118    meta_data: Option<Vec<MetaData>>,
119}
120impl ShippingLineCreate {
121    /// Shipping lines data.
122    pub fn new(
123        method_title: impl Into<String>,
124        method_id: impl Into<String>,
125        total: impl Into<String>,
126    ) -> Self {
127        Self {
128            method_title: method_title.into(),
129            method_id: method_id.into(),
130            total: total.into(),
131            ..Default::default()
132        }
133    }
134    /// Meta data.
135    pub fn meta_data(mut self, key: impl Into<String>, value: impl serde::Serialize) -> Self {
136        self.meta_data.get_or_insert(vec![]).push(MetaData {
137            id: None,
138            key: key.into(),
139            value: serde_json::json!(value),
140        });
141        self
142    }
143}
144#[skip_serializing_none]
145#[derive(Debug, Clone, Serialize, Deserialize)]
146pub struct OrderFeeLineCreate {
147    name: String,
148    tax_class: String,
149    tax_status: TaxStatus,
150    total: String,
151    meta_data: Option<Vec<MetaData>>,
152}
153impl OrderFeeLineCreate {
154    /// Fee name.
155    pub fn name(mut self, name: impl Into<String>) -> Self {
156        self.name = name.into();
157        self
158    }
159    /// Tax class of fee.
160    pub fn tax_class(mut self, tax_class: impl Into<String>) -> Self {
161        self.tax_class = tax_class.into();
162        self
163    }
164    /// Tax status of fee. Options: taxable and none.
165    pub fn tax_status(mut self, tax_status: TaxStatus) -> Self {
166        self.tax_status = tax_status;
167        self
168    }
169    /// Line total (after discounts).
170    pub fn total(mut self, total: impl Into<String>) -> Self {
171        self.total = total.into();
172        self
173    }
174    /// Meta data.
175    pub fn meta_data(mut self, key: impl Into<String>, value: impl Serialize) -> Self {
176        let meta_data = MetaData {
177            id: None,
178            key: key.into(),
179            value: serde_json::json!(value),
180        };
181        self.meta_data.get_or_insert(vec![]).push(meta_data);
182        self
183    }
184}
185
186#[skip_serializing_none]
187#[derive(Debug, Clone, Serialize, Deserialize)]
188pub struct OrderCouponLineCreate {
189    /// Coupon code.
190    pub code: String,
191}
192#[derive(Clone, Default)]
193pub struct CreateOrderBuilder {
194    parent_id: Option<i32>,
195    status: Option<OrderStatus>,
196    currency: Option<CurrencyISO>,
197    customer_id: Option<i32>,
198    customer_note: Option<String>,
199    billing: Option<Billing>,
200    shipping: Option<Shipping>,
201    payment_method: Option<String>,
202    payment_method_title: Option<String>,
203    transaction_id: Option<String>,
204    meta_data: Option<Vec<MetaData>>,
205    line_items: Option<Vec<OrderLineItemCreate>>,
206    shipping_lines: Option<Vec<ShippingLineCreate>>,
207    fee_lines: Option<Vec<OrderFeeLineCreate>>,
208    coupon_lines: Option<Vec<OrderCouponLineCreate>>,
209    set_paid: Option<bool>,
210}
211impl CreateOrderBuilder {
212    pub fn new() -> Self {
213        CreateOrderBuilder::default()
214    }
215    /// Parent order ID.
216    pub fn parent_id(mut self, parent_id: i32) -> Self {
217        let _ = self.parent_id.insert(parent_id);
218        self
219    }
220    /// Order status.
221    pub fn status(mut self, order_status: OrderStatus) -> Self {
222        let _ = self.status.insert(order_status);
223        self
224    }
225    /// Currency the order was created with, in ISO format.
226    pub fn currency(mut self, currency: CurrencyISO) -> Self {
227        let _ = self.currency.insert(currency);
228        self
229    }
230    /// User ID who owns the order. 0 for guests. Default is 0.
231    pub fn customer_id(mut self, customer_id: i32) -> Self {
232        let _ = self.customer_id.insert(customer_id);
233        self
234    }
235    /// Note left by customer during checkout.
236    pub fn customer_note(mut self, customer_note: impl Into<String>) -> Self {
237        let _ = self.customer_note.insert(customer_note.into());
238        self
239    }
240    /// billing first name.
241    pub fn billing_first_name(mut self, first_name: impl Into<String>) -> Self {
242        self.billing.get_or_insert(Billing::default()).first_name = first_name.into();
243        self
244    }
245    /// billing last name.
246    pub fn billing_last_name(mut self, last_name: impl Into<String>) -> Self {
247        self.billing.get_or_insert(Billing::default()).last_name = last_name.into();
248        self
249    }
250    /// billing company name.
251    pub fn billing_company(mut self, company: impl Into<String>) -> Self {
252        self.billing.get_or_insert(Billing::default()).company = company.into();
253        self
254    }
255    /// billing address line 1
256    pub fn billing_address_1(mut self, address_1: impl Into<String>) -> Self {
257        self.billing.get_or_insert(Billing::default()).address_1 = address_1.into();
258        self
259    }
260    /// billing address line 2
261    pub fn billing_address_2(mut self, address_2: impl Into<String>) -> Self {
262        self.billing.get_or_insert(Billing::default()).address_2 = address_2.into();
263        self
264    }
265    /// billing city name.
266    pub fn billing_city(mut self, city: impl Into<String>) -> Self {
267        self.billing.get_or_insert(Billing::default()).city = city.into();
268        self
269    }
270    /// billing ISO code or name of the state, province or district.
271    pub fn billing_state(mut self, state: impl Into<String>) -> Self {
272        self.billing.get_or_insert(Billing::default()).state = state.into();
273        self
274    }
275    /// billing postal code.
276    pub fn billing_postcode(mut self, postcode: impl Into<String>) -> Self {
277        self.billing.get_or_insert(Billing::default()).postcode = postcode.into();
278        self
279    }
280    /// billing ISO code of the country.
281    pub fn billing_country(mut self, country: impl Into<String>) -> Self {
282        self.billing.get_or_insert(Billing::default()).country = country.into();
283        self
284    }
285    /// billing email address.
286    pub fn billing_email(mut self, email: impl Into<String>) -> Self {
287        self.billing.get_or_insert(Billing::default()).email = email.into();
288        self
289    }
290    /// billing phone number.
291    pub fn billing_phone(mut self, phone: impl Into<String>) -> Self {
292        self.billing.get_or_insert(Billing::default()).phone = phone.into();
293        self
294    }
295    /// shipping first name.
296    pub fn shipping_first_name(mut self, first_name: impl Into<String>) -> Self {
297        self.shipping.get_or_insert(Shipping::default()).first_name = first_name.into();
298        self
299    }
300    /// shipping last name.
301    pub fn shipping_last_name(mut self, last_name: impl Into<String>) -> Self {
302        self.shipping.get_or_insert(Shipping::default()).last_name = last_name.into();
303        self
304    }
305    /// shipping company name.
306    pub fn shipping_company(mut self, company: impl Into<String>) -> Self {
307        self.shipping.get_or_insert(Shipping::default()).company = company.into();
308        self
309    }
310    /// shipping address line 1
311    pub fn shipping_address_1(mut self, address_1: impl Into<String>) -> Self {
312        self.shipping.get_or_insert(Shipping::default()).address_1 = address_1.into();
313        self
314    }
315    /// shipping address line 2
316    pub fn shipping_address_2(mut self, address_2: impl Into<String>) -> Self {
317        self.shipping.get_or_insert(Shipping::default()).address_2 = address_2.into();
318        self
319    }
320    /// shipping city name.
321    pub fn shipping_city(mut self, city: impl Into<String>) -> Self {
322        self.shipping.get_or_insert(Shipping::default()).city = city.into();
323        self
324    }
325    /// shipping ISO code or name of the state, province or district.
326    pub fn shipping_state(mut self, state: impl Into<String>) -> Self {
327        self.shipping.get_or_insert(Shipping::default()).state = state.into();
328        self
329    }
330    /// shipping postal code.
331    pub fn shipping_postcode(mut self, postcode: impl Into<String>) -> Self {
332        self.shipping.get_or_insert(Shipping::default()).postcode = postcode.into();
333        self
334    }
335    /// shipping ISO code of the country.
336    pub fn shipping_country(mut self, country: impl Into<String>) -> Self {
337        self.shipping.get_or_insert(Shipping::default()).country = country.into();
338        self
339    }
340    /// Payment method ID.
341    pub fn payment_method(mut self, payment_method: impl Into<String>) -> Self {
342        let _ = self.payment_method.insert(payment_method.into());
343        self
344    }
345    /// Payment method title.
346    pub fn payment_method_title(mut self, payment_method_title: impl Into<String>) -> Self {
347        let _ = self
348            .payment_method_title
349            .insert(payment_method_title.into());
350        self
351    }
352    /// Unique transaction ID.
353    pub fn transaction_id(mut self, transaction_id: impl Into<String>) -> Self {
354        let _ = self.transaction_id.insert(transaction_id.into());
355        self
356    }
357    /// Meta data.
358    pub fn meta_data(mut self, key: impl Into<String>, value: impl serde::Serialize) -> Self {
359        self.meta_data.get_or_insert(vec![]).push(MetaData {
360            id: None,
361            key: key.into(),
362            value: serde_json::json!(value),
363        });
364        self
365    }
366    /// Line items data.
367    pub fn line_item(mut self, line_item: OrderLineItemCreate) -> Self {
368        self.line_items.get_or_insert(vec![]).push(line_item);
369        self
370    }
371    /// Shipping lines data.
372    pub fn shipping_line(mut self, shipping_line: ShippingLineCreate) -> Self {
373        self.shipping_lines
374            .get_or_insert(vec![])
375            .push(shipping_line);
376        self
377    }
378    /// Fee lines data.
379    pub fn fee_line(mut self, fee_line: OrderFeeLineCreate) -> Self {
380        self.fee_lines.get_or_insert(vec![]).push(fee_line);
381        self
382    }
383    /// Coupons line data.
384    pub fn coupon_line(mut self, code: impl Into<String>) -> Self {
385        let coupon = OrderCouponLineCreate { code: code.into() };
386        self.coupon_lines.get_or_insert(vec![]).push(coupon);
387        self
388    }
389    /// Define if the order is paid. It will set the status to processing and reduce stock items. Default is false.
390    pub fn set_paid(mut self, paid: bool) -> Self {
391        let _ = self.set_paid.insert(paid);
392        self
393    }
394    pub fn build(self) -> Result<CreateOrder> {
395        let Some(billing) = self.billing.clone() else {
396            return Err(anyhow!("billing email required!"));
397        };
398        if billing.email.is_empty() {
399            return Err(anyhow!("billing email required!"));
400        }
401        Ok(CreateOrder {
402            parent_id: self.parent_id,
403            status: self.status.clone().unwrap_or_default(),
404            currency: self.currency.clone().unwrap_or_default(),
405            customer_id: self.customer_id.unwrap_or(0),
406            customer_note: self.customer_note.clone(),
407            billing,
408            shipping: self.shipping.clone().unwrap_or_default(),
409            payment_method: self.payment_method.clone().unwrap_or_default(),
410            payment_method_title: self.payment_method_title.clone().unwrap_or_default(),
411            transaction_id: self.transaction_id.clone(),
412            meta_data: self.meta_data.clone().unwrap_or_default(),
413            line_items: self.line_items.clone().unwrap_or_default(),
414            shipping_lines: self.shipping_lines.clone().unwrap_or_default(),
415            fee_lines: self.fee_lines.clone().unwrap_or_default(),
416            coupon_lines: self.coupon_lines.clone().unwrap_or_default(),
417            set_paid: self.set_paid.unwrap_or_default(),
418        })
419    }
420}
421#[derive(Clone, Default)]
422pub struct UpdateOrderBuilder {
423    id: Option<i32>,
424    parent_id: Option<i32>,
425    status: Option<OrderStatus>,
426    currency: Option<CurrencyISO>,
427    customer_id: Option<i32>,
428    customer_note: Option<String>,
429    billing: Option<Billing>,
430    shipping: Option<Shipping>,
431    payment_method: Option<String>,
432    payment_method_title: Option<String>,
433    transaction_id: Option<String>,
434    meta_data: Option<Vec<MetaData>>,
435    line_items: Option<Vec<OrderLineItemCreate>>,
436    shipping_lines: Option<Vec<ShippingLineCreate>>,
437    fee_lines: Option<Vec<OrderFeeLineCreate>>,
438    coupon_lines: Option<Vec<OrderCouponLineCreate>>,
439    set_paid: Option<bool>,
440}
441impl UpdateOrderBuilder {
442    pub fn new() -> Self {
443        UpdateOrderBuilder::default()
444    }
445    /// Unique identifier for the resource.
446    pub fn id(mut self, id: i32) -> Self {
447        let _ = self.id.insert(id);
448        self
449    }
450    /// Parent order ID.
451    pub fn parent_id(mut self, parent_id: i32) -> Self {
452        let _ = self.parent_id.insert(parent_id);
453        self
454    }
455    /// Order status.
456    pub fn status(mut self, order_status: OrderStatus) -> Self {
457        let _ = self.status.insert(order_status);
458        self
459    }
460    /// Currency the order was created with, in ISO format.
461    pub fn currency(mut self, currency: CurrencyISO) -> Self {
462        let _ = self.currency.insert(currency);
463        self
464    }
465    /// User ID who owns the order. 0 for guests. Default is 0.
466    pub fn customer_id(mut self, customer_id: i32) -> Self {
467        let _ = self.customer_id.insert(customer_id);
468        self
469    }
470    /// Note left by customer during checkout.
471    pub fn customer_note(mut self, customer_note: impl Into<String>) -> Self {
472        let _ = self.customer_note.insert(customer_note.into());
473        self
474    }
475    /// billing first name.
476    pub fn billing_first_name(mut self, first_name: impl Into<String>) -> Self {
477        self.billing.get_or_insert(Billing::default()).first_name = first_name.into();
478        self
479    }
480    /// billing last name.
481    pub fn billing_last_name(mut self, last_name: impl Into<String>) -> Self {
482        self.billing.get_or_insert(Billing::default()).last_name = last_name.into();
483        self
484    }
485    /// billing company name.
486    pub fn billing_company(mut self, company: impl Into<String>) -> Self {
487        self.billing.get_or_insert(Billing::default()).company = company.into();
488        self
489    }
490    /// billing address line 1
491    pub fn billing_address_1(mut self, address_1: impl Into<String>) -> Self {
492        self.billing.get_or_insert(Billing::default()).address_1 = address_1.into();
493        self
494    }
495    /// billing address line 2
496    pub fn billing_address_2(mut self, address_2: impl Into<String>) -> Self {
497        self.billing.get_or_insert(Billing::default()).address_2 = address_2.into();
498        self
499    }
500    /// billing city name.
501    pub fn billing_city(mut self, city: impl Into<String>) -> Self {
502        self.billing.get_or_insert(Billing::default()).city = city.into();
503        self
504    }
505    /// billing ISO code or name of the state, province or district.
506    pub fn billing_state(mut self, state: impl Into<String>) -> Self {
507        self.billing.get_or_insert(Billing::default()).state = state.into();
508        self
509    }
510    /// billing postal code.
511    pub fn billing_postcode(mut self, postcode: impl Into<String>) -> Self {
512        self.billing.get_or_insert(Billing::default()).postcode = postcode.into();
513        self
514    }
515    /// billing ISO code of the country.
516    pub fn billing_country(mut self, country: impl Into<String>) -> Self {
517        self.billing.get_or_insert(Billing::default()).country = country.into();
518        self
519    }
520    /// billing email address.
521    pub fn billing_email(mut self, email: impl Into<String>) -> Self {
522        self.billing.get_or_insert(Billing::default()).email = email.into();
523        self
524    }
525    /// billing phone number.
526    pub fn billing_phone(mut self, phone: impl Into<String>) -> Self {
527        self.billing.get_or_insert(Billing::default()).phone = phone.into();
528        self
529    }
530    /// shipping first name.
531    pub fn shipping_first_name(mut self, first_name: impl Into<String>) -> Self {
532        self.shipping.get_or_insert(Shipping::default()).first_name = first_name.into();
533        self
534    }
535    /// shipping last name.
536    pub fn shipping_last_name(mut self, last_name: impl Into<String>) -> Self {
537        self.shipping.get_or_insert(Shipping::default()).last_name = last_name.into();
538        self
539    }
540    /// shipping company name.
541    pub fn shipping_company(mut self, company: impl Into<String>) -> Self {
542        self.shipping.get_or_insert(Shipping::default()).company = company.into();
543        self
544    }
545    /// shipping address line 1
546    pub fn shipping_address_1(mut self, address_1: impl Into<String>) -> Self {
547        self.shipping.get_or_insert(Shipping::default()).address_1 = address_1.into();
548        self
549    }
550    /// shipping address line 2
551    pub fn shipping_address_2(mut self, address_2: impl Into<String>) -> Self {
552        self.shipping.get_or_insert(Shipping::default()).address_2 = address_2.into();
553        self
554    }
555    /// shipping city name.
556    pub fn shipping_city(mut self, city: impl Into<String>) -> Self {
557        self.shipping.get_or_insert(Shipping::default()).city = city.into();
558        self
559    }
560    /// shipping ISO code or name of the state, province or district.
561    pub fn shipping_state(mut self, state: impl Into<String>) -> Self {
562        self.shipping.get_or_insert(Shipping::default()).state = state.into();
563        self
564    }
565    /// shipping postal code.
566    pub fn shipping_postcode(mut self, postcode: impl Into<String>) -> Self {
567        self.shipping.get_or_insert(Shipping::default()).postcode = postcode.into();
568        self
569    }
570    /// shipping ISO code of the country.
571    pub fn shipping_country(mut self, country: impl Into<String>) -> Self {
572        self.shipping.get_or_insert(Shipping::default()).country = country.into();
573        self
574    }
575    /// Payment method ID.
576    pub fn payment_method(mut self, payment_method: impl Into<String>) -> Self {
577        let _ = self.payment_method.insert(payment_method.into());
578        self
579    }
580    /// Payment method title.
581    pub fn payment_method_title(mut self, payment_method_title: impl Into<String>) -> Self {
582        let _ = self
583            .payment_method_title
584            .insert(payment_method_title.into());
585        self
586    }
587    /// Unique transaction ID.
588    pub fn transaction_id(mut self, transaction_id: impl Into<String>) -> Self {
589        let _ = self.transaction_id.insert(transaction_id.into());
590        self
591    }
592    /// Meta data.
593    pub fn meta_data(mut self, key: impl Into<String>, value: impl serde::Serialize) -> Self {
594        self.meta_data.get_or_insert(vec![]).push(MetaData {
595            id: None,
596            key: key.into(),
597            value: serde_json::json!(value),
598        });
599        self
600    }
601    /// Line items data.
602    pub fn line_item(mut self, line_item: OrderLineItemCreate) -> Self {
603        self.line_items.get_or_insert(vec![]).push(line_item);
604        self
605    }
606    /// Shipping lines data.
607    pub fn shipping_line(mut self, shipping_line: ShippingLineCreate) -> Self {
608        self.shipping_lines
609            .get_or_insert(vec![])
610            .push(shipping_line);
611        self
612    }
613    /// Fee lines data.
614    pub fn fee_line(mut self, fee_line: OrderFeeLineCreate) -> Self {
615        self.fee_lines.get_or_insert(vec![]).push(fee_line);
616        self
617    }
618    /// Coupons line data.
619    pub fn coupon_line(mut self, code: impl Into<String>) -> Self {
620        let coupon = OrderCouponLineCreate { code: code.into() };
621        self.coupon_lines.get_or_insert(vec![]).push(coupon);
622        self
623    }
624    /// Define if the order is paid. It will set the status to processing and reduce stock items. Default is false.
625    pub fn set_paid(mut self, paid: bool) -> Self {
626        let _ = self.set_paid.insert(paid);
627        self
628    }
629    pub fn build(self) -> UpdateOrder {
630        UpdateOrder {
631            id: self.id,
632            parent_id: self.parent_id,
633            status: self.status,
634            currency: self.currency,
635            customer_id: self.customer_id,
636            customer_note: self.customer_note,
637            billing: self.billing,
638            shipping: self.shipping,
639            payment_method: self.payment_method,
640            payment_method_title: self.payment_method_title,
641            transaction_id: self.transaction_id,
642            meta_data: self.meta_data,
643            line_items: self.line_items,
644            shipping_lines: self.shipping_lines,
645            fee_lines: self.fee_lines,
646            coupon_lines: self.coupon_lines,
647            set_paid: self.set_paid,
648        }
649    }
650}