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 /// # Examples
23 ///
24 /// ```no_run
25 /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
26 /// use rain_sdk::models::payments::InitiatePaymentRequest;
27 /// use uuid::Uuid;
28 ///
29 /// # #[cfg(feature = "async")]
30 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
31 /// let config = Config::new(Environment::Dev);
32 /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
33 /// let client = RainClient::new(config, auth)?;
34 ///
35 /// let company_id = Uuid::new_v4();
36 /// let request = InitiatePaymentRequest {
37 /// amount: 10000, // $100.00 in cents
38 /// wallet_address: "0x1234...".to_string(),
39 /// chain_id: Some(1), // Ethereum mainnet
40 /// };
41 /// let response = client.initiate_company_payment(&company_id, &request).await?;
42 /// println!("Payment address: {}", response.address);
43 /// # Ok(())
44 /// # }
45 /// ```
46 #[cfg(feature = "async")]
47 pub async fn initiate_company_payment(
48 &self,
49 company_id: &Uuid,
50 request: &InitiatePaymentRequest,
51 ) -> Result<InitiatePaymentResponse> {
52 let path = format!("/issuing/companies/{company_id}/payments");
53 self.post(&path, request).await
54 }
55
56 /// Initiate a payment for an authorized user tenant
57 ///
58 /// # Arguments
59 ///
60 /// * `request` - The payment initiation request
61 ///
62 /// # Returns
63 ///
64 /// Returns an [`InitiatePaymentResponse`] containing the payment address.
65 ///
66 /// # Examples
67 ///
68 /// ```no_run
69 /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
70 /// use rain_sdk::models::payments::InitiatePaymentRequest;
71 ///
72 /// # #[cfg(feature = "async")]
73 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
74 /// let config = Config::new(Environment::Dev);
75 /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
76 /// let client = RainClient::new(config, auth)?;
77 ///
78 /// let request = InitiatePaymentRequest {
79 /// amount: 5000, // $50.00 in cents
80 /// wallet_address: "0x5678...".to_string(),
81 /// chain_id: Some(137), // Polygon
82 /// };
83 /// let response = client.initiate_payment(&request).await?;
84 /// # Ok(())
85 /// # }
86 /// ```
87 #[cfg(feature = "async")]
88 pub async fn initiate_payment(
89 &self,
90 request: &InitiatePaymentRequest,
91 ) -> Result<InitiatePaymentResponse> {
92 let path = "/issuing/payments";
93 self.post(path, request).await
94 }
95
96 /// Initiate a payment for a user
97 ///
98 /// # Arguments
99 ///
100 /// * `user_id` - The unique identifier of the user
101 /// * `request` - The payment initiation request
102 ///
103 /// # Returns
104 ///
105 /// Returns an [`InitiatePaymentResponse`] containing the payment address.
106 ///
107 /// # Examples
108 ///
109 /// ```no_run
110 /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
111 /// use rain_sdk::models::payments::InitiatePaymentRequest;
112 /// use uuid::Uuid;
113 ///
114 /// # #[cfg(feature = "async")]
115 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
116 /// let config = Config::new(Environment::Dev);
117 /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
118 /// let client = RainClient::new(config, auth)?;
119 ///
120 /// let user_id = Uuid::new_v4();
121 /// let request = InitiatePaymentRequest {
122 /// amount: 2500, // $25.00 in cents
123 /// wallet_address: "0xabcd...".to_string(),
124 /// chain_id: Some(1),
125 /// };
126 /// let response = client.initiate_user_payment(&user_id, &request).await?;
127 /// # Ok(())
128 /// # }
129 /// ```
130 #[cfg(feature = "async")]
131 pub async fn initiate_user_payment(
132 &self,
133 user_id: &Uuid,
134 request: &InitiatePaymentRequest,
135 ) -> Result<InitiatePaymentResponse> {
136 let path = format!("/issuing/users/{user_id}/payments");
137 self.post(&path, request).await
138 }
139
140 // ============================================================================
141 // Blocking Methods
142 // ============================================================================
143
144 /// Initiate a payment for a company (blocking)
145 #[cfg(feature = "sync")]
146 pub fn initiate_company_payment_blocking(
147 &self,
148 company_id: &Uuid,
149 request: &InitiatePaymentRequest,
150 ) -> Result<InitiatePaymentResponse> {
151 let path = format!("/issuing/companies/{company_id}/payments");
152 self.post_blocking(&path, request)
153 }
154
155 /// Initiate a payment for an authorized user tenant (blocking)
156 #[cfg(feature = "sync")]
157 pub fn initiate_payment_blocking(
158 &self,
159 request: &InitiatePaymentRequest,
160 ) -> Result<InitiatePaymentResponse> {
161 let path = "/issuing/payments";
162 self.post_blocking(path, request)
163 }
164
165 /// Initiate a payment for a user (blocking)
166 #[cfg(feature = "sync")]
167 pub fn initiate_user_payment_blocking(
168 &self,
169 user_id: &Uuid,
170 request: &InitiatePaymentRequest,
171 ) -> Result<InitiatePaymentResponse> {
172 let path = format!("/issuing/users/{user_id}/payments");
173 self.post_blocking(&path, request)
174 }
175}