delopay 0.20.0

Official Rust SDK for the DeloPay payments API
Documentation
//! The refund model.

use serde::Deserialize;
use serde_json::Value;

use crate::core::models::common::{ExternalRecordMarker, RefundStatus};

/// A refund as returned by the API.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct RefundResponse {
    /// Unique refund ID.
    pub refund_id: String,
    /// ID of the payment this refund belongs to.
    pub payment_id: String,
    /// Refunded amount in minor units.
    pub amount: i64,
    /// Three-letter ISO 4217 currency code.
    pub currency: String,
    /// Current status.
    pub status: RefundStatus,
    /// Connector that processed the refund.
    #[serde(default)]
    pub connector: Option<String>,
    /// Reason provided at creation.
    #[serde(default)]
    pub reason: Option<String>,
    /// Gateway error code if the refund failed.
    #[serde(default)]
    pub error_code: Option<String>,
    /// Human-readable error message if the refund failed.
    #[serde(default)]
    pub error_message: Option<String>,
    /// Shop (business profile) ID.
    #[serde(default)]
    pub profile_id: Option<String>,
    /// ISO 8601 creation timestamp.
    #[serde(default)]
    pub created_at: Option<String>,
    /// Arbitrary metadata.
    #[serde(default)]
    pub metadata: Option<Value>,
    /// The dashboard user who started the refund, by user ID.
    #[serde(default)]
    pub initiated_by: Option<String>,
    /// How the refund's funds were taken back across a split payment (open
    /// shape, per processor).
    #[serde(default)]
    pub split_refunds: Option<Value>,
    /// The marker of a refund recorded by hand rather than reported by the
    /// processor.
    #[serde(default)]
    pub external_record: Option<ExternalRecordMarker>,
    /// Whether the recorded refund has not been looked at yet.
    #[serde(default)]
    pub external_record_unread: bool,
    /// How the refund was credited to the payment's product lines.
    #[serde(default)]
    pub line_credits: Option<Vec<RefundLineCredit>>,
    /// The part of the amount credited to no line, in minor units.
    #[serde(default)]
    pub unallocated_amount: Option<i64>,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// What a refund credited to one product line.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct RefundLineCredit {
    /// The payment line's ID.
    pub line_id: String,
    /// The line's catalog product; `None` on an unattributed line.
    #[serde(default)]
    pub product_id: Option<String>,
    /// The line's name as the payment froze it.
    pub name: String,
    /// Units given back; `None` on an amount-only credit.
    #[serde(default)]
    pub quantity: Option<i32>,
    /// Money credited to the line, in minor units.
    pub amount: i64,
    /// Tax part of `amount`, when one was stated.
    #[serde(default)]
    pub tax_amount: Option<i64>,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn refund_response_decodes_line_credits_and_external_records() {
        let refund: RefundResponse = serde_json::from_str(
            r#"{"refund_id":"ref_1","payment_id":"pay_1","amount":750,"currency":"EUR",
                "status":"succeeded","initiated_by":"usr_1","external_record_unread":true,
                "external_record":{"recorded_by":"usr_1","recorded_at":"2026-09-01T00:00:00Z",
                    "reason":"refunded at the processor","created_by_recording":true,
                    "recording_count":1},
                "line_credits":[{"line_id":"pli_1","name":"Hat","quantity":1,"amount":750}],
                "unallocated_amount":0}"#,
        )
        .unwrap();
        assert_eq!(refund.initiated_by.as_deref(), Some("usr_1"));
        assert!(refund.external_record_unread);
        assert!(
            refund
                .external_record
                .as_ref()
                .unwrap()
                .created_by_recording
        );
        let credit = &refund.line_credits.as_ref().unwrap()[0];
        assert_eq!(credit.quantity, Some(1));
        assert!(credit.tax_amount.is_none());
        assert_eq!(refund.unallocated_amount, Some(0));
    }
}