paddle_rust_sdk/
lib.rs

1#![allow(clippy::result_large_err)]
2
3//! # Paddle API Client
4//!
5//! An async client library for interaction with Paddle API, An async and ergonomic wrapper around Paddle's REST HTTP API.
6//!
7//! Every interaction is done via the [Paddle] client type.
8//!
9//! ## Init and Usage Example
10//!
11//! ```rust,no_run
12//! use paddle_rust_sdk::Paddle;
13//!
14//! #[tokio::main]
15//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
16//!     let client = Paddle::new(std::env::var("PADDLE_API_KEY")?, Paddle::SANDBOX)?;
17//!
18//!     let mut list = client.customers_list();
19//!     let mut paginated = list.per_page(2).send();
20//!     let customers = paginated.all().await?;
21//!
22//!     dbg!(customers);
23//!
24//!     Ok(())
25//! }
26//! ```
27//!
28//! The `examples/` dir has up to date working example code.
29//!
30//! ## Webhook signature verification
31//!
32//! Use the [Paddle::unmarshal] method to verify that received events are genuinely sent from Paddle. Additionally, this method returns the deserialized event struct.
33//!
34
35use reports::ReportType;
36use reqwest::{header::CONTENT_TYPE, IntoUrl, Method, StatusCode, Url};
37use serde::{de::DeserializeOwned, Serialize};
38
39pub mod entities;
40pub mod enums;
41pub mod error;
42pub mod ids;
43pub mod webhooks;
44
45pub mod addresses;
46pub mod adjustments;
47pub mod businesses;
48pub mod customers;
49pub mod discounts;
50pub mod events;
51pub mod paginated;
52pub mod payment_methods;
53pub mod prices;
54pub mod pricing_preview;
55pub mod products;
56pub mod reports;
57pub mod subscriptions;
58pub mod transactions;
59
60pub mod response;
61
62use entities::{
63    CustomerAuthenticationToken, Event, EventType, PricePreviewItem, ReportBase, Subscription,
64    Transaction, TransactionInvoice,
65};
66use enums::{
67    AdjustmentAction, CountryCodeSupported, CurrencyCode, DiscountType, Disposition, TaxCategory,
68};
69use ids::{
70    AddressID, AdjustmentID, BusinessID, CustomerID, DiscountID, PaddleID, PaymentMethodID,
71    PriceID, ProductID, SubscriptionID, TransactionID,
72};
73use webhooks::{MaximumVariance, Signature};
74
75use error::PaddleApiError;
76use response::{ErrorResponse, Response, SuccessResponse};
77
78pub use error::Error;
79
80type Result<T> = std::result::Result<SuccessResponse<T>, Error>;
81
82/// Paddle API client
83///
84/// This struct is used to create a new Paddle client instance.
85#[derive(Clone, Debug)]
86pub struct Paddle {
87    base_url: Url,
88    api_key: String,
89}
90
91impl Paddle {
92    pub const PRODUCTION: &'static str = "https://api.paddle.com";
93    pub const SANDBOX: &'static str = "https://sandbox-api.paddle.com";
94
95    /// List of IP addresses Paddle uses to call webhook endpoints from the Live environment
96    pub const ALLOWED_WEBHOOK_IPS_PRODUCTION: [&str; 6] = [
97        "34.232.58.13",
98        "34.195.105.136",
99        "34.237.3.244",
100        "35.155.119.135",
101        "52.11.166.252",
102        "34.212.5.7",
103    ];
104
105    /// List of IP addresses Paddle uses to call webhook endpoints from the Sandbox environment
106    pub const ALLOWED_WEBHOOK_IPS_SANDBOX: [&str; 6] = [
107        "34.194.127.46",
108        "54.234.237.108",
109        "3.208.120.145",
110        "44.226.236.210",
111        "44.241.183.62",
112        "100.20.172.113",
113    ];
114
115    /// Creates a new Paddle client instance.
116    ///
117    /// Example:
118    ///
119    /// ```rust,no_run
120    /// use paddle_rust_sdk::Paddle;
121    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
122    /// ```
123    pub fn new(
124        api_key: impl Into<String>,
125        base_url: impl IntoUrl,
126    ) -> std::result::Result<Self, Error> {
127        Ok(Self {
128            base_url: base_url.into_url()?,
129            api_key: api_key.into(),
130        })
131    }
132
133    /// Validate the integrity of a Paddle webhook request.
134    ///
135    /// - **request_body** - The raw body of the request. Don't transform or process the raw body of the request, including adding whitespace or applying other formatting. This results in a different signed payload, meaning signatures won't match when you compare.
136    /// - **secret_key** - Secret key created in Paddle dashboard. Each notification destination has it's own secret key.
137    /// - **signature** - "Paddle-Signature" HTTP request header from an incoming webhook sent by Paddle.
138    /// - **maximum_variance** - Maximum allowed age for a generated signature. [MaximumVariance::default] is 5 seconds. Pass `MaximumVariance(None)` to disable timestamp checking.
139    ///
140    /// **Return** - the deserialized [Event] struct.
141    ///
142    /// The `examples/` directory contains a demo webhook handler for Actix web.
143    pub fn unmarshal(
144        request_body: impl AsRef<str>,
145        secret_key: impl AsRef<str>,
146        signature: impl AsRef<str>,
147        maximum_variance: MaximumVariance,
148    ) -> std::result::Result<Event, Error> {
149        let signature: Signature = signature.as_ref().parse()?;
150        signature.verify(request_body.as_ref(), secret_key, maximum_variance)?;
151
152        let event = serde_json::from_str(request_body.as_ref())?;
153
154        Ok(event)
155    }
156
157    /// Get a request builder for fetching products. Use the after method to page through results.
158    ///
159    /// By default, Paddle returns products that are active. Use the status method to return products that are archived.
160    /// Use the include method to include related price entities in the response.
161    ///
162    /// # Example:
163    ///
164    /// ```rust,no_run
165    /// use paddle_rust_sdk::Paddle;
166    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
167    ///
168    /// let mut products_list = client.products_list();
169    /// let mut products = products_list.order_by_asc("id").per_page(20).send();
170    ///
171    /// while let Some(res) = products.next().await.unwrap() {
172    ///     dbg!(res.data);
173    /// }
174    /// ```
175    pub fn products_list(&self) -> products::ProductsList<'_> {
176        products::ProductsList::new(self)
177    }
178
179    /// Get a request builder for creating a new product.
180    ///
181    /// # Example:
182    ///
183    /// ```rust,no_run
184    /// use paddle_rust_sdk::Paddle;
185    /// use paddle_rust_sdk::enums::TaxCategory;
186    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
187    /// let product = client.products_create("My Product", TaxCategory::Standard).send().await.unwrap();
188    /// ```
189    pub fn product_create(
190        &self,
191        name: impl Into<String>,
192        tax_category: TaxCategory,
193    ) -> products::ProductCreate<'_> {
194        products::ProductCreate::new(self, name, tax_category)
195    }
196
197    /// Get a request builder for fetching a specific product.
198    ///
199    /// # Example:
200    ///
201    /// ```rust,no_run
202    /// use paddle_rust_sdk::Paddle;
203    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
204    /// let product = client.product_get("pro_01jqx9rd...").send().await.unwrap();
205    /// ```
206    pub fn product_get(&self, product_id: impl Into<ProductID>) -> products::ProductGet<'_> {
207        products::ProductGet::new(self, product_id)
208    }
209
210    /// Get a request builder for updating a specific product.
211    ///
212    /// # Example:
213    ///
214    /// ```rust,no_run
215    /// use paddle_rust_sdk::Paddle;
216    /// use paddle_rust_sdk::enums::TaxCategory;
217    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
218    /// let product = client.product_update("pro_01jqx9rd...").name("My New Name").send().await.unwrap();
219    /// ```
220    pub fn product_update(&self, product_id: impl Into<ProductID>) -> products::ProductUpdate<'_> {
221        products::ProductUpdate::new(self, product_id)
222    }
223
224    /// Get a request builder listing prices
225    ///
226    /// # Example:
227    ///
228    /// ```rust,no_run
229    /// use paddle_rust_sdk::Paddle;
230    ///
231    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
232    ///
233    /// let mut prices_list = client.prices_list();
234    /// let mut prices = prices_list.order_by_asc("id").per_page(20).send();
235    ///
236    /// while let Some(res) = prices.next().await.unwrap() {
237    ///     dbg!(res.data);
238    /// }
239    /// ```
240    pub fn prices_list(&self) -> prices::PricesList<'_> {
241        prices::PricesList::new(self)
242    }
243
244    /// Get a request builder for creating a new price.
245    ///
246    /// * `product_id` - Paddle ID for the product that this price is for.
247    /// * `description` - Internal description for this price, not shown to customers. Typically notes for your team.
248    /// * `amount` - Amount of the price in the smallest unit of the currency (e.g. 1000 cents for 10 USD).
249    /// * `currency` - Currency code for the price. Use the [CurrencyCode] enum to specify the currency.
250    ///
251    /// # Example:
252    ///
253    /// ```rust,no_run
254    /// use paddle_rust_sdk::Paddle;
255    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
256    /// let price = client.price_create("pro_01jqx9rd...", "Low price", 19.99, CurrencyCode::USD).send().await.unwrap();
257    /// ```
258    pub fn price_create(
259        &self,
260        product_id: impl Into<ProductID>,
261        description: impl Into<String>,
262        amount: u64,
263        currency: CurrencyCode,
264    ) -> prices::PricesCreate<'_> {
265        prices::PricesCreate::new(self, product_id, description, amount, currency)
266    }
267
268    /// Get a request builder for fetching a specific price by id.
269    ///
270    /// # Example:
271    ///
272    /// ```rust,no_run
273    /// use paddle_rust_sdk::Paddle;
274    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
275    /// let price = client.price_get("price_01jqx9rd...").send().await.unwrap();
276    /// ```
277    pub fn price_get(&self, price_id: impl Into<PriceID>) -> prices::PriceGet<'_> {
278        prices::PriceGet::new(self, price_id)
279    }
280
281    /// Get a request builder for updating a specific price.
282    ///
283    /// # Example:
284    ///
285    /// ```rust,no_run
286    /// use paddle_rust_sdk::Paddle;
287    /// use paddle_rust_sdk::enums::TaxCategory;
288    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
289    /// let price = client.price_update("pri_01jqxv...").name("Updated Name").send().await.unwrap();
290    /// ```
291    pub fn price_update(&self, price_id: impl Into<PriceID>) -> prices::PriceUpdate<'_> {
292        prices::PriceUpdate::new(self, price_id)
293    }
294
295    /// Get a request builder for fetching discounts.
296    ///
297    /// # Example:
298    ///
299    /// ```rust,no_run
300    /// use paddle_rust_sdk::Paddle;
301    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
302    ///
303    /// let list = client.discounts_list();
304    /// let mut discounts = list.send();
305    ///
306    /// while let Some(res) = discounts.next().await.unwrap() {
307    ///     dbg!(res.data);
308    /// }
309    /// ```
310    pub fn discounts_list(&self) -> discounts::DiscountsList<'_> {
311        discounts::DiscountsList::new(self)
312    }
313
314    /// Get a request builder for creating discounts.
315    ///
316    /// # Example:
317    ///
318    /// ```rust,no_run
319    /// use paddle_rust_sdk::Paddle;
320    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
321    /// let discount = client.discount_create("15", "Winter Holidays", DiscountType::Percentage).send().await.unwrap();
322    /// ```
323    pub fn discount_create(
324        &self,
325        amount: impl Into<String>,
326        description: impl Into<String>,
327        discount_type: DiscountType,
328    ) -> discounts::DiscountCreate<'_> {
329        discounts::DiscountCreate::new(self, amount, description, discount_type)
330    }
331
332    /// Get a request builder for fetching a specific discount by id.
333    ///
334    /// # Example:
335    ///
336    /// ```rust,no_run
337    /// use paddle_rust_sdk::Paddle;
338    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
339    /// let discount = client.discount_get("dsc_01jqzpbmnq...").send().await.unwrap();
340    /// ```
341    pub fn discount_get(&self, discount_id: impl Into<DiscountID>) -> discounts::DiscountGet<'_> {
342        discounts::DiscountGet::new(self, discount_id)
343    }
344
345    /// Get a request builder for creating discounts.
346    ///
347    /// # Example:
348    ///
349    /// ```rust,no_run
350    /// use paddle_rust_sdk::Paddle;
351    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
352    /// let discount = client.discount_update("dsc_01jqzpbmnq...").amount("18").send().await.unwrap();
353    /// ```
354    pub fn discount_update(
355        &self,
356        discount_id: impl Into<DiscountID>,
357    ) -> discounts::DiscountUpdate<'_> {
358        discounts::DiscountUpdate::new(self, discount_id)
359    }
360
361    /// Get a request builder for fetching customers. Use the after method to page through results.
362    ///
363    /// By default, Paddle returns customers that are `active`. Use the status query parameter to return customers that are archived.
364    ///
365    /// # Example:
366    ///
367    /// ```rust,no_run
368    /// use paddle_rust_sdk::Paddle;
369    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
370    /// let customers = client.customers_list().send().await.unwrap();
371    /// ```
372    pub fn customers_list(&self) -> customers::CustomersList<'_> {
373        customers::CustomersList::new(self)
374    }
375
376    /// Get a request builder for creating a new customer.
377    ///
378    /// # Example:
379    ///
380    /// ```rust,no_run
381    /// use paddle_rust_sdk::Paddle;
382    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
383    /// let customers = client.customer_create("test@example.com").send().await.unwrap();
384    /// ```
385    pub fn customer_create(&self, email: impl Into<String>) -> customers::CustomerCreate<'_> {
386        customers::CustomerCreate::new(self, email.into())
387    }
388
389    /// Get a request builder for fetching a specific customer by id.
390    ///
391    /// # Example:
392    ///
393    /// ```rust,no_run
394    /// use paddle_rust_sdk::Paddle;
395    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
396    /// let discount = client.customer_get("ctm_01jqztc78e1xfdgwhcgjzdrvgd").send().await.unwrap();
397    /// ```
398    pub fn customer_get(&self, customer_id: impl Into<CustomerID>) -> customers::CustomerGet<'_> {
399        customers::CustomerGet::new(self, customer_id)
400    }
401
402    /// Get a request builder for updating customer data.
403    ///
404    /// # Example:
405    ///
406    /// ```rust,no_run
407    /// use paddle_rust_sdk::Paddle;
408    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
409    /// let discount = client.customer_update("ctm_01jqztc78e1xfdgwhcgjzdrvgd").email("new_email@example.com").send().await.unwrap();
410    /// ```
411    pub fn customer_update(
412        &self,
413        customer_id: impl Into<CustomerID>,
414    ) -> customers::CustomerUpdate<'_> {
415        customers::CustomerUpdate::new(self, customer_id)
416    }
417
418    /// Get a request builder for fetching a list of credit balances for each currency for a customer.
419    ///
420    /// Each balance has three totals:
421    ///
422    /// * `available` - total available to use.
423    /// * `reserved` - total temporarily reserved for billed transactions.
424    /// * `used` - total amount of credit used.
425    ///
426    /// Credit is added to the available total initially. When used, it moves to the used total.
427    ///
428    /// The reserved total is used when a credit balance is applied to a transaction that's marked as billed, like when working with an issued invoice. It's not available for other transactions at this point, but isn't considered used until the transaction is completed. If a billed transaction is canceled, any reserved credit moves back to available.
429    ///
430    /// Credit balances are created automatically by Paddle when you take an action that results in Paddle creating a credit for a customer, like making prorated changes to a subscription. An empty data array is returned where a customer has no credit balances.
431    ///
432    /// The response is not paginated.
433    ///
434    /// # Example:
435    ///
436    /// ```rust,no_run
437    /// use paddle_rust_sdk::Paddle;
438    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
439    /// let discount = client.customer_credit_balances("ctm_01jqztc78e1xfdgwhcgjzdrvgd").send().await.unwrap();
440    /// ```
441    pub fn customer_credit_balances(
442        &self,
443        customer_id: impl Into<CustomerID>,
444    ) -> customers::CustomerCreditBalances<'_> {
445        customers::CustomerCreditBalances::new(self, customer_id)
446    }
447
448    /// Generates an authentication token for a customer.
449    ///
450    /// You can pass a generated authentication token to Paddle.js when opening a checkout to let customers work with saved payment methods.
451    ///
452    /// Authentication tokens are temporary and shouldn't be cached. They're valid until the expires_at date returned in the response.
453    pub async fn generate_auth_token(
454        &self,
455        customer_id: impl Into<CustomerID>,
456    ) -> Result<CustomerAuthenticationToken> {
457        let client = reqwest::Client::new();
458
459        let customer_id = customer_id.into();
460
461        let url = format!(
462            "{}customers/{}/auth-token",
463            self.base_url,
464            customer_id.as_ref()
465        );
466
467        let res: Response<_> = client
468            .post(url)
469            .bearer_auth(self.api_key.clone())
470            .send()
471            .await?
472            .json()
473            .await?;
474
475        match res {
476            Response::Success(success) => Ok(success),
477            Response::Error(error) => Err(Error::PaddleApi(error)),
478        }
479    }
480
481    /// Get a request builder for fetching customers addresses.
482    ///
483    /// By default, Paddle returns addresses that are `active`. Use the status query parameter to return addresses that are archived.
484    ///
485    /// # Example:
486    ///
487    /// ```rust,no_run
488    /// use paddle_rust_sdk::Paddle;
489    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
490    /// let customers = client.addresses_list("ctm_01jqztc78e1xfdgwhcgjzdrvgd").send().await.unwrap();
491    /// ```
492    pub fn addresses_list(
493        &self,
494        customer_id: impl Into<CustomerID>,
495    ) -> addresses::AddressesList<'_> {
496        addresses::AddressesList::new(self, customer_id)
497    }
498
499    /// Get a request builder for creating a new customer address.
500    ///
501    /// # Example:
502    ///
503    /// ```rust,no_run
504    /// use paddle_rust_sdk::Paddle;
505    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
506    /// let customers = client.address_create("ctm_01jqztc78e1xfdgwhcgjzdrvgd", CountryCodeSupported::US).send().await.unwrap();
507    /// ```
508    pub fn address_create(
509        &self,
510        customer_id: impl Into<CustomerID>,
511        country_code: CountryCodeSupported,
512    ) -> addresses::AddressCreate<'_> {
513        addresses::AddressCreate::new(self, customer_id, country_code)
514    }
515
516    /// Get a request builder for getting an address for a customer using its ID and related customer ID.
517    ///
518    /// # Example:
519    ///
520    /// ```rust,no_run
521    /// use paddle_rust_sdk::Paddle;
522    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
523    /// let customers = client.address_get("ctm_01jqztc78e1xfdgwhcgjzdrvgd", "add_01hv8gwdfkw5z6d1yy6pa3xyrz").send().await.unwrap();
524    /// ```
525    pub fn address_get(
526        &self,
527        customer_id: impl Into<CustomerID>,
528        address_id: impl Into<AddressID>,
529    ) -> addresses::AddressGet<'_> {
530        addresses::AddressGet::new(self, customer_id, address_id)
531    }
532
533    /// Get a request builder for updating an address for a customer using its ID and related customer ID.
534    ///
535    /// # Example:
536    ///
537    /// ```rust,no_run
538    /// use paddle_rust_sdk::Paddle;
539    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
540    /// let customers = client.address_update("ctm_01jqztc78e1xfdgwhcgjzdrvgd", "add_01hv8gwdfkw5z6d1yy6pa3xyrz").first_line("Test").send().await.unwrap();
541    /// ```
542    pub fn address_update(
543        &self,
544        customer_id: impl Into<CustomerID>,
545        address_id: impl Into<AddressID>,
546    ) -> addresses::AddressUpdate<'_> {
547        addresses::AddressUpdate::new(self, customer_id, address_id)
548    }
549
550    /// Get a request builder for fetching customers businesses.
551    ///
552    /// By default, Paddle returns addresses that are `active`. Use the status query parameter to return businesses that are archived.
553    ///
554    /// # Example:
555    ///
556    /// ```rust,no_run
557    /// use paddle_rust_sdk::Paddle;
558    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
559    /// let customers = client.businesses_list("ctm_01jqztc78e1xfdgwhcgjzdrvgd").send().await.unwrap();
560    /// ```
561    pub fn businesses_list(
562        &self,
563        customer_id: impl Into<CustomerID>,
564    ) -> businesses::BusinessesList<'_> {
565        businesses::BusinessesList::new(self, customer_id)
566    }
567
568    /// Get a request builder for creating a new customer business.
569    ///
570    /// # Example:
571    ///
572    /// ```rust,no_run
573    /// use paddle_rust_sdk::Paddle;
574    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
575    /// let customers = client.business_create("ctm_01jqztc78e1xfdgwhcgjzdrvgd", "Company Inc.").send().await.unwrap();
576    /// ```
577    pub fn business_create(
578        &self,
579        customer_id: impl Into<CustomerID>,
580        name: impl Into<String>,
581    ) -> businesses::BusinessCreate<'_> {
582        businesses::BusinessCreate::new(self, customer_id, name)
583    }
584
585    /// Get a request builder for getting a business for a customer using its ID and related customer ID.
586    ///
587    /// # Example:
588    ///
589    /// ```rust,no_run
590    /// use paddle_rust_sdk::Paddle;
591    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
592    /// let customers = client.business_get("ctm_01jqztc78e1xfdgwhcgjzdrvgd", "biz_01jr85bypq4d3w139m53zw2559").send().await.unwrap();
593    /// ```
594    pub fn business_get(
595        &self,
596        customer_id: impl Into<CustomerID>,
597        business_id: impl Into<BusinessID>,
598    ) -> businesses::BusinessGet<'_> {
599        businesses::BusinessGet::new(self, customer_id, business_id)
600    }
601
602    /// Get a request builder for updating a business for a customer using its ID and related customer ID.
603    ///
604    /// # Example:
605    ///
606    /// ```rust,no_run
607    /// use paddle_rust_sdk::Paddle;
608    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
609    /// let customers = client.business_update("ctm_01jqztc78e1xfdgwhcgjzdrvgd", "biz_01jr85bypq4d3w139m53zw2559").first_line("Test").send().await.unwrap();
610    /// ```
611    pub fn business_update(
612        &self,
613        customer_id: impl Into<CustomerID>,
614        business_id: impl Into<BusinessID>,
615    ) -> businesses::BusinessUpdate<'_> {
616        businesses::BusinessUpdate::new(self, customer_id, business_id)
617    }
618
619    /// Get a request builder for querying customer saved payment methods.
620    ///
621    /// # Example:
622    ///
623    /// ```rust,no_run
624    /// use paddle_rust_sdk::Paddle;
625    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
626    /// let customers = client.payment_methods_list("ctm_01jqztc78e1xfdgwhcgjzdrvgd").send().await.unwrap();
627    /// ```
628    pub fn payment_methods_list(
629        &self,
630        customer_id: impl Into<CustomerID>,
631    ) -> payment_methods::PaymentMethodsList<'_> {
632        payment_methods::PaymentMethodsList::new(self, customer_id)
633    }
634
635    /// Get a request builder for getting a saved payment for a customer using its ID and related customer ID.
636    ///
637    /// # Example:
638    ///
639    /// ```rust,no_run
640    /// use paddle_rust_sdk::Paddle;
641    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
642    /// let customers = client.payment_method_get("ctm_01jqztc78e1xfdgwhcgjzdrvgd", "paymtd_01j2jff1m3es31sdkejpaym164").send().await.unwrap();
643    /// ```
644    pub fn payment_method_get(
645        &self,
646        customer_id: impl Into<CustomerID>,
647        payment_method_id: impl Into<PaymentMethodID>,
648    ) -> payment_methods::PaymentMethodGet<'_> {
649        payment_methods::PaymentMethodGet::new(self, customer_id, payment_method_id)
650    }
651
652    /// Deletes a customer payment method using its ID.
653    ///
654    /// When you delete a customer payment method, it's permanently removed from that customer.
655    ///
656    /// There's no way to recover a deleted payment method.
657    ///
658    /// # Example:
659    ///
660    /// ```rust,no_run
661    /// use paddle_rust_sdk::Paddle;
662    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
663    /// client.payment_method_delete("ctm_01jqztc78e1xfdgwhcgjzdrvgd", "paymtd_01j2jff1m3es31sdkejpaym164").await.unwrap();
664    /// ```
665    pub async fn payment_method_delete(
666        &self,
667        customer_id: impl Into<CustomerID>,
668        payment_method_id: impl Into<PaymentMethodID>,
669    ) -> std::result::Result<bool, Error> {
670        let client = reqwest::Client::new();
671
672        let url = format!(
673            "{}customers/{}/payment-methods/{}",
674            self.base_url,
675            customer_id.into().as_ref(),
676            payment_method_id.into().as_ref()
677        );
678
679        let response = client
680            .delete(url)
681            .bearer_auth(self.api_key.clone())
682            .send()
683            .await?;
684
685        Ok(response.status() == StatusCode::NO_CONTENT)
686    }
687
688    /// Creates a customer portal session for a customer.
689    ///
690    /// You can use this to generate authenticated links for a customer so that they're automatically signed in to the portal. Typically used when linking to the customer portal from your app where customers are already authenticated.
691    ///
692    /// You can include an array of subscription_ids to generate authenticated portal links that let customers make changes to their subscriptions. You can use these links as part of subscription management workflows rather than building your own billing screens.
693    ///
694    /// Customer portal sessions are temporary and shouldn't be cached.
695    ///
696    /// The customer portal is fully hosted by Paddle. For security and the best customer experience, don't embed the customer portal in an iframe.
697    ///
698    /// # Example:
699    ///
700    /// ```rust,no_run
701    /// use paddle_rust_sdk::Paddle;
702    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
703    /// let session = client.create_portal_session("ctm_01jqztc78e1xfdgwhcgjzdrvgd").send().await.unwrap();
704    /// dbg!(session.data.urls.general.overview);
705    /// dbg!(session.data.urls.subscriptions);
706    /// ```
707    pub fn create_portal_session(
708        &self,
709        customer_id: impl Into<CustomerID>,
710    ) -> customers::PortalSessionCreate<'_> {
711        customers::PortalSessionCreate::new(self, customer_id)
712    }
713
714    /// Get a request builder for querying transactions.
715    ///
716    /// Use the include method on the builder to include related entities in the response.
717    ///
718    /// # Example:
719    ///
720    /// ```rust,no_run
721    /// use paddle_rust_sdk::Paddle;
722    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
723    /// let transactions = client.transactions_list().send().await.unwrap();
724    /// ```
725    pub fn transactions_list(&self) -> transactions::TransactionsList<'_> {
726        transactions::TransactionsList::new(self)
727    }
728
729    /// Get a request builder for creating a transaction.
730    ///
731    /// See [Create Transaction](https://developer.paddle.com/api-reference/transactions/create-transaction) for more information.
732    ///
733    /// # Example:
734    ///
735    /// ```rust,no_run
736    /// use paddle_rust_sdk::Paddle;
737    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
738    ///
739    /// let res = client.transaction_create()
740    ///     .append_catalog_item("pri_01jqxvdyjkp961jzv4me7ezg4d", 1)
741    ///     .send()
742    ///     .await
743    ///     .unwrap();
744    ///
745    /// dbg!(res.data);
746    /// ```
747    pub fn transaction_create(&self) -> transactions::TransactionCreate<'_> {
748        transactions::TransactionCreate::new(self)
749    }
750
751    /// Get a request builder for fetching a transaction using its ID.
752    ///
753    /// # Example:
754    ///
755    /// ```rust,no_run
756    /// use paddle_rust_sdk::Paddle;
757    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
758    /// let res = client.transaction_get("txn_01hv8wptq8987qeep44cyrewp9").send().await.unwrap();
759    /// dbg!(res.data);
760    /// ```
761    pub fn transaction_get(
762        &self,
763        transaction_id: impl Into<TransactionID>,
764    ) -> transactions::TransactionGet<'_> {
765        transactions::TransactionGet::new(self, transaction_id)
766    }
767
768    /// Get a request builder for updating a transaction.
769    ///
770    /// # Example:
771    ///
772    /// ```rust,no_run
773    /// use paddle_rust_sdk::{enums::TransactionStatus, Paddle};
774    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
775    /// client.transaction_update("txn_01hv8wptq8987qeep44cyrewp9").status(TransactionStatus::Billed).send().await.unwrap();
776    /// ```
777    pub fn transaction_update(
778        &self,
779        transaction_id: impl Into<TransactionID>,
780    ) -> transactions::TransactionUpdate<'_> {
781        transactions::TransactionUpdate::new(self, transaction_id)
782    }
783
784    /// Returns a link to an invoice PDF for a transaction.
785    ///
786    /// Invoice PDFs are available for both automatically and manually-collected transactions:
787    ///   - The PDF for manually-collected transactions includes payment terms, purchase order number, and notes for your customer. It's a demand for payment from your customer. It's available for transactions that are `billed` or `completed`.
788    ///   - The PDF for automatically-collected transactions lets your customer know that payment was taken successfully. Customers may require this for for tax-reporting purposes. It's available for transactions that are `completed`.
789    ///
790    /// Invoice PDFs aren't available for zero-value transactions.
791    ///
792    /// The link returned is not a permanent link. It expires after an hour.
793    ///
794    /// # Example:
795    ///
796    /// ```rust,no_run
797    /// use paddle_rust_sdk::{enums::Disposition, Paddle};
798    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
799    /// let res = client.transaction_invoice("txn_01hv8wptq8987qeep44cyrewp9", Disposition::Inline).await.unwrap();
800    /// dbg!(res.data.url)
801    /// ```
802    pub async fn transaction_invoice(
803        &self,
804        transaction_id: impl Into<TransactionID>,
805        disposition: Disposition,
806    ) -> Result<TransactionInvoice> {
807        let transaction_id = transaction_id.into();
808
809        let url = format!("/transactions/{}/invoice", transaction_id.as_ref());
810        let params = ("disposition", disposition);
811
812        self.send(params, Method::GET, &url).await
813    }
814
815    /// Get a request builder for generating a transaction preview without creating a transaction entity. Typically used for creating more advanced, dynamic pricing pages where users can build their own plans.
816    ///
817    /// You can provide location information when previewing a transaction. You must provide this if you want Paddle to calculate tax or automatically localize prices. You can provide one of:
818    ///   - `customer_ip_address`: Paddle fetches location using the IP address to calculate totals.
819    ///   - `address`: Paddle uses the country and ZIP code (where supplied) to calculate totals.
820    ///   - `customer_id`, `address_id`, `business_id`: Paddle uses existing customer data to calculate totals. Typically used for logged-in customers.
821    ///
822    /// When supplying items, you can exclude items from the total calculation using the `include_in_totals` boolean.
823    ///
824    /// By default, recurring items with trials are considered to have a zero charge when previewing. Set `ignore_trials` to true to ignore trial periods against prices for transaction preview calculations.
825    ///
826    /// If successful, your response includes the data you sent with a details object that includes totals for the supplied prices.
827    ///
828    /// Transaction previews don't create transactions, so no `id` is returned.
829    pub fn transaction_preview(&self) -> transactions::TransactionPreview<'_> {
830        transactions::TransactionPreview::new(self)
831    }
832
833    /// Get a request builder to revise customer information for a billed or completed transaction.
834    ///
835    /// Revise a transaction to rectify incorrect customer, address, or business information on invoice documents generated by Paddle.
836    ///
837    /// You can revise transaction details that don't impact the tax rates on a transaction. This includes:
838    ///   - Customer name
839    ///   - Business name and tax or VAT number (`tax_identifier`)
840    ///   - Address details, apart from the country
841    ///
842    /// You can't remove a valid tax or VAT number, only replace it with another valid one. If a valid tax or VAT number is added, Paddle automatically creates an adjustment to refund any tax where applicable.
843    ///
844    /// Transactions can only be revised once.
845    ///
846    /// If successful, your response includes a copy of the transaction entity. Get a transaction using the `include` parameter with the `customer`, `address`, and `business` values to see the revised customer information.
847    ///
848    /// Only the customer information for this transaction is updated. The related customer, address, and business entities aren't updated.
849    pub fn transaction_revise(
850        &self,
851        transaction_id: impl Into<TransactionID>,
852    ) -> transactions::TransactionRevise<'_> {
853        transactions::TransactionRevise::new(self, transaction_id)
854    }
855
856    /// Get a request builder for querying subscriptions.
857    ///
858    /// # Example:
859    ///
860    /// ```rust,no_run
861    /// use paddle_rust_sdk::Paddle;
862    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
863    /// let subscriptions = client.subscriptions_list().send().await.unwrap();
864    /// ```
865    pub fn subscriptions_list(&self) -> subscriptions::SubscriptionsList<'_> {
866        subscriptions::SubscriptionsList::new(self)
867    }
868
869    /// Get a request builder for fetching a subscription using its ID.
870    ///
871    /// # Example:
872    ///
873    /// ```rust,no_run
874    /// use paddle_rust_sdk::Paddle;
875    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
876    /// let res = client.subscription_get("sub_01hv8y5ehszzq0yv20ttx3166y").send().await.unwrap();
877    /// dbg!(res.data);
878    /// ```
879    pub fn subscription_get(
880        &self,
881        subscription_id: impl Into<SubscriptionID>,
882    ) -> subscriptions::SubscriptionGet<'_> {
883        subscriptions::SubscriptionGet::new(self, subscription_id)
884    }
885
886    /// Get a request builder for getting a preview of changes to a subscription without actually applying them.
887    ///
888    /// Typically used for previewing proration before making changes to a subscription.
889    ///
890    /// If successful, your response includes `immediate_transaction`, `next_transaction`, and `recurring_transaction_details` so you can see expected transactions for the changes.
891    ///
892    /// The `update_summary` object contains details of prorated credits and charges created, along with the overall result of the update.
893    ///
894    /// # Example:
895    ///
896    /// ```rust,no_run
897    /// use paddle_rust_sdk::Paddle;
898    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
899    ///
900    /// let res = client.subscription_preview_update("sub_01hv8y5ehszzq0yv20ttx3166y")
901    ///     .next_billed_at(Utc::now() + Days::new(10))
902    ///     .proration_billing_mode(ProrationBillingMode::ProratedImmediately)
903    ///     .send()
904    ///     .await
905    ///     .unwrap();
906    ///
907    /// dbg!(res.data);
908    /// ```
909    pub fn subscription_preview_update(
910        &self,
911        subscription_id: impl Into<SubscriptionID>,
912    ) -> subscriptions::SubscriptionPreviewUpdate<'_> {
913        subscriptions::SubscriptionPreviewUpdate::new(self, subscription_id)
914    }
915
916    /// Get a request builder for updating a subscription using its ID.
917    ///
918    /// When making changes to items or the next billing date for a subscription, you must include the `proration_billing_mode` field to tell Paddle how to bill for those changes.
919    ///
920    /// Send the complete list of items that you'd like to be on a subscription — including existing items. If you omit items, they're removed from the subscription.
921    ///
922    /// For each item, send `price_id` and `quantity`. Paddle responds with the full price object for each price. If you're updating an existing item, you can omit the `quantity` if you don't want to update it.
923    ///
924    /// If successful, your response includes a copy of the updated subscription entity. When an update results in an immediate charge, responses may take longer than usual while a payment attempt is processed.
925    ///
926    /// # Example:
927    ///
928    /// ```rust,no_run
929    /// use paddle_rust_sdk::Paddle;
930    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
931    /// let res = client.subscription_get("sub_01hv8y5ehszzq0yv20ttx3166y").send().await.unwrap();
932    /// dbg!(res.data);
933    /// ```
934    pub fn subscription_update(
935        &self,
936        subscription_id: impl Into<SubscriptionID>,
937    ) -> subscriptions::SubscriptionUpdate<'_> {
938        subscriptions::SubscriptionUpdate::new(self, subscription_id)
939    }
940
941    /// Returns a transaction that you can pass to a checkout to let customers update their payment details. Only for subscriptions where collection_mode is automatic.
942    ///
943    /// The transaction returned depends on the status of the related subscription:
944    /// - Where a subscription is `past_due`, it returns the most recent `past_due` transaction.
945    /// - Where a subscription is `active`, it creates a new zero amount transaction for the items on a subscription.
946    ///
947    /// You can use the returned `checkout.url`, or pass the returned transaction ID to Paddle.js to open a checkout to present customers with a way of updating their payment details.
948    ///
949    /// The `customer`, `address`, `business`, `discount`, `adjustments` and `adjustments_totals` properties are only returned in the response if the API key has read permissions for those related entities.
950    pub async fn subscription_update_payment_method_transaction(
951        &self,
952        subscription_id: impl Into<SubscriptionID>,
953    ) -> Result<Transaction> {
954        let subscription_id = subscription_id.into();
955
956        let url = format!(
957            "/subscriptions/{}/update-payment-method-transaction",
958            subscription_id.as_ref()
959        );
960
961        self.send((), Method::GET, &url).await
962    }
963
964    /// Get a request builder for previewing creating a one-time charge for a subscription without billing that charge. Typically used for previewing calculations before making changes to a subscription.
965    ///
966    /// One-time charges are non-recurring items. These are price entities where the `billing_cycle` is `null`.
967    ///
968    /// If successful, your response includes `immediate_transaction`, `next_transaction`, and `recurring_transaction_details` so you can see expected transactions for the changes.
969    pub fn subscription_preview_one_time_charge(
970        &self,
971        subscription_id: impl Into<SubscriptionID>,
972    ) -> subscriptions::SubscriptionOneTimeChargePreview<'_> {
973        subscriptions::SubscriptionOneTimeChargePreview::new(self, subscription_id)
974    }
975
976    /// Get a request builder for creating a new one-time charge for a subscription. Use to bill non-recurring items to a subscription. Non-recurring items are price entities where the `billing_cycle` is `null`.
977    ///
978    /// If successful, Paddle responds with the updated subscription entity. However, one-time charges aren't held against the subscription entity, so the charges billed aren't returned in the response.
979    ///
980    /// Once created, to get details of a one-time charge:
981    /// - When created with `effective_from` as `next_billing_period`, get the subscription the charge was billed to and use the `include` query parameter with the `next_transaction` value.
982    /// - When created with `effective_from` as `immediately`, list transactions and use the `subscription_id` query parameter with the subscription ID of the subscription the charge was billed to.
983    ///
984    /// When an update results in an immediate charge, responses may take longer than usual while a payment attempt is processed.
985    pub fn subscription_one_time_charge(
986        &self,
987        subscription_id: impl Into<SubscriptionID>,
988    ) -> subscriptions::SubscriptionOneTimeCharge<'_> {
989        subscriptions::SubscriptionOneTimeCharge::new(self, subscription_id)
990    }
991
992    /// Activates a trialing subscription using its ID. Only automatically-collected subscriptions where the status is trialing can be activated.
993    ///
994    /// On activation, Paddle bills for a subscription immediately. Subscription billing dates are recalculated based on the activation date (the time the activation request is made).
995    ///
996    /// If successful, Paddle returns a copy of the updated subscription entity. The subscription status is `active`, and billing dates are updated to reflect the activation date.
997    ///
998    /// This operation results in an immediate charge, so responses may take longer than usual while a payment attempt is processed.
999    pub async fn subscription_activate(
1000        &self,
1001        subscription_id: impl Into<SubscriptionID>,
1002    ) -> Result<Subscription> {
1003        let subscription_id = subscription_id.into();
1004
1005        let url = format!("/subscriptions/{}/activate", subscription_id.as_ref());
1006
1007        self.send(serde_json::json!({}), Method::POST, &url).await
1008    }
1009
1010    /// Get a request builder for pausing a subscription using its ID.
1011    ///
1012    /// By default, subscriptions are paused at the end of the billing period. When you send a request to pause, Paddle creates a `scheduled_change` against the subscription entity to say that it should pause at the end of the current billing period. Its `status` remains `active` until after the effective date of the scheduled change, at which point it changes to `paused`.
1013    ///
1014    /// To set a resume date, include the `resume_at` field in your request. The subscription remains paused until the resume date, or until you send a resume request. Omit to create an open-ended pause. The subscription remains paused indefinitely, until you send a resume request.
1015    pub fn subscription_pause(
1016        &self,
1017        subscription_id: impl Into<SubscriptionID>,
1018    ) -> subscriptions::SubscriptionPause<'_> {
1019        subscriptions::SubscriptionPause::new(self, subscription_id)
1020    }
1021
1022    /// Resumes a paused subscription using its ID. Only `paused` subscriptions can be resumed. If an `active` subscription has a scheduled change to pause in the future, use this operation to set or change the resume date.
1023    ///
1024    /// You can't resume a `canceled` subscription.
1025    ///
1026    /// On resume, Paddle bills for a subscription immediately by default. Subscription billing dates are recalculated based on the resume date. Use the `on_resume` field to change this behavior.
1027    ///
1028    /// If successful, Paddle returns a copy of the updated subscription entity:
1029    /// - When resuming a `paused` subscription immediately, the subscription status is `active`, and billing dates are updated to reflect the resume date.
1030    /// - When scheduling a `paused` subscription to resume on a date in the future, the subscription status is `paused`, and `scheduled_change.resume_at` is updated to reflect the scheduled resume date.
1031    /// - When changing the resume date for an `active` subscription that's scheduled to pause, the subscription status is `active` and `scheduled_change.resume_at` is updated to reflect the scheduled resume date.
1032    ///
1033    /// This operation may result in an immediate charge, so responses may take longer than usual while a payment attempt is processed.
1034    pub fn subscription_resume(
1035        &self,
1036        subscription_id: impl Into<SubscriptionID>,
1037    ) -> subscriptions::SubscriptionResume<'_> {
1038        subscriptions::SubscriptionResume::new(self, subscription_id)
1039    }
1040
1041    /// Get a request builder for canceling a subscription.
1042    ///
1043    /// By default, active subscriptions are canceled at the end of the billing period. When you send a request to cancel, Paddle creates a `scheduled_change` against the subscription entity to say that it should cancel at the end of the current billing period. Its `status` remains `active` until after the effective date of the scheduled change, at which point it changes to `canceled`.
1044    ///
1045    /// You can cancel a subscription right away by including `effective_from` in your request, setting the value to `immediately`. If successful, your response includes a copy of the updated subscription entity with the `status` of `canceled`. Canceling immediately is the default behavior for paused subscriptions.
1046    ///
1047    /// You can't reinstate a canceled subscription.
1048    pub fn subscription_cancel(
1049        &self,
1050        subscription_id: impl Into<SubscriptionID>,
1051    ) -> subscriptions::SubscriptionCancel<'_> {
1052        subscriptions::SubscriptionCancel::new(self, subscription_id)
1053    }
1054
1055    /// Get a request builder for retrieving adjustments from Paddle.
1056    ///
1057    /// Use the builder parameters to filter and page through results.
1058    ///
1059    /// # Example:
1060    ///
1061    /// ```rust,no_run
1062    /// use paddle_rust_sdk::Paddle;
1063    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
1064    /// let res = client.adjustments_list().send().await.unwrap();
1065    /// dbg!(res.data);
1066    /// ```
1067    pub fn adjustments_list(&self) -> adjustments::AdjustmentsList<'_> {
1068        adjustments::AdjustmentsList::new(self)
1069    }
1070
1071    /// Get a request builder for creating an adjustment for one or more transaction items.
1072    ///
1073    /// You can create adjustments to refund or credit all or part of a transaction and its items:
1074    /// - Refunds return an amount to a customer's original payment method. You can create refund adjustments for transactions that are `completed`.
1075    /// - Credits reduce the amount that a customer has to pay for a transaction. You can create credit adjustments for manually-collected transactions that are `billed` or `past_due`.
1076    ///
1077    /// You can create adjustments to refund transactions that are `completed`, or to reduce the amount to due on manually-collected transactions that are `billed` or `past_due`. Most refunds for live accounts are created with the status of `pending_approval` until reviewed by Paddle, but some are automatically approved. For sandbox accounts, Paddle automatically approves refunds every ten minutes.
1078    ///
1079    /// Adjustments can apply to some or all items on a transaction. You'll need the Paddle ID of the transaction to create a refund or credit for, along with the Paddle ID of any transaction items `(details.line_items[].id)`.
1080    ///
1081    /// # Example:
1082    ///
1083    /// ```rust,no_run
1084    /// use paddle_rust_sdk::{
1085    ///     enums::{AdjustmentAction, AdjustmentType},
1086    ///     Paddle,
1087    /// };
1088    ///
1089    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
1090    ///
1091    /// let res = client.adjustment_create("txn_01jkfx8v9z4pee0p5bd35x95bp", AdjustmentAction::Refund, "Refund reason")
1092    ///     .r#type(AdjustmentType::Full)
1093    ///     .send()
1094    ///     .await
1095    ///     .unwrap();
1096    ///
1097    /// dbg!(res.data);
1098    /// ```
1099    pub fn adjustment_create(
1100        &self,
1101        transaction_id: impl Into<TransactionID>,
1102        action: AdjustmentAction,
1103        reason: impl Into<String>,
1104    ) -> adjustments::AdjustmentCreate<'_> {
1105        adjustments::AdjustmentCreate::new(self, transaction_id, action, reason)
1106    }
1107
1108    /// Returns a link to a credit note PDF for an adjustment.
1109    ///
1110    /// Credit note PDFs are created for refunds and credits as a record of an adjustment.
1111    ///
1112    /// The link returned is not a permanent link. It expires after an hour.
1113    ///
1114    /// # Example:
1115    ///
1116    /// ```rust,no_run
1117    /// use paddle_rust_sdk::{enums::Disposition, Paddle};
1118    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
1119    /// let res = client.adjustment_credit_note("txn_01hv8wptq8987qeep44cyrewp9", Disposition::Inline).await.unwrap();
1120    /// dbg!(res.data.url)
1121    /// ```
1122    pub async fn adjustment_credit_note(
1123        &self,
1124        adjustment_id: impl Into<AdjustmentID>,
1125        disposition: Disposition,
1126    ) -> Result<TransactionInvoice> {
1127        let adjustment_id = adjustment_id.into();
1128
1129        let url = format!("/adjustments/{}/credit-note", adjustment_id.as_ref());
1130        let params = ("disposition", disposition);
1131
1132        self.send(params, Method::GET, &url).await
1133    }
1134
1135    /// Get a request builder for fetching pricing previews for one or more prices. Typically used for building pricing pages.
1136    ///
1137    /// You can provide location information when previewing prices. You must provide this if you want Paddle to calculate tax or automatically localize prices. You can provide one of:
1138    /// - `customer_ip_address`: Paddle fetches location using the IP address to calculate totals.
1139    /// - `address`: Paddle uses the country and ZIP code (where supplied) to calculate totals.
1140    /// - `customer_id`, `address_id`, `business_id`: Paddle uses existing customer data to calculate totals. Typically used for logged-in customers.
1141    ///
1142    /// If successful, your response includes the data you sent with a details object that includes totals for the supplied prices.
1143    ///
1144    /// Each line item includes `formatted_unit_totals` and `formatted_totals` objects that return totals formatted for the country or region you're working with, including the currency symbol.
1145    ///
1146    /// You can work with the preview prices operation using the Paddle.PricePreview() method in Paddle.js. When working with Paddle.PricePreview(), request and response fields are camelCase rather than snake_case.
1147    ///
1148    /// # Example:
1149    ///
1150    /// ```rust,no_run
1151    /// use paddle_rust_sdk::{enums::Disposition, Paddle};
1152    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
1153    ///
1154    /// let res = client.pricing_preview()
1155    ///     .send([PricePreviewItem { price_id: "pri_01jqxvdyjkp961jzv4me7ezg4d".into(), quantity: 1, }])
1156    ///     .await
1157    ///     .unwrap();
1158    ///
1159    /// dbg!(res.data)
1160    /// ```
1161    pub fn pricing_preview(
1162        &self,
1163        items: impl IntoIterator<Item = PricePreviewItem>,
1164    ) -> pricing_preview::PricingPreview<'_> {
1165        pricing_preview::PricingPreview::new(self, items)
1166    }
1167
1168    /// Get a request builder for fetching a single report in Paddle.
1169    pub fn reports_list<'a>(&'a self) -> reports::ReportsList<'a> {
1170        reports::ReportsList::new(self)
1171    }
1172
1173    /// Returns a report using its ID.
1174    pub async fn report_get(&self, report_id: impl Into<PaddleID>) -> Result<ReportBase> {
1175        let report_id = report_id.into();
1176
1177        let url = format!("/reports/{}", report_id.as_ref());
1178
1179        self.send((), Method::GET, &url).await
1180    }
1181
1182    /// Returns a link to a CSV file for a report.
1183    ///
1184    /// Only returned for reports that are ready. This means Paddle has completed processing the report and it's ready to download.
1185    ///
1186    /// The link returned is not a permanent link. It expires after 3 minutes.
1187    pub async fn report_download_url(
1188        &self,
1189        report_id: impl Into<PaddleID>,
1190    ) -> Result<TransactionInvoice> {
1191        let report_id = report_id.into();
1192
1193        let url = format!("/reports/{}/download-url", report_id.as_ref());
1194
1195        self.send((), Method::GET, &url).await
1196    }
1197
1198    /// Get a request builder for creating reports in Paddle.
1199    ///
1200    /// Reports are created as `pending` initially while Paddle generates your report. They move to `ready` when they're ready to download.
1201    ///
1202    /// You can download a report when it's ready using the get a CSV file for a report operation.
1203    ///
1204    /// If successful, your response includes a copy of the new report entity.
1205    pub fn report_create<'a, T: ReportType + DeserializeOwned>(
1206        &'a self,
1207        report_type: T,
1208    ) -> reports::ReportCreate<'a, T> {
1209        reports::ReportCreate::new(self, report_type)
1210    }
1211
1212    /// Returns a list of event types.
1213    ///
1214    /// The response is not paginated.
1215    ///
1216    /// # Example:
1217    ///
1218    /// ```rust,no_run
1219    /// use paddle_rust_sdk::{enums::Disposition, Paddle};
1220    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
1221    /// let res = client.event_types_list().await.unwrap();
1222    /// dbg!(res.data)
1223    /// ```
1224    pub async fn event_types_list(&self) -> Result<Vec<EventType>> {
1225        self.send((), Method::GET, "/event-types").await
1226    }
1227
1228    /// Returns a list of event types.
1229    ///
1230    /// The response is not paginated.
1231    ///
1232    /// # Example:
1233    ///
1234    /// ```rust,no_run
1235    /// use paddle_rust_sdk::{enums::Disposition, Paddle};
1236    /// let client = Paddle::new("your_api_key", Paddle::SANDBOX).unwrap();
1237    /// let res = client.events_list().send().await.unwrap();
1238    /// dbg!(res.data)
1239    /// ```
1240    pub fn events_list(&self) -> events::EventsList<'_> {
1241        events::EventsList::new(self)
1242    }
1243
1244    async fn send<T: DeserializeOwned>(
1245        &self,
1246        req: impl Serialize,
1247        method: Method,
1248        path: &str,
1249    ) -> Result<T> {
1250        let mut url = self.base_url.join(path)?;
1251        let client = reqwest::Client::new();
1252
1253        if method == reqwest::Method::GET {
1254            url.set_query(Some(&serde_qs::to_string(&req)?));
1255        }
1256
1257        let mut builder = client
1258            .request(method.clone(), url)
1259            .bearer_auth(self.api_key.clone())
1260            .header(CONTENT_TYPE, "application/json; charset=utf-8");
1261
1262        builder = match method {
1263            reqwest::Method::POST | reqwest::Method::PUT | reqwest::Method::PATCH => {
1264                builder.json(&req)
1265            }
1266            _ => builder,
1267        };
1268
1269        // Uncomment this to see the raw text response
1270        // let text = builder.send().await?.text().await?;
1271        // println!("{}", text);
1272        // todo!();
1273
1274        // Uncomment this to attempt to deserialize the response into an entity
1275        // Needed due to https://github.com/serde-rs/serde/issues/2157
1276
1277        // let res: serde_json::Value = builder.send().await?.json().await?;
1278        // let data_json = serde_json::to_string(&res["data"]).unwrap();
1279        // let res: Vec<entities::ReportBase> = serde_json::from_str(&data_json).unwrap();
1280        // // println!("{}", serde_json::to_string(&res["data"]).unwrap());
1281        // todo!();
1282
1283        let res: Response<_> = builder.send().await?.json().await?;
1284
1285        match res {
1286            Response::Success(success) => Ok(success),
1287            Response::Error(error) => Err(Error::PaddleApi(error)),
1288        }
1289    }
1290}
1291
1292fn comma_separated<S, T>(
1293    values: &Option<Vec<T>>,
1294    serializer: S,
1295) -> std::result::Result<S::Ok, S::Error>
1296where
1297    S: serde::Serializer,
1298    T: AsRef<str>,
1299{
1300    match values {
1301        Some(values) => {
1302            let values = values
1303                .iter()
1304                .map(|v| v.as_ref())
1305                .collect::<Vec<_>>()
1306                .join(",");
1307
1308            serializer.serialize_str(&values)
1309        }
1310        None => serializer.serialize_none(),
1311    }
1312}
1313
1314fn comma_separated_enum<S, T>(
1315    values: &Option<Vec<T>>,
1316    serializer: S,
1317) -> std::result::Result<S::Ok, S::Error>
1318where
1319    S: serde::Serializer,
1320    T: Serialize,
1321{
1322    match values {
1323        Some(values) => {
1324            let mut serialized = vec![];
1325
1326            for val in values {
1327                let serde_value = serde_json::to_value(val).map_err(serde::ser::Error::custom)?;
1328                let serialized_value = serde_value
1329                    .as_str()
1330                    .ok_or(serde::ser::Error::custom("Failed to serialize enum"))?
1331                    .to_string();
1332
1333                serialized.push(serialized_value);
1334            }
1335
1336            serializer.serialize_str(serialized.join(",").as_str())
1337        }
1338        None => serializer.serialize_none(),
1339    }
1340}