stripe/resources/
payment_source.rs

1use serde::ser::SerializeStruct;
2use serde::{Deserialize, Serialize};
3
4use crate::ids::{PaymentSourceId, SourceId, TokenId};
5use crate::params::Object;
6use crate::resources::{Account, BankAccount, Card, Currency, Source};
7
8/// A PaymentSourceParams represents all of the supported ways that can
9/// be used.
10///
11/// This includes creating a new customer with a payment method or creating
12/// a payment method directly for a customer via `Customer::attach_source`.
13/// Not to be confused with `SourceParams` which is used by `Source::create`
14/// to create a source that is not necessarily attached to a customer.
15#[derive(Clone, Debug, Serialize, Deserialize)]
16#[serde(untagged)]
17pub enum PaymentSourceParams {
18    /// Creates a payment method (e.g. card or bank account) from tokenized data,
19    /// using a token typically received from Stripe Elements.
20    Token(TokenId),
21
22    /// Attach an existing source to an existing customer or
23    /// create a new customer from an existing source.
24    Source(SourceId),
25}
26
27/// A PaymentSource represents a payment method _associated with a customer or charge_.
28/// This value is usually returned as a subresource on another request.
29///
30/// Not to be confused with `Source` which represents a "generic" payment method
31/// returned by the `Source::get` (which could still be a credit card, etc)
32/// but is not necessarily attached to either a customer or charge.
33#[derive(Clone, Debug, Deserialize, Serialize)]
34#[serde(tag = "object", rename_all = "snake_case")]
35pub enum PaymentSource {
36    Card(Card),
37    Source(Source),
38    Account(Account),
39    BankAccount(BankAccount),
40}
41
42impl Object for PaymentSource {
43    type Id = PaymentSourceId;
44    fn id(&self) -> Self::Id {
45        match self {
46            PaymentSource::Card(x) => PaymentSourceId::Card(x.id()),
47            PaymentSource::Source(x) => PaymentSourceId::Source(x.id()),
48            PaymentSource::Account(x) => PaymentSourceId::Account(x.id()),
49            PaymentSource::BankAccount(x) => PaymentSourceId::BankAccount(x.id()),
50        }
51    }
52    fn object(&self) -> &'static str {
53        match self {
54            PaymentSource::Card(x) => x.object(),
55            PaymentSource::Source(x) => x.object(),
56            PaymentSource::Account(x) => x.object(),
57            PaymentSource::BankAccount(x) => x.object(),
58        }
59    }
60}
61
62#[derive(Clone, Debug, Default, Deserialize)]
63pub struct BankAccountParams<'a> {
64    pub country: &'a str,
65    pub currency: Currency,
66    pub account_holder_name: Option<&'a str>,
67    pub account_holder_type: Option<&'a str>,
68    pub routing_number: Option<&'a str>,
69    pub account_number: &'a str,
70}
71
72impl<'a> serde::ser::Serialize for BankAccountParams<'a> {
73    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
74    where
75        S: serde::ser::Serializer,
76    {
77        let mut s = serializer.serialize_struct("BankAccountParams", 6)?;
78        s.serialize_field("object", "bank_account")?;
79        s.serialize_field("country", &self.country)?;
80        s.serialize_field("currency", &self.currency)?;
81        s.serialize_field("account_holder_name", &self.account_holder_name)?;
82        s.serialize_field("routing_number", &self.routing_number)?;
83        s.serialize_field("account_number", &self.account_number)?;
84        s.end()
85    }
86}
87
88#[derive(Clone, Debug, Default, Deserialize)]
89pub struct CardParams<'a> {
90    pub exp_month: &'a str, // eg. "12"
91    pub exp_year: &'a str,  // eg. "17" or 2017"
92
93    pub number: &'a str,       // card number
94    pub name: Option<&'a str>, // cardholder's full name
95    pub cvc: Option<&'a str>,  // card security code
96}
97
98impl<'a> serde::ser::Serialize for CardParams<'a> {
99    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
100    where
101        S: serde::ser::Serializer,
102    {
103        let mut s = serializer.serialize_struct("CardParams", 6)?;
104        s.serialize_field("object", "card")?;
105        s.serialize_field("exp_month", &self.exp_month)?;
106        s.serialize_field("exp_year", &self.exp_year)?;
107        s.serialize_field("number", &self.number)?;
108        s.serialize_field("name", &self.name)?;
109        s.serialize_field("cvc", &self.cvc)?;
110        s.end()
111    }
112}