vylth-flow 0.3.2

Official Rust SDK for Vylth Flow — self-custody crypto payment processing
Documentation
use std::collections::HashMap;
use std::sync::Arc;

use crate::error::FlowResult;
use crate::http::HttpClient;

pub struct SubscriptionService {
    pub(crate) http: Arc<HttpClient>,
}

impl SubscriptionService {
    pub async fn create_plan(&self, params: &serde_json::Value) -> FlowResult<serde_json::Value> {
        let data = self.http.post("/merchants/me/subscription-plans", Some(params)).await?;
        Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
    }

    pub async fn list_plans(&self, page: Option<u32>, limit: Option<u32>) -> FlowResult<serde_json::Value> {
        let mut params = HashMap::new();
        if let Some(p) = page {
            params.insert("page".to_string(), p.to_string());
        }
        if let Some(l) = limit {
            params.insert("limit".to_string(), l.to_string());
        }
        let p = if params.is_empty() { None } else { Some(&params) };
        let data = self.http.get("/merchants/me/subscription-plans", p).await?;
        Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
    }

    pub async fn update_plan(&self, plan_id: &str, fields: &serde_json::Value) -> FlowResult<serde_json::Value> {
        let data = self.http.patch(&format!("/merchants/me/subscription-plans/{}", plan_id), Some(fields)).await?;
        Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
    }

    pub async fn delete_plan(&self, plan_id: &str) -> FlowResult<()> {
        self.http.delete(&format!("/merchants/me/subscription-plans/{}", plan_id)).await?;
        Ok(())
    }

    pub async fn list(&self, status: Option<&str>, page: Option<u32>, limit: Option<u32>) -> FlowResult<serde_json::Value> {
        let mut params = HashMap::new();
        if let Some(s) = status {
            params.insert("status".to_string(), s.to_string());
        }
        if let Some(p) = page {
            params.insert("page".to_string(), p.to_string());
        }
        if let Some(l) = limit {
            params.insert("limit".to_string(), l.to_string());
        }
        let p = if params.is_empty() { None } else { Some(&params) };
        let data = self.http.get("/merchants/me/subscriptions", p).await?;
        Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
    }

    pub async fn cancel(&self, subscription_id: &str) -> FlowResult<serde_json::Value> {
        let data = self.http.post::<()>(&format!("/merchants/me/subscriptions/{}/cancel", subscription_id), None).await?;
        Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
    }

    pub async fn stats(&self) -> FlowResult<serde_json::Value> {
        let data = self.http.get("/merchants/me/subscriptions/stats", None).await?;
        Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
    }

    pub async fn list_payments(&self, page: Option<u32>, limit: Option<u32>) -> FlowResult<serde_json::Value> {
        let mut params = HashMap::new();
        if let Some(p) = page {
            params.insert("page".to_string(), p.to_string());
        }
        if let Some(l) = limit {
            params.insert("limit".to_string(), l.to_string());
        }
        let p = if params.is_empty() { None } else { Some(&params) };
        let data = self.http.get("/merchants/me/subscription-payments", p).await?;
        Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
    }
}