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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
//! One-off payments: the `billing_payments` and `billing_refunds` tables,
//! their status types and the [`Unsupported`] error a gateway answers with
//! for an operation it cannot do.

use std::fmt;

use renox::money::Money;
use renox::prelude::*;
use serde::Serialize;

use crate::{Billing, Owner};

/// What a payment is (`billing_payments.kind`).
#[derive(DbEnum, Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum PaymentKind {
    /// A charge the app made for a customer.
    #[default]
    Charge,
    /// A payment link anyone with the URL can pay.
    Link,
}

/// Where a payment is, as the gateway last said (`billing_payments.status`).
#[derive(DbEnum, Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum PaymentStatus {
    /// Waiting for the customer to pay.
    #[default]
    Pending,
    /// Paid.
    Succeeded,
    /// The payment was refused or abandoned.
    Failed,
    /// The link or session ran out before it was paid.
    Expired,
    /// Refunded in full.
    Refunded,
    /// Refunded in part.
    PartiallyRefunded,
    /// The customer disputed it (a chargeback is open).
    Disputed,
}

/// Where a refund is (`billing_refunds.status`).
#[derive(DbEnum, Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum RefundStatus {
    /// Asked for; the gateway has not finished it.
    #[default]
    Pending,
    /// The money went back.
    Succeeded,
    /// The gateway could not refund it.
    Failed,
}

/// A one-off payment: a charge or a payment link, with the app's reference
/// to what it is for. Not to be confused with [`crate::Payment`], the
/// gateway's notice that an invoice was paid.
#[derive(Model, Serialize, Debug, Clone)]
#[model(table = "billing_payments")]
pub struct BillingPayment {
    /// The row's id.
    pub id: i64,
    /// What kind of owner paid; `None` for a link without an owner.
    pub billable_type: Option<String>,
    /// The owner's id.
    pub billable_id: Option<i64>,
    /// The kind of the app's record this pays for (`job`, `order`).
    pub reference_type: Option<String>,
    /// That record's id.
    pub reference_id: Option<String>,
    /// What is charged.
    #[model(money(currency = "currency"))]
    pub amount: Money,
    /// The gateway's name.
    pub gateway: String,
    /// The gateway's account; `None` is its default account.
    pub account: Option<String>,
    /// The payment's id at the gateway; `None` until it made one.
    pub gateway_id: Option<String>,
    /// The payment's second id at the gateway, once known (Stripe's
    /// PaymentIntent, which its disputes and dashboard refunds name).
    pub gateway_intent: Option<String>,
    /// Charge or link.
    pub kind: PaymentKind,
    /// As the gateway last said.
    pub status: PaymentStatus,
    /// The sum of the refunds that succeeded, in the payment's currency.
    pub refunded_amount: i64,
    /// The payment page, for a link or a hosted charge.
    pub url: Option<String>,
    /// When the link or session stops working.
    pub expires_at: Option<DateTime>,
    /// When the evidence for an open dispute is due.
    pub evidence_due_by: Option<DateTime>,
    /// The newest gateway event applied (unix seconds).
    #[serde(skip)]
    pub synced_at: Option<i64>,
    /// When it was made.
    pub created_at: Option<DateTime>,
    /// When it last changed.
    pub updated_at: Option<DateTime>,
}

impl BillingPayment {
    /// A new pending payment of `amount` through `gateway`, not saved yet.
    pub fn new(gateway: impl Into<String>, kind: PaymentKind, amount: Money) -> Self {
        Self {
            id: 0,
            billable_type: None,
            billable_id: None,
            reference_type: None,
            reference_id: None,
            amount,
            gateway: gateway.into(),
            account: None,
            gateway_id: None,
            gateway_intent: None,
            kind,
            status: PaymentStatus::Pending,
            refunded_amount: 0,
            url: None,
            expires_at: None,
            evidence_due_by: None,
            synced_at: None,
            created_at: None,
            updated_at: None,
        }
    }

