lemonsqueezy/modules/checkout.rs
1pub use crate::types::checkout::*;
2
3use serde_json::json;
4
5use crate::utils::{Response, ResponseData, VecResponse};
6
7pub struct WebhookRedemptionsFilters {
8 pub store_id: Option<i64>,
9}
10
11pub struct Checkout {
12 pub(crate) api: crate::LemonSqueezy,
13}
14
15impl Checkout {
16 pub fn build(api: crate::LemonSqueezy) -> Self {
17 Self { api }
18 }
19
20 /// Retrieve a checkout
21 ///
22 /// # Arguments
23 /// - checkout_id: The ID of the checkout to retrieve
24 ///
25 /// # Returns
26 /// - `anyhow::Result<Response<CheckoutResponse>, crate::errors::NetworkError>` object
27 ///
28 /// # Example
29 /// ```
30 /// use lemonsqueezy::checkout::Checkout;
31 /// let checkout = Checkout::build(lemonsqueezy);
32 /// let checkout = checkout.retrieve(1).await;
33 /// ```
34 pub async fn retrieve(
35 &self,
36 checkout_id: String,
37 ) -> anyhow::Result<Response<CheckoutResponse>, crate::errors::NetworkError> {
38 let response = self
39 .api
40 .get::<Response<CheckoutResponse>>(&format!("/v1/checkouts/{}", checkout_id))
41 .await?;
42
43 Ok(response)
44 }
45
46 /// Retrieve all checkouts
47 ///
48 /// # Arguments
49 /// - filters: The checkout filters
50 ///
51 /// # Returns
52 /// - `anyhow::Result<VecResponse<Vec<ResponseData<CheckoutResponse>>>, crate::errors::NetworkError>` object
53 ///
54 /// # Example
55 /// ```
56 /// use lemonsqueezy::checkout::Checkout;
57 /// let checkout = Checkout::build(lemonsqueezy);
58 /// let checkout = checkout.get_all(None).await;
59 /// ```
60 pub async fn get_all(
61 &self,
62 filters: Option<WebhookRedemptionsFilters>,
63 ) -> anyhow::Result<VecResponse<Vec<ResponseData<CheckoutResponse>>>, crate::errors::NetworkError>
64 {
65 let mut url = "/v1/checkouts".to_string();
66
67 //https://api.lemonsqueezy.com/v1/customers?filter[store_id]=11
68 if filters.is_some() {
69 let filter: WebhookRedemptionsFilters = filters.unwrap();
70
71 if let Some(store_id) = filter.store_id {
72 url.push_str(&format!("?filter[store_id]={}", store_id));
73 }
74 }
75
76 let response = self.api.get(&url).await?;
77
78 Ok(response)
79 }
80
81 /// Create a checkout
82 ///
83 /// # Arguments
84 /// - data: The checkout data
85 ///
86 /// # Returns
87 /// - `anyhow::Result<Response<CheckoutResponse>, crate::errors::NetworkError>` object
88 ///
89 /// # Example
90 /// ```
91 /// use lemonsqueezy::checkout::Checkout;
92 /// use lemonsqueezy::types::checkout::*;
93 ///
94 /// let checkout = Checkout::build(lemonsqueezy);
95 /// let checkout = checkout.create(CreateCheckout {
96 /// store_id: 1,
97 /// customer_id: 1,
98 /// // ... other fields
99 /// }).await;
100 /// ```
101 pub async fn create(
102 &self,
103 data: CreateCheckout,
104 ) -> anyhow::Result<Response<CheckoutResponse>, crate::errors::NetworkError> {
105 let data = json!({
106 "data": data,
107 });
108
109 let response = self.api.post("/v1/checkouts", data).await?;
110
111 Ok(response)
112 }
113}