Skip to main content

autogen_squareup/apis/
refunds_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 [`get_payment_refund`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum GetPaymentRefundError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`list_payment_refunds`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum ListPaymentRefundsError {
29    UnknownValue(serde_json::Value),
30}
31
32/// struct for typed errors of method [`refund_payment`]
33#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(untagged)]
35pub enum RefundPaymentError {
36    UnknownValue(serde_json::Value),
37}
38
39
40/// Retrieves a specific refund using the `refund_id`.
41pub async fn get_payment_refund(configuration: &configuration::Configuration, refund_id: &str) -> Result<models::GetPaymentRefundResponse, Error<GetPaymentRefundError>> {
42    // add a prefix to parameters to efficiently prevent name collisions
43    let p_refund_id = refund_id;
44
45    let uri_str = format!("{}/v2/refunds/{refund_id}", configuration.base_path, refund_id=crate::apis::urlencode(p_refund_id));
46    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
47
48    if let Some(ref user_agent) = configuration.user_agent {
49        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
50    }
51    if let Some(ref token) = configuration.oauth_access_token {
52        req_builder = req_builder.bearer_auth(token.to_owned());
53    };
54
55    let req = req_builder.build()?;
56    let resp = configuration.client.execute(req).await?;
57
58    let status = resp.status();
59    let content_type = resp
60        .headers()
61        .get("content-type")
62        .and_then(|v| v.to_str().ok())
63        .unwrap_or("application/octet-stream");
64    let content_type = super::ContentType::from(content_type);
65
66    if !status.is_client_error() && !status.is_server_error() {
67        let content = resp.text().await?;
68        match content_type {
69            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
70            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GetPaymentRefundResponse`"))),
71            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::GetPaymentRefundResponse`")))),
72        }
73    } else {
74        let content = resp.text().await?;
75        let entity: Option<GetPaymentRefundError> = serde_json::from_str(&content).ok();
76        Err(Error::ResponseError(ResponseContent { status, content, entity }))
77    }
78}
79
80/// Retrieves a list of refunds for the account making the request.  Results are eventually consistent, and new refunds or changes to refunds might take several seconds to appear.  The maximum results per page is 100.
81pub async fn list_payment_refunds(configuration: &configuration::Configuration, begin_time: Option<&str>, end_time: Option<&str>, sort_order: Option<&str>, cursor: Option<&str>, location_id: Option<&str>, status: Option<&str>, source_type: Option<&str>, limit: Option<i32>, updated_at_begin_time: Option<&str>, updated_at_end_time: Option<&str>, sort_field: Option<models::ListPaymentRefundsRequestSortField>) -> Result<models::ListPaymentRefundsResponse, Error<ListPaymentRefundsError>> {
82    // add a prefix to parameters to efficiently prevent name collisions
83    let p_begin_time = begin_time;
84    let p_end_time = end_time;
85    let p_sort_order = sort_order;
86    let p_cursor = cursor;
87    let p_location_id = location_id;
88    let p_status = status;
89    let p_source_type = source_type;
90    let p_limit = limit;
91    let p_updated_at_begin_time = updated_at_begin_time;
92    let p_updated_at_end_time = updated_at_end_time;
93    let p_sort_field = sort_field;
94
95    let uri_str = format!("{}/v2/refunds", configuration.base_path);
96    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
97
98    if let Some(ref param_value) = p_begin_time {
99        req_builder = req_builder.query(&[("begin_time", &param_value.to_string())]);
100    }
101    if let Some(ref param_value) = p_end_time {
102        req_builder = req_builder.query(&[("end_time", &param_value.to_string())]);
103    }
104    if let Some(ref param_value) = p_sort_order {
105        req_builder = req_builder.query(&[("sort_order", &param_value.to_string())]);
106    }
107    if let Some(ref param_value) = p_cursor {
108        req_builder = req_builder.query(&[("cursor", &param_value.to_string())]);
109    }
110    if let Some(ref param_value) = p_location_id {
111        req_builder = req_builder.query(&[("location_id", &param_value.to_string())]);
112    }
113    if let Some(ref param_value) = p_status {
114        req_builder = req_builder.query(&[("status", &param_value.to_string())]);
115    }
116    if let Some(ref param_value) = p_source_type {
117        req_builder = req_builder.query(&[("source_type", &param_value.to_string())]);
118    }
119    if let Some(ref param_value) = p_limit {
120        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
121    }
122    if let Some(ref param_value) = p_updated_at_begin_time {
123        req_builder = req_builder.query(&[("updated_at_begin_time", &param_value.to_string())]);
124    }
125    if let Some(ref param_value) = p_updated_at_end_time {
126        req_builder = req_builder.query(&[("updated_at_end_time", &param_value.to_string())]);
127    }
128    if let Some(ref param_value) = p_sort_field {
129        req_builder = req_builder.query(&[("sort_field", &param_value.to_string())]);
130    }
131    if let Some(ref user_agent) = configuration.user_agent {
132        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
133    }
134    if let Some(ref token) = configuration.oauth_access_token {
135        req_builder = req_builder.bearer_auth(token.to_owned());
136    };
137
138    let req = req_builder.build()?;
139    let resp = configuration.client.execute(req).await?;
140
141    let status = resp.status();
142    let content_type = resp
143        .headers()
144        .get("content-type")
145        .and_then(|v| v.to_str().ok())
146        .unwrap_or("application/octet-stream");
147    let content_type = super::ContentType::from(content_type);
148
149    if !status.is_client_error() && !status.is_server_error() {
150        let content = resp.text().await?;
151        match content_type {
152            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
153            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ListPaymentRefundsResponse`"))),
154            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::ListPaymentRefundsResponse`")))),
155        }
156    } else {
157        let content = resp.text().await?;
158        let entity: Option<ListPaymentRefundsError> = serde_json::from_str(&content).ok();
159        Err(Error::ResponseError(ResponseContent { status, content, entity }))
160    }
161}
162
163/// Refunds a payment. You can refund the entire payment amount or a portion of it. You can use this endpoint to refund a card payment or record a  refund of a cash or external payment. For more information, see [Refund Payment](https://developer.squareup.com/docs/payments-api/refund-payments).
164pub async fn refund_payment(configuration: &configuration::Configuration, refund_payment_request: models::RefundPaymentRequest) -> Result<models::RefundPaymentResponse, Error<RefundPaymentError>> {
165    // add a prefix to parameters to efficiently prevent name collisions
166    let p_refund_payment_request = refund_payment_request;
167
168    let uri_str = format!("{}/v2/refunds", configuration.base_path);
169    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
170
171    if let Some(ref user_agent) = configuration.user_agent {
172        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
173    }
174    if let Some(ref token) = configuration.oauth_access_token {
175        req_builder = req_builder.bearer_auth(token.to_owned());
176    };
177    req_builder = req_builder.json(&p_refund_payment_request);
178
179    let req = req_builder.build()?;
180    let resp = configuration.client.execute(req).await?;
181
182    let status = resp.status();
183    let content_type = resp
184        .headers()
185        .get("content-type")
186        .and_then(|v| v.to_str().ok())
187        .unwrap_or("application/octet-stream");
188    let content_type = super::ContentType::from(content_type);
189
190    if !status.is_client_error() && !status.is_server_error() {
191        let content = resp.text().await?;
192        match content_type {
193            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
194            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::RefundPaymentResponse`"))),
195            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::RefundPaymentResponse`")))),
196        }
197    } else {
198        let content = resp.text().await?;
199        let entity: Option<RefundPaymentError> = serde_json::from_str(&content).ok();
200        Err(Error::ResponseError(ResponseContent { status, content, entity }))
201    }
202}
203