paddle_rust_sdk/
payment_methods.rs

1//! Builders for making requests to the Paddle API for customer saved payment methods.
2//!
3//! See the [Paddle API](https://developer.paddle.com/api-reference/payment-methods/overview) documentation for more information.
4
5use reqwest::Method;
6use serde::Serialize;
7use serde_with::skip_serializing_none;
8
9use crate::entities::PaymentMethod;
10use crate::ids::{AddressID, CustomerID, PaymentMethodID};
11use crate::paginated::Paginated;
12use crate::{Paddle, Result};
13
14/// Request builder for fetching businesses from Paddle API.
15#[skip_serializing_none]
16#[derive(Serialize)]
17pub struct PaymentMethodsList<'a> {
18    #[serde(skip)]
19    client: &'a Paddle,
20    #[serde(skip)]
21    customer_id: CustomerID,
22    #[serde(serialize_with = "crate::comma_separated")]
23    address_id: Option<Vec<AddressID>>,
24    after: Option<PaymentMethodID>,
25    order_by: Option<String>,
26    per_page: Option<usize>,
27    supports_checkout: Option<bool>,
28}
29
30impl<'a> PaymentMethodsList<'a> {
31    pub fn new(client: &'a Paddle, customer_id: impl Into<CustomerID>) -> Self {
32        Self {
33            client,
34            customer_id: customer_id.into(),
35            after: None,
36            address_id: None,
37            order_by: None,
38            per_page: None,
39            supports_checkout: None,
40        }
41    }
42
43    /// Return entities related to the specified addresses.
44    pub fn address_ids(
45        &mut self,
46        address_ids: impl IntoIterator<Item = impl Into<AddressID>>,
47    ) -> &mut Self {
48        self.address_id = Some(address_ids.into_iter().map(Into::into).collect());
49        self
50    }
51
52    /// Return entities after the specified Paddle ID when working with paginated endpoints. Used in the `meta.pagination.next` URL in responses for list operations.
53    pub fn after(&mut self, id: impl Into<PaymentMethodID>) -> &mut Self {
54        self.after = Some(id.into());
55        self
56    }
57
58    /// Order returned entities by the specified field. Valid fields for ordering: id
59    pub fn order_by_asc(&mut self, field: &str) -> &mut Self {
60        self.order_by = Some(format!("{}[ASC]", field));
61        self
62    }
63
64    /// Order returned entities by the specified field. Valid fields for ordering: id
65    pub fn order_by_desc(&mut self, field: &str) -> &mut Self {
66        self.order_by = Some(format!("{}[DESC]", field));
67        self
68    }
69
70    /// Set how many entities are returned per page. Paddle returns the maximum number of results if a number greater than the maximum is requested.
71    /// Check `meta.pagination.per_page` in the response to see how many were returned.
72    ///
73    /// Default: `50`; Maximum: `200`.
74    pub fn per_page(&mut self, entities_per_page: usize) -> &mut Self {
75        self.per_page = Some(entities_per_page);
76        self
77    }
78
79    /// Return entities that support being presented at checkout (`true`) or not (`false`).
80    pub fn supports_checkout(&mut self, flag: bool) -> &mut Self {
81        self.supports_checkout = Some(flag);
82        self
83    }
84
85    /// Returns a paginator for fetching pages of entities from Paddle
86    pub fn send(&self) -> Paginated<Vec<PaymentMethod>> {
87        let url = format!("/customers/{}/payment-methods", self.customer_id.as_ref());
88
89        Paginated::new(self.client, &url, self)
90    }
91}
92
93/// Request builder for fetching a single payment method from Paddle API.
94#[skip_serializing_none]
95#[derive(Serialize)]
96pub struct PaymentMethodGet<'a> {
97    #[serde(skip)]
98    client: &'a Paddle,
99    #[serde(skip)]
100    customer_id: CustomerID,
101    #[serde(skip)]
102    payment_method_id: PaymentMethodID,
103}
104
105impl<'a> PaymentMethodGet<'a> {
106    pub fn new(
107        client: &'a Paddle,
108        customer_id: impl Into<CustomerID>,
109        payment_method_id: impl Into<PaymentMethodID>,
110    ) -> Self {
111        Self {
112            client,
113            customer_id: customer_id.into(),
114            payment_method_id: payment_method_id.into(),
115        }
116    }
117
118    /// Send the request to Paddle and return the response.
119    pub async fn send(&self) -> Result<PaymentMethod> {
120        self.client
121            .send(
122                self,
123                Method::GET,
124                &format!(
125                    "/customers/{}/payment-methods/{}",
126                    self.customer_id.as_ref(),
127                    self.payment_method_id.as_ref()
128                ),
129            )
130            .await
131    }
132}