Skip to main content

razorpay_api/resources/
cards.rs

1//! Cards — read-only details of the instrument behind a card payment.
2
3use serde::Deserialize;
4
5use crate::client::RazorpayClient;
6use crate::error::RazorpayError;
7
8/// Card details.
9///
10/// Only the last four digits and network metadata are ever exposed — Razorpay
11/// never returns a full card number.
12#[derive(Debug, Clone, PartialEq, Deserialize)]
13pub struct Card {
14    /// Unique identifier, e.g. `card_JXPULjlR3DjqLp`.
15    #[serde(default)]
16    pub id: Option<String>,
17    /// Always `"card"`.
18    #[serde(default)]
19    pub entity: String,
20    /// Name printed on the card.
21    #[serde(default)]
22    pub name: Option<String>,
23    /// Last four digits.
24    #[serde(default)]
25    pub last4: Option<String>,
26    /// Card network, e.g. `Visa`, `MasterCard`, `RuPay`.
27    #[serde(default)]
28    pub network: Option<String>,
29    /// `credit`, `debit`, or `prepaid`.
30    #[serde(rename = "type", default)]
31    pub card_type: Option<String>,
32    /// Issuing bank code.
33    #[serde(default)]
34    pub issuer: Option<String>,
35    /// Whether the card is enrolled for international payments.
36    #[serde(default)]
37    pub international: bool,
38    /// Whether the card supports EMI.
39    #[serde(default)]
40    pub emi: bool,
41    /// Whether the card can be used for subscriptions.
42    #[serde(default)]
43    pub sub_type: Option<String>,
44    /// First six digits, when Razorpay exposes them.
45    #[serde(default)]
46    pub iin: Option<String>,
47    /// Expiry month, 1–12.
48    #[serde(default)]
49    pub expiry_month: Option<u32>,
50    /// Expiry year, four digits.
51    #[serde(default)]
52    pub expiry_year: Option<u32>,
53}
54
55/// Card endpoints. Obtain one from [`RazorpayClient::cards`].
56pub struct CardsClient<'a> {
57    pub(crate) client: &'a RazorpayClient,
58}
59
60impl<'a> CardsClient<'a> {
61    /// Fetch card details by id — `GET /v1/cards/{id}`.
62    ///
63    /// The id comes from [`Payment::card_id`](crate::resources::payments::Payment::card_id).
64    pub async fn fetch(&self, id: &str) -> Result<Card, RazorpayError> {
65        self.client.get::<(), _>(&format!("cards/{id}"), None).await
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn type_field_is_renamed_from_reserved_word() {
75        let card: Card = serde_json::from_str(r#"{"id":"card_1","type":"credit","last4":"1111"}"#).unwrap();
76        assert_eq!(card.card_type.as_deref(), Some("credit"));
77        assert_eq!(card.last4.as_deref(), Some("1111"));
78    }
79}