use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{client::Sendry, error::Error};
#[derive(Debug, Clone)]
pub struct Billing {
client: Sendry,
}
impl Billing {
pub(crate) fn new(client: Sendry) -> Self {
Self { client }
}
pub async fn get_plan(&self) -> Result<BillingPlan, Error> {
self.client
.request(
self.client
.build::<()>(Method::GET, "/v1/billing/plan", &[], None),
)
.await
}
pub async fn get_usage(&self) -> Result<BillingUsage, Error> {
self.client
.request(
self.client
.build::<()>(Method::GET, "/v1/billing/usage", &[], None),
)
.await
}
pub async fn create_checkout(
&self,
params: CreateCheckout,
) -> Result<CheckoutSession, Error> {
self.client
.request(self.client.build(
Method::POST,
"/v1/billing/checkout",
&[],
Some(¶ms),
))
.await
}
pub async fn create_portal(&self, params: CreatePortal) -> Result<PortalSession, Error> {
self.client
.request(self.client.build(
Method::POST,
"/v1/billing/portal",
&[],
Some(¶ms),
))
.await
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct BillingPlan {
pub plan: String,
#[serde(rename = "hasSubscription")]
pub has_subscription: bool,
#[serde(rename = "billingPeriod")]
pub billing_period: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct BillingUsage {
pub emails_sent_this_period: u64,
pub plan_limit: u64,
pub overage_count: u64,
pub overage_rate: Option<f64>,
pub period_end: Option<String>,
}
#[derive(Debug, Clone, Serialize, Default)]
pub struct CreateCheckout {
pub plan: String,
#[serde(rename = "billingPeriod", skip_serializing_if = "Option::is_none")]
pub billing_period: Option<String>,
#[serde(rename = "successUrl", skip_serializing_if = "Option::is_none")]
pub success_url: Option<String>,
#[serde(rename = "cancelUrl", skip_serializing_if = "Option::is_none")]
pub cancel_url: Option<String>,
}
#[derive(Debug, Clone, Serialize, Default)]
pub struct CreatePortal {
#[serde(rename = "returnUrl", skip_serializing_if = "Option::is_none")]
pub return_url: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CheckoutSession {
pub url: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct PortalSession {
pub url: String,
}