renox-billing 1.3.1

Subscriptions for Renox apps (plans, trials, upgrades, cancellations) with Stripe and Xendit.
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! Who pays ([`Owner`], [`Billable`]) and what they can do
//! ([`Customer`]): Laravel Cashier's billable methods.

use std::sync::Arc;

use renox::auth::User;
use renox::prelude::*;
use serde::Serialize;

use crate::events::SubscriptionCreated;
use crate::model::{BillingCustomer, Subscription, SubscriptionStatus};
use crate::payment::Charge;
use crate::sync::{apply, emit_changes};
use crate::{Billing, BillingPayment, CheckoutRequest, Remote, Setup};

/// Who pays: a kind (`user`, or the app's name for what it bills, such
/// as `team`) and an id, with an email address and a name for the
/// gateway's customer record.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct Owner {
    /// What kind of owner: `user`, `team`, …
    pub kind: String,
    /// Its id.
    pub id: i64,
    /// The address the gateway sends receipts to.
    pub email: Option<String>,
    /// The name on the gateway's customer record.
    pub name: Option<String>,
}

impl Owner {
    /// The owner `id` of kind `kind`.
    pub fn new(kind: impl Into<String>, id: i64) -> Self {
        Self {
            kind: kind.into(),
            id,
            email: None,
            name: None,
        }
    }

    /// A user, with their address and name.
    pub fn user(user: &User) -> Self {
        Self::new("user", user.id)
            .email(user.email.clone())
            .name(user.name.clone())
    }

    /// With the address the gateway sends receipts to.
    pub fn email(mut self, email: impl Into<String>) -> Self {
        self.email = Some(email.into()).filter(|e| !e.is_empty());
        self
    }

    /// With the name on the gateway's customer record.
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into()).filter(|n| !n.trim().is_empty());
        self
    }

    /// `kind:id`, e.g. `user:5`: what the gateways store as `renox_billable`.
    pub fn key(&self) -> String {
        format!("{}:{}", self.kind, self.id)
    }

    /// The owner of a [`Owner::key`].
    pub fn from_key(key: &str) -> Option<Self> {
        let (kind, id) = key.rsplit_once(':')?;
        let id = id.parse().ok()?;
        (!kind.is_empty()).then(|| Self::new(kind, id))
    }

    /// Whether this is the user `user_id`.
    pub fn is_user(&self, user_id: i64) -> bool {
        self.kind == "user" && self.id == user_id
    }
}

/// What can subscribe. Users are billable as they are; for a team (or an
/// organization…), say which [`Owner`] it is:
///
/// ```
/// use renox_billing::{Billable, Owner};
///
/// struct Team {
///     id: i64,
///     name: String,
///     billing_email: String,
/// }
///
/// impl Billable for Team {
///     fn owner(&self) -> Owner {
///         Owner::new("team", self.id)
///             .name(self.name.clone())
///             .email(self.billing_email.clone())
///     }
/// }
/// ```
pub trait Billable {
    /// The owner the subscriptions belong to.
    fn owner(&self) -> Owner;
}

impl Billable for User {
    fn owner(&self) -> Owner {
        Owner::user(self)
    }
}

impl Billable for AuthUser {
    fn owner(&self) -> Owner {
        Owner::user(self)
    }
}

impl Billable for Owner {
    fn owner(&self) -> Owner {
        self.clone()
    }
}

/// An owner's subscriptions, and what they can do with them: from
/// [`Billing::of`]. Every method is about one subscription name,
/// `default` unless [`Customer::named`] says another.
///
/// ```
/// # use renox::prelude::*;
/// use renox_billing::Billing;
///
/// async fn dashboard(State(state): State<AppState>, user: AuthUser) -> Result<View> {
///     let billing = Billing::of(&state, &*user);
///     let subscribed = billing.subscribed().await?;
///     let on_trial = billing.on_trial().await?;
///     let pro = billing.subscribed_to("pro").await?;
///     Ok(view("dashboard.html", context! { subscribed, on_trial, pro }))
/// }
/// ```
pub struct Customer<'a> {
    state: &'a AppState,
    owner: Owner,
    name: String,
}

impl Billing {
    /// `billable`'s subscriptions (a [`User`], or any [`Billable`]).
    pub fn of<'a>(state: &'a AppState, billable: &impl Billable) -> Customer<'a> {
        Customer {
            state,
            owner: billable.owner(),
            name: "default".into(),
        }
    }
}

