razorpay_api/resources/
plans.rs1use serde::{Deserialize, Serialize};
4
5use crate::client::RazorpayClient;
6use crate::error::RazorpayError;
7use crate::resources::common::Notes;
8use crate::resources::items::Item;
9use crate::util::pagination::{Collection, ListOptions};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
13#[serde(rename_all = "snake_case")]
14pub enum PlanPeriod {
15 Daily,
17 Weekly,
19 Monthly,
21 Yearly,
23 #[serde(other)]
25 Unknown,
26}
27
28#[derive(Debug, Clone, PartialEq, Deserialize)]
30pub struct Plan {
31 pub id: String,
33 #[serde(default)]
35 pub entity: String,
36 pub period: PlanPeriod,
38 pub interval: u32,
41 pub item: Item,
43 #[serde(default)]
45 pub notes: Notes,
46 #[serde(default)]
48 pub created_at: i64,
49}
50
51#[derive(Debug, Clone, PartialEq, Serialize)]
53pub struct CreatePlanParams {
54 pub period: PlanPeriod,
56 pub interval: u32,
58 pub item: PlanItem,
60 #[serde(skip_serializing_if = "Option::is_none")]
62 pub notes: Option<Notes>,
63}
64
65#[derive(Debug, Clone, PartialEq, Serialize)]
69pub struct PlanItem {
70 pub name: String,
72 pub amount: i64,
74 pub currency: String,
76 #[serde(skip_serializing_if = "Option::is_none")]
78 pub description: Option<String>,
79}
80
81impl CreatePlanParams {
82 pub fn new(
98 period: PlanPeriod,
99 interval: u32,
100 name: impl Into<String>,
101 amount: i64,
102 currency: impl Into<String>,
103 ) -> Self {
104 Self {
105 period,
106 interval,
107 item: PlanItem {
108 name: name.into(),
109 amount,
110 currency: currency.into(),
111 description: None,
112 },
113 notes: None,
114 }
115 }
116
117 pub fn description(mut self, description: impl Into<String>) -> Self {
119 self.item.description = Some(description.into());
120 self
121 }
122
123 pub fn notes(mut self, notes: Notes) -> Self {
125 self.notes = Some(notes);
126 self
127 }
128}
129
130pub struct PlansClient<'a> {
135 pub(crate) client: &'a RazorpayClient,
136}
137
138impl<'a> PlansClient<'a> {
139 pub async fn create(&self, params: CreatePlanParams) -> Result<Plan, RazorpayError> {
141 self.client.post("plans", Some(¶ms)).await
142 }
143
144 pub async fn fetch(&self, id: &str) -> Result<Plan, RazorpayError> {
146 self.client.get::<(), _>(&format!("plans/{id}"), None).await
147 }
148
149 pub async fn all(&self, options: ListOptions) -> Result<Collection<Plan>, RazorpayError> {
151 self.client.get("plans", Some(&options)).await
152 }
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158
159 #[test]
160 fn plan_serializes_with_nested_item() {
161 let json =
162 serde_json::to_string(&CreatePlanParams::new(PlanPeriod::Monthly, 1, "Pro", 100, "INR"))
163 .unwrap();
164 assert_eq!(
165 json,
166 r#"{"period":"monthly","interval":1,"item":{"name":"Pro","amount":100,"currency":"INR"}}"#
167 );
168 }
169
170 #[test]
171 fn unknown_period_decodes_to_fallback() {
172 let plan: Plan = serde_json::from_str(
173 r#"{"id":"plan_1","period":"fortnightly","interval":1,
174 "item":{"id":"item_1","name":"x","amount":1,"currency":"INR"}}"#,
175 )
176 .unwrap();
177 assert_eq!(plan.period, PlanPeriod::Unknown);
178 }
179}