stripe_pay_core/refund/impl.rs
1use super::*;
2
3impl RefundReason {
4 /// Return the wire string Stripe uses for this reason.
5 ///
6 /// # Returns
7 ///
8 /// - `&'static str` - the `reason` value in a Stripe request.
9 pub const fn as_str(&self) -> &'static str {
10 match self {
11 RefundReason::RequestedByCustomer => REASON_REQUESTED_BY_CUSTOMER,
12 RefundReason::Duplicate => REASON_DUPLICATE,
13 RefundReason::Fraudulent => REASON_FRAUDULENT,
14 RefundReason::OrderCancellation => REASON_ORDER_CANCELLATION,
15 }
16 }
17
18 /// Return whether the reason marks an involuntary return.
19 ///
20 /// # Returns
21 ///
22 /// - `bool` - `true` for reasons the merchant did not choose.
23 pub const fn is_involuntary(&self) -> bool {
24 matches!(self, RefundReason::Duplicate | RefundReason::Fraudulent)
25 }
26}
27
28impl Display for RefundReason {
29 /// Render the wire string.
30 ///
31 /// # Arguments
32 ///
33 /// - `&mut Formatter<'_>` - the formatter to write the value into.
34 ///
35 /// # Returns
36 ///
37 /// A `Formatter` writing the reason as Stripe spells it.
38 fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
39 write!(formatter, "{}", self.as_str())
40 }
41}
42
43impl Refund {
44 /// Build a Refund from its wire fields.
45 ///
46 /// # Arguments
47 ///
48 /// - `String` - Stripe's identifier for the refund.
49 /// - `String` - the charge being refunded.
50 /// - `Money` - the amount returned.
51 /// - `RefundReason` - why the money was returned.
52 ///
53 /// # Returns
54 ///
55 /// - `Self` - the assembled refund.
56 pub fn new(id: String, charge: String, amount: Money, reason: RefundReason) -> Self {
57 Self {
58 id,
59 charge,
60 amount: amount.get_amount(),
61 currency: String::from(amount.currency_code()),
62 reason,
63 succeeded: true,
64 }
65 }
66
67 /// Return the amount this refund returns.
68 ///
69 /// # Returns
70 ///
71 /// - `Result<Money, StripeParseError>` - the amount, or an error
72 /// when the response carried a currency this crate does not model.
73 pub fn get_amount(&self) -> Result<Money, StripeParseError> {
74 let currency: Currency = self.currency.parse()?;
75 Ok(Money::from_minor(self.amount, currency))
76 }
77
78 /// Return whether Stripe has fully processed this refund.
79 ///
80 /// # Returns
81 ///
82 /// - `bool` - `true` once the money is on its way back.
83 pub fn is_succeeded(&self) -> bool {
84 self.get_succeeded()
85 }
86
87 /// Return whether the merchant chose this reason themselves.
88 ///
89 /// # Returns
90 ///
91 /// - `bool` - `true` for reasons the merchant did not pick.
92 pub fn is_involuntary(&self) -> bool {
93 self.get_reason().is_involuntary()
94 }
95}