paddle_rust_sdk/
pricing_preview.rs

1//! Builders for making requests to the Paddle API for previewing prices.
2//!
3//! See the [Paddle API](https://developer.paddle.com/api-reference/pricing-preview/overview) documentation for more information.
4
5use reqwest::Method;
6use serde::Serialize;
7use serde_with::skip_serializing_none;
8
9use crate::entities::{self, AddressPreview, PricePreviewItem};
10use crate::enums::CurrencyCode;
11use crate::ids::{AddressID, BusinessID, CustomerID, DiscountID};
12use crate::{Paddle, Result};
13
14/// Request builder for fetching transactions from Paddle API.
15#[skip_serializing_none]
16#[derive(Serialize)]
17pub struct PricingPreview<'a> {
18    #[serde(skip)]
19    client: &'a Paddle,
20    items: Vec<PricePreviewItem>,
21    customer_id: Option<CustomerID>,
22    address_id: Option<AddressID>,
23    business_id: Option<BusinessID>,
24    currency_code: Option<CurrencyCode>,
25    discount_id: Option<DiscountID>,
26    address: Option<AddressPreview>,
27    customer_ip_address: Option<String>,
28}
29
30impl<'a> PricingPreview<'a> {
31    pub fn new(client: &'a Paddle, items: impl IntoIterator<Item = PricePreviewItem>) -> Self {
32        Self {
33            client,
34            items: items.into_iter().collect(),
35            customer_id: None,
36            address_id: None,
37            business_id: None,
38            currency_code: None,
39            discount_id: None,
40            address: None,
41            customer_ip_address: None,
42        }
43    }
44
45    /// Paddle ID of the customer that this preview is for.
46    pub fn customer_id(&mut self, customer_id: impl Into<CustomerID>) -> &mut Self {
47        self.customer_id = Some(customer_id.into());
48        self
49    }
50
51    /// Paddle ID of the address that this preview is for.
52    ///
53    /// Send one of `address_id`, `customer_ip_address`, or the `address` object when previewing.
54    pub fn address_id(&mut self, address_id: impl Into<AddressID>) -> &mut Self {
55        self.address_id = Some(address_id.into());
56        self
57    }
58
59    /// Paddle ID of the business that this preview is for.
60    pub fn business_id(&mut self, business_id: impl Into<BusinessID>) -> &mut Self {
61        self.business_id = Some(business_id.into());
62        self
63    }
64
65    /// Supported three-letter ISO 4217 currency code.
66    pub fn currency_code(&mut self, currency_code: CurrencyCode) -> &mut Self {
67        self.currency_code = Some(currency_code);
68        self
69    }
70
71    /// Paddle ID of the discount applied to this preview
72    pub fn discount_id(&mut self, discount_id: impl Into<DiscountID>) -> &mut Self {
73        self.discount_id = Some(discount_id.into());
74        self
75    }
76
77    /// Address for this preview. Send one of `address_id`, `customer_ip_address`, or the `address` object when previewing.
78    pub fn address(&mut self, address: AddressPreview) -> &mut Self {
79        self.address = Some(address);
80        self
81    }
82
83    /// IP address for this transaction preview. Send one of `address_id`, `customer_ip_address`, or the `address` object when previewing.
84    pub fn customer_ip_address(&mut self, ip: String) -> &mut Self {
85        self.customer_ip_address = Some(ip);
86        self
87    }
88
89    /// Send the request to Paddle and return the response.
90    pub async fn send(&self) -> Result<entities::PricingPreview> {
91        self.client
92            .send(self, Method::POST, "/pricing-preview")
93            .await
94    }
95}