square_api_client/api/invoices_api.rs
1//! Create and manage invoices.
2//!
3//! The Invoices API lets you create and manage invoices for orders that were created using the
4//! Orders API. After you create the invoice and configure its delivery method, payment schedule,
5//! and other invoice settings, you can publish the invoice. Depending on the invoice settings,
6//! Square sends the invoice to the customer or automatically charges a card on file. Square also
7//! hosts each invoice on a web page where customers can easily pay for it.
8
9use crate::{
10 config::Configuration,
11 http::client::HttpClient,
12 models::{
13 errors::ApiError, CancelInvoiceRequest, CancelInvoiceResponse, CreateInvoiceRequest,
14 CreateInvoiceResponse, DeleteInvoiceParameters, DeleteInvoiceResponse, GetInvoiceResponse,
15 ListInvoicesParameters, ListInvoicesResponse, PublishInvoiceRequest,
16 PublishInvoiceResponse, SearchInvoicesRequest, SearchInvoicesResponse,
17 UpdateInvoiceRequest, UpdateInvoiceResponse,
18 },
19};
20
21const DEFAULT_URI: &str = "/invoices";
22
23/// Create and manage invoices.
24pub struct InvoicesApi {
25 /// App config information
26 config: Configuration,
27 /// HTTP Client for requests to the Invoices API endpoints
28 client: HttpClient,
29}
30
31impl InvoicesApi {
32 pub fn new(config: Configuration, client: HttpClient) -> Self {
33 Self { config, client }
34 }
35
36 /// Returns a list of invoices for a given location.
37 ///
38 /// The response is paginated. If truncated, the response includes a `cursor` that you use in a
39 /// subsequent request to retrieve the next set of invoices.
40 pub async fn list_invoices(
41 &self,
42 params: &ListInvoicesParameters,
43 ) -> Result<ListInvoicesResponse, ApiError> {
44 let url = format!("{}{}", &self.url(), params.to_query_string());
45 let response = self.client.get(&url).await?;
46
47 response.deserialize().await
48 }
49
50 /// Creates a draft [Invoice] for an order created using the Orders API.
51 ///
52 /// A draft invoice remains in your account and no action is taken. You must publish the invoice
53 /// before Square can process it (send it to the customer's email address or charge the
54 /// customer’s card on file).
55 pub async fn create_invoice(
56 &self,
57 body: &CreateInvoiceRequest,
58 ) -> Result<CreateInvoiceResponse, ApiError> {
59 let response = self.client.post(&self.url(), body).await?;
60
61 response.deserialize().await
62 }
63
64 /// Searches for invoices from a location specified in the filter.
65 ///
66 /// You can optionally specify customers in the filter for whom to retrieve invoices. In the
67 /// current implementation, you can only specify one location and optionally one customer.
68 ///
69 /// The response is paginated. If truncated, the response includes a `cursor` that you use in a
70 /// subsequent request to retrieve the next set of invoices.
71 pub async fn search_invoices(
72 &self,
73 body: &SearchInvoicesRequest,
74 ) -> Result<SearchInvoicesResponse, ApiError> {
75 let url = format!("{}/search", &self.url());
76 let response = self.client.post(&url, body).await?;
77
78 response.deserialize().await
79 }
80
81 /// Deletes the specified invoice.
82 ///
83 /// When an invoice is deleted, the associated order status changes to CANCELED. You can only
84 /// delete a draft invoice (you cannot delete a published invoice, including one that is
85 /// scheduled for processing).
86 pub async fn delete_invoice(
87 &self,
88 invoice_id: &str,
89 params: &DeleteInvoiceParameters,
90 ) -> Result<DeleteInvoiceResponse, ApiError> {
91 let url = format!("{}/{}{}", &self.url(), invoice_id, params.to_query_string());
92 let response = self.client.delete(&url).await?;
93
94 response.deserialize().await
95 }
96
97 /// Retrieves an invoice by invoice ID.
98 pub async fn get_invoice(&self, invoice_id: &str) -> Result<GetInvoiceResponse, ApiError> {
99 let url = format!("{}/{}", &self.url(), invoice_id);
100 let response = self.client.get(&url).await?;
101
102 response.deserialize().await
103 }
104
105 /// Updates an invoice by modifying fields, clearing fields, or both.
106 ///
107 /// For most updates, you can use a sparse [Invoice] object to add fields or change values and
108 /// use the `fields_to_clear` field to specify fields to clear. However, some restrictions
109 /// apply. For example, you cannot change the `order_id` or `location_id` field and you must
110 /// provide the complete `custom_fields` list to update a custom field. Published invoices have
111 /// additional restrictions.
112 pub async fn update_invoice(
113 &self,
114 invoice_id: &str,
115 body: &UpdateInvoiceRequest,
116 ) -> Result<UpdateInvoiceResponse, ApiError> {
117 let url = format!("{}/{}", &self.url(), invoice_id);
118 let response = self.client.put(&url, body).await?;
119
120 response.deserialize().await
121 }
122
123 /// Cancels an invoice.
124 ///
125 /// The seller cannot collect payments for the canceled invoice.
126 ///
127 /// You cannot cancel an invoice in the `DRAFT` state or in a terminal state: `PAID`,
128 /// `REFUNDED`, `CANCELED`, or `FAILED`.
129 pub async fn cancel_invoice(
130 &self,
131 invoice_id: &str,
132 body: &CancelInvoiceRequest,
133 ) -> Result<CancelInvoiceResponse, ApiError> {
134 let url = format!("{}/{}/cancel", &self.url(), invoice_id);
135 let response = self.client.post(&url, body).await?;
136
137 response.deserialize().await
138 }
139
140 /// Publishes the specified draft invoice.
141 ///
142 /// After an invoice is published, Square follows up based on the invoice configuration. For
143 /// example, Square sends the invoice to the customer's email address, charges the customer's
144 /// card on file, or does nothing. Square also makes the invoice available on a Square-hosted
145 /// invoice page.
146 ///
147 /// The invoice `status` also changes from `DRAFT` to a status based on the invoice
148 /// configuration. For example, the status changes to `UNPAID` if Square emails the invoice or
149 /// `PARTIALLY_PAID` if Square charge a card on file for a portion of the invoice amount.
150 pub async fn publish_invoice(
151 &self,
152 invoice_id: &str,
153 body: &PublishInvoiceRequest,
154 ) -> Result<PublishInvoiceResponse, ApiError> {
155 let url = format!("{}/{}/publish", &self.url(), invoice_id);
156 let response = self.client.post(&url, body).await?;
157
158 response.deserialize().await
159 }
160
161 fn url(&self) -> String {
162 format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
163 }
164}