Skip to main content

Subscriptions

Struct Subscriptions 

Source
pub struct Subscriptions { /* private fields */ }
Expand description

Subscription management interface.

Implementations§

Source§

impl Subscriptions

Source

pub fn create_plan( &self, input: CreateSubscriptionPlan, ) -> Result<SubscriptionPlan>

Create a new subscription plan.

§Example
use stateset_embedded::{Commerce, CreateSubscriptionPlan, BillingInterval};
use rust_decimal_macros::dec;

let commerce = Commerce::new(":memory:")?;

let plan = commerce.subscriptions().create_plan(CreateSubscriptionPlan {
    name: "Weekly Meal Kit".into(),
    billing_interval: BillingInterval::Weekly,
    price: dec!(79.99),
    trial_days: Some(7),
    ..Default::default()
})?;
Source

pub fn get_plan(&self, id: Uuid) -> Result<Option<SubscriptionPlan>>

Get a subscription plan by ID.

Source

pub fn get_plan_by_code(&self, code: &str) -> Result<Option<SubscriptionPlan>>

Get a subscription plan by its code.

Source

pub fn list_plans( &self, filter: SubscriptionPlanFilter, ) -> Result<Vec<SubscriptionPlan>>

List subscription plans with optional filtering.

§Example
use stateset_embedded::{Commerce, SubscriptionPlanFilter, PlanStatus};

let commerce = Commerce::new(":memory:")?;

// List active plans
let plans = commerce.subscriptions().list_plans(SubscriptionPlanFilter {
    status: Some(PlanStatus::Active),
    ..Default::default()
})?;
Source

pub fn update_plan( &self, id: Uuid, input: UpdateSubscriptionPlan, ) -> Result<SubscriptionPlan>

Update a subscription plan.

Source

pub fn activate_plan(&self, id: Uuid) -> Result<SubscriptionPlan>

Activate a subscription plan (make it available for new subscriptions).

Source

pub fn archive_plan(&self, id: Uuid) -> Result<SubscriptionPlan>

Archive a subscription plan (no new subscriptions, existing ones continue).

Source

pub fn subscribe(&self, input: CreateSubscription) -> Result<Subscription>

Create a new subscription for a customer.

§Example
use stateset_embedded::{Commerce, CreateSubscription};
use uuid::Uuid;

let commerce = Commerce::new(":memory:")?;

let subscription = commerce.subscriptions().subscribe(CreateSubscription {
    customer_id: Uuid::new_v4(),
    plan_id: Uuid::new_v4(),
    payment_method_id: Some("pm_1234".into()),
    ..Default::default()
})?;

println!("Created subscription #{}", subscription.subscription_number);
Source

pub fn get(&self, id: SubscriptionId) -> Result<Option<Subscription>>

Get a subscription by ID.

Source

pub fn get_by_number(&self, number: &str) -> Result<Option<Subscription>>

Get a subscription by its number (e.g., “SUB-123456”).

Source

pub fn list(&self, filter: SubscriptionFilter) -> Result<Vec<Subscription>>

List subscriptions with optional filtering.

§Example
use stateset_embedded::{Commerce, SubscriptionFilter, SubscriptionStatus};
use uuid::Uuid;

let commerce = Commerce::new(":memory:")?;

// List active subscriptions for a customer
let subs = commerce.subscriptions().list(SubscriptionFilter {
    customer_id: Some(Uuid::new_v4()),
    status: Some(SubscriptionStatus::Active),
    ..Default::default()
})?;
Source

pub fn update( &self, id: SubscriptionId, input: UpdateSubscription, ) -> Result<Subscription>

Update a subscription.

Source

pub fn pause( &self, id: SubscriptionId, input: PauseSubscription, ) -> Result<Subscription>

Pause a subscription.

Pausing stops billing but preserves the subscription. The customer can resume at any time.

§Example
use stateset_embedded::{Commerce, PauseSubscription};
use uuid::Uuid;
use chrono::{Utc, Duration};

let commerce = Commerce::new(":memory:")?;

// Pause for 30 days
commerce.subscriptions().pause(Uuid::new_v4(), PauseSubscription {
    resume_at: Some(Utc::now() + Duration::days(30)),
    reason: Some("Customer requested vacation hold".into()),
})?;
Source

