use std::sync::Arc;
use serde::Serialize;
use crate::error::FlowResult;
use crate::http::HttpClient;
pub struct SubscriptionService {
pub(crate) http: Arc<HttpClient>,
}
#[derive(Serialize)]
pub struct CreatePlanParams {
pub name: String,
pub amount: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub interval_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub interval_count: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub trial_days: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_subscribers: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub webhook_url: Option<String>,
}
impl SubscriptionService {
pub async fn create_plan(&self, params: CreatePlanParams) -> FlowResult<serde_json::Value> {
let data = self.http.post("/merchants/me/subscription-plans", Some(¶ms)).await?;
Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
}
pub async fn list_plans(&self) -> FlowResult<serde_json::Value> {
let data = self.http.get("/merchants/me/subscription-plans", None).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_subscriptions(&self) -> FlowResult<serde_json::Value> {
let data = self.http.get("/merchants/me/subscriptions", None).await?;
Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
}
pub async fn cancel_subscription(&self, sub_id: &str) -> FlowResult<serde_json::Value> {
let data = self.http.post::<()>(&format!("/merchants/me/subscriptions/{}/cancel", sub_id), None).await?;
Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
}
pub async fn get_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) -> FlowResult<serde_json::Value> {
let data = self.http.get("/merchants/me/subscription-payments", None).await?;
Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
}
}