    /// The payment a gateway notice names: by its id, or by its second id
    /// (`gateway_intent`) when the notice carries only that one.
    pub async fn find_named(db: &Db, gateway: &str, id: &str) -> Result<Option<Self>> {
        if let Some(row) = Self::find_at(db, gateway, id).await? {
            return Ok(Some(row));
        }
        Self::where_eq("gateway", gateway)
            .where_eq("gateway_intent", id)
            .order_by_desc("id")
            .first(db)
            .await
    }

    /// The part refunded so far, in the payment's currency.
    pub fn refunded(&self) -> Money {
        Money::new(self.refunded_amount, self.amount.currency)
    }

    /// What can still be refunded; zero before it is paid.
    pub fn refundable(&self) -> Money {
        let paid = matches!(
            self.status,
            PaymentStatus::Succeeded | PaymentStatus::PartiallyRefunded | PaymentStatus::Disputed
        );
        let left = if paid {
            (self.amount.minor - self.refunded_amount).max(0)
        } else {
            0
        };
        Money::new(left, self.amount.currency)
    }

    /// Who it belongs to, if it has an owner.
    pub fn owner(&self) -> Option<Owner> {
        Some(Owner::new(self.billable_type.clone()?, self.billable_id?))
    }

    /// The payment with this id at `gateway`.
    pub async fn find_at(db: &Db, gateway: &str, gateway_id: &str) -> Result<Option<Self>> {
        Self::where_eq("gateway", gateway)
            .where_eq("gateway_id", gateway_id)
            .first(db)
            .await
    }

    /// The payments for the app's record `reference_type` / `reference_id`,
    /// newest first.
    pub async fn for_reference(
        db: &Db,
        reference_type: &str,
        reference_id: &str,
    ) -> Result<Vec<Self>> {
        Self::where_eq("reference_type", reference_type)
            .where_eq("reference_id", reference_id)
            .order_by_desc("id")
            .get(db)
            .await
    }

    /// The refunds of this payment, oldest first.
    pub async fn refunds(&self, db: &Db) -> Result<Vec<BillingRefund>> {
        BillingRefund::where_eq("payment_id", self.id)
            .order_by("id")
            .get(db)
            .await
    }
}

/// A refund of a [`BillingPayment`], whole or partial.
#[derive(Model, Serialize, Default, Debug, Clone)]
#[model(table = "billing_refunds")]
pub struct BillingRefund {
    /// The row's id.
    pub id: i64,
    /// The payment refunded.
    pub payment_id: i64,
    /// The refund's id at the gateway; `None` until it made one.
    pub gateway_id: Option<String>,
    /// The amount refunded, in the payment's currency.
    pub amount: i64,
    /// As the gateway last said.
    pub status: RefundStatus,
    /// Why, as the app or the gateway said.
    pub reason: Option<String>,
    /// When it was asked for.
    pub created_at: Option<DateTime>,
    /// When it last changed.
    pub updated_at: Option<DateTime>,
}

/// A gateway cannot do an operation (Xendit has no disputes to accept, a
/// custom gateway may not refund). It travels inside an [`Error`]; find it
/// again with [`Unsupported::of`].
///
/// ```
/// use renox::prelude::*;
/// use renox_billing::payment::Unsupported;
///
/// let err: Error = Unsupported::new("xendit", "accept_dispute").into();
/// let found = Unsupported::of(&err).expect("an unsupported error");
/// assert_eq!(found.gateway, "xendit");
/// assert_eq!(found.operation, "accept_dispute");
/// assert_eq!(err.to_string(), "The gateway xendit does not support accept_dispute.");
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Unsupported {
    /// The gateway's name.
    pub gateway: String,
    /// What was asked: `charge`, `payment_link`, `refund`, …
    pub operation: String,
}

impl Unsupported {
    /// The refusal of `operation` by `gateway`.
    pub fn new(gateway: impl Into<String>, operation: impl Into<String>) -> Self {
        Self {
            gateway: gateway.into(),
            operation: operation.into(),
        }
    }

    /// The refusal inside `err`, if that is what it carries.
    pub fn of(err: &Error) -> Option<&Unsupported> {
        match err {
            Error::Internal(inner) => inner.downcast_ref::<Unsupported>(),
            _ => None,
        }
    }
}

