Skip to main content

autogen_squareup/apis/
orders_api.rs

1/*
2 * Square
3 *
4 * Use Square APIs to manage and run business including payment, customer, product, inventory, and employee management.
5 *
6 * The version of the OpenAPI document: 2.0
7 * Contact: developers@squareup.com
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`batch_retrieve_orders`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum BatchRetrieveOrdersError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`calculate_order`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum CalculateOrderError {
29    UnknownValue(serde_json::Value),
30}
31
32/// struct for typed errors of method [`clone_order`]
33#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(untagged)]
35pub enum CloneOrderError {
36    UnknownValue(serde_json::Value),
37}
38
39/// struct for typed errors of method [`create_order`]
40#[derive(Debug, Clone, Serialize, Deserialize)]
41#[serde(untagged)]
42pub enum CreateOrderError {
43    UnknownValue(serde_json::Value),
44}
45
46/// struct for typed errors of method [`pay_order`]
47#[derive(Debug, Clone, Serialize, Deserialize)]
48#[serde(untagged)]
49pub enum PayOrderError {
50    UnknownValue(serde_json::Value),
51}
52
53/// struct for typed errors of method [`retrieve_order`]
54#[derive(Debug, Clone, Serialize, Deserialize)]
55#[serde(untagged)]
56pub enum RetrieveOrderError {
57    UnknownValue(serde_json::Value),
58}
59
60/// struct for typed errors of method [`search_orders`]
61#[derive(Debug, Clone, Serialize, Deserialize)]
62#[serde(untagged)]
63pub enum SearchOrdersError {
64    UnknownValue(serde_json::Value),
65}
66
67/// struct for typed errors of method [`update_order`]
68#[derive(Debug, Clone, Serialize, Deserialize)]
69#[serde(untagged)]
70pub enum UpdateOrderError {
71    UnknownValue(serde_json::Value),
72}
73
74
75/// Retrieves a set of [orders](entity:Order) by their IDs.  If a given order ID does not exist, the ID is ignored instead of generating an error.
76pub async fn batch_retrieve_orders(configuration: &configuration::Configuration, batch_retrieve_orders_request: models::BatchRetrieveOrdersRequest) -> Result<models::BatchRetrieveOrdersResponse, Error<BatchRetrieveOrdersError>> {
77    // add a prefix to parameters to efficiently prevent name collisions
78    let p_batch_retrieve_orders_request = batch_retrieve_orders_request;
79
80    let uri_str = format!("{}/v2/orders/batch-retrieve", configuration.base_path);
81    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
82
83    if let Some(ref user_agent) = configuration.user_agent {
84        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
85    }
86    if let Some(ref token) = configuration.oauth_access_token {
87        req_builder = req_builder.bearer_auth(token.to_owned());
88    };
89    req_builder = req_builder.json(&p_batch_retrieve_orders_request);
90
91    let req = req_builder.build()?;
92    let resp = configuration.client.execute(req).await?;
93
94    let status = resp.status();
95    let content_type = resp
96        .headers()
97        .get("content-type")
98        .and_then(|v| v.to_str().ok())
99        .unwrap_or("application/octet-stream");
100    let content_type = super::ContentType::from(content_type);
101
102    if !status.is_client_error() && !status.is_server_error() {
103        let content = resp.text().await?;
104        match content_type {
105            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
106            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::BatchRetrieveOrdersResponse`"))),
107            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::BatchRetrieveOrdersResponse`")))),
108        }
109    } else {
110        let content = resp.text().await?;
111        let entity: Option<BatchRetrieveOrdersError> = serde_json::from_str(&content).ok();
112        Err(Error::ResponseError(ResponseContent { status, content, entity }))
113    }
114}
115
116/// Enables applications to preview order pricing without creating an order.
117pub async fn calculate_order(configuration: &configuration::Configuration, calculate_order_request: models::CalculateOrderRequest) -> Result<models::CalculateOrderResponse, Error<CalculateOrderError>> {
118    // add a prefix to parameters to efficiently prevent name collisions
119    let p_calculate_order_request = calculate_order_request;
120
121    let uri_str = format!("{}/v2/orders/calculate", configuration.base_path);
122    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
123
124    if let Some(ref user_agent) = configuration.user_agent {
125        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
126    }
127    if let Some(ref token) = configuration.oauth_access_token {
128        req_builder = req_builder.bearer_auth(token.to_owned());
129    };
130    req_builder = req_builder.json(&p_calculate_order_request);
131
132    let req = req_builder.build()?;
133    let resp = configuration.client.execute(req).await?;
134
135    let status = resp.status();
136    let content_type = resp
137        .headers()
138        .get("content-type")
139        .and_then(|v| v.to_str().ok())
140        .unwrap_or("application/octet-stream");
141    let content_type = super::ContentType::from(content_type);
142
143    if !status.is_client_error() && !status.is_server_error() {
144        let content = resp.text().await?;
145        match content_type {
146            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
147            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CalculateOrderResponse`"))),
148            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CalculateOrderResponse`")))),
149        }
150    } else {
151        let content = resp.text().await?;
152        let entity: Option<CalculateOrderError> = serde_json::from_str(&content).ok();
153        Err(Error::ResponseError(ResponseContent { status, content, entity }))
154    }
155}
156
157/// Creates a new order, in the `DRAFT` state, by duplicating an existing order. The newly created order has only the core fields (such as line items, taxes, and discounts) copied from the original order.
158pub async fn clone_order(configuration: &configuration::Configuration, clone_order_request: models::CloneOrderRequest) -> Result<models::CloneOrderResponse, Error<CloneOrderError>> {
159    // add a prefix to parameters to efficiently prevent name collisions
160    let p_clone_order_request = clone_order_request;
161
162    let uri_str = format!("{}/v2/orders/clone", configuration.base_path);
163    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
164
165    if let Some(ref user_agent) = configuration.user_agent {
166        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
167    }
168    if let Some(ref token) = configuration.oauth_access_token {
169        req_builder = req_builder.bearer_auth(token.to_owned());
170    };
171    req_builder = req_builder.json(&p_clone_order_request);
172
173    let req = req_builder.build()?;
174    let resp = configuration.client.execute(req).await?;
175
176    let status = resp.status();
177    let content_type = resp
178        .headers()
179        .get("content-type")
180        .and_then(|v| v.to_str().ok())
181        .unwrap_or("application/octet-stream");
182    let content_type = super::ContentType::from(content_type);
183
184    if !status.is_client_error() && !status.is_server_error() {
185        let content = resp.text().await?;
186        match content_type {
187            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
188            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CloneOrderResponse`"))),
189            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CloneOrderResponse`")))),
190        }
191    } else {
192        let content = resp.text().await?;
193        let entity: Option<CloneOrderError> = serde_json::from_str(&content).ok();
194        Err(Error::ResponseError(ResponseContent { status, content, entity }))
195    }
196}
197
198/// Creates a new [order](entity:Order) that can include information about products for purchase and settings to apply to the purchase.  To pay for a created order, see [Pay for Orders](https://developer.squareup.com/docs/orders-api/pay-for-orders).  You can modify open orders using the [UpdateOrder](api-endpoint:Orders-UpdateOrder) endpoint.
199pub async fn create_order(configuration: &configuration::Configuration, create_order_request: models::CreateOrderRequest) -> Result<models::CreateOrderResponse, Error<CreateOrderError>> {
200    // add a prefix to parameters to efficiently prevent name collisions
201    let p_create_order_request = create_order_request;
202
203    let uri_str = format!("{}/v2/orders", configuration.base_path);
204    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
205
206    if let Some(ref user_agent) = configuration.user_agent {
207        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
208    }
209    if let Some(ref token) = configuration.oauth_access_token {
210        req_builder = req_builder.bearer_auth(token.to_owned());
211    };
212    req_builder = req_builder.json(&p_create_order_request);
213
214    let req = req_builder.build()?;
215    let resp = configuration.client.execute(req).await?;
216
217    let status = resp.status();
218    let content_type = resp
219        .headers()
220        .get("content-type")
221        .and_then(|v| v.to_str().ok())
222        .unwrap_or("application/octet-stream");
223    let content_type = super::ContentType::from(content_type);
224
225    if !status.is_client_error() && !status.is_server_error() {
226        let content = resp.text().await?;
227        match content_type {
228            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
229            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateOrderResponse`"))),
230            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CreateOrderResponse`")))),
231        }
232    } else {
233        let content = resp.text().await?;
234        let entity: Option<CreateOrderError> = serde_json::from_str(&content).ok();
235        Err(Error::ResponseError(ResponseContent { status, content, entity }))
236    }
237}
238
239/// Pay for an [order](entity:Order) using one or more approved [payments](entity:Payment) or settle an order with a total of `0`.  The total of the `payment_ids` listed in the request must be equal to the order total. Orders with a total amount of `0` can be marked as paid by specifying an empty array of `payment_ids` in the request.  To be used with `PayOrder`, a payment must:  - Reference the order by specifying the `order_id` when [creating the payment](api-endpoint:Payments-CreatePayment). Any approved payments that reference the same `order_id` not specified in the `payment_ids` is canceled. - Be approved with [delayed capture](https://developer.squareup.com/docs/payments-api/take-payments/card-payments/delayed-capture). Using a delayed capture payment with `PayOrder` completes the approved payment.
240pub async fn pay_order(configuration: &configuration::Configuration, order_id: &str, pay_order_request: models::PayOrderRequest) -> Result<models::PayOrderResponse, Error<PayOrderError>> {
241    // add a prefix to parameters to efficiently prevent name collisions
242    let p_order_id = order_id;
243    let p_pay_order_request = pay_order_request;
244
245    let uri_str = format!("{}/v2/orders/{order_id}/pay", configuration.base_path, order_id=crate::apis::urlencode(p_order_id));
246    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
247
248    if let Some(ref user_agent) = configuration.user_agent {
249        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
250    }
251    if let Some(ref token) = configuration.oauth_access_token {
252        req_builder = req_builder.bearer_auth(token.to_owned());
253    };
254    req_builder = req_builder.json(&p_pay_order_request);
255
256    let req = req_builder.build()?;
257    let resp = configuration.client.execute(req).await?;
258
259    let status = resp.status();
260    let content_type = resp
261        .headers()
262        .get("content-type")
263        .and_then(|v| v.to_str().ok())
264        .unwrap_or("application/octet-stream");
265    let content_type = super::ContentType::from(content_type);
266
267    if !status.is_client_error() && !status.is_server_error() {
268        let content = resp.text().await?;
269        match content_type {
270            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
271            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::PayOrderResponse`"))),
272            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::PayOrderResponse`")))),
273        }
274    } else {
275        let content = resp.text().await?;
276        let entity: Option<PayOrderError> = serde_json::from_str(&content).ok();
277        Err(Error::ResponseError(ResponseContent { status, content, entity }))
278    }
279}
280
281/// Retrieves an [Order](entity:Order) by ID.
282pub async fn retrieve_order(configuration: &configuration::Configuration, order_id: &str) -> Result<models::RetrieveOrderResponse, Error<RetrieveOrderError>> {
283    // add a prefix to parameters to efficiently prevent name collisions
284    let p_order_id = order_id;
285
286    let uri_str = format!("{}/v2/orders/{order_id}", configuration.base_path, order_id=crate::apis::urlencode(p_order_id));
287    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
288
289    if let Some(ref user_agent) = configuration.user_agent {
290        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
291    }
292    if let Some(ref token) = configuration.oauth_access_token {
293        req_builder = req_builder.bearer_auth(token.to_owned());
294    };
295
296    let req = req_builder.build()?;
297    let resp = configuration.client.execute(req).await?;
298
299    let status = resp.status();
300    let content_type = resp
301        .headers()
302        .get("content-type")
303        .and_then(|v| v.to_str().ok())
304        .unwrap_or("application/octet-stream");
305    let content_type = super::ContentType::from(content_type);
306
307    if !status.is_client_error() && !status.is_server_error() {
308        let content = resp.text().await?;
309        match content_type {
310            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
311            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::RetrieveOrderResponse`"))),
312            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::RetrieveOrderResponse`")))),
313        }
314    } else {
315        let content = resp.text().await?;
316        let entity: Option<RetrieveOrderError> = serde_json::from_str(&content).ok();
317        Err(Error::ResponseError(ResponseContent { status, content, entity }))
318    }
319}
320
321/// Search all orders for one or more locations. Orders include all sales, returns, and exchanges regardless of how or when they entered the Square ecosystem (such as Point of Sale, Invoices, and Connect APIs).  `SearchOrders` requests need to specify which locations to search and define a [SearchOrdersQuery](entity:SearchOrdersQuery) object that controls how to sort or filter the results. Your `SearchOrdersQuery` can:    Set filter criteria.   Set the sort order.   Determine whether to return results as complete `Order` objects or as [OrderEntry](entity:OrderEntry) objects.  Note that details for orders processed with Square Point of Sale while in offline mode might not be transmitted to Square for up to 72 hours. Offline orders have a `created_at` value that reflects the time the order was created, not the time it was subsequently transmitted to Square.
322pub async fn search_orders(configuration: &configuration::Configuration, search_orders_request: models::SearchOrdersRequest) -> Result<models::SearchOrdersResponse, Error<SearchOrdersError>> {
323    // add a prefix to parameters to efficiently prevent name collisions
324    let p_search_orders_request = search_orders_request;
325
326    let uri_str = format!("{}/v2/orders/search", configuration.base_path);
327    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
328
329    if let Some(ref user_agent) = configuration.user_agent {
330        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
331    }
332    if let Some(ref token) = configuration.oauth_access_token {
333        req_builder = req_builder.bearer_auth(token.to_owned());
334    };
335    req_builder = req_builder.json(&p_search_orders_request);
336
337    let req = req_builder.build()?;
338    let resp = configuration.client.execute(req).await?;
339
340    let status = resp.status();
341    let content_type = resp
342        .headers()
343        .get("content-type")
344        .and_then(|v| v.to_str().ok())
345        .unwrap_or("application/octet-stream");
346    let content_type = super::ContentType::from(content_type);
347
348    if !status.is_client_error() && !status.is_server_error() {
349        let content = resp.text().await?;
350        match content_type {
351            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
352            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::SearchOrdersResponse`"))),
353            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::SearchOrdersResponse`")))),
354        }
355    } else {
356        let content = resp.text().await?;
357        let entity: Option<SearchOrdersError> = serde_json::from_str(&content).ok();
358        Err(Error::ResponseError(ResponseContent { status, content, entity }))
359    }
360}
361
362/// Updates an open [order](entity:Order) by adding, replacing, or deleting fields. Orders with a `COMPLETED` or `CANCELED` state cannot be updated.  An `UpdateOrder` request requires the following:  - The `order_id` in the endpoint path, identifying the order to update. - The latest `version` of the order to update. - The [sparse order](https://developer.squareup.com/docs/orders-api/manage-orders/update-orders#sparse-order-objects) containing only the fields to update and the version to which the update is being applied. - If deleting fields, the [dot notation paths](https://developer.squareup.com/docs/orders-api/manage-orders/update-orders#identifying-fields-to-delete) identifying the fields to clear.  To pay for an order, see [Pay for Orders](https://developer.squareup.com/docs/orders-api/pay-for-orders).
363pub async fn update_order(configuration: &configuration::Configuration, order_id: &str, update_order_request: models::UpdateOrderRequest) -> Result<models::UpdateOrderResponse, Error<UpdateOrderError>> {
364    // add a prefix to parameters to efficiently prevent name collisions
365    let p_order_id = order_id;
366    let p_update_order_request = update_order_request;
367
368    let uri_str = format!("{}/v2/orders/{order_id}", configuration.base_path, order_id=crate::apis::urlencode(p_order_id));
369    let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
370
371    if let Some(ref user_agent) = configuration.user_agent {
372        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
373    }
374    if let Some(ref token) = configuration.oauth_access_token {
375        req_builder = req_builder.bearer_auth(token.to_owned());
376    };
377    req_builder = req_builder.json(&p_update_order_request);
378
379    let req = req_builder.build()?;
380    let resp = configuration.client.execute(req).await?;
381
382    let status = resp.status();
383    let content_type = resp
384        .headers()
385        .get("content-type")
386        .and_then(|v| v.to_str().ok())
387        .unwrap_or("application/octet-stream");
388    let content_type = super::ContentType::from(content_type);
389
390    if !status.is_client_error() && !status.is_server_error() {
391        let content = resp.text().await?;
392        match content_type {
393            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
394            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::UpdateOrderResponse`"))),
395            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::UpdateOrderResponse`")))),
396        }
397    } else {
398        let content = resp.text().await?;
399        let entity: Option<UpdateOrderError> = serde_json::from_str(&content).ok();
400        Err(Error::ResponseError(ResponseContent { status, content, entity }))
401    }
402}
403