use serde::{Deserialize, Serialize};
use crate::client::RazorpayClient;
use crate::error::RazorpayError;
use crate::resources::common::{Notes, NotesUpdate};
use crate::util::pagination::{Collection, ListOptions};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RefundSpeed {
Normal,
Optimum,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RefundState {
Pending,
Processed,
Failed,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct Refund {
pub id: String,
#[serde(default)]
pub entity: String,
pub amount: i64,
pub currency: String,
pub payment_id: String,
#[serde(default)]
pub status: Option<RefundState>,
#[serde(default)]
pub speed_requested: Option<RefundSpeed>,
#[serde(default)]
pub speed_processed: Option<RefundSpeed>,
#[serde(default)]
pub receipt: Option<String>,
#[serde(default)]
pub acquirer_data: Option<serde_json::Value>,
#[serde(default)]
pub batch_id: Option<String>,
#[serde(default)]
pub notes: Notes,
pub created_at: i64,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize)]
pub struct CreateRefundParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub speed: Option<RefundSpeed>,
#[serde(skip_serializing_if = "Option::is_none")]
pub receipt: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub notes: Option<Notes>,
}
impl CreateRefundParams {
pub fn full() -> Self {
Self::default()
}
pub fn partial(amount: i64) -> Self {
Self {
amount: Some(amount),
..Self::default()
}
}
pub fn speed(mut self, speed: RefundSpeed) -> Self {
self.speed = Some(speed);
self
}
pub fn receipt(mut self, receipt: impl Into<String>) -> Self {
self.receipt = Some(receipt.into());
self
}
pub fn notes(mut self, notes: Notes) -> Self {
self.notes = Some(notes);
self
}
}
pub struct RefundsClient<'a> {
pub(crate) client: &'a RazorpayClient,
}
impl<'a> RefundsClient<'a> {
pub async fn fetch(&self, id: &str) -> Result<Refund, RazorpayError> {
self.client.get::<(), _>(&format!("refunds/{id}"), None).await
}
pub async fn all(&self, options: ListOptions) -> Result<Collection<Refund>, RazorpayError> {
self.client.get("refunds", Some(&options)).await
}
pub async fn edit(&self, id: &str, notes: Notes) -> Result<Refund, RazorpayError> {
self.client
.patch(&format!("refunds/{id}"), Some(&NotesUpdate::new(notes)))
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn full_refund_omits_amount() {
assert_eq!(serde_json::to_string(&CreateRefundParams::full()).unwrap(), "{}");
}
#[test]
fn partial_refund_sends_amount() {
let json = serde_json::to_string(&CreateRefundParams::partial(500)).unwrap();
assert_eq!(json, r#"{"amount":500}"#);
}
#[test]
fn speed_serializes_snake_case() {
let json =
serde_json::to_string(&CreateRefundParams::full().speed(RefundSpeed::Optimum)).unwrap();
assert_eq!(json, r#"{"speed":"optimum"}"#);
}
}