stripe_shared/
refund.rs

1/// Refund objects allow you to refund a previously created charge that isn't
2/// refunded yet. Funds are refunded to the credit or debit card that's
3/// initially charged.
4///
5/// Related guide: [Refunds](https://stripe.com/docs/refunds)
6///
7/// For more details see <<https://stripe.com/docs/api/refunds/object>>.
8#[derive(Clone, Debug)]
9#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
10pub struct Refund {
11    /// Amount, in cents (or local equivalent).
12    pub amount: i64,
13    /// Balance transaction that describes the impact on your account balance.
14    pub balance_transaction: Option<stripe_types::Expandable<stripe_shared::BalanceTransaction>>,
15    /// ID of the charge that's refunded.
16    pub charge: Option<stripe_types::Expandable<stripe_shared::Charge>>,
17    /// Time at which the object was created. Measured in seconds since the Unix epoch.
18    pub created: stripe_types::Timestamp,
19    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
20    /// Must be a [supported currency](https://stripe.com/docs/currencies).
21    pub currency: stripe_types::Currency,
22    /// An arbitrary string attached to the object.
23    /// You can use this for displaying to users (available on non-card refunds only).
24    pub description: Option<String>,
25    pub destination_details: Option<stripe_shared::RefundDestinationDetails>,
26    /// After the refund fails, this balance transaction describes the adjustment made on your account balance that reverses the initial balance transaction.
27    pub failure_balance_transaction:
28        Option<stripe_types::Expandable<stripe_shared::BalanceTransaction>>,
29    /// Provides the reason for the refund failure.
30    /// Possible values are: `lost_or_stolen_card`, `expired_or_canceled_card`, `charge_for_pending_refund_disputed`, `insufficient_funds`, `declined`, `merchant_request`, or `unknown`.
31    pub failure_reason: Option<String>,
32    /// Unique identifier for the object.
33    pub id: stripe_shared::RefundId,
34    /// For payment methods without native refund support (for example, Konbini, PromptPay), provide an email address for the customer to receive refund instructions.
35    pub instructions_email: Option<String>,
36    /// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object.
37    /// This can be useful for storing additional information about the object in a structured format.
38    pub metadata: Option<std::collections::HashMap<String, String>>,
39    pub next_action: Option<stripe_shared::RefundNextAction>,
40    /// ID of the PaymentIntent that's refunded.
41    pub payment_intent: Option<stripe_types::Expandable<stripe_shared::PaymentIntent>>,
42    /// Provides the reason for why the refund is pending.
43    /// Possible values are: `processing`, `insufficient_funds`, or `charge_pending`.
44    pub pending_reason: Option<RefundPendingReason>,
45    pub presentment_details: Option<stripe_shared::PaymentFlowsPaymentIntentPresentmentDetails>,
46    /// Reason for the refund, which is either user-provided (`duplicate`, `fraudulent`, or `requested_by_customer`) or generated by Stripe internally (`expired_uncaptured_charge`).
47    pub reason: Option<RefundReason>,
48    /// This is the transaction number that appears on email receipts sent for this refund.
49    pub receipt_number: Option<String>,
50    /// The transfer reversal that's associated with the refund.
51    /// Only present if the charge came from another Stripe account.
52    pub source_transfer_reversal: Option<stripe_types::Expandable<stripe_shared::TransferReversal>>,
53    /// Status of the refund.
54    /// This can be `pending`, `requires_action`, `succeeded`, `failed`, or `canceled`.
55    /// Learn more about [failed refunds](https://stripe.com/docs/refunds#failed-refunds).
56    pub status: Option<String>,
57    /// This refers to the transfer reversal object if the accompanying transfer reverses.
58    /// This is only applicable if the charge was created using the destination parameter.
59    pub transfer_reversal: Option<stripe_types::Expandable<stripe_shared::TransferReversal>>,
60}
61#[doc(hidden)]
62pub struct RefundBuilder {
63    amount: Option<i64>,
64    balance_transaction:
65        Option<Option<stripe_types::Expandable<stripe_shared::BalanceTransaction>>>,
66    charge: Option<Option<stripe_types::Expandable<stripe_shared::Charge>>>,
67    created: Option<stripe_types::Timestamp>,
68    currency: Option<stripe_types::Currency>,
69    description: Option<Option<String>>,
70    destination_details: Option<Option<stripe_shared::RefundDestinationDetails>>,
71    failure_balance_transaction:
72        Option<Option<stripe_types::Expandable<stripe_shared::BalanceTransaction>>>,
73    failure_reason: Option<Option<String>>,
74    id: Option<stripe_shared::RefundId>,
75    instructions_email: Option<Option<String>>,
76    metadata: Option<Option<std::collections::HashMap<String, String>>>,
77    next_action: Option<Option<stripe_shared::RefundNextAction>>,
78    payment_intent: Option<Option<stripe_types::Expandable<stripe_shared::PaymentIntent>>>,
79    pending_reason: Option<Option<RefundPendingReason>>,
80    presentment_details: Option<Option<stripe_shared::PaymentFlowsPaymentIntentPresentmentDetails>>,
81    reason: Option<Option<RefundReason>>,
82    receipt_number: Option<Option<String>>,
83    source_transfer_reversal:
84        Option<Option<stripe_types::Expandable<stripe_shared::TransferReversal>>>,
85    status: Option<Option<String>>,
86    transfer_reversal: Option<Option<stripe_types::Expandable<stripe_shared::TransferReversal>>>,
87}
88
89#[allow(
90    unused_variables,
91    irrefutable_let_patterns,
92    clippy::let_unit_value,
93    clippy::match_single_binding,
94    clippy::single_match
95)]
96const _: () = {
97    use miniserde::de::{Map, Visitor};
98    use miniserde::json::Value;
99    use miniserde::{make_place, Deserialize, Result};
100    use stripe_types::miniserde_helpers::FromValueOpt;
101    use stripe_types::{MapBuilder, ObjectDeser};
102
103    make_place!(Place);
104
105    impl Deserialize for Refund {
106        fn begin(out: &mut Option<Self>) -> &mut dyn Visitor {
107            Place::new(out)
108        }
109    }
110
111    struct Builder<'a> {
112        out: &'a mut Option<Refund>,
113        builder: RefundBuilder,
114    }
115
116    impl Visitor for Place<Refund> {
117        fn map(&mut self) -> Result<Box<dyn Map + '_>> {
118            Ok(Box::new(Builder { out: &mut self.out, builder: RefundBuilder::deser_default() }))
119        }
120    }
121
122    impl MapBuilder for RefundBuilder {
123        type Out = Refund;
124        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
125            Ok(match k {
126                "amount" => Deserialize::begin(&mut self.amount),
127                "balance_transaction" => Deserialize::begin(&mut self.balance_transaction),
128                "charge" => Deserialize::begin(&mut self.charge),
129                "created" => Deserialize::begin(&mut self.created),
130                "currency" => Deserialize::begin(&mut self.currency),
131                "description" => Deserialize::begin(&mut self.description),
132                "destination_details" => Deserialize::begin(&mut self.destination_details),
133                "failure_balance_transaction" => {
134                    Deserialize::begin(&mut self.failure_balance_transaction)
135                }
136                "failure_reason" => Deserialize::begin(&mut self.failure_reason),
137                "id" => Deserialize::begin(&mut self.id),
138                "instructions_email" => Deserialize::begin(&mut self.instructions_email),
139                "metadata" => Deserialize::begin(&mut self.metadata),
140                "next_action" => Deserialize::begin(&mut self.next_action),
141                "payment_intent" => Deserialize::begin(&mut self.payment_intent),
142                "pending_reason" => Deserialize::begin(&mut self.pending_reason),
143                "presentment_details" => Deserialize::begin(&mut self.presentment_details),
144                "reason" => Deserialize::begin(&mut self.reason),
145                "receipt_number" => Deserialize::begin(&mut self.receipt_number),
146                "source_transfer_reversal" => {
147                    Deserialize::begin(&mut self.source_transfer_reversal)
148                }
149                "status" => Deserialize::begin(&mut self.status),
150                "transfer_reversal" => Deserialize::begin(&mut self.transfer_reversal),
151
152                _ => <dyn Visitor>::ignore(),
153            })
154        }
155
156        fn deser_default() -> Self {
157            Self {
158                amount: Deserialize::default(),
159                balance_transaction: Deserialize::default(),
160                charge: Deserialize::default(),
161                created: Deserialize::default(),
162                currency: Deserialize::default(),
163                description: Deserialize::default(),
164                destination_details: Deserialize::default(),
165                failure_balance_transaction: Deserialize::default(),
166                failure_reason: Deserialize::default(),
167                id: Deserialize::default(),
168                instructions_email: Deserialize::default(),
169                metadata: Deserialize::default(),
170                next_action: Deserialize::default(),
171                payment_intent: Deserialize::default(),
172                pending_reason: Deserialize::default(),
173                presentment_details: Deserialize::default(),
174                reason: Deserialize::default(),
175                receipt_number: Deserialize::default(),
176                source_transfer_reversal: Deserialize::default(),
177                status: Deserialize::default(),
178                transfer_reversal: Deserialize::default(),
179            }
180        }
181
182        fn take_out(&mut self) -> Option<Self::Out> {
183            let (
184                Some(amount),
185                Some(balance_transaction),
186                Some(charge),
187                Some(created),
188                Some(currency),
189                Some(description),
190                Some(destination_details),
191                Some(failure_balance_transaction),
192                Some(failure_reason),
193                Some(id),
194                Some(instructions_email),
195                Some(metadata),
196                Some(next_action),
197                Some(payment_intent),
198                Some(pending_reason),
199                Some(presentment_details),
200                Some(reason),
201                Some(receipt_number),
202                Some(source_transfer_reversal),
203                Some(status),
204                Some(transfer_reversal),
205            ) = (
206                self.amount,
207                self.balance_transaction.take(),
208                self.charge.take(),
209                self.created,
210                self.currency,
211                self.description.take(),
212                self.destination_details.take(),
213                self.failure_balance_transaction.take(),
214                self.failure_reason.take(),
215                self.id.take(),
216                self.instructions_email.take(),
217                self.metadata.take(),
218                self.next_action.take(),
219                self.payment_intent.take(),
220                self.pending_reason,
221                self.presentment_details,
222                self.reason,
223                self.receipt_number.take(),
224                self.source_transfer_reversal.take(),
225                self.status.take(),
226                self.transfer_reversal.take(),
227            )
228            else {
229                return None;
230            };
231            Some(Self::Out {
232                amount,
233                balance_transaction,
234                charge,
235                created,
236                currency,
237                description,
238                destination_details,
239                failure_balance_transaction,
240                failure_reason,
241                id,
242                instructions_email,
243                metadata,
244                next_action,
245                payment_intent,
246                pending_reason,
247                presentment_details,
248                reason,
249                receipt_number,
250                source_transfer_reversal,
251                status,
252                transfer_reversal,
253            })
254        }
255    }
256
257    impl<'a> Map for Builder<'a> {
258        fn key(&mut self, k: &str) -> Result<&mut dyn Visitor> {
259            self.builder.key(k)
260        }
261
262        fn finish(&mut self) -> Result<()> {
263            *self.out = self.builder.take_out();
264            Ok(())
265        }
266    }
267
268    impl ObjectDeser for Refund {
269        type Builder = RefundBuilder;
270    }
271
272    impl FromValueOpt for Refund {
273        fn from_value(v: Value) -> Option<Self> {
274            let Value::Object(obj) = v else {
275                return None;
276            };
277            let mut b = RefundBuilder::deser_default();
278            for (k, v) in obj {
279                match k.as_str() {
280                    "amount" => b.amount = FromValueOpt::from_value(v),
281                    "balance_transaction" => b.balance_transaction = FromValueOpt::from_value(v),
282                    "charge" => b.charge = FromValueOpt::from_value(v),
283                    "created" => b.created = FromValueOpt::from_value(v),
284                    "currency" => b.currency = FromValueOpt::from_value(v),
285                    "description" => b.description = FromValueOpt::from_value(v),
286                    "destination_details" => b.destination_details = FromValueOpt::from_value(v),
287                    "failure_balance_transaction" => {
288                        b.failure_balance_transaction = FromValueOpt::from_value(v)
289                    }
290                    "failure_reason" => b.failure_reason = FromValueOpt::from_value(v),
291                    "id" => b.id = FromValueOpt::from_value(v),
292                    "instructions_email" => b.instructions_email = FromValueOpt::from_value(v),
293                    "metadata" => b.metadata = FromValueOpt::from_value(v),
294                    "next_action" => b.next_action = FromValueOpt::from_value(v),
295                    "payment_intent" => b.payment_intent = FromValueOpt::from_value(v),
296                    "pending_reason" => b.pending_reason = FromValueOpt::from_value(v),
297                    "presentment_details" => b.presentment_details = FromValueOpt::from_value(v),
298                    "reason" => b.reason = FromValueOpt::from_value(v),
299                    "receipt_number" => b.receipt_number = FromValueOpt::from_value(v),
300                    "source_transfer_reversal" => {
301                        b.source_transfer_reversal = FromValueOpt::from_value(v)
302                    }
303                    "status" => b.status = FromValueOpt::from_value(v),
304                    "transfer_reversal" => b.transfer_reversal = FromValueOpt::from_value(v),
305
306                    _ => {}
307                }
308            }
309            b.take_out()
310        }
311    }
312};
313#[cfg(feature = "serialize")]
314impl serde::Serialize for Refund {
315    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
316        use serde::ser::SerializeStruct;
317        let mut s = s.serialize_struct("Refund", 22)?;
318        s.serialize_field("amount", &self.amount)?;
319        s.serialize_field("balance_transaction", &self.balance_transaction)?;
320        s.serialize_field("charge", &self.charge)?;
321        s.serialize_field("created", &self.created)?;
322        s.serialize_field("currency", &self.currency)?;
323        s.serialize_field("description", &self.description)?;
324        s.serialize_field("destination_details", &self.destination_details)?;
325        s.serialize_field("failure_balance_transaction", &self.failure_balance_transaction)?;
326        s.serialize_field("failure_reason", &self.failure_reason)?;
327        s.serialize_field("id", &self.id)?;
328        s.serialize_field("instructions_email", &self.instructions_email)?;
329        s.serialize_field("metadata", &self.metadata)?;
330        s.serialize_field("next_action", &self.next_action)?;
331        s.serialize_field("payment_intent", &self.payment_intent)?;
332        s.serialize_field("pending_reason", &self.pending_reason)?;
333        s.serialize_field("presentment_details", &self.presentment_details)?;
334        s.serialize_field("reason", &self.reason)?;
335        s.serialize_field("receipt_number", &self.receipt_number)?;
336        s.serialize_field("source_transfer_reversal", &self.source_transfer_reversal)?;
337        s.serialize_field("status", &self.status)?;
338        s.serialize_field("transfer_reversal", &self.transfer_reversal)?;
339
340        s.serialize_field("object", "refund")?;
341        s.end()
342    }
343}
344/// Provides the reason for why the refund is pending.
345/// Possible values are: `processing`, `insufficient_funds`, or `charge_pending`.
346#[derive(Copy, Clone, Eq, PartialEq)]
347pub enum RefundPendingReason {
348    ChargePending,
349    InsufficientFunds,
350    Processing,
351}
352impl RefundPendingReason {
353    pub fn as_str(self) -> &'static str {
354        use RefundPendingReason::*;
355        match self {
356            ChargePending => "charge_pending",
357            InsufficientFunds => "insufficient_funds",
358            Processing => "processing",
359        }
360    }
361}
362
363impl std::str::FromStr for RefundPendingReason {
364    type Err = stripe_types::StripeParseError;
365    fn from_str(s: &str) -> Result<Self, Self::Err> {
366        use RefundPendingReason::*;
367        match s {
368            "charge_pending" => Ok(ChargePending),
369            "insufficient_funds" => Ok(InsufficientFunds),
370            "processing" => Ok(Processing),
371            _ => Err(stripe_types::StripeParseError),
372        }
373    }
374}
375impl std::fmt::Display for RefundPendingReason {
376    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
377        f.write_str(self.as_str())
378    }
379}
380
381impl std::fmt::Debug for RefundPendingReason {
382    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
383        f.write_str(self.as_str())
384    }
385}
386#[cfg(feature = "serialize")]
387impl serde::Serialize for RefundPendingReason {
388    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
389    where
390        S: serde::Serializer,
391    {
392        serializer.serialize_str(self.as_str())
393    }
394}
395impl miniserde::Deserialize for RefundPendingReason {
396    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
397        crate::Place::new(out)
398    }
399}
400
401impl miniserde::de::Visitor for crate::Place<RefundPendingReason> {
402    fn string(&mut self, s: &str) -> miniserde::Result<()> {
403        use std::str::FromStr;
404        self.out = Some(RefundPendingReason::from_str(s).map_err(|_| miniserde::Error)?);
405        Ok(())
406    }
407}
408
409stripe_types::impl_from_val_with_from_str!(RefundPendingReason);
410#[cfg(feature = "deserialize")]
411impl<'de> serde::Deserialize<'de> for RefundPendingReason {
412    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
413        use std::str::FromStr;
414        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
415        Self::from_str(&s)
416            .map_err(|_| serde::de::Error::custom("Unknown value for RefundPendingReason"))
417    }
418}
419/// Reason for the refund, which is either user-provided (`duplicate`, `fraudulent`, or `requested_by_customer`) or generated by Stripe internally (`expired_uncaptured_charge`).
420#[derive(Copy, Clone, Eq, PartialEq)]
421pub enum RefundReason {
422    Duplicate,
423    ExpiredUncapturedCharge,
424    Fraudulent,
425    RequestedByCustomer,
426}
427impl RefundReason {
428    pub fn as_str(self) -> &'static str {
429        use RefundReason::*;
430        match self {
431            Duplicate => "duplicate",
432            ExpiredUncapturedCharge => "expired_uncaptured_charge",
433            Fraudulent => "fraudulent",
434            RequestedByCustomer => "requested_by_customer",
435        }
436    }
437}
438
439impl std::str::FromStr for RefundReason {
440    type Err = stripe_types::StripeParseError;
441    fn from_str(s: &str) -> Result<Self, Self::Err> {
442        use RefundReason::*;
443        match s {
444            "duplicate" => Ok(Duplicate),
445            "expired_uncaptured_charge" => Ok(ExpiredUncapturedCharge),
446            "fraudulent" => Ok(Fraudulent),
447            "requested_by_customer" => Ok(RequestedByCustomer),
448            _ => Err(stripe_types::StripeParseError),
449        }
450    }
451}
452impl std::fmt::Display for RefundReason {
453    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
454        f.write_str(self.as_str())
455    }
456}
457
458impl std::fmt::Debug for RefundReason {
459    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
460        f.write_str(self.as_str())
461    }
462}
463#[cfg(feature = "serialize")]
464impl serde::Serialize for RefundReason {
465    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
466    where
467        S: serde::Serializer,
468    {
469        serializer.serialize_str(self.as_str())
470    }
471}
472impl miniserde::Deserialize for RefundReason {
473    fn begin(out: &mut Option<Self>) -> &mut dyn miniserde::de::Visitor {
474        crate::Place::new(out)
475    }
476}
477
478impl miniserde::de::Visitor for crate::Place<RefundReason> {
479    fn string(&mut self, s: &str) -> miniserde::Result<()> {
480        use std::str::FromStr;
481        self.out = Some(RefundReason::from_str(s).map_err(|_| miniserde::Error)?);
482        Ok(())
483    }
484}
485
486stripe_types::impl_from_val_with_from_str!(RefundReason);
487#[cfg(feature = "deserialize")]
488impl<'de> serde::Deserialize<'de> for RefundReason {
489    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
490        use std::str::FromStr;
491        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
492        Self::from_str(&s).map_err(|_| serde::de::Error::custom("Unknown value for RefundReason"))
493    }
494}
495impl stripe_types::Object for Refund {
496    type Id = stripe_shared::RefundId;
497    fn id(&self) -> &Self::Id {
498        &self.id
499    }
500
501    fn into_id(self) -> Self::Id {
502        self.id
503    }
504}
505stripe_types::def_id!(RefundId);