use super::common::*;
#[derive(
Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, typed_builder::TypedBuilder,
)]
#[builder(doc)]
pub struct FinancialPayout {
#[builder(setter(doc = "Sets [`amount`](FinancialPayout::amount)."))]
pub amount: f32,
#[builder(setter(into, doc = "Sets [`currency`](FinancialPayout::currency)."))]
pub currency: String,
#[builder(setter(doc = "Sets [`date`](FinancialPayout::date)."))]
pub date: crate::datetime::Date,
#[builder(setter(doc = "Sets [`fee`](FinancialPayout::fee)."))]
pub fee: f32,
#[builder(setter(doc = "Sets [`id`](FinancialPayout::id)."))]
pub id: i64,
#[builder(setter(into, doc = "Sets [`reference`](FinancialPayout::reference)."))]
pub reference: String,
#[builder(setter(doc = "Sets [`status`](FinancialPayout::status)."))]
pub status: FinancialPayoutStatus,
#[builder(setter(
into,
doc = "Sets [`transaction_code`](FinancialPayout::transaction_code)."
))]
pub transaction_code: String,
#[serde(rename = "type")]
#[builder(setter(doc = "Sets [`r#type`](FinancialPayout::r#type)."))]
pub r#type: FinancialPayoutType,
}
pub type FinancialPayouts = Vec<FinancialPayout>;
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum FinancialPayoutStatus {
#[serde(rename = "SUCCESSFUL")]
Successful,
#[serde(rename = "FAILED")]
Failed,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum FinancialPayoutType {
#[serde(rename = "PAYOUT")]
Payout,
#[serde(rename = "CHARGE_BACK_DEDUCTION")]
ChargeBackDeduction,
#[serde(rename = "REFUND_DEDUCTION")]
RefundDeduction,
#[serde(rename = "DD_RETURN_DEDUCTION")]
DdReturnDeduction,
#[serde(rename = "BALANCE_DEDUCTION")]
BalanceDeduction,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum ListParamsFormat {
#[serde(rename = "json")]
Json,
#[serde(rename = "csv")]
Csv,
#[serde(untagged)]
Other(String),
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum ListParamsOrder {
#[serde(rename = "asc")]
Asc,
#[serde(rename = "desc")]
Desc,
#[serde(untagged)]
Other(String),
}
#[derive(
Debug,
Clone,
Default,
PartialEq,
serde::Serialize,
serde::Deserialize,
typed_builder::TypedBuilder,
)]
#[builder(doc)]
pub struct ListParams {
#[builder(setter(doc = "Sets [`start_date`](ListParams::start_date)."))]
pub start_date: crate::datetime::Date,
#[builder(setter(doc = "Sets [`end_date`](ListParams::end_date)."))]
pub end_date: crate::datetime::Date,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(strip_option, doc = "Sets [`format`](ListParams::format).")
)]
pub format: Option<ListParamsFormat>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(strip_option, doc = "Sets [`limit`](ListParams::limit).")
)]
pub limit: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(
default,
setter(strip_option, doc = "Sets [`order`](ListParams::order).")
)]
pub order: Option<ListParamsOrder>,
}
use crate::client::Client;
#[derive(Debug, PartialEq)]
pub enum ListErrorBody {
BadRequest(crate::error::UnknownApiBody),
Unauthorized(Problem),
}
impl crate::error::ProblemDetails for ListErrorBody {
fn problem(&self) -> Option<&crate::resources::common::Problem> {
match self {
Self::BadRequest(body) => crate::error::ProblemDetails::problem(body),
Self::Unauthorized(problem) => Some(problem),
}
}
}
#[derive(Debug)]
pub struct PayoutsClient<'a> {
client: &'a Client,
}
impl<'a> PayoutsClient<'a> {
pub(crate) fn new(client: &'a Client) -> Self {
Self { client }
}
pub fn client(&self) -> &Client {
self.client
}
pub async fn list(
&self,
merchant_code: impl AsRef<str>,
params: ListParams,
) -> crate::error::SdkResult<FinancialPayouts, ListErrorBody> {
let path = format!("/v1.0/merchants/{}/payouts", merchant_code.as_ref());
let url = format!("{}{}", self.client.base_url(), path);
let mut request = self
.client
.http_client()
.get(&url)
.header("User-Agent", crate::version::user_agent())
.timeout(self.client.timeout());
if let Some(authorization) = self.client.authorization() {
request = request.header("Authorization", format!("Bearer {}", authorization));
}
for (header_name, header_value) in self.client.runtime_headers() {
request = request.header(*header_name, header_value);
}
request = request.query(&[("start_date", ¶ms.start_date)]);
request = request.query(&[("end_date", ¶ms.end_date)]);
if let Some(ref value) = params.format {
request = request.query(&[("format", value)]);
}
if let Some(ref value) = params.limit {
request = request.query(&[("limit", value)]);
}
if let Some(ref value) = params.order {
request = request.query(&[("order", value)]);
}
let response = request.send().await?;
let status = response.status();
match status {
reqwest::StatusCode::OK => {
let data: FinancialPayouts = response.json().await?;
Ok(data)
}
reqwest::StatusCode::BAD_REQUEST => {
Err(crate::error::SdkError::from_response(response, |raw| {
Ok(ListErrorBody::BadRequest(
crate::error::UnknownApiBody::from_bytes(raw),
))
})
.await)
}
reqwest::StatusCode::UNAUTHORIZED => {
Err(crate::error::SdkError::from_response(response, |raw| {
serde_json::from_slice::<Problem>(raw).map(ListErrorBody::Unauthorized)
})
.await)
}
_ => Err(crate::error::SdkError::unexpected_response(response).await),
}
}
}