impl Customer<'_> {
    /// About the subscription named `name` rather than `default`, for an
    /// app that sells more than one thing.
    pub fn named(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    /// Who this is.
    pub fn owner(&self) -> &Owner {
        &self.owner
    }

    fn setup(&self) -> Result<Arc<Setup>> {
        crate::setup(self.state)
    }

    /// Charges the owner a one-off `charge`: the payment is stored
    /// (pending) with its reference and the gateway's page is in
    /// `payment.url`; the result arrives by webhook. A gateway that cannot
    /// charge answers [`Unsupported`](crate::Unsupported) and leaves no row.
    pub async fn charge(&self, charge: Charge) -> Result<BillingPayment> {
        let mut request = charge.request().clone();
        request.owner = Some(self.owner.clone());
        crate::payment::start(self.state, charge.gateway(), request).await
    }

    /// The newest subscription of this name, whatever its state.
    pub async fn subscription(&self) -> Result<Option<Subscription>> {
        Subscription::latest(&self.state.db, &self.owner, &self.name).await
    }

    /// Every subscription the owner has had, of any name, newest first.
    pub async fn subscriptions(&self) -> Result<Vec<Subscription>> {
        Subscription::of(&self.state.db, &self.owner).await
    }

    /// Whether the owner has a [valid](Subscription::valid) subscription:
    /// a trial, paid for, or canceled with time left.
    pub async fn subscribed(&self) -> Result<bool> {
        Ok(self.subscription().await?.is_some_and(|s| s.valid()))
    }

    /// Whether they're subscribed to the plan `key`.
    pub async fn subscribed_to(&self, key: &str) -> Result<bool> {
        Ok(self
            .subscription()
            .await?
            .is_some_and(|s| s.valid() && s.has_plan(key)))
    }

    /// Whether the subscription is in its free trial.
    pub async fn on_trial(&self) -> Result<bool> {
        Ok(self.subscription().await?.is_some_and(|s| s.on_trial()))
    }

    /// Whether it was canceled and still runs until its end.
    pub async fn on_grace_period(&self) -> Result<bool> {
        Ok(self
            .subscription()
            .await?
            .is_some_and(|s| s.on_grace_period()))
    }

    /// Whether the owner may still get a plan's free trial: they've never
    /// had a subscription of this name (a trial without a payment method
    /// that is still running carries over to the checkout).
    pub async fn trial_available(&self) -> Result<bool> {
        Ok(self.subscription().await?.is_none())
    }

    /// The owner's customer id at `gateway`, made there on first use.
    pub async fn customer_id(&self, gateway: &str) -> Result<String> {
        if let Some(customer) = BillingCustomer::of(&self.state.db, &self.owner, gateway).await? {
            return Ok(customer.gateway_id);
        }
        let setup = self.setup()?;
        let gateway = setup.gateway(self.state, gateway).ok_or_else(|| {
            renox::anyhow::anyhow!("the payment gateway `{gateway}` isn't set up")
        })?;
        let id = gateway.create_customer(self.state, &self.owner).await?;
        BillingCustomer::create(
            &self.state.db,
            BillingCustomer {
                billable_type: self.owner.kind.clone(),
                billable_id: self.owner.id,
                gateway: gateway.name().to_owned(),
                gateway_id: id.clone(),
                ..Default::default()
            },
        )
        .await?;
        Ok(id)
    }

    /// Starts subscribing to the plan `key`: answers the gateway's page
    /// where the customer pays (redirect them there). They come back to the
    /// `billing.return` route; the subscription arrives by webhook.
    ///
    /// The plan's free trial applies to a first subscription; a running
    /// trial without a payment method carries its days over, and a
    /// canceled subscription's remaining time is not charged again.
    pub async fn checkout(&self, key: &str) -> Result<String> {
        let setup = self.setup()?;
        let plan = setup.plan(key)?.clone();
        let gateway = setup.gateway_for(self.state, &plan)?;
        let current = self.subscription().await?;
        if let Some(current) = &current
            && (current.valid() || current.paused())
            && !current.is_generic_trial()
            && !current.canceled()
        {
            return Err(Error::BadRequest(
                "You're subscribed already: change your plan instead.".into(),
            ));
        }
        let now = renox::db::now();
        let trial_ends_at = match &current {
            None if plan.trial_days > 0 => {
                Some(now + renox::chrono::Duration::days(i64::from(plan.trial_days)))
            }
            None => None,
            Some(s) if s.is_generic_trial() && s.on_trial() => s.trial_ends_at,
            Some(s) if s.on_grace_period() => s.ends_at,
            Some(_) => None,
        };
        let customer_id = self.customer_id(gateway.name()).await?;
        let request = CheckoutRequest {
            owner: self.owner.clone(),
            customer_id,
            name: self.name.clone(),
            price_id: plan.price_id_for(gateway.name(), &self.state.config),
            plan,
            trial_ends_at,
            success_url: self.state.absolute_url("billing.return", &[])?,
            cancel_url: self.state.absolute_url("billing.plans", &[])?,
        };
        let checkout = gateway.checkout(self.state, &request).await?;
        Ok(checkout.url)
    }

    /// Starts the plan `key`'s free trial without a payment method (a
    /// "generic trial"). The owner subscribes with a payment method before
    /// it ends ([`Customer::checkout`] keeps the days left).
    pub async fn start_trial(&self, key: &str) -> Result<Subscription> {
        let setup = self.setup()?;
        let plan = setup.plan(key)?;
        if plan.trial_days == 0 {
            return Err(Error::BadRequest(format!(
                "{} has no free trial.",
                plan.label
            )));
        }
        if !self.trial_available().await? {
            return Err(Error::BadRequest(
                "You've had a subscription already, so there's no free trial.".into(),
            ));
        }
        let ends = renox::db::now() + renox::chrono::Duration::days(i64::from(plan.trial_days));
        let subscription = Subscription::create(
            &self.state.db,
            Subscription {
                billable_type: self.owner.kind.clone(),
                billable_id: self.owner.id,
                name: self.name.clone(),
                plan: plan.key.clone(),
                status: SubscriptionStatus::Trialing,
                trial_ends_at: Some(ends),
                ..Default::default()
            },
        )
        .await?;
        self.state
            .emit(SubscriptionCreated {
                subscription: subscription.clone(),
            })
            .await?;
        Ok(subscription)
    }

    /// The subscription to change: valid and not ended.
    async fn current(&self) -> Result<Subscription> {
        self.subscription()
            .await?
            .filter(|s| s.valid() || (s.paused() && !s.ended()))
            .ok_or_else(|| Error::BadRequest("You're not subscribed.".into()))
    }

    /// Moves the subscription to the plan `key` (an upgrade or a
    /// downgrade), prorated where the gateway does that unless the module
    /// says [`Billing::without_proration`].
    pub async fn swap(&self, key: &str) -> Result<Subscription> {
        let setup = self.setup()?;
        let plan = setup.plan(key)?.clone();
        let current = self.current().await?;
        if current.plan == plan.key {
            return Ok(current);
        }
        if current.canceled() {
            return Err(Error::BadRequest(
                "Resume the subscription before changing its plan.".into(),
            ));
        }
        if current.is_generic_trial() {
            let mut changed = current.clone();
            changed.plan = plan.key.clone();
            changed.save(&self.state.db).await?;
            emit_changes(self.state, &current, &changed).await?;
            return Ok(changed);
        }
        let gateway = setup.gateway_for(self.state, &plan)?;
        if gateway.name() != current.gateway {
            return Err(Error::BadRequest(format!(
                "{} is sold through another payment provider: cancel, then subscribe to it.",
                plan.label
            )));
        }
        let remote = gateway
            .swap(self.state, &current, &plan, setup.prorate)
            .await?;
        let remote = Remote {
            plan: Some(plan.key.clone()),
            price_id: None,
            ..remote
        };
        self.applied(&setup, &current, remote).await
    }

    /// Cancels at the end of the period paid for (or of the trial): the
    /// owner keeps access until then (the grace period).
    pub async fn cancel(&self) -> Result<Subscription> {
        self.cancel_with(true).await
    }

    /// Cancels now: access ends at once.
    pub async fn cancel_now(&self) -> Result<Subscription> {
        self.cancel_with(false).await
    }

    async fn cancel_with(&self, at_period_end: bool) -> Result<Subscription> {
        let setup = self.setup()?;
        let current = self.current().await?;
        if at_period_end && current.canceled() {
            return Ok(current);
        }
        if current.is_generic_trial() {
            let mut changed = current.clone();
            changed.status = SubscriptionStatus::Canceled;
            changed.ends_at = Some(match (at_period_end, changed.trial_ends_at) {
                (true, Some(at)) => at,
                _ => renox::db::now(),
            });
            changed.save(&self.state.db).await?;
            emit_changes(self.state, &current, &changed).await?;
            return Ok(changed);
        }
        let gateway = setup.gateway(self.state, &current.gateway).ok_or_else(|| {
            renox::anyhow::anyhow!("the payment gateway `{}` isn't set up", current.gateway)
        })?;
        let remote = gateway.cancel(self.state, &current, at_period_end).await?;
        self.applied(&setup, &current, remote).await
    }

    /// Whether the subscription can be [resumed](Customer::resume): in its
    /// grace period, at a gateway that resumes.
    pub async fn can_resume(&self) -> Result<bool> {
        let Some(current) = self.subscription().await? else {
            return Ok(false);
        };
        if !current.on_grace_period() {
            return Ok(false);
        }
        if current.is_generic_trial() {
            return Ok(true);
        }
        let setup = self.setup()?;
        Ok(setup
            .gateway(self.state, &current.gateway)
            .is_some_and(|g| g.resumes()))
    }

    /// Takes back a cancellation during the grace period: charging goes on
    /// as before.
    pub async fn resume(&self) -> Result<Subscription> {
        let setup = self.setup()?;
        let current = self.current().await?;
        if !current.on_grace_period() {
            return Err(Error::BadRequest(
                "Only a canceled subscription that hasn't ended can be resumed.".into(),
            ));
        }
        if current.is_generic_trial() {
            let mut changed = current.clone();
            changed.status = SubscriptionStatus::Trialing;
            changed.ends_at = None;
            changed.save(&self.state.db).await?;
            emit_changes(self.state, &current, &changed).await?;
            return Ok(changed);
        }
        let gateway = setup.gateway(self.state, &current.gateway).ok_or_else(|| {
            renox::anyhow::anyhow!("the payment gateway `{}` isn't set up", current.gateway)
        })?;
        let remote = gateway.resume(self.state, &current).await?;
        self.applied(&setup, &current, remote).await
    }

    /// Whether billing can be [paused](Customer::pause): an active
    /// subscription with a payment method, not canceled, at a gateway that
    /// pauses.
    pub async fn can_pause(&self) -> Result<bool> {
        let Some(current) = self.subscription().await? else {
            return Ok(false);
        };
        if current.is_generic_trial()
            || current.canceled()
            || current.status != SubscriptionStatus::Active
        {
            return Ok(false);
        }
        let setup = self.setup()?;
        Ok(setup
            .gateway(self.state, &current.gateway)
            .is_some_and(|g| g.pauses()))
    }

    /// Stops billing for a while: nothing is charged and the subscription
    /// is not [valid](Subscription::valid) (the guards turn the owner
    /// away) until [`Customer::unpause`], or, with `resumes_at`, until the
    /// gateway takes it up again then. A gateway that can't pause
    /// answers a refusal ([`Error::BadRequest`]); so does a subscription
    /// that isn't active.
    pub async fn pause(&self, resumes_at: Option<DateTime>) -> Result<Subscription> {
        let setup = self.setup()?;
        let current = self.current().await?;
        if current.paused() {
            return Ok(current);
        }
        if current.is_generic_trial() || current.status != SubscriptionStatus::Active {
            return Err(Error::BadRequest(
                "Only a subscription that is paid for can be paused.".into(),
            ));
        }
        if current.canceled() {
            return Err(Error::BadRequest(
                "A canceled subscription can't be paused: resume it first.".into(),
            ));
        }
        let gateway = setup.gateway(self.state, &current.gateway).ok_or_else(|| {
            renox::anyhow::anyhow!("the payment gateway `{}` isn't set up", current.gateway)
        })?;
        let remote = gateway.pause(self.state, &current, resumes_at).await?;
        let remote = Remote {
            status: Some(SubscriptionStatus::Paused),
            ..remote
        };
        self.applied(&setup, &current, remote).await
    }

    /// Takes billing up again after [`Customer::pause`].
    pub async fn unpause(&self) -> Result<Subscription> {
        let setup = self.setup()?;
        let current = self.current().await?;
        if !current.paused() {
            return Err(Error::BadRequest(
                "Only a paused subscription can be taken up again.".into(),
            ));
        }
        let gateway = setup.gateway(self.state, &current.gateway).ok_or_else(|| {
            renox::anyhow::anyhow!("the payment gateway `{}` isn't set up", current.gateway)
        })?;
        let remote = gateway.unpause(self.state, &current).await?;
        let remote = Remote {
            status: remote
                .status
                .filter(|s| *s != SubscriptionStatus::Paused)
                .or(Some(SubscriptionStatus::Active)),
            ..remote
        };
        self.applied(&setup, &current, remote).await
    }

    /// Applies what the gateway answered to `current`, and reads it back.
    async fn applied(
        &self,
        setup: &Setup,
        current: &Subscription,
        remote: Remote,
    ) -> Result<Subscription> {
        // Only webhooks move `synced_at`: the gateway's clock orders them.
        let remote = Remote { at: None, ..remote };
        let gateway = current.gateway.clone();
        Ok(apply(self.state, setup, &gateway, remote)
            .await?
            .unwrap_or_else(|| current.clone()))
    }
}