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, Timestamp};
use crate::error::BachsResult;
use reqwest::Method;
use serde::{Deserialize, Serialize};

/// Opens a hosted portal session so a customer can manage their own
/// subscriptions, invoices, and saved cards.
#[derive(Debug, Clone, Serialize)]
pub struct CreateCustomerSessionRequest {
    pub customer_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub return_url: Option<String>,
}

impl CreateCustomerSessionRequest {
    pub fn new(customer_id: impl Into<String>) -> Self {
        Self {
            customer_id: customer_id.into(),
            return_url: None,
        }
    }

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

#[derive(Debug, Clone, Deserialize)]
pub struct CustomerSession {
    /// The URL to redirect the customer to for their self-service portal.
    pub url: String,
    #[serde(default)]
    pub customer_id: Option<String>,
    #[serde(default)]
    pub expires_at: Option<Timestamp>,
    #[serde(flatten)]
    pub extra: FlexObject,
}

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

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

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