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
use crate::config::{Client, Response};
use crate::params::{Identifiable, List, Metadata, RangeQuery, Timestamp};
use crate::resources::Currency;
use serde_derive::{Deserialize, Serialize};

/// An enum representing the possible values of a `Refund`'s `reason` field.
///
/// For more details see [https://stripe.com/docs/api/refunds/object#refund_object-reason](https://stripe.com/docs/api/refunds/object#refund_object-reason)
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Copy, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RefundReason {
    Duplicate,
    Fraudulent,
    RequestedByCustomer,

    /// A variant not yet supported by the library.
    /// It is an error to send `Other` as part of a request.
    #[serde(other, skip_serializing)]
    Other,
}

/// An enum representing the possible values of a `Refund`'s `status` field.
///
/// For more details see [https://stripe.com/docs/api/refunds/object#refund_object-status](https://stripe.com/docs/api/refunds/object#refund_object-status)
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RefundStatus {
    Pending,
    Succeeded,
    Failed,
    Canceled,

    /// A variant not yet supported by the library.
    /// It is an error to send `Other` as part of a request.
    #[serde(other, skip_serializing)]
    Other,
}

/// An enum representing the possible values of a `Refund`'s `failure_reason` field.
///
/// For more details see [https://stripe.com/docs/api/refunds/object#refund_object-failure_reason](https://stripe.com/docs/api/refunds/object#refund_object-failure_reason)
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RefundFailureReason {
    LostOrStolenCard,
    ExpiredOrCanceledCard,
    Unknown,

    /// A variant not yet supported by the library.
    /// It is an error to send `Other` as part of a request.
    #[serde(other, skip_serializing)]
    Other,
}

/// The set of parameters that can be used when creating refund object.
///
/// For more details see [https://stripe.com/docs/api/refunds/create](https://stripe.com/docs/api/refunds/create).
#[derive(Clone, Debug, Default, Serialize)]
pub struct RefundParams<'a> {
    pub charge: &'a str,
    pub amount: Option<u64>,
    pub metadata: Metadata,
    pub reason: Option<RefundReason>,
    pub refund_application_fee: Option<bool>,
    pub reverse_transfer: Option<bool>,
}

/// The set of parameters that can be used when listing refund.
///
/// For more details see [https://stripe.com/docs/api/refunds/list](https://stripe.com/docs/api/refunds/list)
#[derive(Clone, Debug, Default, Serialize)]
pub struct RefundListParams<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub charge: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created: Option<RangeQuery<Timestamp>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ending_before: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub starting_after: Option<&'a str>,
}

/// The resource representing a Stripe refund.
///
/// For more details see https://stripe.com/docs/api#refunds.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Refund {
    pub id: String,
    pub object: String,
    pub amount: u64,
    pub balance_transaction: String,
    pub charge: String,
    pub created: Timestamp,
    pub currency: Currency,
    pub failure_balance_transaction: Option<String>,
    pub failure_reason: Option<RefundFailureReason>,
    pub metadata: Metadata,
    pub reason: Option<RefundReason>,
    pub receipt_number: Option<String>,
    pub status: RefundStatus,
}

impl Refund {
    /// Creates a new refund.
    ///
    /// For more details see [https://stripe.com/docs/api/refunds/create](https://stripe.com/docs/api/refunds/create).
    pub fn create(client: &Client, params: RefundParams<'_>) -> Response<Refund> {
        client.post_form("/refunds", params)
    }

    /// Retrieves the details of a refund.
    ///
    /// For more details see [https://stripe.com/docs/api/refunds/retrieve](https://stripe.com/docs/api/refunds/retrieve).
    pub fn retrieve(client: &Client, refund_id: &str) -> Response<Refund> {
        client.get(&format!("/refunds/{}", refund_id))
    }

    /// Updates a refund's properties.
    ///
    /// For more details see [https://stripe.com/docs/api/refunds/update](https://stripe.com/docs/api/refunds/update).
    pub fn update(
        client: &Client,
        refund_id: &str,
        metadata: Option<Metadata>,
    ) -> Response<Refund> {
        client.post_form(&format!("/refunds/{}", refund_id), metadata)
    }

    /// List all refunds.
    ///
    /// For more details see [https://stripe.com/docs/api#list_refunds](https://stripe.com/docs/api#list_refunds).
    pub fn list(client: &Client, params: RefundListParams<'_>) -> Response<List<Refund>> {
        client.get_query("/refunds", &params)
    }
}

impl Identifiable for Refund {
    fn id(&self) -> &str {
        &self.id
    }
}