stripe_client.rs

  1#[cfg(test)]
  2mod fake_stripe_client;
  3mod real_stripe_client;
  4
  5use std::sync::Arc;
  6
  7use anyhow::Result;
  8use async_trait::async_trait;
  9
 10#[cfg(test)]
 11pub use fake_stripe_client::*;
 12pub use real_stripe_client::*;
 13use serde::{Deserialize, Serialize};
 14
 15#[derive(Debug, PartialEq, Eq, Hash, Clone, derive_more::Display, Serialize)]
 16pub struct StripeCustomerId(pub Arc<str>);
 17
 18#[derive(Debug, Clone)]
 19pub struct StripeCustomer {
 20    pub id: StripeCustomerId,
 21    pub email: Option<String>,
 22}
 23
 24#[derive(Debug)]
 25pub struct CreateCustomerParams<'a> {
 26    pub email: Option<&'a str>,
 27}
 28
 29#[derive(Debug, PartialEq, Eq, Hash, Clone, derive_more::Display)]
 30pub struct StripeSubscriptionId(pub Arc<str>);
 31
 32#[derive(Debug, Clone)]
 33pub struct StripeSubscription {
 34    pub id: StripeSubscriptionId,
 35    pub items: Vec<StripeSubscriptionItem>,
 36}
 37
 38#[derive(Debug, PartialEq, Eq, Hash, Clone, derive_more::Display)]
 39pub struct StripeSubscriptionItemId(pub Arc<str>);
 40
 41#[derive(Debug, Clone)]
 42pub struct StripeSubscriptionItem {
 43    pub id: StripeSubscriptionItemId,
 44    pub price: Option<StripePrice>,
 45}
 46
 47#[derive(Debug, Clone)]
 48pub struct UpdateSubscriptionParams {
 49    pub items: Option<Vec<UpdateSubscriptionItems>>,
 50    pub trial_settings: Option<UpdateSubscriptionTrialSettings>,
 51}
 52
 53#[derive(Debug, PartialEq, Clone)]
 54pub struct UpdateSubscriptionItems {
 55    pub price: Option<StripePriceId>,
 56}
 57
 58#[derive(Debug, Clone)]
 59pub struct UpdateSubscriptionTrialSettings {
 60    pub end_behavior: UpdateSubscriptionTrialSettingsEndBehavior,
 61}
 62
 63#[derive(Debug, Clone)]
 64pub struct UpdateSubscriptionTrialSettingsEndBehavior {
 65    pub missing_payment_method: UpdateSubscriptionTrialSettingsEndBehaviorMissingPaymentMethod,
 66}
 67
 68#[derive(Debug, PartialEq, Eq, Clone, Copy)]
 69pub enum UpdateSubscriptionTrialSettingsEndBehaviorMissingPaymentMethod {
 70    Cancel,
 71    CreateInvoice,
 72    Pause,
 73}
 74
 75#[derive(Debug, PartialEq, Eq, Hash, Clone, derive_more::Display)]
 76pub struct StripePriceId(pub Arc<str>);
 77
 78#[derive(Debug, Clone)]
 79pub struct StripePrice {
 80    pub id: StripePriceId,
 81    pub unit_amount: Option<i64>,
 82    pub lookup_key: Option<String>,
 83    pub recurring: Option<StripePriceRecurring>,
 84}
 85
 86#[derive(Debug, Clone)]
 87pub struct StripePriceRecurring {
 88    pub meter: Option<String>,
 89}
 90
 91#[derive(Debug, PartialEq, Eq, Hash, Clone, derive_more::Display, Deserialize)]
 92pub struct StripeMeterId(pub Arc<str>);
 93
 94#[derive(Debug, Clone, Deserialize)]
 95pub struct StripeMeter {
 96    pub id: StripeMeterId,
 97    pub event_name: String,
 98}
 99
100#[derive(Debug, Serialize)]
101pub struct StripeCreateMeterEventParams<'a> {
102    pub identifier: &'a str,
103    pub event_name: &'a str,
104    pub payload: StripeCreateMeterEventPayload<'a>,
105    pub timestamp: Option<i64>,
106}
107
108#[derive(Debug, Serialize)]
109pub struct StripeCreateMeterEventPayload<'a> {
110    pub value: u64,
111    pub stripe_customer_id: &'a StripeCustomerId,
112}
113
114#[async_trait]
115pub trait StripeClient: Send + Sync {
116    async fn list_customers_by_email(&self, email: &str) -> Result<Vec<StripeCustomer>>;
117
118    async fn create_customer(&self, params: CreateCustomerParams<'_>) -> Result<StripeCustomer>;
119
120    async fn get_subscription(
121        &self,
122        subscription_id: &StripeSubscriptionId,
123    ) -> Result<StripeSubscription>;
124
125    async fn update_subscription(
126        &self,
127        subscription_id: &StripeSubscriptionId,
128        params: UpdateSubscriptionParams,
129    ) -> Result<()>;
130
131    async fn list_prices(&self) -> Result<Vec<StripePrice>>;
132
133    async fn list_meters(&self) -> Result<Vec<StripeMeter>>;
134
135    async fn create_meter_event(&self, params: StripeCreateMeterEventParams<'_>) -> Result<()>;
136}