razorpay_api/resources/
refunds.rs1use serde::{Deserialize, Serialize};
4
5use crate::client::RazorpayClient;
6use crate::error::RazorpayError;
7use crate::resources::common::{Notes, NotesUpdate};
8use crate::util::pagination::{Collection, ListOptions};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(rename_all = "snake_case")]
13pub enum RefundSpeed {
14 Normal,
16 Optimum,
18 #[serde(other)]
20 Unknown,
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
25#[serde(rename_all = "snake_case")]
26pub enum RefundState {
27 Pending,
29 Processed,
31 Failed,
33 #[serde(other)]
35 Unknown,
36}
37
38#[derive(Debug, Clone, PartialEq, Deserialize)]
40pub struct Refund {
41 pub id: String,
43 #[serde(default)]
45 pub entity: String,
46 pub amount: i64,
48 pub currency: String,
50 pub payment_id: String,
52 #[serde(default)]
54 pub status: Option<RefundState>,
55 #[serde(default)]
57 pub speed_requested: Option<RefundSpeed>,
58 #[serde(default)]
60 pub speed_processed: Option<RefundSpeed>,
61 #[serde(default)]
63 pub receipt: Option<String>,
64 #[serde(default)]
66 pub acquirer_data: Option<serde_json::Value>,
67 #[serde(default)]
69 pub batch_id: Option<String>,
70 #[serde(default)]
72 pub notes: Notes,
73 pub created_at: i64,
75}
76
77#[derive(Debug, Clone, Default, PartialEq, Serialize)]
82pub struct CreateRefundParams {
83 #[serde(skip_serializing_if = "Option::is_none")]
85 pub amount: Option<i64>,
86 #[serde(skip_serializing_if = "Option::is_none")]
88 pub speed: Option<RefundSpeed>,
89 #[serde(skip_serializing_if = "Option::is_none")]
91 pub receipt: Option<String>,
92 #[serde(skip_serializing_if = "Option::is_none")]
94 pub notes: Option<Notes>,
95}
96
97impl CreateRefundParams {
98 pub fn full() -> Self {
105 Self::default()
106 }
107
108 pub fn partial(amount: i64) -> Self {
115 Self {
116 amount: Some(amount),
117 ..Self::default()
118 }
119 }
120
121 pub fn speed(mut self, speed: RefundSpeed) -> Self {
123 self.speed = Some(speed);
124 self
125 }
126
127 pub fn receipt(mut self, receipt: impl Into<String>) -> Self {
129 self.receipt = Some(receipt.into());
130 self
131 }
132
133 pub fn notes(mut self, notes: Notes) -> Self {
135 self.notes = Some(notes);
136 self
137 }
138}
139
140pub struct RefundsClient<'a> {
145 pub(crate) client: &'a RazorpayClient,
146}
147
148impl<'a> RefundsClient<'a> {
149 pub async fn fetch(&self, id: &str) -> Result<Refund, RazorpayError> {
151 self.client.get::<(), _>(&format!("refunds/{id}"), None).await
152 }
153
154 pub async fn all(&self, options: ListOptions) -> Result<Collection<Refund>, RazorpayError> {
156 self.client.get("refunds", Some(&options)).await
157 }
158
159 pub async fn edit(&self, id: &str, notes: Notes) -> Result<Refund, RazorpayError> {
161 self.client
162 .patch(&format!("refunds/{id}"), Some(&NotesUpdate::new(notes)))
163 .await
164 }
165}
166
167#[cfg(test)]
168mod tests {
169 use super::*;
170
171 #[test]
172 fn full_refund_omits_amount() {
173 assert_eq!(serde_json::to_string(&CreateRefundParams::full()).unwrap(), "{}");
174 }
175
176 #[test]
177 fn partial_refund_sends_amount() {
178 let json = serde_json::to_string(&CreateRefundParams::partial(500)).unwrap();
179 assert_eq!(json, r#"{"amount":500}"#);
180 }
181
182 #[test]
183 fn speed_serializes_snake_case() {
184 let json =
185 serde_json::to_string(&CreateRefundParams::full().speed(RefundSpeed::Optimum)).unwrap();
186 assert_eq!(json, r#"{"speed":"optimum"}"#);
187 }
188}