impl fmt::Display for Unsupported {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "The gateway {} does not support {}.",
            self.gateway, self.operation
        )
    }
}

impl std::error::Error for Unsupported {}

/// Starts a charge or a link: the row is written first (the gateway stores
/// its id), then the gateway is called. If the gateway cannot (or fails),
/// the row is removed again, so nothing is left behind.
pub(crate) async fn start(
    state: &AppState,
    gateway: Option<&str>,
    mut request: ChargeRequest,
) -> Result<BillingPayment> {
    if request.amount.minor <= 0 {
        return Err(Error::BadRequest(
            "A payment must be for more than zero.".into(),
        ));
    }
    let gateway = crate::setup(state)?.payment_gateway(state, gateway)?;
    let mut row = BillingPayment::new(gateway.name(), request.kind, request.amount);
    row.account = request.account.clone();
    row.expires_at = request.expires_at;
    row.reference_type = request.reference_type.clone();
    row.reference_id = request.reference_id.clone();
    if let Some(owner) = &request.owner {
        row.billable_type = Some(owner.kind.clone());
        row.billable_id = Some(owner.id);
    }
    let mut row = BillingPayment::create(&state.db, row).await?;
    request.payment_id = row.id;
    let page = match request.kind {
        PaymentKind::Charge => gateway.charge(state, &request).await,
        PaymentKind::Link => gateway.payment_link(state, &request).await,
    };
    let page = match page {
        Ok(page) => page,
        Err(err) => {
            BillingPayment::query()
                .where_eq("id", row.id)
                .delete(&state.db)
                .await?;
            return Err(err);
        }
    };
    row.gateway_id = Some(page.gateway_id);
    row.url = page.url;
    if page.expires_at.is_some() {
        row.expires_at = page.expires_at;
    }
    row.save(&state.db).await?;
    Ok(row)
}

impl Billing {
    /// Makes a payment link anyone can pay, with or without an owner. The
    /// payment is stored (pending) with its reference; `payment.url` is the
    /// link. A gateway that cannot make links answers [`Unsupported`] and
    /// leaves no row.
    pub async fn payment_link(state: &AppState, link: PaymentLink) -> Result<BillingPayment> {
        start(state, link.gateway(), link.request().clone()).await
    }

    /// Refunds `refund.amount` of the payment `payment_id` (partial refunds
    /// add up). More than what is left to refund, counting refunds still
    /// pending, is refused before the gateway is called. The refund is
    /// pending unless the gateway refunded at once; then the payment moves
    /// to refunded or partially refunded and `RefundSucceeded` and
    /// `PaymentRefunded` are announced.
    pub async fn refund(
        state: &AppState,
        payment_id: i64,
        refund: Refund,
    ) -> Result<BillingRefund> {
        let mut payment = BillingPayment::find(&state.db, payment_id)
            .await?
            .ok_or(Error::NotFound)?;
        if refund.amount.currency != payment.amount.currency || refund.amount.minor <= 0 {
            return Err(Error::BadRequest(
                "A refund is a positive amount in the payment's currency.".into(),
            ));
        }
        let pending: i64 = BillingRefund::where_eq("payment_id", payment.id)
            .where_eq("status", RefundStatus::Pending)
            .sum(&state.db, "amount")
            .await?;
        if refund.amount.minor > payment.refundable().minor - pending {
            return Err(Error::BadRequest(
                "That is more than is left to refund.".into(),
            ));
        }
        let gateway = crate::setup(state)?.payment_gateway(state, Some(&payment.gateway))?;
        let mut row = BillingRefund::create(
            &state.db,
            BillingRefund {
                payment_id: payment.id,
                amount: refund.amount.minor,
                reason: refund.reason.clone(),
                ..Default::default()
            },
        )
        .await?;
        let result = match gateway.refund(state, &payment, &refund).await {
            Ok(result) => result,
            Err(err) => {
                BillingRefund::query()
                    .where_eq("id", row.id)
                    .delete(&state.db)
                    .await?;
                return Err(err);
            }
        };
        row.gateway_id = result.gateway_id;
        row.status = result.status;
        row.save(&state.db).await?;
        if row.status == RefundStatus::Succeeded {
            crate::sync::refund_done(state, &mut payment, row.amount).await?;
        }
        Ok(row)
    }

