rain_sdk/api/payments.rs
1//! Payments API
2//!
3//! This module provides functionality to initiate payments.
4
5use crate::client::RainClient;
6use crate::error::Result;
7use crate::models::payments::*;
8use uuid::Uuid;
9
10impl RainClient {
11 /// Initiate a payment for a company
12 ///
13 /// # Arguments
14 ///
15 /// * `company_id` - The unique identifier of the company
16 /// * `request` - The payment initiation request
17 ///
18 /// # Returns
19 ///
20 /// Returns an [`InitiatePaymentResponse`] containing the payment address.
21 ///
22 /// # Errors
23 ///
24 /// This method can return the following errors:
25 /// - `400` - Invalid request
26 /// - `401` - Invalid authorization
27 /// - `404` - Company not found
28 /// - `423` - User address is locked
29 /// - `500` - Internal server error
30 ///
31 /// # Examples
32 ///
33 /// ```no_run
34 /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
35 /// use rain_sdk::models::payments::InitiatePaymentRequest;
36 /// use uuid::Uuid;
37 ///
38 /// # #[cfg(feature = "async")]
39 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
40 /// let config = Config::new(Environment::Dev);
41 /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
42 /// let client = RainClient::new(config, auth)?;
43 ///
44 /// let company_id = Uuid::new_v4();
45 /// let request = InitiatePaymentRequest {
46 /// amount: 10000, // $100.00 in cents
47 /// wallet_address: "0x1234...".to_string(),
48 /// chain_id: Some(1), // Ethereum mainnet
49 /// };
50 /// let response = client.initiate_company_payment(&company_id, &request).await?;
51 /// println!("Payment address: {}", response.address);
52 /// # Ok(())
53 /// # }
54 /// ```
55 #[cfg(feature = "async")]
56 pub async fn initiate_company_payment(
57 &self,
58 company_id: &Uuid,
59 request: &InitiatePaymentRequest,
60 ) -> Result<InitiatePaymentResponse> {
61 let path = format!("/companies/{company_id}/payments");
62 self.post(&path, request).await
63 }
64
65 /// Initiate a payment for an authorized user tenant
66 ///
67 /// # Arguments
68 ///
69 /// * `request` - The payment initiation request
70 ///
71 /// # Returns
72 ///
73 /// Returns an [`InitiatePaymentResponse`] containing the payment address.
74 ///
75 /// # Errors
76 ///
77 /// This method can return the following errors:
78 /// - `400` - Invalid request
79 /// - `401` - Invalid authorization
80 /// - `423` - User address is locked
81 /// - `500` - Internal server error
82 ///
83 /// # Examples
84 ///
85 /// ```no_run
86 /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
87 /// use rain_sdk::models::payments::InitiatePaymentRequest;
88 ///
89 /// # #[cfg(feature = "async")]
90 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
91 /// let config = Config::new(Environment::Dev);
92 /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
93 /// let client = RainClient::new(config, auth)?;
94 ///
95 /// let request = InitiatePaymentRequest {
96 /// amount: 5000, // $50.00 in cents
97 /// wallet_address: "0x5678...".to_string(),
98 /// chain_id: Some(137), // Polygon
99 /// };
100 /// let response = client.initiate_payment(&request).await?;
101 /// # Ok(())
102 /// # }
103 /// ```
104 #[cfg(feature = "async")]
105 pub async fn initiate_payment(
106 &self,
107 request: &InitiatePaymentRequest,
108 ) -> Result<InitiatePaymentResponse> {
109 let path = "/payments";
110 self.post(path, request).await
111 }
112
113 /// Initiate a payment for a user
114 ///
115 /// # Arguments
116 ///
117 /// * `user_id` - The unique identifier of the user
118 /// * `request` - The payment initiation request
119 ///
120 /// # Returns
121 ///
122 /// Returns an [`InitiatePaymentResponse`] containing the payment address.
123 ///
124 /// # Errors
125 ///
126 /// This method can return the following errors:
127 /// - `400` - Invalid request
128 /// - `401` - Invalid authorization
129 /// - `404` - User not found
130 /// - `423` - User address is locked
131 /// - `500` - Internal server error
132 ///
133 /// # Examples
134 ///
135 /// ```no_run
136 /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
137 /// use rain_sdk::models::payments::InitiatePaymentRequest;
138 /// use uuid::Uuid;
139 ///
140 /// # #[cfg(feature = "async")]
141 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
142 /// let config = Config::new(Environment::Dev);
143 /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
144 /// let client = RainClient::new(config, auth)?;
145 ///
146 /// let user_id = Uuid::new_v4();
147 /// let request = InitiatePaymentRequest {
148 /// amount: 2500, // $25.00 in cents
149 /// wallet_address: "0xabcd...".to_string(),
150 /// chain_id: Some(1),
151 /// };
152 /// let response = client.initiate_user_payment(&user_id, &request).await?;
153 /// # Ok(())
154 /// # }
155 /// ```
156 #[cfg(feature = "async")]
157 pub async fn initiate_user_payment(
158 &self,
159 user_id: &Uuid,
160 request: &InitiatePaymentRequest,
161 ) -> Result<InitiatePaymentResponse> {
162 let path = format!("/users/{user_id}/payments");
163 self.post(&path, request).await
164 }
165
166 // ============================================================================
167 // Blocking Methods
168 // ============================================================================
169
170 /// Initiate a payment for a company (blocking)
171 #[cfg(feature = "sync")]
172 pub fn initiate_company_payment_blocking(
173 &self,
174 company_id: &Uuid,
175 request: &InitiatePaymentRequest,
176 ) -> Result<InitiatePaymentResponse> {
177 let path = format!("/companies/{company_id}/payments");
178 self.post_blocking(&path, request)
179 }
180
181 /// Initiate a payment for an authorized user tenant (blocking)
182 #[cfg(feature = "sync")]
183 pub fn initiate_payment_blocking(
184 &self,
185 request: &InitiatePaymentRequest,
186 ) -> Result<InitiatePaymentResponse> {
187 let path = "/payments";
188 self.post_blocking(path, request)
189 }
190
191 /// Initiate a payment for a user (blocking)
192 #[cfg(feature = "sync")]
193 pub fn initiate_user_payment_blocking(
194 &self,
195 user_id: &Uuid,
196 request: &InitiatePaymentRequest,
197 ) -> Result<InitiatePaymentResponse> {
198 let path = format!("/users/{user_id}/payments");
199 self.post_blocking(&path, request)
200 }
201}