bachs-sdk 0.1.0

Unofficial Rust SDK for the Bachs API — payments and billing for internet businesses.
Documentation
use crate::client::{Bachs, RequestOptions};
use crate::common::{FlexObject, ListParams, ListResponse, Metadata, Timestamp};
use crate::error::BachsResult;
use reqwest::Method;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize)]
pub struct CreateRefundRequest {
    /// The payment/charge to refund.
    pub payment_id: String,
    /// Partial refund amount as a decimal string, e.g. `"10.00"`. Omit for a
    /// full refund.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amount: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Metadata>,
}

impl CreateRefundRequest {
    pub fn new(payment_id: impl Into<String>) -> Self {
        Self {
            payment_id: payment_id.into(),
            amount: None,
            reason: None,
            metadata: None,
        }
    }

    pub fn amount(mut self, amount: impl Into<String>) -> Self {
        self.amount = Some(amount.into());
        self
    }

    pub fn reason(mut self, reason: impl Into<String>) -> Self {
        self.reason = Some(reason.into());
        self
    }
}

#[derive(Debug, Clone, Deserialize)]
pub struct Refund {
    /// Prefixed with `ref_`.
    #[serde(default)]
    pub refund_id: Option<String>,
    #[serde(default)]
    pub payment_id: Option<String>,
    #[serde(default)]
    pub status: Option<String>,
    #[serde(default)]
    pub amount: Option<String>,
    #[serde(default)]
    pub currency: Option<String>,
    #[serde(default)]
    pub metadata: Metadata,
    #[serde(default)]
    pub created_at: Option<Timestamp>,
    #[serde(flatten)]
    pub extra: FlexObject,
}

pub struct Refunds<'a> {
    pub(crate) client: &'a Bachs,
}

impl<'a> Refunds<'a> {
    pub async fn create(&self, req: &CreateRefundRequest) -> BachsResult<Refund> {
        self.create_with_options(req, None).await
    }

    pub async fn create_with_options(
        &self,
        req: &CreateRefundRequest,
        options: Option<&RequestOptions>,
    ) -> BachsResult<Refund> {
        self.client
            .send(Method::POST, "/v1/refunds", Some(req), None, options)
            .await
    }

    pub async fn get(&self, refund_id: &str) -> BachsResult<Refund> {
        self.client
            .send::<(), _>(
                Method::GET,
                &format!("/v1/refunds/{refund_id}"),
                None,
                None,
                None,
            )
            .await
    }

    pub async fn list(&self, params: &ListParams) -> BachsResult<ListResponse<Refund>> {
        let query = params.to_query();
        self.client
            .send::<(), _>(Method::GET, "/v1/refunds", None, Some(&query), None)
            .await
    }
}