stripe_core/refund/
requests.rs

1use stripe_client_core::{
2    RequestBuilder, StripeBlockingClient, StripeClient, StripeMethod, StripeRequest,
3};
4
5#[derive(Clone, Debug, serde::Serialize)]
6struct ListRefundBuilder {
7    #[serde(skip_serializing_if = "Option::is_none")]
8    charge: Option<String>,
9    #[serde(skip_serializing_if = "Option::is_none")]
10    created: Option<stripe_types::RangeQueryTs>,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    ending_before: Option<String>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    expand: Option<Vec<String>>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    limit: Option<i64>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    payment_intent: Option<String>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    starting_after: Option<String>,
21}
22impl ListRefundBuilder {
23    fn new() -> Self {
24        Self {
25            charge: None,
26            created: None,
27            ending_before: None,
28            expand: None,
29            limit: None,
30            payment_intent: None,
31            starting_after: None,
32        }
33    }
34}
35/// Returns a list of all refunds you created.
36/// We return the refunds in sorted order, with the most recent refunds appearing first.
37/// The 10 most recent refunds are always available by default on the Charge object.
38#[derive(Clone, Debug, serde::Serialize)]
39pub struct ListRefund {
40    inner: ListRefundBuilder,
41}
42impl ListRefund {
43    /// Construct a new `ListRefund`.
44    pub fn new() -> Self {
45        Self { inner: ListRefundBuilder::new() }
46    }
47    /// Only return refunds for the charge specified by this charge ID.
48    pub fn charge(mut self, charge: impl Into<String>) -> Self {
49        self.inner.charge = Some(charge.into());
50        self
51    }
52    /// Only return refunds that were created during the given date interval.
53    pub fn created(mut self, created: impl Into<stripe_types::RangeQueryTs>) -> Self {
54        self.inner.created = Some(created.into());
55        self
56    }
57    /// A cursor for use in pagination.
58    /// `ending_before` is an object ID that defines your place in the list.
59    /// For instance, if you make a list request and receive 100 objects, starting with `obj_bar`, your subsequent call can include `ending_before=obj_bar` in order to fetch the previous page of the list.
60    pub fn ending_before(mut self, ending_before: impl Into<String>) -> Self {
61        self.inner.ending_before = Some(ending_before.into());
62        self
63    }
64    /// Specifies which fields in the response should be expanded.
65    pub fn expand(mut self, expand: impl Into<Vec<String>>) -> Self {
66        self.inner.expand = Some(expand.into());
67        self
68    }
69    /// A limit on the number of objects to be returned.
70    /// Limit can range between 1 and 100, and the default is 10.
71    pub fn limit(mut self, limit: impl Into<i64>) -> Self {
72        self.inner.limit = Some(limit.into());
73        self
74    }
75    /// Only return refunds for the PaymentIntent specified by this ID.
76    pub fn payment_intent(mut self, payment_intent: impl Into<String>) -> Self {
77        self.inner.payment_intent = Some(payment_intent.into());
78        self
79    }
80    /// A cursor for use in pagination.
81    /// `starting_after` is an object ID that defines your place in the list.
82    /// For instance, if you make a list request and receive 100 objects, ending with `obj_foo`, your subsequent call can include `starting_after=obj_foo` in order to fetch the next page of the list.
83    pub fn starting_after(mut self, starting_after: impl Into<String>) -> Self {
84        self.inner.starting_after = Some(starting_after.into());
85        self
86    }
87}
88impl Default for ListRefund {
89    fn default() -> Self {
90        Self::new()
91    }
92}
93impl ListRefund {
94    /// Send the request and return the deserialized response.
95    pub async fn send<C: StripeClient>(
96        &self,
97        client: &C,
98    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
99        self.customize().send(client).await
100    }
101
102    /// Send the request and return the deserialized response, blocking until completion.
103    pub fn send_blocking<C: StripeBlockingClient>(
104        &self,
105        client: &C,
106    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
107        self.customize().send_blocking(client)
108    }
109
110    pub fn paginate(
111        &self,
112    ) -> stripe_client_core::ListPaginator<stripe_types::List<stripe_shared::Refund>> {
113        stripe_client_core::ListPaginator::new_list("/refunds", &self.inner)
114    }
115}
116
117impl StripeRequest for ListRefund {
118    type Output = stripe_types::List<stripe_shared::Refund>;
119
120    fn build(&self) -> RequestBuilder {
121        RequestBuilder::new(StripeMethod::Get, "/refunds").query(&self.inner)
122    }
123}
124#[derive(Clone, Debug, serde::Serialize)]
125struct RetrieveRefundBuilder {
126    #[serde(skip_serializing_if = "Option::is_none")]
127    expand: Option<Vec<String>>,
128}
129impl RetrieveRefundBuilder {
130    fn new() -> Self {
131        Self { expand: None }
132    }
133}
134/// Retrieves the details of an existing refund.
135#[derive(Clone, Debug, serde::Serialize)]
136pub struct RetrieveRefund {
137    inner: RetrieveRefundBuilder,
138    refund: stripe_shared::RefundId,
139}
140impl RetrieveRefund {
141    /// Construct a new `RetrieveRefund`.
142    pub fn new(refund: impl Into<stripe_shared::RefundId>) -> Self {
143        Self { refund: refund.into(), inner: RetrieveRefundBuilder::new() }
144    }
145    /// Specifies which fields in the response should be expanded.
146    pub fn expand(mut self, expand: impl Into<Vec<String>>) -> Self {
147        self.inner.expand = Some(expand.into());
148        self
149    }
150}
151impl RetrieveRefund {
152    /// Send the request and return the deserialized response.
153    pub async fn send<C: StripeClient>(
154        &self,
155        client: &C,
156    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
157        self.customize().send(client).await
158    }
159
160    /// Send the request and return the deserialized response, blocking until completion.
161    pub fn send_blocking<C: StripeBlockingClient>(
162        &self,
163        client: &C,
164    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
165        self.customize().send_blocking(client)
166    }
167}
168
169impl StripeRequest for RetrieveRefund {
170    type Output = stripe_shared::Refund;
171
172    fn build(&self) -> RequestBuilder {
173        let refund = &self.refund;
174        RequestBuilder::new(StripeMethod::Get, format!("/refunds/{refund}")).query(&self.inner)
175    }
176}
177#[derive(Clone, Debug, serde::Serialize)]
178struct CreateRefundBuilder {
179    #[serde(skip_serializing_if = "Option::is_none")]
180    amount: Option<i64>,
181    #[serde(skip_serializing_if = "Option::is_none")]
182    charge: Option<String>,
183    #[serde(skip_serializing_if = "Option::is_none")]
184    currency: Option<stripe_types::Currency>,
185    #[serde(skip_serializing_if = "Option::is_none")]
186    customer: Option<String>,
187    #[serde(skip_serializing_if = "Option::is_none")]
188    expand: Option<Vec<String>>,
189    #[serde(skip_serializing_if = "Option::is_none")]
190    instructions_email: Option<String>,
191    #[serde(skip_serializing_if = "Option::is_none")]
192    metadata: Option<std::collections::HashMap<String, String>>,
193    #[serde(skip_serializing_if = "Option::is_none")]
194    origin: Option<CreateRefundOrigin>,
195    #[serde(skip_serializing_if = "Option::is_none")]
196    payment_intent: Option<String>,
197    #[serde(skip_serializing_if = "Option::is_none")]
198    reason: Option<CreateRefundReason>,
199    #[serde(skip_serializing_if = "Option::is_none")]
200    refund_application_fee: Option<bool>,
201    #[serde(skip_serializing_if = "Option::is_none")]
202    reverse_transfer: Option<bool>,
203}
204impl CreateRefundBuilder {
205    fn new() -> Self {
206        Self {
207            amount: None,
208            charge: None,
209            currency: None,
210            customer: None,
211            expand: None,
212            instructions_email: None,
213            metadata: None,
214            origin: None,
215            payment_intent: None,
216            reason: None,
217            refund_application_fee: None,
218            reverse_transfer: None,
219        }
220    }
221}
222/// Origin of the refund
223#[derive(Copy, Clone, Eq, PartialEq)]
224pub enum CreateRefundOrigin {
225    CustomerBalance,
226}
227impl CreateRefundOrigin {
228    pub fn as_str(self) -> &'static str {
229        use CreateRefundOrigin::*;
230        match self {
231            CustomerBalance => "customer_balance",
232        }
233    }
234}
235
236impl std::str::FromStr for CreateRefundOrigin {
237    type Err = stripe_types::StripeParseError;
238    fn from_str(s: &str) -> Result<Self, Self::Err> {
239        use CreateRefundOrigin::*;
240        match s {
241            "customer_balance" => Ok(CustomerBalance),
242            _ => Err(stripe_types::StripeParseError),
243        }
244    }
245}
246impl std::fmt::Display for CreateRefundOrigin {
247    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
248        f.write_str(self.as_str())
249    }
250}
251
252impl std::fmt::Debug for CreateRefundOrigin {
253    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
254        f.write_str(self.as_str())
255    }
256}
257impl serde::Serialize for CreateRefundOrigin {
258    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
259    where
260        S: serde::Serializer,
261    {
262        serializer.serialize_str(self.as_str())
263    }
264}
265#[cfg(feature = "deserialize")]
266impl<'de> serde::Deserialize<'de> for CreateRefundOrigin {
267    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
268        use std::str::FromStr;
269        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
270        Self::from_str(&s)
271            .map_err(|_| serde::de::Error::custom("Unknown value for CreateRefundOrigin"))
272    }
273}
274/// String indicating the reason for the refund.
275/// If set, possible values are `duplicate`, `fraudulent`, and `requested_by_customer`.
276/// If you believe the charge to be fraudulent, specifying `fraudulent` as the reason will add the associated card and email to your [block lists](https://stripe.com/docs/radar/lists), and will also help us improve our fraud detection algorithms.
277#[derive(Copy, Clone, Eq, PartialEq)]
278pub enum CreateRefundReason {
279    Duplicate,
280    Fraudulent,
281    RequestedByCustomer,
282}
283impl CreateRefundReason {
284    pub fn as_str(self) -> &'static str {
285        use CreateRefundReason::*;
286        match self {
287            Duplicate => "duplicate",
288            Fraudulent => "fraudulent",
289            RequestedByCustomer => "requested_by_customer",
290        }
291    }
292}
293
294impl std::str::FromStr for CreateRefundReason {
295    type Err = stripe_types::StripeParseError;
296    fn from_str(s: &str) -> Result<Self, Self::Err> {
297        use CreateRefundReason::*;
298        match s {
299            "duplicate" => Ok(Duplicate),
300            "fraudulent" => Ok(Fraudulent),
301            "requested_by_customer" => Ok(RequestedByCustomer),
302            _ => Err(stripe_types::StripeParseError),
303        }
304    }
305}
306impl std::fmt::Display for CreateRefundReason {
307    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
308        f.write_str(self.as_str())
309    }
310}
311
312impl std::fmt::Debug for CreateRefundReason {
313    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
314        f.write_str(self.as_str())
315    }
316}
317impl serde::Serialize for CreateRefundReason {
318    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
319    where
320        S: serde::Serializer,
321    {
322        serializer.serialize_str(self.as_str())
323    }
324}
325#[cfg(feature = "deserialize")]
326impl<'de> serde::Deserialize<'de> for CreateRefundReason {
327    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
328        use std::str::FromStr;
329        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
330        Self::from_str(&s)
331            .map_err(|_| serde::de::Error::custom("Unknown value for CreateRefundReason"))
332    }
333}
334/// When you create a new refund, you must specify a Charge or a PaymentIntent object on which to create it.
335///
336/// Creating a new refund will refund a charge that has previously been created but not yet refunded.
337/// Funds will be refunded to the credit or debit card that was originally charged.
338///
339/// You can optionally refund only part of a charge.
340/// You can do so multiple times, until the entire charge has been refunded.
341///
342/// Once entirely refunded, a charge can’t be refunded again.
343/// This method will raise an error when called on an already-refunded charge,
344/// or when trying to refund more money than is left on a charge.
345#[derive(Clone, Debug, serde::Serialize)]
346pub struct CreateRefund {
347    inner: CreateRefundBuilder,
348}
349impl CreateRefund {
350    /// Construct a new `CreateRefund`.
351    pub fn new() -> Self {
352        Self { inner: CreateRefundBuilder::new() }
353    }
354    pub fn amount(mut self, amount: impl Into<i64>) -> Self {
355        self.inner.amount = Some(amount.into());
356        self
357    }
358    /// The identifier of the charge to refund.
359    pub fn charge(mut self, charge: impl Into<String>) -> Self {
360        self.inner.charge = Some(charge.into());
361        self
362    }
363    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
364    /// Must be a [supported currency](https://stripe.com/docs/currencies).
365    pub fn currency(mut self, currency: impl Into<stripe_types::Currency>) -> Self {
366        self.inner.currency = Some(currency.into());
367        self
368    }
369    /// Customer whose customer balance to refund from.
370    pub fn customer(mut self, customer: impl Into<String>) -> Self {
371        self.inner.customer = Some(customer.into());
372        self
373    }
374    /// Specifies which fields in the response should be expanded.
375    pub fn expand(mut self, expand: impl Into<Vec<String>>) -> Self {
376        self.inner.expand = Some(expand.into());
377        self
378    }
379    /// For payment methods without native refund support (e.g., Konbini, PromptPay), use this email from the customer to receive refund instructions.
380    pub fn instructions_email(mut self, instructions_email: impl Into<String>) -> Self {
381        self.inner.instructions_email = Some(instructions_email.into());
382        self
383    }
384    /// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object.
385    /// This can be useful for storing additional information about the object in a structured format.
386    /// Individual keys can be unset by posting an empty value to them.
387    /// All keys can be unset by posting an empty value to `metadata`.
388    pub fn metadata(
389        mut self,
390        metadata: impl Into<std::collections::HashMap<String, String>>,
391    ) -> Self {
392        self.inner.metadata = Some(metadata.into());
393        self
394    }
395    /// Origin of the refund
396    pub fn origin(mut self, origin: impl Into<CreateRefundOrigin>) -> Self {
397        self.inner.origin = Some(origin.into());
398        self
399    }
400    /// The identifier of the PaymentIntent to refund.
401    pub fn payment_intent(mut self, payment_intent: impl Into<String>) -> Self {
402        self.inner.payment_intent = Some(payment_intent.into());
403        self
404    }
405    /// String indicating the reason for the refund.
406    /// If set, possible values are `duplicate`, `fraudulent`, and `requested_by_customer`.
407    /// If you believe the charge to be fraudulent, specifying `fraudulent` as the reason will add the associated card and email to your [block lists](https://stripe.com/docs/radar/lists), and will also help us improve our fraud detection algorithms.
408    pub fn reason(mut self, reason: impl Into<CreateRefundReason>) -> Self {
409        self.inner.reason = Some(reason.into());
410        self
411    }
412    /// Boolean indicating whether the application fee should be refunded when refunding this charge.
413    /// If a full charge refund is given, the full application fee will be refunded.
414    /// Otherwise, the application fee will be refunded in an amount proportional to the amount of the charge refunded.
415    /// An application fee can be refunded only by the application that created the charge.
416    pub fn refund_application_fee(mut self, refund_application_fee: impl Into<bool>) -> Self {
417        self.inner.refund_application_fee = Some(refund_application_fee.into());
418        self
419    }
420    /// Boolean indicating whether the transfer should be reversed when refunding this charge.
421    /// The transfer will be reversed proportionally to the amount being refunded (either the entire or partial amount).
422    ///
423    /// A transfer can be reversed only by the application that created the charge.
424    pub fn reverse_transfer(mut self, reverse_transfer: impl Into<bool>) -> Self {
425        self.inner.reverse_transfer = Some(reverse_transfer.into());
426        self
427    }
428}
429impl Default for CreateRefund {
430    fn default() -> Self {
431        Self::new()
432    }
433}
434impl CreateRefund {
435    /// Send the request and return the deserialized response.
436    pub async fn send<C: StripeClient>(
437        &self,
438        client: &C,
439    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
440        self.customize().send(client).await
441    }
442
443    /// Send the request and return the deserialized response, blocking until completion.
444    pub fn send_blocking<C: StripeBlockingClient>(
445        &self,
446        client: &C,
447    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
448        self.customize().send_blocking(client)
449    }
450}
451
452impl StripeRequest for CreateRefund {
453    type Output = stripe_shared::Refund;
454
455    fn build(&self) -> RequestBuilder {
456        RequestBuilder::new(StripeMethod::Post, "/refunds").form(&self.inner)
457    }
458}
459#[derive(Clone, Debug, serde::Serialize)]
460struct UpdateRefundBuilder {
461    #[serde(skip_serializing_if = "Option::is_none")]
462    expand: Option<Vec<String>>,
463    #[serde(skip_serializing_if = "Option::is_none")]
464    metadata: Option<std::collections::HashMap<String, String>>,
465}
466impl UpdateRefundBuilder {
467    fn new() -> Self {
468        Self { expand: None, metadata: None }
469    }
470}
471/// Updates the refund that you specify by setting the values of the passed parameters.
472/// Any parameters that you don’t provide remain unchanged.
473///
474/// This request only accepts `metadata` as an argument.
475#[derive(Clone, Debug, serde::Serialize)]
476pub struct UpdateRefund {
477    inner: UpdateRefundBuilder,
478    refund: stripe_shared::RefundId,
479}
480impl UpdateRefund {
481    /// Construct a new `UpdateRefund`.
482    pub fn new(refund: impl Into<stripe_shared::RefundId>) -> Self {
483        Self { refund: refund.into(), inner: UpdateRefundBuilder::new() }
484    }
485    /// Specifies which fields in the response should be expanded.
486    pub fn expand(mut self, expand: impl Into<Vec<String>>) -> Self {
487        self.inner.expand = Some(expand.into());
488        self
489    }
490    /// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object.
491    /// This can be useful for storing additional information about the object in a structured format.
492    /// Individual keys can be unset by posting an empty value to them.
493    /// All keys can be unset by posting an empty value to `metadata`.
494    pub fn metadata(
495        mut self,
496        metadata: impl Into<std::collections::HashMap<String, String>>,
497    ) -> Self {
498        self.inner.metadata = Some(metadata.into());
499        self
500    }
501}
502impl UpdateRefund {
503    /// Send the request and return the deserialized response.
504    pub async fn send<C: StripeClient>(
505        &self,
506        client: &C,
507    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
508        self.customize().send(client).await
509    }
510
511    /// Send the request and return the deserialized response, blocking until completion.
512    pub fn send_blocking<C: StripeBlockingClient>(
513        &self,
514        client: &C,
515    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
516        self.customize().send_blocking(client)
517    }
518}
519
520impl StripeRequest for UpdateRefund {
521    type Output = stripe_shared::Refund;
522
523    fn build(&self) -> RequestBuilder {
524        let refund = &self.refund;
525        RequestBuilder::new(StripeMethod::Post, format!("/refunds/{refund}")).form(&self.inner)
526    }
527}
528#[derive(Clone, Debug, serde::Serialize)]
529struct CancelRefundBuilder {
530    #[serde(skip_serializing_if = "Option::is_none")]
531    expand: Option<Vec<String>>,
532}
533impl CancelRefundBuilder {
534    fn new() -> Self {
535        Self { expand: None }
536    }
537}
538/// Cancels a refund with a status of `requires_action`.
539///
540/// You can’t cancel refunds in other states.
541/// Only refunds for payment methods that require customer action can enter the `requires_action` state.
542#[derive(Clone, Debug, serde::Serialize)]
543pub struct CancelRefund {
544    inner: CancelRefundBuilder,
545    refund: stripe_shared::RefundId,
546}
547impl CancelRefund {
548    /// Construct a new `CancelRefund`.
549    pub fn new(refund: impl Into<stripe_shared::RefundId>) -> Self {
550        Self { refund: refund.into(), inner: CancelRefundBuilder::new() }
551    }
552    /// Specifies which fields in the response should be expanded.
553    pub fn expand(mut self, expand: impl Into<Vec<String>>) -> Self {
554        self.inner.expand = Some(expand.into());
555        self
556    }
557}
558impl CancelRefund {
559    /// Send the request and return the deserialized response.
560    pub async fn send<C: StripeClient>(
561        &self,
562        client: &C,
563    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
564        self.customize().send(client).await
565    }
566
567    /// Send the request and return the deserialized response, blocking until completion.
568    pub fn send_blocking<C: StripeBlockingClient>(
569        &self,
570        client: &C,
571    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
572        self.customize().send_blocking(client)
573    }
574}
575
576impl StripeRequest for CancelRefund {
577    type Output = stripe_shared::Refund;
578
579    fn build(&self) -> RequestBuilder {
580        let refund = &self.refund;
581        RequestBuilder::new(StripeMethod::Post, format!("/refunds/{refund}/cancel"))
582            .form(&self.inner)
583    }
584}
585#[derive(Clone, Debug, serde::Serialize)]
586struct ExpireRefundBuilder {
587    #[serde(skip_serializing_if = "Option::is_none")]
588    expand: Option<Vec<String>>,
589}
590impl ExpireRefundBuilder {
591    fn new() -> Self {
592        Self { expand: None }
593    }
594}
595/// Expire a refund with a status of `requires_action`.
596#[derive(Clone, Debug, serde::Serialize)]
597pub struct ExpireRefund {
598    inner: ExpireRefundBuilder,
599    refund: String,
600}
601impl ExpireRefund {
602    /// Construct a new `ExpireRefund`.
603    pub fn new(refund: impl Into<String>) -> Self {
604        Self { refund: refund.into(), inner: ExpireRefundBuilder::new() }
605    }
606    /// Specifies which fields in the response should be expanded.
607    pub fn expand(mut self, expand: impl Into<Vec<String>>) -> Self {
608        self.inner.expand = Some(expand.into());
609        self
610    }
611}
612impl ExpireRefund {
613    /// Send the request and return the deserialized response.
614    pub async fn send<C: StripeClient>(
615        &self,
616        client: &C,
617    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
618        self.customize().send(client).await
619    }
620
621    /// Send the request and return the deserialized response, blocking until completion.
622    pub fn send_blocking<C: StripeBlockingClient>(
623        &self,
624        client: &C,
625    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
626        self.customize().send_blocking(client)
627    }
628}
629
630impl StripeRequest for ExpireRefund {
631    type Output = stripe_shared::Refund;
632
633    fn build(&self) -> RequestBuilder {
634        let refund = &self.refund;
635        RequestBuilder::new(StripeMethod::Post, format!("/test_helpers/refunds/{refund}/expire"))
636            .form(&self.inner)
637    }
638}