    /// Marks the payment links that ran out unpaid as expired and announces
    /// `PaymentLinkExpired` for each; returns how many it expired. This is
    /// what the scheduled `billing:expire-links` task runs, for gateways that
    /// send no expiry webhook.
    ///
    /// A pending link is expired only when its `expires_at` has passed, and
    /// the gateway is asked first (`payment_status`): a link it reports paid
    /// is marked succeeded instead, never expired. A gateway that cannot
    /// answer ([`Unsupported`]) leaves the link to expire by its date; one
    /// that fails, or is no longer set up, leaves it pending for the next
    /// sweep.
    pub async fn expire_links(state: &AppState) -> Result<usize> {
        let due = BillingPayment::where_eq("kind", PaymentKind::Link)
            .where_eq("status", PaymentStatus::Pending)
            .where_op("expires_at", "<=", renox::db::now())
            .order_by("id")
            .get(&state.db)
            .await?;
        let setup = crate::setup(state)?;
        let mut expired = 0;
        for link in due {
            let id = link.id;
            if link.gateway_id.is_some() {
                let asked = match setup.payment_gateway(state, Some(&link.gateway)) {
                    Ok(gateway) => gateway.payment_status(state, &link).await,
                    Err(err) => Err(err),
                };
                match asked {
                    Ok(reported) => crate::sync::apply_state(state, link, &reported).await?,
                    Err(err) if Unsupported::of(&err).is_some() => {}
                    Err(err) => {
                        tracing::warn!(error = ?err, payment = id, "billing: couldn't check a payment link before expiring it");
                        continue;
                    }
                }
            }
            let Some(link) = BillingPayment::find(&state.db, id).await? else {
                continue;
            };
            if link.status == PaymentStatus::Pending {
                crate::sync::apply_status(state, link, PaymentStatus::Expired, None).await?;
                expired += 1;
            }
        }
        Ok(expired)
    }

    /// Asks the gateway where the payment `payment_id` stands now (with the
    /// account it was made through), applies the answer to the row and its
    /// refunds the way a webhook would (a success always wins over failed
    /// or expired; a failure never undoes a success) and returns it.
    pub async fn payment_status(state: &AppState, payment_id: i64) -> Result<PaymentState> {
        let payment = BillingPayment::find(&state.db, payment_id)
            .await?
            .ok_or(Error::NotFound)?;
        let gateway = crate::setup(state)?.payment_gateway(state, Some(&payment.gateway))?;
        let reported = gateway.payment_status(state, &payment).await?;
        crate::sync::apply_state(state, payment, &reported).await?;
        Ok(reported)
    }
}

/// The builder methods `Charge` and `PaymentLink` share.
macro_rules! request_builder {
    ($name:ident) => {
        impl $name {
            /// Who pays; a payment link may have no owner.
            pub fn owner(mut self, owner: Owner) -> Self {
                self.request.owner = Some(owner);
                self
            }

            /// The app's record this pays for (`"job"`, `"42"`), kept on the
            /// payment and carried by its events.
            pub fn reference(mut self, kind: impl Into<String>, id: impl ToString) -> Self {
                self.request.reference_type = Some(kind.into());
                self.request.reference_id = Some(id.to_string());
                self
            }

            /// What the customer sees on the payment page.
            pub fn description(mut self, text: impl Into<String>) -> Self {
                self.request.description = Some(text.into());
                self
            }

            /// Where the gateway sends the customer after paying (absolute).
            pub fn success_url(mut self, url: impl Into<String>) -> Self {
                self.request.success_url = Some(url.into());
                self
            }

            /// Where it sends them when they give up (absolute).
            pub fn cancel_url(mut self, url: impl Into<String>) -> Self {
                self.request.cancel_url = Some(url.into());
                self
            }

            /// When the page stops working.
            pub fn expires_at(mut self, at: DateTime) -> Self {
                self.request.expires_at = Some(at);
                self
            }

            /// Uses this gateway (by name) instead of the default one.
            pub fn via(mut self, gateway: impl Into<String>) -> Self {
                self.gateway = Some(gateway.into());
                self
            }

            /// Uses this named account of the gateway; without it, the
            /// gateway's default account (and URLs) apply. The account is
            /// stored on the payment, so refunds, status checks and webhooks
            /// use the same account's keys.
            pub fn account(mut self, account: impl Into<String>) -> Self {
                self.request.account = Some(account.into());
                self
            }

            /// The gateway asked for, if [`via`](Self::via) named one.
            pub fn gateway(&self) -> Option<&str> {
                self.gateway.as_deref()
            }

            /// The request a [`Gateway`](crate::Gateway) gets.
            pub fn request(&self) -> &ChargeRequest {
                &self.request
            }
        }
    };
}

