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