noah_sdk/api/
checkout.rs

1//! Checkout API
2//!
3//! This module provides functionality to create checkout sessions for payments.
4//! Supports crypto payin, fiat payin, and fiat payout operations.
5//!
6//! # Examples
7//!
8//! ## Create Crypto Payin Session
9//!
10//! ```no_run
11//! use noah_sdk::{NoahClient, Config, Environment, AuthConfig};
12//! use noah_sdk::api::checkout::CryptoPayinRequest;
13//! use noah_sdk::models::common::*;
14//!
15//! # #[cfg(feature = "async")]
16//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
17//! let config = Config::new(Environment::Sandbox);
18//! let auth = AuthConfig::with_api_key("your-api-key".to_string());
19//! let client = NoahClient::new(config, auth)?;
20//!
21//! let request = CryptoPayinRequest {
22//!     crypto_currency: CryptoCurrencyCode::from("BTC"),
23//!     crypto_amount: PositiveDecimal::from("0.001"),
24//!     return_url: ReturnURL::from("https://example.com/return"),
25//!     customer_id: CustomerID::from("customer-123"),
26//!     external_id: None,
27//!     customer: None,
28//!     line_items: Default::default(),
29//!     nonce: Nonce::from("unique-nonce"),
30//! };
31//!
32//! let session = client.create_crypto_payin_session(&request).await?;
33//! println!("Checkout URL: {}", session.checkout_url);
34//! # Ok(())
35//! # }
36//! ```
37
38use crate::client::NoahClient;
39use crate::error::Result;
40use crate::models::checkout::CheckoutSessionResponse;
41use crate::models::common::*;
42use serde::Serialize;
43
44/// Request to create a cryptocurrency payin checkout session
45///
46/// This request is used to create a checkout session where customers can pay
47/// using cryptocurrency (e.g., Bitcoin, Ethereum).
48///
49/// # Examples
50///
51/// ```no_run
52/// use noah_sdk::api::checkout::CryptoPayinRequest;
53/// use noah_sdk::models::common::*;
54///
55/// let request = CryptoPayinRequest {
56///     crypto_currency: CryptoCurrencyCode::from("BTC"),
57///     crypto_amount: PositiveDecimal::from("0.001"),
58///     return_url: ReturnURL::from("https://example.com/return"),
59///     customer_id: CustomerID::from("customer-123"),
60///     external_id: Some(ExternalID::from("order-456")),
61///     customer: None,
62///     line_items: Default::default(),
63///     nonce: Nonce::from("unique-nonce-123"),
64/// };
65/// ```
66#[derive(Debug, Clone, Serialize)]
67pub struct CryptoPayinRequest {
68    /// Cryptocurrency
69    #[serde(rename = "CryptoCurrency")]
70    pub crypto_currency: CryptoCurrencyCode,
71    /// Crypto amount
72    #[serde(rename = "CryptoAmount")]
73    pub crypto_amount: PositiveDecimal,
74    /// Return URL
75    #[serde(rename = "ReturnURL")]
76    pub return_url: ReturnURL,
77    /// Customer ID
78    #[serde(rename = "CustomerID")]
79    pub customer_id: CustomerID,
80    /// External ID (optional)
81    #[serde(rename = "ExternalID")]
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub external_id: Option<ExternalID>,
84    /// Customer (optional)
85    #[serde(rename = "Customer")]
86    #[serde(skip_serializing_if = "Option::is_none")]
87    pub customer: Option<serde_json::Value>,
88    /// Line items
89    #[serde(rename = "LineItems")]
90    pub line_items: crate::models::checkout::LineItems,
91    /// Nonce
92    #[serde(rename = "Nonce")]
93    pub nonce: Nonce,
94}
95
96/// Request to create a fiat currency payin checkout session
97///
98/// This request is used to create a checkout session where customers can pay
99/// using fiat currency (e.g., USD, EUR) via various payment methods.
100///
101/// # Examples
102///
103/// ```no_run
104/// use noah_sdk::api::checkout::FiatPayinRequest;
105/// use noah_sdk::models::common::*;
106///
107/// let request = FiatPayinRequest {
108///     payment_method_category: PaymentMethodCategory::Card,
109///     fiat_currency: FiatCurrencyCode::from("USD"),
110///     crypto_currency: CryptoCurrencyCode::from("BTC"),
111///     fiat_amount: PositiveDecimal::from("100.00"),
112///     return_url: ReturnURL::from("https://example.com/return"),
113///     customer_id: CustomerID::from("customer-123"),
114///     external_id: None,
115///     customer: None,
116///     line_items: Default::default(),
117///     nonce: Nonce::from("unique-nonce"),
118/// };
119/// ```
120#[derive(Debug, Clone, Serialize)]
121pub struct FiatPayinRequest {
122    /// Payment method category
123    #[serde(rename = "PaymentMethodCategory")]
124    pub payment_method_category: PaymentMethodCategory,
125    /// Fiat currency
126    #[serde(rename = "FiatCurrency")]
127    pub fiat_currency: FiatCurrencyCode,
128    /// Cryptocurrency
129    #[serde(rename = "CryptoCurrency")]
130    pub crypto_currency: CryptoCurrencyCode,
131    /// Fiat amount
132    #[serde(rename = "FiatAmount")]
133    pub fiat_amount: PositiveDecimal,
134    /// Return URL
135    #[serde(rename = "ReturnURL")]
136    pub return_url: ReturnURL,
137    /// Customer ID
138    #[serde(rename = "CustomerID")]
139    pub customer_id: CustomerID,
140    /// External ID (optional)
141    #[serde(rename = "ExternalID")]
142    #[serde(skip_serializing_if = "Option::is_none")]
143    pub external_id: Option<ExternalID>,
144    /// Customer (optional)
145    #[serde(rename = "Customer")]
146    #[serde(skip_serializing_if = "Option::is_none")]
147    pub customer: Option<serde_json::Value>,
148    /// Line items
149    #[serde(rename = "LineItems")]
150    pub line_items: crate::models::checkout::LineItems,
151    /// Nonce
152    #[serde(rename = "Nonce")]
153    pub nonce: Nonce,
154}
155
156/// Request to create a fiat currency payout checkout session
157///
158/// This request is used to create a checkout session where customers can sell
159/// cryptocurrency and receive fiat currency.
160///
161/// # Examples
162///
163/// ```no_run
164/// use noah_sdk::api::checkout::FiatPayoutRequest;
165/// use noah_sdk::models::common::*;
166///
167/// let request = FiatPayoutRequest {
168///     crypto_currency: CryptoCurrencyCode::from("BTC"),
169///     fiat_currency: FiatCurrencyCode::from("USD"),
170///     fiat_amount: PositiveDecimal::from("1000.00"),
171///     crypto_authorized_amount: PositiveDecimal::from("0.025"),
172///     return_url: ReturnURL::from("https://example.com/return"),
173///     customer_id: CustomerID::from("customer-123"),
174///     external_id: None,
175///     customer: None,
176///     line_items: Default::default(),
177///     nonce: Nonce::from("unique-nonce"),
178/// };
179/// ```
180#[derive(Debug, Clone, Serialize)]
181pub struct FiatPayoutRequest {
182    /// Cryptocurrency
183    #[serde(rename = "CryptoCurrency")]
184    pub crypto_currency: CryptoCurrencyCode,
185    /// Fiat currency
186    #[serde(rename = "FiatCurrency")]
187    pub fiat_currency: FiatCurrencyCode,
188    /// Fiat amount
189    #[serde(rename = "FiatAmount")]
190    pub fiat_amount: PositiveDecimal,
191    /// Crypto authorized amount
192    #[serde(rename = "CryptoAuthorizedAmount")]
193    pub crypto_authorized_amount: PositiveDecimal,
194    /// Return URL
195    #[serde(rename = "ReturnURL")]
196    pub return_url: ReturnURL,
197    /// Customer ID
198    #[serde(rename = "CustomerID")]
199    pub customer_id: CustomerID,
200    /// External ID (optional)
201    #[serde(rename = "ExternalID")]
202    #[serde(skip_serializing_if = "Option::is_none")]
203    pub external_id: Option<ExternalID>,
204    /// Customer (optional)
205    #[serde(rename = "Customer")]
206    #[serde(skip_serializing_if = "Option::is_none")]
207    pub customer: Option<serde_json::Value>,
208    /// Line items
209    #[serde(rename = "LineItems")]
210    pub line_items: crate::models::checkout::LineItems,
211    /// Nonce
212    #[serde(rename = "Nonce")]
213    pub nonce: Nonce,
214}
215
216impl NoahClient {
217    /// Create a cryptocurrency payin checkout session
218    ///
219    /// Creates a checkout session that allows customers to pay using cryptocurrency.
220    /// Returns a session with a checkout URL that the customer can be redirected to.
221    ///
222    /// # Arguments
223    ///
224    /// * `request` - The crypto payin request containing payment details
225    ///
226    /// # Returns
227    ///
228    /// Returns a [`CheckoutSessionResponse`] containing the checkout URL and session details.
229    ///
230    /// # Errors
231    ///
232    /// This function will return an error if:
233    /// - The request data is invalid
234    /// - The API request fails
235    /// - Authentication fails
236    ///
237    /// # Examples
238    ///
239    /// ```no_run
240    /// use noah_sdk::{NoahClient, Config, Environment, AuthConfig};
241    /// use noah_sdk::api::checkout::CryptoPayinRequest;
242    /// use noah_sdk::models::common::*;
243    ///
244    /// # #[cfg(feature = "async")]
245    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
246    /// let config = Config::new(Environment::Sandbox);
247    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
248    /// let client = NoahClient::new(config, auth)?;
249    ///
250    /// let request = CryptoPayinRequest {
251    ///     crypto_currency: CryptoCurrencyCode::from("BTC"),
252    ///     crypto_amount: PositiveDecimal::from("0.001"),
253    ///     return_url: ReturnURL::from("https://example.com/return"),
254    ///     customer_id: CustomerID::from("customer-123"),
255    ///     external_id: None,
256    ///     customer: None,
257    ///     line_items: Default::default(),
258    ///     nonce: Nonce::from("unique-nonce"),
259    /// };
260    ///
261    /// let session = client.create_crypto_payin_session(&request).await?;
262    /// println!("Redirect customer to: {}", session.checkout_url);
263    /// # Ok(())
264    /// # }
265    /// ```
266    #[cfg(feature = "async")]
267    pub async fn create_crypto_payin_session(
268        &self,
269        request: &CryptoPayinRequest,
270    ) -> Result<CheckoutSessionResponse> {
271        self.post("/checkout/payin/crypto", request).await
272    }
273
274    /// Create a cryptocurrency payin checkout session (blocking)
275    ///
276    /// Synchronous version of [`create_crypto_payin_session`](Self::create_crypto_payin_session).
277    /// See that method for detailed documentation.
278    #[cfg(feature = "sync")]
279    pub fn create_crypto_payin_session_blocking(
280        &self,
281        request: &CryptoPayinRequest,
282    ) -> Result<CheckoutSessionResponse> {
283        self.post_blocking("/checkout/payin/crypto", request)
284    }
285
286    /// Create a fiat currency payin checkout session
287    ///
288    /// Creates a checkout session that allows customers to pay using fiat currency
289    /// via various payment methods (e.g., credit card, bank transfer).
290    ///
291    /// # Arguments
292    ///
293    /// * `request` - The fiat payin request containing payment details
294    ///
295    /// # Returns
296    ///
297    /// Returns a [`CheckoutSessionResponse`] containing the checkout URL and session details.
298    ///
299    /// # Examples
300    ///
301    /// ```no_run
302    /// use noah_sdk::{NoahClient, Config, Environment, AuthConfig};
303    /// use noah_sdk::api::checkout::FiatPayinRequest;
304    /// use noah_sdk::models::common::*;
305    ///
306    /// # #[cfg(feature = "async")]
307    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
308    /// let config = Config::new(Environment::Sandbox);
309    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
310    /// let client = NoahClient::new(config, auth)?;
311    ///
312    /// let request = FiatPayinRequest {
313    ///     payment_method_category: PaymentMethodCategory::Card,
314    ///     fiat_currency: FiatCurrencyCode::from("USD"),
315    ///     crypto_currency: CryptoCurrencyCode::from("BTC"),
316    ///     fiat_amount: PositiveDecimal::from("100.00"),
317    ///     return_url: ReturnURL::from("https://example.com/return"),
318    ///     customer_id: CustomerID::from("customer-123"),
319    ///     external_id: None,
320    ///     customer: None,
321    ///     line_items: Default::default(),
322    ///     nonce: Nonce::from("unique-nonce"),
323    /// };
324    ///
325    /// let session = client.create_fiat_payin_session(&request).await?;
326    /// # Ok(())
327    /// # }
328    /// ```
329    #[cfg(feature = "async")]
330    pub async fn create_fiat_payin_session(
331        &self,
332        request: &FiatPayinRequest,
333    ) -> Result<CheckoutSessionResponse> {
334        self.post("/checkout/payin/fiat", request).await
335    }
336
337    /// Create a fiat currency payin checkout session (blocking)
338    ///
339    /// Synchronous version of [`create_fiat_payin_session`](Self::create_fiat_payin_session).
340    /// See that method for detailed documentation.
341    #[cfg(feature = "sync")]
342    pub fn create_fiat_payin_session_blocking(
343        &self,
344        request: &FiatPayinRequest,
345    ) -> Result<CheckoutSessionResponse> {
346        self.post_blocking("/checkout/payin/fiat", request)
347    }
348
349    /// Create a fiat currency payout checkout session
350    ///
351    /// Creates a checkout session that allows customers to sell cryptocurrency
352    /// and receive fiat currency in return.
353    ///
354    /// # Arguments
355    ///
356    /// * `request` - The fiat payout request containing sale details
357    ///
358    /// # Returns
359    ///
360    /// Returns a [`CheckoutSessionResponse`] containing the checkout URL and session details.
361    ///
362    /// # Examples
363    ///
364    /// ```no_run
365    /// use noah_sdk::{NoahClient, Config, Environment, AuthConfig};
366    /// use noah_sdk::api::checkout::FiatPayoutRequest;
367    /// use noah_sdk::models::common::*;
368    ///
369    /// # #[cfg(feature = "async")]
370    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
371    /// let config = Config::new(Environment::Sandbox);
372    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
373    /// let client = NoahClient::new(config, auth)?;
374    ///
375    /// let request = FiatPayoutRequest {
376    ///     crypto_currency: CryptoCurrencyCode::from("BTC"),
377    ///     fiat_currency: FiatCurrencyCode::from("USD"),
378    ///     fiat_amount: PositiveDecimal::from("1000.00"),
379    ///     crypto_authorized_amount: PositiveDecimal::from("0.025"),
380    ///     return_url: ReturnURL::from("https://example.com/return"),
381    ///     customer_id: CustomerID::from("customer-123"),
382    ///     external_id: None,
383    ///     customer: None,
384    ///     line_items: Default::default(),
385    ///     nonce: Nonce::from("unique-nonce"),
386    /// };
387    ///
388    /// let session = client.create_fiat_payout_session(&request).await?;
389    /// # Ok(())
390    /// # }
391    /// ```
392    #[cfg(feature = "async")]
393    pub async fn create_fiat_payout_session(
394        &self,
395        request: &FiatPayoutRequest,
396    ) -> Result<CheckoutSessionResponse> {
397        self.post("/checkout/payout/fiat", request).await
398    }
399
400    /// Create a fiat currency payout checkout session (blocking)
401    ///
402    /// Synchronous version of [`create_fiat_payout_session`](Self::create_fiat_payout_session).
403    /// See that method for detailed documentation.
404    #[cfg(feature = "sync")]
405    pub fn create_fiat_payout_session_blocking(
406        &self,
407        request: &FiatPayoutRequest,
408    ) -> Result<CheckoutSessionResponse> {
409        self.post_blocking("/checkout/payout/fiat", request)
410    }
411}