use reqwest::Method;
use serde::Serialize;
use serde_with::skip_serializing_none;
use crate::entities::PaymentMethod;
use crate::ids::{AddressID, CustomerID, PaymentMethodID};
use crate::paginated::Paginated;
use crate::{Paddle, Result};
#[skip_serializing_none]
#[derive(Serialize)]
pub struct PaymentMethodsList<'a> {
#[serde(skip)]
client: &'a Paddle,
#[serde(skip)]
customer_id: CustomerID,
#[serde(serialize_with = "crate::comma_separated")]
address_id: Option<Vec<AddressID>>,
after: Option<PaymentMethodID>,
order_by: Option<String>,
per_page: Option<usize>,
supports_checkout: Option<bool>,
}
impl<'a> PaymentMethodsList<'a> {
pub fn new(client: &'a Paddle, customer_id: impl Into<CustomerID>) -> Self {
Self {
client,
customer_id: customer_id.into(),
after: None,
address_id: None,
order_by: None,
per_page: None,
supports_checkout: None,
}
}
pub fn address_ids(
&mut self,
address_ids: impl IntoIterator<Item = impl Into<AddressID>>,
) -> &mut Self {
self.address_id = Some(address_ids.into_iter().map(Into::into).collect());
self
}
pub fn after(&mut self, id: impl Into<PaymentMethodID>) -> &mut Self {
self.after = Some(id.into());
self
}
pub fn order_by_asc(&mut self, field: &str) -> &mut Self {
self.order_by = Some(format!("{}[ASC]", field));
self
}
pub fn order_by_desc(&mut self, field: &str) -> &mut Self {
self.order_by = Some(format!("{}[DESC]", field));
self
}
pub fn per_page(&mut self, entities_per_page: usize) -> &mut Self {
self.per_page = Some(entities_per_page);
self
}
pub fn supports_checkout(&mut self, flag: bool) -> &mut Self {
self.supports_checkout = Some(flag);
self
}
pub fn send(&self) -> Paginated<'_, Vec<PaymentMethod>> {
let url = format!("/customers/{}/payment-methods", self.customer_id.as_ref());
Paginated::new(self.client, &url, self)
}
}
#[skip_serializing_none]
#[derive(Serialize)]
pub struct PaymentMethodGet<'a> {
#[serde(skip)]
client: &'a Paddle,
#[serde(skip)]
customer_id: CustomerID,
#[serde(skip)]
payment_method_id: PaymentMethodID,
}
impl<'a> PaymentMethodGet<'a> {
pub fn new(
client: &'a Paddle,
customer_id: impl Into<CustomerID>,
payment_method_id: impl Into<PaymentMethodID>,
) -> Self {
Self {
client,
customer_id: customer_id.into(),
payment_method_id: payment_method_id.into(),
}
}
pub async fn send(&self) -> Result<PaymentMethod> {
self.client
.send(
self,
Method::GET,
&format!(
"/customers/{}/payment-methods/{}",
self.customer_id.as_ref(),
self.payment_method_id.as_ref()
),
)
.await
}
}