tideway 0.7.17

A batteries-included Rust web framework built on Axum for building SaaS applications quickly
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Consolidated Stripe client types.
//!
//! This module provides convenience types for working with all Stripe operations.
//! Individual traits remain separate for flexibility, but this module provides
//! unified types for when you need multiple capabilities.

use super::checkout::StripeCheckoutClient;
use super::customer::StripeClient;
use super::invoice::StripeInvoiceClient;
use super::payment::StripePaymentMethodClient;
use super::portal::StripePortalClient;
use super::subscription::StripeSubscriptionClient;

/// A type that implements all Stripe client traits.
///
/// Use this trait bound when you need a client that can perform all Stripe operations.
/// Individual managers use specific traits to maintain flexibility, but this is useful
/// for creating unified implementations or for integration testing.
///
/// # Example
///
/// ```rust,ignore
/// fn create_billing_system<C: FullStripeClient>(client: C) {
///     // Can use client for any Stripe operation
/// }
/// ```
pub trait FullStripeClient:
    StripeClient
    + StripeCheckoutClient
    + StripeSubscriptionClient
    + StripePortalClient
    + StripeInvoiceClient
    + StripePaymentMethodClient
{
}

/// Blanket implementation for any type that implements all traits.
impl<T> FullStripeClient for T where
    T: StripeClient
        + StripeCheckoutClient
        + StripeSubscriptionClient
        + StripePortalClient
        + StripeInvoiceClient
        + StripePaymentMethodClient
{
}

/// Mock Stripe client for testing that implements all client traits.
#[cfg(any(test, feature = "test-billing"))]
pub mod test {
    use super::super::checkout::{CheckoutSession, CreateCheckoutSessionRequest};
    use super::super::customer::{CreateCustomerRequest, UpdateCustomerRequest};
    use super::super::invoice::{Invoice, InvoiceLineItem, InvoiceList, InvoiceStatus};
    use super::super::payment::{PaymentMethod, PaymentMethodList};
    use super::super::portal::{CreatePortalSessionRequest, PortalFlow, PortalSession};
    use super::super::subscription::{
        StripeSubscriptionData, SubscriptionMetadata, UpdateSubscriptionRequest,
    };
    use super::*;
    use crate::error::Result;
    use std::collections::HashMap;
    use std::sync::RwLock;
    use std::sync::atomic::{AtomicU64, Ordering};

    /// A comprehensive mock Stripe client that implements all client traits.
    ///
    /// Use this for integration tests where you need full billing functionality.
    /// For unit tests, prefer the individual mock clients for better isolation.
    pub struct ComprehensiveMockStripeClient {
        customer_counter: AtomicU64,
        session_counter: AtomicU64,
        portal_counter: AtomicU64,
        payment_methods: std::sync::Arc<RwLock<HashMap<String, Vec<PaymentMethod>>>>,
        default_payment_methods: std::sync::Arc<RwLock<HashMap<String, String>>>,
        /// Default currency for mock invoices and refunds (e.g., "gbp", "usd").
        pub default_currency: String,
    }

    impl Default for ComprehensiveMockStripeClient {
        fn default() -> Self {
            Self {
                customer_counter: AtomicU64::new(0),
                session_counter: AtomicU64::new(0),
                portal_counter: AtomicU64::new(0),
                payment_methods: std::sync::Arc::new(RwLock::new(HashMap::new())),
                default_payment_methods: std::sync::Arc::new(RwLock::new(HashMap::new())),
                default_currency: "gbp".to_string(),
            }
        }
    }

    impl ComprehensiveMockStripeClient {
        /// Create a new comprehensive mock client with GBP as the default currency.
        #[must_use]
        pub fn new() -> Self {
            Self::default()
        }

        /// Create a new comprehensive mock client with a specific default currency.
        #[must_use]
        pub fn with_currency(currency: impl Into<String>) -> Self {
            Self {
                default_currency: currency.into().to_lowercase(),
                ..Self::default()
            }
        }
    }

    impl StripeClient for ComprehensiveMockStripeClient {
        async fn create_customer(&self, _request: CreateCustomerRequest) -> Result<String> {
            let id = format!(
                "cus_mock_{}",
                self.customer_counter.fetch_add(1, Ordering::SeqCst)
            );
            Ok(id)
        }

        async fn update_customer(
            &self,
            _customer_id: &str,
            _request: UpdateCustomerRequest,
        ) -> Result<()> {
            Ok(())
        }

        async fn delete_customer(&self, _customer_id: &str) -> Result<()> {
            Ok(())
        }

        async fn get_default_payment_method(&self, _customer_id: &str) -> Result<Option<String>> {
            Ok(Some("pm_mock_default".to_string()))
        }
    }

