Skip to main content

stripe_pay_core/charge/
impl.rs

1use super::*;
2
3impl ChargeStatus {
4    /// Return the wire string Stripe uses for this status.
5    ///
6    /// # Returns
7    ///
8    /// - `&'static str` - the `status` value in a Stripe response.
9    pub const fn as_str(&self) -> &'static str {
10        match self {
11            ChargeStatus::Pending => CHARGE_STATUS_PENDING,
12            ChargeStatus::Succeeded => CHARGE_STATUS_SUCCEEDED,
13            ChargeStatus::Authorized => CHARGE_STATUS_AUTHORIZED,
14            ChargeStatus::Failed => CHARGE_STATUS_FAILED,
15        }
16    }
17
18    /// Return whether the charge moved money.
19    ///
20    /// An authorised charge has reserved funds but not taken them, so
21    /// it is deliberately not counted as captured.
22    ///
23    /// # Returns
24    ///
25    /// - `bool` - `true` only for a succeeded charge.
26    pub const fn is_captured(&self) -> bool {
27        matches!(self, ChargeStatus::Succeeded)
28    }
29}
30
31impl Display for ChargeStatus {
32    /// Render the wire string.
33    ///
34    /// # Arguments
35    ///
36    /// - `&mut Formatter<'_>` - the formatter to write the value into.
37    ///
38    /// # Returns
39    ///
40    /// A `Formatter` writing the status as Stripe spells it.
41    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
42        write!(formatter, "{}", self.as_str())
43    }
44}
45
46impl PaymentMethodKind {
47    /// Return the wire string Stripe uses for this method kind.
48    ///
49    /// # Returns
50    ///
51    /// - `&'static str` - the `type` value in a Stripe response.
52    pub const fn as_str(&self) -> &'static str {
53        match self {
54            PaymentMethodKind::Card => METHOD_KIND_CARD,
55            PaymentMethodKind::SepaDebit => METHOD_KIND_SEPA_DEBIT,
56            PaymentMethodKind::BankTransfer => METHOD_KIND_BANK_TRANSFER,
57            PaymentMethodKind::Wallet => METHOD_KIND_WALLET,
58        }
59    }
60}
61
62impl Charge {
63    /// Build a Charge from its wire fields.
64    ///
65    /// # Arguments
66    ///
67    /// - `String` - Stripe's identifier for the charge.
68    /// - `Money` - the amount captured.
69    /// - `ChargeStatus` - whether the charge moved money.
70    /// - `PaymentMethodKind` - how the customer paid.
71    ///
72    /// # Returns
73    ///
74    /// - `Self` - the assembled charge.
75    pub fn new(
76        id: String,
77        amount: Money,
78        status: ChargeStatus,
79        payment_method_kind: PaymentMethodKind,
80    ) -> Self {
81        Self {
82            id,
83            amount: amount.get_amount(),
84            currency: String::from(amount.currency_code()),
85            status,
86            payment_method_kind,
87            disputed: false,
88            payment_intent: None,
89        }
90    }
91
92    /// Return the amount this charge captured.
93    ///
94    /// # Returns
95    ///
96    /// - `Result<Money, StripeParseError>` - the amount, or an error
97    ///   when the response carried a currency this crate does not model.
98    pub fn get_amount(&self) -> Result<Money, StripeParseError> {
99        let currency: Currency = self.currency.parse()?;
100        Ok(Money::from_minor(self.amount, currency))
101    }
102
103    /// Return whether the charge is under dispute.
104    ///
105    /// # Returns
106    ///
107    /// - `bool` - `true` once a customer has challenged the charge.
108    pub fn is_disputed(&self) -> bool {
109        self.get_disputed()
110    }
111
112    /// Return whether this charge actually moved the customer's money.
113    ///
114    /// An authorised charge has reserved funds but not taken them, so
115    /// it is deliberately not counted as captured.
116    ///
117    /// # Returns
118    ///
119    /// - `bool` - `true` only for a succeeded charge.
120    pub fn is_captured(&self) -> bool {
121        self.get_status().is_captured()
122    }
123}