autumn-billing 0.10.0

Subscription billing plugin (Stripe checkout, portal, webhook mirror, dunning) for autumn-web applications
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
//! Stripe REST calls.
//!
//! Every call goes through [`StripeProvider::send`], which adds the bearer
//! token and decodes the reply. Bodies are form-encoded with Stripe's bracket
//! syntax (`line_items[0][price]`), the only encoding the API accepts.
//!
//! # Errors never carry secrets
//!
//! A [`BillingError::Provider`] message names the method, the path, the HTTP
//! status and Stripe's own error summary. It never contains the request body
//! or the `Authorization` header, and the secret key is scrubbed from any text
//! Stripe echoes back.
//!
//! # Idempotency keys
//!
//! Only `retry_invoice_payment` sends an `Idempotency-Key`: the dunning job
//! owns a key that is stable per `(invoice, attempt)`, so a crashed job can
//! repeat the call safely. The `create_*` calls send none. The provider has no
//! access to `AppState`, so it cannot draw a random key, and a key derived from
//! the request fields would be wrong: two checkouts for the same customer and
//! price are two distinct sessions (the first one may have expired), and a
//! derived key would make Stripe return the stale one. `POST /checkout` guards
//! against duplicates with an in-process per-user slot instead.

use serde_json::Value;

use super::{PROVIDER_NAME, StripeProvider};
use crate::config::SecretString;
use crate::error::BillingError;
use crate::model::ProviderId;
use crate::provider::{
    CheckoutRequest, CustomerRequest, HostedSession, PaymentAttemptOutcome, PortalRequest,
};

/// Stripe error codes that mean the invoice cannot be collected right now.
/// The outcome is a decline, not a transport failure.
const DECLINE_CODES: &[&str] = &[
    "card_declined",
    "payment_intent_authentication_failure",
    "invoice_payment_intent_requires_action",
];

/// A decoded reply. The body is `Null` when it is empty or not JSON.
struct Reply {
    status: u16,
    body: Value,
}

impl Reply {
    const fn is_success(&self) -> bool {
        self.status >= 200 && self.status < 300
    }

    fn str_field(&self, name: &str) -> Option<&str> {
        self.body.get(name).and_then(Value::as_str)
    }

    /// The `error` object Stripe returns on a failure.
    fn error(&self) -> ApiError {
        let error = self.body.get("error");
        let text = |name: &str| {
            error
                .and_then(|e| e.get(name))
                .and_then(Value::as_str)
                .map(str::to_owned)
        };
        ApiError {
            kind: text("type"),
            code: text("code"),
            decline_code: text("decline_code"),
            message: text("message"),
        }
    }
}

/// The parts of a Stripe error object the mapping reads.
struct ApiError {
    kind: Option<String>,
    code: Option<String>,
    decline_code: Option<String>,
    message: Option<String>,
}

impl ApiError {
    /// One line for a provider error message.
    fn summary(&self) -> String {
        let mut parts = Vec::new();
        if let Some(kind) = &self.kind {
            parts.push(kind.clone());
        }
        if let Some(code) = &self.code {
            parts.push(code.clone());
        }
        let head = parts.join("/");
        match (&self.message, head.is_empty()) {
            (Some(message), false) => format!("{head}: {message}"),
            (Some(message), true) => message.clone(),
            (None, false) => head,
            (None, true) => "no error detail".to_owned(),
        }
    }

    fn is_already_paid(&self) -> bool {
        self.code.as_deref() == Some("invoice_already_paid")
            || self
                .message
                .as_deref()
                .is_some_and(|m| m.to_ascii_lowercase().contains("already paid"))
    }

    /// The decline reason reported to the dunning job.
    fn decline_reason(&self) -> String {
        if let Some(code) = &self.code {
            return code.clone();
        }
        if let Some(code) = &self.decline_code {
            return code.clone();
        }
        if self.kind.as_deref() == Some("card_error") {
            return "card_declined".to_owned();
        }
        "declined".to_owned()
    }

    fn is_known_decline(&self) -> bool {
        self.kind.as_deref() == Some("card_error")
            || self
                .code
                .as_deref()
                .is_some_and(|code| DECLINE_CODES.contains(&code))
    }
}

/// Form-encode `fields` with Stripe's bracket keys.
fn form(fields: &[(&str, &str)]) -> Result<String, BillingError> {
    serde_urlencoded::to_string(fields)
        .map_err(|e| BillingError::provider(PROVIDER_NAME, format!("form encoding: {e}")))
}

