use async_trait::async_trait;
use crate::error::Result;
use super::subscription::StripeSubscriptionData;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BillingEventContext {
pub event_id: String,
pub created: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum BillingEvent {
CheckoutCompleted {
context: BillingEventContext,
subscription_id: String,
customer_id: Option<String>,
billable_id: Option<String>,
},
SubscriptionCreated {
context: BillingEventContext,
subscription: StripeSubscriptionData,
},
SubscriptionUpdated {
context: BillingEventContext,
subscription: StripeSubscriptionData,
},
SubscriptionDeleted {
context: BillingEventContext,
subscription_id: String,
customer_id: Option<String>,
billable_id: Option<String>,
},
InvoicePaid {
context: BillingEventContext,
invoice_id: Option<String>,
subscription_id: Option<String>,
customer_id: Option<String>,
},
InvoicePaymentFailed {
context: BillingEventContext,
invoice_id: Option<String>,
subscription_id: Option<String>,
customer_id: Option<String>,
},
}
impl BillingEvent {
#[must_use]
pub fn event_id(&self) -> &str {
&self.context().event_id
}
#[must_use]
pub fn created(&self) -> u64 {
self.context().created
}
#[must_use]
pub fn kind(&self) -> &'static str {
match self {
Self::CheckoutCompleted { .. } => "checkout_completed",
Self::SubscriptionCreated { .. } => "subscription_created",
Self::SubscriptionUpdated { .. } => "subscription_updated",
Self::SubscriptionDeleted { .. } => "subscription_deleted",
Self::InvoicePaid { .. } => "invoice_paid",
Self::InvoicePaymentFailed { .. } => "invoice_payment_failed",
}
}
fn context(&self) -> &BillingEventContext {
match self {
Self::CheckoutCompleted { context, .. }
| Self::SubscriptionCreated { context, .. }
| Self::SubscriptionUpdated { context, .. }
| Self::SubscriptionDeleted { context, .. }
| Self::InvoicePaid { context, .. }
| Self::InvoicePaymentFailed { context, .. } => context,
}
}
}
#[async_trait]
pub trait BillingEventSink: Send + Sync {
async fn handle(&self, event: &BillingEvent) -> Result<()>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct NoOpBillingEventSink;
#[async_trait]
impl BillingEventSink for NoOpBillingEventSink {
async fn handle(&self, _event: &BillingEvent) -> Result<()> {
Ok(())
}
}