pub fn resume(&self, id: SubscriptionId) -> Result<Subscription>

Resume a paused subscription.

This reactivates billing and creates a new billing period.

Source

pub fn cancel( &self, id: SubscriptionId, input: CancelSubscription, ) -> Result<Subscription>

Cancel a subscription.

By default, cancellation takes effect at the end of the current billing period. Use immediate: true to cancel immediately.

§Example
use stateset_embedded::{Commerce, CancelSubscription};
use uuid::Uuid;

let commerce = Commerce::new(":memory:")?;

// Cancel at end of period
commerce.subscriptions().cancel(Uuid::new_v4(), CancelSubscription {
    reason: Some("Customer found alternative".into()),
    ..Default::default()
})?;

// Immediate cancellation
commerce.subscriptions().cancel(Uuid::new_v4(), CancelSubscription {
    immediate: Some(true),
    reason: Some("Refund requested".into()),
    ..Default::default()
})?;
Source

pub fn skip_next_cycle( &self, id: SubscriptionId, input: SkipBillingCycle, ) -> Result<Subscription>

Skip the next billing cycle.

The subscription remains active, but the next billing date is pushed forward by one interval.

§Example
use stateset_embedded::{Commerce, SkipBillingCycle};
use uuid::Uuid;

let commerce = Commerce::new(":memory:")?;

commerce.subscriptions().skip_next_cycle(Uuid::new_v4(), SkipBillingCycle {
    reason: Some("Customer traveling".into()),
})?;
Source

pub fn create_billing_cycle( &self, subscription_id: SubscriptionId, cycle_number: i32, period_start: DateTime<Utc>, period_end: DateTime<Utc>, ) -> Result<BillingCycle>

Create a billing cycle for a subscription.

This is typically called by the billing system when processing subscription renewals.

Source

pub fn get_billing_cycle(&self, id: Uuid) -> Result<Option<BillingCycle>>

Get a billing cycle by ID.

Source

pub fn list_billing_cycles( &self, filter: BillingCycleFilter, ) -> Result<Vec<BillingCycle>>

List billing cycles with optional filtering.

Source

pub fn update_billing_cycle_status( &self, id: Uuid, status: BillingCycleStatus, ) -> Result<BillingCycle>

Update billing cycle status (mark as paid, failed, etc.).

Source

pub fn mark_cycle_paid(&self, id: Uuid) -> Result<BillingCycle>

Mark a billing cycle as paid.

Source

pub fn mark_cycle_failed(&self, id: Uuid) -> Result<BillingCycle>

Mark a billing cycle as failed.

Source

pub fn get_events( &self, subscription_id: SubscriptionId, ) -> Result<Vec<SubscriptionEvent>>

Get subscription events (audit log).

§Example
use stateset_embedded::Commerce;
use uuid::Uuid;

let commerce = Commerce::new(":memory:")?;

let events = commerce.subscriptions().get_events(Uuid::new_v4())?;
for event in events {
    println!("{}: {}", event.event_type, event.description);
}
Source

pub fn get_active_plans(&self) -> Result<Vec<SubscriptionPlan>>

Get all active plans.

Source

pub fn get_customer_subscriptions( &self, customer_id: CustomerId, ) -> Result<Vec<Subscription>>

Get all subscriptions for a customer.

Source

pub fn get_active_customer_subscriptions( &self, customer_id: CustomerId, ) -> Result<Vec<Subscription>>

Get active subscriptions for a customer.

Source

pub fn is_active(&self, id: SubscriptionId) -> Result<bool>

Check if a subscription is active.

Source

pub fn is_in_trial(&self, id: SubscriptionId) -> Result<bool>

Check if a subscription is in trial period.

Source

pub fn get_due_for_billing( &self, before: DateTime<Utc>, ) -> Result<Vec<Subscription>>

Get subscriptions due for billing.

Returns subscriptions where next_billing_date is on or before the specified date.

Source

pub fn get_trials_ending(&self, within_days: i64) -> Result<Vec<Subscription>>

Get subscriptions with trials ending soon.

Returns subscriptions in trial status where trial ends within the specified number of days.

Trait Implementations§

Source§

impl Debug for Subscriptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more