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