impl StripeProvider {
    fn secret(&self) -> &str {
        self.config
            .secret_key
            .as_ref()
            .map_or("", SecretString::expose)
    }

    /// Scrub the secret key from text Stripe echoed back.
    fn redact(&self, text: &str) -> String {
        let secret = self.secret();
        if secret.is_empty() {
            text.to_owned()
        } else {
            text.replace(secret, "<redacted>")
        }
    }

    fn provider_error(&self, method: &str, path: &str, detail: &str) -> BillingError {
        BillingError::provider(
            PROVIDER_NAME,
            format!("{method} {path}: {}", self.redact(detail)),
        )
    }

    fn http_error(&self, method: &str, path: &str, reply: &Reply) -> BillingError {
        let detail = format!("HTTP {} {}", reply.status, reply.error().summary());
        self.provider_error(method, path, &detail)
    }

    /// Send `request` with the bearer token and decode the reply.
    async fn send(
        &self,
        request: autumn_web::http::RequestBuilder,
        method: &str,
        path: &str,
    ) -> Result<Reply, BillingError> {
        let response = request
            .header("authorization", format!("Bearer {}", self.secret()))
            .send()
            .await
            .map_err(|e| self.provider_error(method, path, &format!("request failed: {e}")))?;
        let status = response.status().as_u16();
        let bytes = response.bytes();
        let body = if bytes.is_empty() {
            Value::Null
        } else {
            serde_json::from_slice(&bytes).unwrap_or(Value::Null)
        };
        Ok(Reply { status, body })
    }

    /// `POST path` with a form body. Fails on any non-2xx reply.
    async fn post_form(
        &self,
        path: &str,
        fields: &[(&str, &str)],
        idempotency_key: Option<&str>,
    ) -> Result<Reply, BillingError> {
        let body = form(fields)?;
        let mut request = self
            .client
            .post(path)
            .header("content-type", "application/x-www-form-urlencoded")
            .text_body(body);
        if let Some(key) = idempotency_key {
            request = request.header("idempotency-key", key);
        }
        self.send(request, "POST", path).await
    }

    /// `POST path`, then read `id` and, when present, `url` from the reply.
    async fn create(
        &self,
        path: &str,
        fields: &[(&str, &str)],
    ) -> Result<(ProviderId, Option<String>), BillingError> {
        let reply = self.post_form(path, fields, None).await?;
        if !reply.is_success() {
            return Err(self.http_error("POST", path, &reply));
        }
        let id = reply
            .str_field("id")
            .ok_or_else(|| self.provider_error("POST", path, "reply has no id"))?;
        let url = reply.str_field("url").map(str::to_owned);
        Ok((ProviderId::new(id), url))
    }

    async fn hosted_session(
        &self,
        path: &str,
        fields: &[(&str, &str)],
    ) -> Result<HostedSession, BillingError> {
        let (id, url) = self.create(path, fields).await?;
        let url = url.ok_or_else(|| self.provider_error("POST", path, "reply has no url"))?;
        Ok(HostedSession::new(id, url))
    }

    pub(super) async fn customer(
        &self,
        request: CustomerRequest,
    ) -> Result<ProviderId, BillingError> {
        let mut fields = vec![
            (
                "metadata[autumn_customer_id]",
                request.local_customer_id.as_str(),
            ),
            ("metadata[autumn_user_id]", request.user_id.as_str()),
        ];
        if let Some(email) = &request.email {
            fields.push(("email", email.as_str()));
        }
        let (id, _) = self.create("/v1/customers", &fields).await?;
        Ok(id)
    }

    pub(super) async fn checkout(
        &self,
        request: CheckoutRequest,
    ) -> Result<HostedSession, BillingError> {
        let quantity = request.quantity.to_string();
        let fields = [
            ("mode", "subscription"),
            ("customer", request.provider_customer_id.as_str()),
            ("client_reference_id", request.local_customer_id.as_str()),
            ("line_items[0][price]", request.provider_price_id.as_str()),
            ("line_items[0][quantity]", quantity.as_str()),
            ("success_url", request.success_url.as_str()),
            ("cancel_url", request.cancel_url.as_str()),
        ];
        self.hosted_session("/v1/checkout/sessions", &fields).await
    }

    pub(super) async fn portal(
        &self,
        request: PortalRequest,
    ) -> Result<HostedSession, BillingError> {
        let fields = [
            ("customer", request.provider_customer_id.as_str()),
            ("return_url", request.return_url.as_str()),
        ];
        self.hosted_session("/v1/billing_portal/sessions", &fields)
            .await
    }

