Skip to main content

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