    impl StripeCheckoutClient for ComprehensiveMockStripeClient {
        async fn create_checkout_session(
            &self,
            _request: CreateCheckoutSessionRequest,
        ) -> Result<CheckoutSession> {
            let id = format!(
                "cs_mock_{}",
                self.session_counter.fetch_add(1, Ordering::SeqCst)
            );
            Ok(CheckoutSession {
                id: id.clone(),
                url: format!("https://checkout.stripe.com/c/pay/{}", id),
            })
        }
    }

    impl StripeSubscriptionClient for ComprehensiveMockStripeClient {
        async fn cancel_subscription(&self, _subscription_id: &str) -> Result<()> {
            Ok(())
        }

        async fn cancel_subscription_at_period_end(&self, _subscription_id: &str) -> Result<()> {
            Ok(())
        }

        async fn resume_subscription(&self, _subscription_id: &str) -> Result<()> {
            Ok(())
        }

        async fn get_subscription(&self, subscription_id: &str) -> Result<StripeSubscriptionData> {
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs();

            Ok(StripeSubscriptionData {
                id: subscription_id.to_string(),
                customer_id: "cus_mock_0".to_string(),
                plan_id: "starter".to_string(),
                status: "active".to_string(),
                current_period_start: now,
                current_period_end: now + 30 * 24 * 60 * 60,
                extra_seats: 0,
                trial_end: None,
                cancel_at_period_end: false,
                base_item_id: Some("si_base_mock".to_string()),
                seat_item_id: None,
                metadata: SubscriptionMetadata {
                    billable_id: None,
                    billable_type: None,
                },
            })
        }

        async fn update_subscription(
            &self,
            subscription_id: &str,
            _request: UpdateSubscriptionRequest,
        ) -> Result<StripeSubscriptionData> {
            // Return updated subscription data
            self.get_subscription(subscription_id).await
        }

        async fn extend_trial(
            &self,
            subscription_id: &str,
            new_trial_end: u64,
        ) -> Result<StripeSubscriptionData> {
            let mut data = self.get_subscription(subscription_id).await?;
            data.trial_end = Some(new_trial_end);
            data.status = "trialing".to_string();
            Ok(data)
        }

        async fn pause_subscription(&self, _subscription_id: &str) -> Result<()> {
            Ok(())
        }

        async fn resume_paused_subscription(
            &self,
            subscription_id: &str,
        ) -> Result<StripeSubscriptionData> {
            let mut data = self.get_subscription(subscription_id).await?;
            data.status = "active".to_string();
            Ok(data)
        }
    }

    impl StripePortalClient for ComprehensiveMockStripeClient {
        async fn create_portal_session(
            &self,
            _request: CreatePortalSessionRequest,
        ) -> Result<PortalSession> {
            let id = format!(
                "bps_mock_{}",
                self.portal_counter.fetch_add(1, Ordering::SeqCst)
            );
            Ok(PortalSession {
                id: id.clone(),
                url: format!("https://billing.stripe.com/p/session/{}", id),
            })
        }

        async fn create_portal_session_with_flow(
            &self,
            _request: CreatePortalSessionRequest,
            _flow: PortalFlow,
        ) -> Result<PortalSession> {
            let id = format!(
                "bps_mock_{}",
                self.portal_counter.fetch_add(1, Ordering::SeqCst)
            );
            Ok(PortalSession {
                id: id.clone(),
                url: format!("https://billing.stripe.com/p/session/{}", id),
            })
        }
    }

    #[async_trait::async_trait]
    impl StripeInvoiceClient for ComprehensiveMockStripeClient {
        async fn list_invoices(
            &self,
            _customer_id: &str,
            _limit: u8,
            _starting_after: Option<&str>,
            _status: Option<InvoiceStatus>,
        ) -> Result<InvoiceList> {
            Ok(InvoiceList {
                invoices: vec![],
                has_more: false,
                next_cursor: None,
            })
        }

        async fn get_invoice(&self, invoice_id: &str) -> Result<Invoice> {
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs();

            Ok(Invoice {
                id: invoice_id.to_string(),
                customer_id: "cus_mock_0".to_string(),
                subscription_id: Some("sub_mock".to_string()),
                status: InvoiceStatus::Paid,
                amount_due: 2999,
                amount_paid: 2999,
                amount_remaining: 0,
                currency: self.default_currency.clone(),
                created: now,
                due_date: Some(now + 30 * 24 * 60 * 60),
                period_start: now,
                period_end: now + 30 * 24 * 60 * 60,
                invoice_pdf: Some(format!("https://pay.stripe.com/invoice/{}/pdf", invoice_id)),
                hosted_invoice_url: Some(format!("https://invoice.stripe.com/{}", invoice_id)),
                number: Some(format!("INV-{}", invoice_id)),
                paid: true,
                line_items: None,
            })
        }

        async fn get_upcoming_invoice(&self, _customer_id: &str) -> Result<Option<Invoice>> {
            Ok(None)
        }

