lockbook-server 26.11.26

Lockbook's server, exposed as a library for testing.
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
use std::sync::Arc;

use async_trait::async_trait;
use serde_json::Value;
use tracing::*;

use super::stripe_error::SimplifiedStripeError;

const STRIPE_API_BASE: &str = "https://api.stripe.com/v1";

/// Minimal subscription fields Lockbook actually uses. Intentionally excludes
/// buyer PII (email, name, billing address, card fingerprint) and closed
/// Stripe enums so API additions never break deserialization or leak into logs.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StripeSubscription {
    pub id: String,
    pub status: String,
    /// Unix seconds.
    pub current_period_end: u64,
    /// Unix seconds.
    pub created: u64,
    pub cancel_at_period_end: bool,
    pub cancel_at: Option<u64>,
    pub price_id: String,
    pub payment_method_id: String,
    pub card_last4: String,
}

impl StripeSubscription {
    /// `past_due` keeps Premium while Stripe retries the payment.
    pub fn is_live(&self) -> bool {
        matches!(self.status.as_str(), "active" | "trialing" | "past_due")
    }
}

#[async_trait]
pub trait StripeClient: Send + Sync + Clone + 'static {
    /// Creates an anonymous Stripe customer and returns its `cus_...` ID.
    async fn create_customer(&self) -> Result<String, SimplifiedStripeError>;

    /// A hosted page where `customer_id` subscribes to `price_id`. Returns the session URL.
    async fn create_checkout_session(
        &self, customer_id: &str, price_id: &str, success_url: &str, cancel_url: &str,
    ) -> Result<String, SimplifiedStripeError>;

    /// A hosted page where `customer_id` manages their subscription. Returns the portal URL.
    async fn create_billing_portal_session(
        &self, customer_id: &str, return_url: &str,
    ) -> Result<String, SimplifiedStripeError>;

    async fn cancel_subscription_at_period_end(
        &self, subscription_id: &str,
    ) -> Result<StripeSubscription, SimplifiedStripeError>;

    /// Ends a subscription immediately (used when a Lockbook account is deleted).
    async fn cancel_subscription_immediately(
        &self, subscription_id: &str,
    ) -> Result<(), SimplifiedStripeError>;

    async fn get_subscription(
        &self, subscription_id: &str,
    ) -> Result<StripeSubscription, SimplifiedStripeError>;

    /// Every subscription of `customer_id`, with its default payment method expanded.
    async fn list_subscriptions(
        &self, customer_id: &str,
    ) -> Result<Vec<StripeSubscription>, SimplifiedStripeError>;

    /// Expires `customer_id`'s checkout pages that are still open, so only the newest can be paid.
    async fn expire_open_checkout_sessions(
        &self, customer_id: &str,
    ) -> Result<(), SimplifiedStripeError>;
}

#[derive(Clone)]
pub struct StripeHttpClient {
    http: reqwest::Client,
    secret_key: Arc<String>,
}

impl StripeHttpClient {
    pub fn new(secret_key: impl Into<String>) -> Self {
        Self { http: reqwest::Client::new(), secret_key: Arc::new(secret_key.into()) }
    }

    async fn parse_response(resp: reqwest::Response) -> Result<Value, SimplifiedStripeError> {
        let status = resp.status();
        let body = resp.text().await?;
        let json: Value = serde_json::from_str(&body).map_err(|e| {
            SimplifiedStripeError(format!("Stripe HTTP {status} invalid JSON: {e}"))
        })?;
        if !status.is_success() {
            let err_type = json
                .pointer("/error/type")
                .and_then(Value::as_str)
                .unwrap_or("unknown");
            let err_code = json
                .pointer("/error/code")
                .and_then(Value::as_str)
                .unwrap_or("");
            let err_msg = json
                .pointer("/error/message")
                .and_then(Value::as_str)
                .unwrap_or("Stripe request failed");
            return Err(SimplifiedStripeError(format!(
                "Stripe HTTP {status} ({err_type}/{err_code}): {err_msg}"
            )));
        }
        Ok(json)
    }

    async fn get_json(
        &self, path: &str, query: &[(&str, &str)],
    ) -> Result<Value, SimplifiedStripeError> {
        let resp = self
            .http
            .get(format!("{STRIPE_API_BASE}{path}"))
            .bearer_auth(&*self.secret_key)
            .query(query)
            .send()
            .await?;
        Self::parse_response(resp).await
    }

    async fn post_form(
        &self, path: &str, form: &[(&str, &str)],
    ) -> Result<Value, SimplifiedStripeError> {
        let resp = self
            .http
            .post(format!("{STRIPE_API_BASE}{path}"))
            .bearer_auth(&*self.secret_key)
            .form(form)
            .send()
            .await?;
        Self::parse_response(resp).await
    }
}