/// A one-off charge of an amount, built before it is handed to the
/// gateway. The customer pays on the gateway's page.
///
/// ```
/// use renox::money::{Currency, Money};
/// use renox_billing::payment::Charge;
///
/// let usd = Currency::parse("USD").unwrap();
/// let charge = Charge::new(Money::new(4_500, usd))
///     .reference("job", 42)
///     .description("Brake service")
///     .via("stripe")
///     .account("acme-llc");
/// assert_eq!(charge.gateway(), Some("stripe"));
/// assert_eq!(charge.request().account.as_deref(), Some("acme-llc"));
/// assert_eq!(charge.request().reference_id.as_deref(), Some("42"));
/// ```
#[derive(Debug, Clone)]
pub struct Charge {
    gateway: Option<String>,
    request: ChargeRequest,
}

impl Charge {
    /// A charge of `amount`.
    pub fn new(amount: Money) -> Self {
        Self {
            gateway: None,
            request: ChargeRequest::new(PaymentKind::Charge, amount),
        }
    }
}

request_builder!(Charge);

/// A payment link: a URL anyone can pay, with or without an owner.
///
/// ```
/// use renox::money::{Currency, Money};
/// use renox_billing::payment::PaymentLink;
///
/// let usd = Currency::parse("USD").unwrap();
/// let link = PaymentLink::new(Money::new(900, usd)).reference("invoice", "INV-7");
/// assert_eq!(link.request().reference_type.as_deref(), Some("invoice"));
/// assert!(link.gateway().is_none()); // the default gateway
/// ```
#[derive(Debug, Clone)]
pub struct PaymentLink {
    gateway: Option<String>,
    request: ChargeRequest,
}

impl PaymentLink {
    /// A link that collects `amount`.
    pub fn new(amount: Money) -> Self {
        Self {
            gateway: None,
            request: ChargeRequest::new(PaymentKind::Link, amount),
        }
    }
}

request_builder!(PaymentLink);

/// What [`Gateway::charge`](crate::Gateway::charge) and
/// [`Gateway::payment_link`](crate::Gateway::payment_link) get.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ChargeRequest {
    /// Charge or link.
    pub kind: PaymentKind,
    /// The row's id in `billing_payments` (0 before it is saved).
    pub payment_id: i64,
    /// How much.
    pub amount: Money,
    /// Who pays, if known.
    pub owner: Option<Owner>,
    /// The kind of the app's record this pays for.
    pub reference_type: Option<String>,
    /// That record's id.
    pub reference_id: Option<String>,
    /// What the customer sees.
    pub description: Option<String>,
    /// Where the customer goes after paying.
    pub success_url: Option<String>,
    /// Where the customer goes when they give up.
    pub cancel_url: Option<String>,
    /// When the page stops working.
    pub expires_at: Option<DateTime>,
    /// The gateway's account; `None` is its default account.
    pub account: Option<String>,
}

impl ChargeRequest {
    /// A request of `kind` for `amount`, with nothing else set.
    pub fn new(kind: PaymentKind, amount: Money) -> Self {
        Self {
            kind,
            payment_id: 0,
            amount,
            owner: None,
            reference_type: None,
            reference_id: None,
            description: None,
            success_url: None,
            cancel_url: None,
            expires_at: None,
            account: None,
        }
    }

