stripe_core/payment_source/
requests.rs

1use stripe_client_core::{
2    RequestBuilder, StripeBlockingClient, StripeClient, StripeMethod, StripeRequest,
3};
4
5#[derive(Clone, Debug, serde::Serialize)]
6struct ListCustomerPaymentSourceBuilder {
7    #[serde(skip_serializing_if = "Option::is_none")]
8    ending_before: Option<String>,
9    #[serde(skip_serializing_if = "Option::is_none")]
10    expand: Option<Vec<String>>,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    limit: Option<i64>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    object: Option<String>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    starting_after: Option<String>,
17}
18impl ListCustomerPaymentSourceBuilder {
19    fn new() -> Self {
20        Self { ending_before: None, expand: None, limit: None, object: None, starting_after: None }
21    }
22}
23/// List sources for a specified customer.
24#[derive(Clone, Debug, serde::Serialize)]
25pub struct ListCustomerPaymentSource {
26    inner: ListCustomerPaymentSourceBuilder,
27    customer: stripe_shared::CustomerId,
28}
29impl ListCustomerPaymentSource {
30    /// Construct a new `ListCustomerPaymentSource`.
31    pub fn new(customer: impl Into<stripe_shared::CustomerId>) -> Self {
32        Self { customer: customer.into(), inner: ListCustomerPaymentSourceBuilder::new() }
33    }
34    /// A cursor for use in pagination.
35    /// `ending_before` is an object ID that defines your place in the list.
36    /// For instance, if you make a list request and receive 100 objects, starting with `obj_bar`, your subsequent call can include `ending_before=obj_bar` in order to fetch the previous page of the list.
37    pub fn ending_before(mut self, ending_before: impl Into<String>) -> Self {
38        self.inner.ending_before = Some(ending_before.into());
39        self
40    }
41    /// Specifies which fields in the response should be expanded.
42    pub fn expand(mut self, expand: impl Into<Vec<String>>) -> Self {
43        self.inner.expand = Some(expand.into());
44        self
45    }
46    /// A limit on the number of objects to be returned.
47    /// Limit can range between 1 and 100, and the default is 10.
48    pub fn limit(mut self, limit: impl Into<i64>) -> Self {
49        self.inner.limit = Some(limit.into());
50        self
51    }
52    /// Filter sources according to a particular object type.
53    pub fn object(mut self, object: impl Into<String>) -> Self {
54        self.inner.object = Some(object.into());
55        self
56    }
57    /// A cursor for use in pagination.
58    /// `starting_after` is an object ID that defines your place in the list.
59    /// For instance, if you make a list request and receive 100 objects, ending with `obj_foo`, your subsequent call can include `starting_after=obj_foo` in order to fetch the next page of the list.
60    pub fn starting_after(mut self, starting_after: impl Into<String>) -> Self {
61        self.inner.starting_after = Some(starting_after.into());
62        self
63    }
64}
65impl ListCustomerPaymentSource {
66    /// Send the request and return the deserialized response.
67    pub async fn send<C: StripeClient>(
68        &self,
69        client: &C,
70    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
71        self.customize().send(client).await
72    }
73
74    /// Send the request and return the deserialized response, blocking until completion.
75    pub fn send_blocking<C: StripeBlockingClient>(
76        &self,
77        client: &C,
78    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
79        self.customize().send_blocking(client)
80    }
81
82    pub fn paginate(
83        &self,
84    ) -> stripe_client_core::ListPaginator<stripe_types::List<stripe_shared::PaymentSource>> {
85        let customer = &self.customer;
86
87        stripe_client_core::ListPaginator::new_list(
88            format!("/customers/{customer}/sources"),
89            &self.inner,
90        )
91    }
92}
93
94impl StripeRequest for ListCustomerPaymentSource {
95    type Output = stripe_types::List<stripe_shared::PaymentSource>;
96
97    fn build(&self) -> RequestBuilder {
98        let customer = &self.customer;
99        RequestBuilder::new(StripeMethod::Get, format!("/customers/{customer}/sources"))
100            .query(&self.inner)
101    }
102}
103#[derive(Clone, Debug, serde::Serialize)]
104struct RetrievePaymentSourceBuilder {
105    #[serde(skip_serializing_if = "Option::is_none")]
106    expand: Option<Vec<String>>,
107}
108impl RetrievePaymentSourceBuilder {
109    fn new() -> Self {
110        Self { expand: None }
111    }
112}
113/// Retrieve a specified source for a given customer.
114#[derive(Clone, Debug, serde::Serialize)]
115pub struct RetrievePaymentSource {
116    inner: RetrievePaymentSourceBuilder,
117    customer: stripe_shared::CustomerId,
118    id: String,
119}
120impl RetrievePaymentSource {
121    /// Construct a new `RetrievePaymentSource`.
122    pub fn new(customer: impl Into<stripe_shared::CustomerId>, id: impl Into<String>) -> Self {
123        Self {
124            customer: customer.into(),
125            id: id.into(),
126            inner: RetrievePaymentSourceBuilder::new(),
127        }
128    }
129    /// Specifies which fields in the response should be expanded.
130    pub fn expand(mut self, expand: impl Into<Vec<String>>) -> Self {
131        self.inner.expand = Some(expand.into());
132        self
133    }
134}
135impl RetrievePaymentSource {
136    /// Send the request and return the deserialized response.
137    pub async fn send<C: StripeClient>(
138        &self,
139        client: &C,
140    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
141        self.customize().send(client).await
142    }
143
144    /// Send the request and return the deserialized response, blocking until completion.
145    pub fn send_blocking<C: StripeBlockingClient>(
146        &self,
147        client: &C,
148    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
149        self.customize().send_blocking(client)
150    }
151}
152
153impl StripeRequest for RetrievePaymentSource {
154    type Output = stripe_shared::PaymentSource;
155
156    fn build(&self) -> RequestBuilder {
157        let customer = &self.customer;
158        let id = &self.id;
159        RequestBuilder::new(StripeMethod::Get, format!("/customers/{customer}/sources/{id}"))
160            .query(&self.inner)
161    }
162}
163#[derive(Clone, Debug, serde::Serialize)]
164struct CreateCustomerPaymentSourceBuilder {
165    #[serde(skip_serializing_if = "Option::is_none")]
166    expand: Option<Vec<String>>,
167    #[serde(skip_serializing_if = "Option::is_none")]
168    metadata: Option<std::collections::HashMap<String, String>>,
169    source: String,
170    #[serde(skip_serializing_if = "Option::is_none")]
171    validate: Option<bool>,
172}
173impl CreateCustomerPaymentSourceBuilder {
174    fn new(source: impl Into<String>) -> Self {
175        Self { expand: None, metadata: None, source: source.into(), validate: None }
176    }
177}
178/// When you create a new credit card, you must specify a customer or recipient on which to create it.
179///
180/// If the card’s owner has no default card, then the new card will become the default.
181/// However, if the owner already has a default, then it will not change.
182/// To change the default, you should [update the customer](https://stripe.com/docs/api#update_customer) to have a new `default_source`.
183#[derive(Clone, Debug, serde::Serialize)]
184pub struct CreateCustomerPaymentSource {
185    inner: CreateCustomerPaymentSourceBuilder,
186    customer: stripe_shared::CustomerId,
187}
188impl CreateCustomerPaymentSource {
189    /// Construct a new `CreateCustomerPaymentSource`.
190    pub fn new(customer: impl Into<stripe_shared::CustomerId>, source: impl Into<String>) -> Self {
191        Self {
192            customer: customer.into(),
193            inner: CreateCustomerPaymentSourceBuilder::new(source.into()),
194        }
195    }
196    /// Specifies which fields in the response should be expanded.
197    pub fn expand(mut self, expand: impl Into<Vec<String>>) -> Self {
198        self.inner.expand = Some(expand.into());
199        self
200    }
201    /// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object.
202    /// This can be useful for storing additional information about the object in a structured format.
203    /// Individual keys can be unset by posting an empty value to them.
204    /// All keys can be unset by posting an empty value to `metadata`.
205    pub fn metadata(
206        mut self,
207        metadata: impl Into<std::collections::HashMap<String, String>>,
208    ) -> Self {
209        self.inner.metadata = Some(metadata.into());
210        self
211    }
212    pub fn validate(mut self, validate: impl Into<bool>) -> Self {
213        self.inner.validate = Some(validate.into());
214        self
215    }
216}
217impl CreateCustomerPaymentSource {
218    /// Send the request and return the deserialized response.
219    pub async fn send<C: StripeClient>(
220        &self,
221        client: &C,
222    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
223        self.customize().send(client).await
224    }
225
226    /// Send the request and return the deserialized response, blocking until completion.
227    pub fn send_blocking<C: StripeBlockingClient>(
228        &self,
229        client: &C,
230    ) -> Result<<Self as StripeRequest>::Output, C::Err> {
231        self.customize().send_blocking(client)
232    }
233}
234
235impl StripeRequest for CreateCustomerPaymentSource {
236    type Output = stripe_shared::PaymentSource;
237
238    fn build(&self) -> RequestBuilder {
239        let customer = &self.customer;
240        RequestBuilder::new(StripeMethod::Post, format!("/customers/{customer}/sources"))
241            .form(&self.inner)
242    }
243}