square_api_client/api/
refunds_api.rs

1//! Manage and issue refunds for payments made to Square sellers.
2//!
3//! The following applies to refunds:
4//!
5//! * You cannot refund more than what was originally collected.
6//! * The refund amount must be available in the account's Square balance. If the amount is not
7//!   available, Square attempts to take money out of the associated bank account. Refunds are in a
8//!   state of PENDING until the funds are secured.
9//! * If funds cannot be secured, the refund is not completed and the buyer does not receive a
10//!   credit. The refund has a status of FAILED. Future refunds to this payment are not allowed and
11//!   the buyer should be reimbursed by other means.
12//! * You can refund only payments with status COMPLETED. You cannot refund an APPROVED payment;
13//!   however, you can cancel an approved payment.
14
15use crate::{
16    config::Configuration,
17    http::client::HttpClient,
18    models::{
19        errors::ApiError, GetPaymentRefundResponse, ListPaymentRefundsParameters,
20        ListPaymentRefundsResponse, RefundPaymentRequest, RefundPaymentResponse,
21    },
22};
23
24const DEFAULT_URI: &str = "/refunds";
25
26/// Manage and issue refunds for payments made to Square sellers.
27pub struct RefundsApi {
28    /// App config information
29    config: Configuration,
30    /// HTTP Client for requests to the Refunds API endpoints
31    client: HttpClient,
32}
33
34impl RefundsApi {
35    pub fn new(config: Configuration, client: HttpClient) -> Self {
36        Self { config, client }
37    }
38
39    /// Retrieves a list of refunds for the account making the request.
40    ///
41    /// Results are eventually consistent, and new refunds or changes to refunds might take several
42    /// seconds to appear.
43    ///
44    /// The maximum results per page is 100.
45    pub async fn list_payment_refunds(
46        &self,
47        params: &ListPaymentRefundsParameters,
48    ) -> Result<ListPaymentRefundsResponse, ApiError> {
49        let url = format!("{}{}", &self.url(), params.to_query_string());
50        let response = self.client.get(&url).await?;
51
52        response.deserialize().await
53    }
54
55    /// Refunds a payment.
56    ///
57    /// You can refund the entire payment amount or a portion of it. You can use this endpoint to
58    /// refund a card payment or record a refund of a cash or external payment. For more
59    /// information, see
60    /// [Refund Payment](https://developer.squareup.com/docs/payments-api/refund-payments).
61    pub async fn refund_payment(
62        &self,
63        body: &RefundPaymentRequest,
64    ) -> Result<RefundPaymentResponse, ApiError> {
65        let response = self.client.post(&self.url(), body).await?;
66
67        response.deserialize().await
68    }
69
70    /// Retrieves a specific refund using the `refund_id`.
71    pub async fn get_payment_refund(
72        &self,
73        refund_id: &str,
74    ) -> Result<GetPaymentRefundResponse, ApiError> {
75        let url = format!("{}/{}", &self.url(), refund_id);
76        let response = self.client.get(&url).await?;
77
78        response.deserialize().await
79    }
80
81    /// Constructs the basic entity URL including domain and entity path. Any additional path
82    /// elements (e.g. path parameters) will need to be appended to this URL.
83    fn url(&self) -> String {
84        format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
85    }
86}