    /// What the gateway stores with the payment so its webhooks find the
    /// row again: `renox_payment` (the row's id), plus `renox_billable`,
    /// `renox_reference_type` and `renox_reference_id` when set.
    pub fn metadata(&self) -> Vec<(&'static str, String)> {
        let mut meta = vec![("renox_payment", self.payment_id.to_string())];
        if let Some(owner) = &self.owner {
            meta.push(("renox_billable", owner.key()));
        }
        if let Some(kind) = &self.reference_type {
            meta.push(("renox_reference_type", kind.clone()));
        }
        if let Some(id) = &self.reference_id {
            meta.push(("renox_reference_id", id.clone()));
        }
        meta
    }
}

/// What a gateway answers to a charge or a link: where the customer pays.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct PaymentPage {
    /// The payment's id at the gateway.
    pub gateway_id: String,
    /// The page to send the customer to.
    pub url: Option<String>,
    /// When it stops working, if the gateway says.
    pub expires_at: Option<DateTime>,
}

impl PaymentPage {
    /// The payment `gateway_id` is paid at `url`.
    pub fn new(gateway_id: impl Into<String>, url: impl Into<String>) -> Self {
        Self {
            gateway_id: gateway_id.into(),
            url: Some(url.into()),
            expires_at: None,
        }
    }

    /// It stops working at `at`.
    pub fn expires_at(mut self, at: DateTime) -> Self {
        self.expires_at = Some(at);
        self
    }
}

/// A refund to ask for: the whole payment or part of it.
///
/// ```
/// use renox::money::{Currency, Money};
/// use renox_billing::payment::Refund;
///
/// let usd = Currency::parse("USD").unwrap();
/// let refund = Refund::new(Money::new(1_000, usd)).reason("duplicate");
/// assert_eq!(refund.amount.minor, 1_000);
/// assert_eq!(refund.reason.as_deref(), Some("duplicate"));
/// ```
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Refund {
    /// How much to give back.
    pub amount: Money,
    /// Why.
    pub reason: Option<String>,
}

impl Refund {
    /// A refund of `amount`.
    pub fn new(amount: Money) -> Self {
        Self {
            amount,
            reason: None,
        }
    }

    /// Says why.
    pub fn reason(mut self, reason: impl Into<String>) -> Self {
        self.reason = Some(reason.into());
        self
    }
}

/// What a gateway answers to [`Gateway::refund`](crate::Gateway::refund).
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct RefundResult {
    /// The refund's id at the gateway.
    pub gateway_id: Option<String>,
    /// [`RefundStatus::Pending`] unless the gateway refunded at once.
    pub status: RefundStatus,
}

impl RefundResult {
    /// The gateway took the refund and will finish it later.
    pub fn pending(gateway_id: impl Into<String>) -> Self {
        Self {
            gateway_id: Some(gateway_id.into()),
            status: RefundStatus::Pending,
        }
    }

    /// The gateway refunded at once.
    pub fn succeeded(gateway_id: impl Into<String>) -> Self {
        Self {
            gateway_id: Some(gateway_id.into()),
            status: RefundStatus::Succeeded,
        }
    }
}

/// A payment as the gateway reports it when asked
/// ([`Gateway::payment_status`](crate::Gateway::payment_status)).
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct PaymentState {
    /// Pending, succeeded, failed or expired (or a later state).
    pub status: PaymentStatus,
    /// When the gateway said so (unix seconds).
    pub at: Option<i64>,
    /// The state of its refunds the gateway knows.
    pub refunds: Vec<RefundUpdate>,
    /// The payment's second id at the gateway, when it says (see
    /// [`BillingPayment::gateway_intent`]).
    pub intent: Option<String>,
}

impl PaymentState {
    /// A payment in `status`.
    pub fn new(status: PaymentStatus) -> Self {
        Self {
            status,
            at: None,
            refunds: Vec::new(),
            intent: None,
        }
    }

    /// With the payment's second id at the gateway.
    pub fn intent(mut self, id: impl Into<String>) -> Self {
        self.intent = Some(id.into());
        self
    }

