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;
14
15#[derive(Debug, PartialEq, Eq, Hash, Clone, derive_more::Display)]
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#[async_trait]
101pub trait StripeClient: Send + Sync {
102 async fn list_customers_by_email(&self, email: &str) -> Result<Vec<StripeCustomer>>;
103
104 async fn create_customer(&self, params: CreateCustomerParams<'_>) -> Result<StripeCustomer>;
105
106 async fn get_subscription(
107 &self,
108 subscription_id: &StripeSubscriptionId,
109 ) -> Result<StripeSubscription>;
110
111 async fn update_subscription(
112 &self,
113 subscription_id: &StripeSubscriptionId,
114 params: UpdateSubscriptionParams,
115 ) -> Result<()>;
116
117 async fn list_prices(&self) -> Result<Vec<StripePrice>>;
118
119 async fn list_meters(&self) -> Result<Vec<StripeMeter>>;
120}