use crate::{
SquareClient,
config::Configuration,
http::client::HttpClient,
models::{
CancelPaymentByIdempotencyKeyRequest, CancelPaymentByIdempotencyKeyResponse,
CancelPaymentResponse, CompletePaymentRequest, CompletePaymentResponse,
CreatePaymentRequest, CreatePaymentResponse, GetPaymentResponse, ListPaymentsParameters,
ListPaymentsResponse, UpdatePaymentRequest, UpdatePaymentResponse, errors::SquareApiError,
},
};
const DEFAULT_URI: &str = "/payments";
pub struct PaymentsApi {
config: Configuration,
http_client: HttpClient,
}
impl PaymentsApi {
pub fn new(square_client: SquareClient) -> PaymentsApi {
PaymentsApi {
config: square_client.config,
http_client: square_client.http_client,
}
}
pub async fn list_payments(
&self,
params: &ListPaymentsParameters,
) -> Result<ListPaymentsResponse, SquareApiError> {
let url = format!("{}{}", &self.url(), params.to_query_string());
let response = self.http_client.get(&url).await?;
response.deserialize().await
}
pub async fn create_payment(
&self,
body: &CreatePaymentRequest,
) -> Result<CreatePaymentResponse, SquareApiError> {
let response = self.http_client.post(&self.url(), body).await?;
response.deserialize().await
}
pub async fn cancel_payment_by_idempotency_key(
&self,
body: &CancelPaymentByIdempotencyKeyRequest,
) -> Result<CancelPaymentByIdempotencyKeyResponse, SquareApiError> {
let url = format!("{}/cancel", &self.url());
let response = self.http_client.post(&url, body).await?;
response.deserialize().await
}
pub async fn get_payment(
&self,
payment_id: impl AsRef<str>,
) -> Result<GetPaymentResponse, SquareApiError> {
let url = format!("{}/{}", &self.url(), payment_id.as_ref());
let response = self.http_client.get(&url).await?;
response.deserialize().await
}
pub async fn update_payment(
&self,
payment_id: impl AsRef<str>,
body: &UpdatePaymentRequest,
) -> Result<UpdatePaymentResponse, SquareApiError> {
let url = format!("{}/{}", &self.url(), payment_id.as_ref());
let response = self.http_client.put(&url, &body).await?;
response.deserialize().await
}
pub async fn cancel_payment(
&self,
payment_id: impl AsRef<str>,
) -> Result<CancelPaymentResponse, SquareApiError> {
let url = format!("{}/{}/cancel", &self.url(), payment_id.as_ref());
let response = self.http_client.post::<Option<()>>(&url, &None).await?;
response.deserialize().await
}
pub async fn complete_payment(
&self,
payment_id: impl AsRef<str>,
body: &CompletePaymentRequest,
) -> Result<CompletePaymentResponse, SquareApiError> {
let url = format!("{}/{}/complete", &self.url(), payment_id.as_ref());
let response = self.http_client.post(&url, body).await?;
response.deserialize().await
}
fn url(&self) -> String {
format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
}
}