    /// The gateway said this at `unix_seconds`.
    pub fn at(mut self, unix_seconds: i64) -> Self {
        self.at = Some(unix_seconds);
        self
    }

    /// With the state of one of its refunds.
    pub fn refund(mut self, update: RefundUpdate) -> Self {
        self.refunds.push(update);
        self
    }
}

/// A webhook says a one-off payment changed.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct PaymentUpdate {
    /// The payment's id at the gateway.
    pub gateway_id: String,
    /// Its new status.
    pub status: PaymentStatus,
    /// When the gateway said so (unix seconds).
    pub at: Option<i64>,
    /// The payment's second id at the gateway, when the event names it.
    pub intent: Option<String>,
}

impl PaymentUpdate {
    /// Payment `gateway_id` is now `status`.
    pub fn new(gateway_id: impl Into<String>, status: PaymentStatus) -> Self {
        Self {
            gateway_id: gateway_id.into(),
            status,
            at: None,
            intent: None,
        }
    }

    /// The gateway said this at `unix_seconds`.
    pub fn at(mut self, unix_seconds: i64) -> Self {
        self.at = Some(unix_seconds);
        self
    }

    /// The payment's second id at the gateway.
    pub fn intent(mut self, id: impl Into<String>) -> Self {
        self.intent = Some(id.into());
        self
    }
}

/// A refund changed, from a webhook or a status check.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct RefundUpdate {
    /// The refund's id at the gateway.
    pub gateway_id: String,
    /// The payment's id at the gateway, when the event names it.
    pub payment_gateway_id: Option<String>,
    /// Where the refund is.
    pub status: RefundStatus,
    /// The amount, in the payment's currency, when the event says.
    pub amount: Option<i64>,
    /// Why it failed, or the reason given.
    pub reason: Option<String>,
    /// When the gateway said so (unix seconds).
    pub at: Option<i64>,
}

impl RefundUpdate {
    /// Refund `gateway_id` is now `status`.
    pub fn new(gateway_id: impl Into<String>, status: RefundStatus) -> Self {
        Self {
            gateway_id: gateway_id.into(),
            payment_gateway_id: None,
            status,
            amount: None,
            reason: None,
            at: None,
        }
    }

    /// Of the payment `id` at the gateway.
    pub fn payment(mut self, id: impl Into<String>) -> Self {
        self.payment_gateway_id = Some(id.into());
        self
    }

    /// For `minor` units of the payment's currency.
    pub fn amount(mut self, minor: i64) -> Self {
        self.amount = Some(minor);
        self
    }

    /// With this reason.
    pub fn reason(mut self, reason: impl Into<String>) -> Self {
        self.reason = Some(reason.into());
        self
    }

    /// The gateway said this at `unix_seconds`.
    pub fn at(mut self, unix_seconds: i64) -> Self {
        self.at = Some(unix_seconds);
        self
    }
}

/// How a dispute stands.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum DisputeState {
    /// The customer disputed the payment; evidence is due.
    Opened,
    /// Decided for the merchant.
    Won,
    /// Decided against the merchant.
    Lost,
}

/// A dispute opened or closed, from a webhook.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct DisputeUpdate {
    /// The disputed payment's id at the gateway.
    pub payment_gateway_id: String,
    /// How it stands.
    pub state: DisputeState,
    /// When the evidence is due, for an opened dispute.
    pub evidence_due_by: Option<DateTime>,
    /// When the gateway said so (unix seconds).
    pub at: Option<i64>,
}

impl DisputeUpdate {
    /// The dispute on payment `payment_gateway_id` is now `state`.
    pub fn new(payment_gateway_id: impl Into<String>, state: DisputeState) -> Self {
        Self {
            payment_gateway_id: payment_gateway_id.into(),
            state,
            evidence_due_by: None,
            at: None,
        }
    }

    /// The evidence is due by `at`.
    pub fn evidence_due_by(mut self, at: DateTime) -> Self {
        self.evidence_due_by = Some(at);
        self
    }

    /// The gateway said this at `unix_seconds`.
    pub fn at(mut self, unix_seconds: i64) -> Self {
        self.at = Some(unix_seconds);
        self
    }
}