        async fn list_invoice_line_items(
            &self,
            invoice_id: &str,
            _limit: u8,
        ) -> Result<Vec<InvoiceLineItem>> {
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs();

            Ok(vec![InvoiceLineItem {
                id: format!("il_{}_1", invoice_id),
                description: Some("Pro Plan".to_string()),
                amount: 2999,
                currency: self.default_currency.clone(),
                quantity: Some(1),
                price_id: Some("price_pro".to_string()),
                period_start: now,
                period_end: now + 30 * 24 * 60 * 60,
            }])
        }
    }

    impl StripePaymentMethodClient for ComprehensiveMockStripeClient {
        async fn list_payment_methods(
            &self,
            customer_id: &str,
            _limit: u8,
        ) -> Result<PaymentMethodList> {
            let methods = self.payment_methods.read().unwrap();
            let defaults = self.default_payment_methods.read().unwrap();
            let default_id = defaults.get(customer_id);

            let customer_methods = methods
                .get(customer_id)
                .cloned()
                .unwrap_or_default()
                .into_iter()
                .map(|mut m| {
                    m.is_default = default_id.map(|d| d == &m.id).unwrap_or(false);
                    m
                })
                .collect();

            Ok(PaymentMethodList {
                methods: customer_methods,
                has_more: false,
            })
        }

        async fn attach_payment_method(
            &self,
            payment_method_id: &str,
            customer_id: &str,
        ) -> Result<PaymentMethod> {
            let method = PaymentMethod {
                id: payment_method_id.to_string(),
                card_brand: Some("visa".to_string()),
                card_last4: Some("4242".to_string()),
                card_exp_month: Some(12),
                card_exp_year: Some(2099),
                is_default: false,
            };

            let mut methods = self.payment_methods.write().unwrap();
            methods
                .entry(customer_id.to_string())
                .or_default()
                .push(method.clone());

            Ok(method)
        }

        async fn detach_payment_method(&self, payment_method_id: &str) -> Result<()> {
            let mut methods = self.payment_methods.write().unwrap();
            for customer_methods in methods.values_mut() {
                customer_methods.retain(|m| m.id != payment_method_id);
            }
            Ok(())
        }

        async fn set_default_payment_method(
            &self,
            customer_id: &str,
            payment_method_id: &str,
        ) -> Result<()> {
            let mut defaults = self.default_payment_methods.write().unwrap();
            defaults.insert(customer_id.to_string(), payment_method_id.to_string());
            Ok(())
        }
    }

    // Implement Clone manually since AtomicU64 doesn't implement Clone
    impl Clone for ComprehensiveMockStripeClient {
        fn clone(&self) -> Self {
            // Create a fresh instance - counters won't be shared but that's fine for tests
            Self::new()
        }
    }

    // Re-export with shorter alias for convenience
    pub use ComprehensiveMockStripeClient as FullMockStripeClient;
}

#[cfg(test)]
mod tests {
    use super::super::checkout::{CheckoutMetadata, CheckoutMode, CreateCheckoutSessionRequest};
    use super::super::customer::CreateCustomerRequest;
    use super::super::portal::CreatePortalSessionRequest;
    use super::test::ComprehensiveMockStripeClient;
    use super::*;

    #[tokio::test]
    async fn test_mock_client_implements_all_traits() {
        let client = ComprehensiveMockStripeClient::new();

        // Test StripeClient
        let customer_id = client
            .create_customer(CreateCustomerRequest {
                email: "test@example.com".to_string(),
                name: None,
                metadata: None,
            })
            .await
            .unwrap();
        assert!(customer_id.starts_with("cus_mock_"));

        // Test StripeCheckoutClient
        let session = client
            .create_checkout_session(CreateCheckoutSessionRequest {
                customer_id: customer_id.clone(),
                line_items: vec![],
                success_url: "https://example.com/success".to_string(),
                cancel_url: "https://example.com/cancel".to_string(),
                mode: CheckoutMode::Subscription,
                allow_promotion_codes: false,
                trial_period_days: None,
                metadata: CheckoutMetadata {
                    billable_id: "org_test".to_string(),
                    billable_type: "org".to_string(),
                    plan_id: "starter".to_string(),
                },
                tax_id_collection: false,
                billing_address_collection: false,
                coupon: None,
                payment_method_collection: None,
            })
            .await
            .unwrap();
        assert!(session.id.starts_with("cs_mock_"));

        // Test StripeSubscriptionClient
        let sub = client.get_subscription("sub_123").await.unwrap();
        assert_eq!(sub.id, "sub_123");

        // Test StripePortalClient
        let portal = client
            .create_portal_session(CreatePortalSessionRequest {
                customer_id,
                return_url: "https://example.com/billing".to_string(),
                configuration_id: None,
            })
            .await
            .unwrap();
        assert!(portal.id.starts_with("bps_mock_"));
    }

    #[test]
    fn test_full_stripe_client_trait() {
        fn accepts_full_client<C: FullStripeClient>(_client: C) {}

        let client = ComprehensiveMockStripeClient::new();
        accepts_full_client(client);
    }
}