fn parse_subscription(v: &Value) -> Result<StripeSubscription, SimplifiedStripeError> {
    let id = v
        .get("id")
        .and_then(Value::as_str)
        .ok_or_else(|| SimplifiedStripeError("Stripe subscription missing id".into()))?
        .to_string();
    let status = v
        .get("status")
        .and_then(Value::as_str)
        .unwrap_or("")
        .to_string();

    // Pre-2025-03-31.basil puts current_period_end on the subscription; basil+ puts it on items.data[0].
    let current_period_end = v
        .get("current_period_end")
        .and_then(Value::as_i64)
        .or_else(|| {
            v.pointer("/items/data/0/current_period_end")
                .and_then(Value::as_i64)
        })
        .and_then(|ts| u64::try_from(ts).ok())
        .unwrap_or(0);

    let created = v
        .get("created")
        .and_then(Value::as_i64)
        .and_then(|ts| u64::try_from(ts).ok())
        .unwrap_or(0);

    let cancel_at_period_end = v
        .get("cancel_at_period_end")
        .and_then(Value::as_bool)
        .unwrap_or(false);

    let cancel_at = v
        .get("cancel_at")
        .and_then(Value::as_i64)
        .and_then(|ts| u64::try_from(ts).ok());

    let price_id = v
        .pointer("/items/data/0/price/id")
        .and_then(Value::as_str)
        .unwrap_or("")
        .to_string();

    let (payment_method_id, card_last4) = match v.get("default_payment_method") {
        Some(Value::String(id)) => (id.clone(), String::new()),
        Some(Value::Object(pm)) => {
            let id = pm
                .get("id")
                .and_then(Value::as_str)
                .unwrap_or("")
                .to_string();
            let last4 = pm
                .get("card")
                .and_then(|c| c.get("last4"))
                .and_then(Value::as_str)
                .unwrap_or("")
                .to_string();
            (id, last4)
        }
        _ => (String::new(), String::new()),
    };

    Ok(StripeSubscription {
        id,
        status,
        current_period_end,
        created,
        cancel_at_period_end,
        cancel_at,
        price_id,
        payment_method_id,
        card_last4,
    })
}

#[async_trait]
impl StripeClient for StripeHttpClient {
    async fn create_customer(&self) -> Result<String, SimplifiedStripeError> {
        let json = self.post_form("/customers", &[]).await?;
        let id = json
            .get("id")
            .and_then(Value::as_str)
            .ok_or_else(|| SimplifiedStripeError("Stripe customer missing id".into()))?
            .to_string();
        info!(customer_id = id.as_str(), "Created stripe customer");
        Ok(id)
    }

    async fn create_checkout_session(
        &self, customer_id: &str, price_id: &str, success_url: &str, cancel_url: &str,
    ) -> Result<String, SimplifiedStripeError> {
        info!(customer_id, price_id, "Creating stripe checkout session");

        let json = self
            .post_form(
                "/checkout/sessions",
                &[
                    ("mode", "subscription"),
                    ("customer", customer_id),
                    ("line_items[0][price]", price_id),
                    ("line_items[0][quantity]", "1"),
                    ("success_url", success_url),
                    ("cancel_url", cancel_url),
                    (
                        "custom_text[submit][message]",
                        "Billing is handled by Stripe. Lockbook never receives your card number or email.",
                    ),
                ],
            )
            .await?;

        let session_id = json.get("id").and_then(Value::as_str).unwrap_or("");
        let url = json
            .get("url")
            .and_then(Value::as_str)
            .ok_or_else(|| {
                SimplifiedStripeError(format!("Stripe checkout session has no url: {session_id}"))
            })?
            .to_string();
        debug!(session_id, "Created stripe checkout session");
        Ok(url)
    }

    async fn create_billing_portal_session(
        &self, customer_id: &str, return_url: &str,
    ) -> Result<String, SimplifiedStripeError> {
        info!(customer_id, "Creating stripe billing portal session");

        let json = self
            .post_form(
                "/billing_portal/sessions",
                &[("customer", customer_id), ("return_url", return_url)],
            )
            .await?;

        let session_id = json.get("id").and_then(Value::as_str).unwrap_or("");
        let url = json
            .get("url")
            .and_then(Value::as_str)
            .ok_or_else(|| {
                SimplifiedStripeError(format!(
                    "Stripe billing portal session has no url: {session_id}"
                ))
            })?
            .to_string();
        debug!(session_id, "Created stripe billing portal session");
        Ok(url)
    }

    async fn cancel_subscription_at_period_end(
        &self, subscription_id: &str,
    ) -> Result<StripeSubscription, SimplifiedStripeError> {
        info!(subscription_id, "Canceling stripe subscription at period end");

        let json = self
            .post_form(
                &format!("/subscriptions/{subscription_id}"),
                &[("cancel_at_period_end", "true")],
            )
            .await?;
        let sub = parse_subscription(&json)?;
        debug!(
            subscription_id = sub.id.as_str(),
            status = sub.status.as_str(),
            "Scheduled stripe subscription to cancel"
        );
        Ok(sub)
    }