    pub(super) async fn pay_invoice(
        &self,
        invoice: &ProviderId,
        idempotency_key: &str,
    ) -> Result<PaymentAttemptOutcome, BillingError> {
        let path = format!("/v1/invoices/{invoice}/pay");
        let reply = self.post_form(&path, &[], Some(idempotency_key)).await?;
        if reply.is_success() {
            return Ok(match reply.str_field("status") {
                Some("paid") => PaymentAttemptOutcome::Paid,
                status => PaymentAttemptOutcome::Declined {
                    reason: format!("invoice_{}", status.unwrap_or("unknown")),
                },
            });
        }
        match reply.status {
            // Not a payment outcome: bad credentials, rate limit, or a Stripe
            // failure. The job keeps the attempt and retries later.
            401 | 403 | 429 | 500..=599 => Err(self.http_error("POST", &path, &reply)),
            400..=499 => {
                let error = reply.error();
                if error.is_already_paid() {
                    return Ok(PaymentAttemptOutcome::AlreadyPaid);
                }
                if !error.is_known_decline() {
                    tracing::debug!(
                        invoice = %invoice,
                        status = reply.status,
                        detail = %self.redact(&error.summary()),
                        "stripe invoice pay rejected; treated as a decline"
                    );
                }
                Ok(PaymentAttemptOutcome::Declined {
                    reason: error.decline_reason(),
                })
            }
            _ => Err(self.http_error("POST", &path, &reply)),
        }
    }

    /// `GET /v1/subscriptions/{id}`: the subscription as the provider holds it
    /// now. `None` when the provider has no such subscription (404).
    pub(super) async fn subscription(
        &self,
        subscription: &ProviderId,
    ) -> Result<Option<Value>, BillingError> {
        let path = format!("/v1/subscriptions/{subscription}");
        let request = self.client.get(&path);
        let reply = self.send(request, "GET", &path).await?;
        if reply.is_success() {
            return Ok(Some(reply.body));
        }
        if reply.status == 404 {
            return Ok(None);
        }
        Err(self.http_error("GET", &path, &reply))
    }

    pub(super) async fn cancel(&self, subscription: &ProviderId) -> Result<(), BillingError> {
        let path = format!("/v1/subscriptions/{subscription}");
        let request = self.client.delete(&path);
        let reply = self.send(request, "DELETE", &path).await?;
        // 404: the subscription is already gone, which is the state we want.
        if reply.is_success() || reply.status == 404 {
            return Ok(());
        }
        Err(self.http_error("DELETE", &path, &reply))
    }
}

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

    #[test]
    fn form_uses_bracket_keys() {
        let encoded = form(&[
            ("line_items[0][price]", "price_x"),
            ("mode", "subscription"),
        ])
        .unwrap();
        assert_eq!(
            encoded,
            "line_items%5B0%5D%5Bprice%5D=price_x&mode=subscription"
        );
    }

    #[test]
    fn already_paid_is_read_from_code_or_message() {
        let by_code = ApiError {
            kind: None,
            code: Some("invoice_already_paid".into()),
            decline_code: None,
            message: None,
        };
        assert!(by_code.is_already_paid());
        let by_message = ApiError {
            kind: None,
            code: None,
            decline_code: None,
            message: Some("Invoice is Already Paid".into()),
        };
        assert!(by_message.is_already_paid());
    }

    #[test]
    fn decline_reason_prefers_code_then_decline_code_then_kind() {
        let coded = ApiError {
            kind: Some("card_error".into()),
            code: Some("card_declined".into()),
            decline_code: Some("insufficient_funds".into()),
            message: None,
        };
        assert_eq!(coded.decline_reason(), "card_declined");
        let decline_only = ApiError {
            kind: Some("card_error".into()),
            code: None,
            decline_code: Some("insufficient_funds".into()),
            message: None,
        };
        assert_eq!(decline_only.decline_reason(), "insufficient_funds");
        let bare_card_error = ApiError {
            kind: Some("card_error".into()),
            code: None,
            decline_code: None,
            message: None,
        };
        assert_eq!(bare_card_error.decline_reason(), "card_declined");
        let nothing = ApiError {
            kind: None,
            code: None,
            decline_code: None,
            message: None,
        };
        assert_eq!(nothing.decline_reason(), "declined");
    }
}