    async fn cancel_subscription_immediately(
        &self, subscription_id: &str,
    ) -> Result<(), SimplifiedStripeError> {
        info!(subscription_id, "Canceling stripe subscription immediately");
        let resp = self
            .http
            .delete(format!("{STRIPE_API_BASE}/subscriptions/{subscription_id}"))
            .bearer_auth(&*self.secret_key)
            .send()
            .await?;
        if resp.status() == reqwest::StatusCode::NOT_FOUND {
            debug!(subscription_id, "Stripe subscription already gone");
            return Ok(());
        }
        let json = Self::parse_response(resp).await?;
        let status = json.get("status").and_then(Value::as_str).unwrap_or("");
        debug!(subscription_id, status, "Canceled stripe subscription immediately");
        Ok(())
    }

    async fn get_subscription(
        &self, subscription_id: &str,
    ) -> Result<StripeSubscription, SimplifiedStripeError> {
        let json = self
            .get_json(
                &format!("/subscriptions/{subscription_id}"),
                &[("expand[]", "default_payment_method")],
            )
            .await?;
        let sub = parse_subscription(&json)?;
        debug!(
            subscription_id = sub.id.as_str(),
            status = sub.status.as_str(),
            "Retrieved stripe subscription"
        );
        Ok(sub)
    }

    async fn list_subscriptions(
        &self, customer_id: &str,
    ) -> Result<Vec<StripeSubscription>, SimplifiedStripeError> {
        let json = self
            .get_json(
                "/subscriptions",
                &[
                    ("customer", customer_id),
                    ("status", "all"),
                    ("limit", "100"),
                    ("expand[]", "data.default_payment_method"),
                ],
            )
            .await?;
        let items = json.get("data").and_then(Value::as_array).ok_or_else(|| {
            SimplifiedStripeError("Stripe subscriptions list missing data".into())
        })?;
        let mut subs = Vec::with_capacity(items.len());
        for item in items {
            subs.push(parse_subscription(item)?);
        }
        debug!(customer_id, count = subs.len(), "Listed stripe subscriptions");
        Ok(subs)
    }

    async fn expire_open_checkout_sessions(
        &self, customer_id: &str,
    ) -> Result<(), SimplifiedStripeError> {
        let json = self
            .get_json(
                "/checkout/sessions",
                &[("customer", customer_id), ("status", "open"), ("limit", "100")],
            )
            .await?;
        let Some(items) = json.get("data").and_then(Value::as_array) else {
            return Ok(());
        };
        for item in items {
            let Some(session_id) = item.get("id").and_then(Value::as_str) else {
                continue;
            };
            info!(session = session_id, "Expiring an older stripe checkout session");
            let resp = self
                .http
                .post(format!("{STRIPE_API_BASE}/checkout/sessions/{session_id}/expire"))
                .bearer_auth(&*self.secret_key)
                .send()
                .await?;
            // A session that expired or completed between list and expire returns 400 — don't abort checkout.
            if resp.status() == reqwest::StatusCode::BAD_REQUEST {
                debug!(session = session_id, "Older checkout session already closed");
                continue;
            }
            Self::parse_response(resp).await?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn parse_subscription_handles_pre_and_post_basil_and_payment_methods() {
        // Pre-2025-03-31.basil: top-level current_period_end + expanded card payment method.
        let pre_basil = parse_subscription(&json!({
            "id": "sub_1",
            "status": "active",
            "current_period_end": 1_800_000_000,
            "created": 1_700_000_000,
            "cancel_at_period_end": false,
            "default_payment_method": {
                "id": "pm_card",
                "type": "card",
                "card": { "last4": "4242" }
            },
            "items": { "data": [{ "price": { "id": "price_monthly" } }] }
        }))
        .unwrap();
        assert!(pre_basil.is_live());
        assert_eq!(pre_basil.current_period_end, 1_800_000_000);
        assert_eq!(pre_basil.payment_method_id, "pm_card");
        assert_eq!(pre_basil.card_last4, "4242");
        assert_eq!(pre_basil.price_id, "price_monthly");

        // Post-basil: current_period_end lives on items.data[0], Link/future method (no card).
        let post_basil = parse_subscription(&json!({
            "id": "sub_2",
            "status": "past_due",
            "created": 1_750_000_000,
            "cancel_at_period_end": false,
            "cancel_at": 1_850_000_000,
            "default_payment_method": { "id": "pm_link", "type": "amazon_pay" },
            "items": {
                "data": [{
                    "current_period_end": 1_850_000_000,
                    "price": { "id": "price_monthly" }
                }]
            },
            "future_unknown_field": { "nested": true }
        }))
        .unwrap();
        assert!(post_basil.is_live());
        assert_eq!(post_basil.current_period_end, 1_850_000_000);
        assert_eq!(post_basil.cancel_at, Some(1_850_000_000));
        assert_eq!(post_basil.payment_method_id, "pm_link");
        assert_eq!(post_basil.card_last4, "");

        // Unexpanded string payment method ID + non-live status.
        let ended = parse_subscription(&json!({
            "id": "sub_3",
            "status": "canceled",
            "current_period_end": 1_700_000_000,
            "created": 1_690_000_000,
            "default_payment_method": "pm_str",
            "items": { "data": [] }
        }))
        .unwrap();
        assert!(!ended.is_live());
        assert_eq!(ended.payment_method_id, "pm_str");
        assert_eq!(ended.card_last4, "");
    }
}