1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use crate::resources::common::object::Object;
use crate::resources::common::path::{UrlPath};
use crate::resources::core::charges::BillingDetails;
use crate::resources::paymentmethods::bank::AccountHolderType;
use crate::resources::paymentmethods::cards::{CardCheck, CardType};
use crate::resources::paymentmethods::source::PaymentSourceParam;
use crate::util::{List, Expandable};
use crate::{Client};
use std::collections::HashMap;
use crate::resources::core::customer::Customer;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct PaymentMethods {
    pub id: String,
    pub object: Object,
    pub billing_details: BillingDetails,
    pub card: PaymentCard,
    pub card_present: Option<String>,
    pub created: i64,
    pub customer: Option<Expandable<Customer>>,
    pub livemode: bool,
    pub metadata: HashMap<String, String>,
    #[serde(rename = "type")]
    pub payment_method_type: PaymentMethodsType,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct PaymentCard {
    pub brand: PaymentCardBrand,
    pub checks: ChargeChecks,
    pub country: String,
    pub exp_month: i32,
    pub exp_year: i32,
    pub fingerprint: String,
    pub funding: CardType,
    pub generated_from: Option<GenerateFrom>,
    pub last4: String,
    pub three_d_secure_usage: ThreeDSecureUsage,
    pub wallet: Option<String>,
}

#[derive(Deserialize, Serialize, Debug, PartialEq)]
#[serde(rename_all="lowercase")]
pub enum PaymentCardBrand {
    Visa,
    #[serde(rename = "amex")]
    AmericanExpress,
    MasterCard,
    Discover,
    JCB,
    #[serde(rename = "diners")]
    DinersClub,
    UnionPay,
    Unknown,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct ThreeDSecureUsage {
    pub supported: bool,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct GenerateFrom {
    pub charge: String,
    pub payment_method_details: PaymentMethodsDetails,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct PaymentMethodsDetails {
    #[serde(rename = "type")]
    pub method_type: String,
    #[serde(flatten, with = "PaymentMethodsDetailsInner")]
    pub method: PaymentMethodsDetailsInner,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(remote = "Self", rename_all = "snake_case")]
pub enum PaymentMethodsDetailsInner {
    AchCreditTransfer {
        account_number: String,
        bank_name: String,
        routing_number: String,
        swift_code: String,
    },
    AchDebit {
        account_holder_type: AccountHolderType,
        bank_name: String,
        country: String,
        fingerprint: String,
        last4: String,
        routing_number: String,
    },
    AliPay,
    BanContact {
        bank_code: String,
        bank_name: String,
        bic: String,
        iban_last4: String,
        preferred_language: String,
        verified_name: String,
    },
    Card {
        brand: String,
        checks: ChargeChecks,
        country: String,
        exp_month: i32,
        exp_year: i32,
        fingerprint: String,
        funding: CardType,
        last4: String,
        three_d_secure: Option<String>,
        wallet: Option<String>,
    },
    //TODO: Complete
}

//TODO: Relook at this in the future before 0.6.0 release
//impl<'de> Deserialize<'de> for PaymentMethodsDetails {
//    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
//        #[derive(Deserialize, PartialEq)]
//        struct Inner {
//            #[serde(rename = "type")]
//            _type: String,
//            #[serde(flatten, with = "PaymentMethodsDetails")]
//            inner: PaymentMethodsDetails,
//        }
//        Inner::deserialize(deserializer).map(|w| w.inner)
//    }
//}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct ChargeChecks {
    pub address_line1_check: Option<CardCheck>,
    pub address_postal_code_check: Option<CardCheck>,
    pub cvc_check: Option<CardCheck>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum PaymentMethodsType {
    Card,
    CardPresent,
}

#[derive(Default, Serialize, Debug, PartialEq)]
pub struct PaymentMethodsParam<'a> {
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub payment_method_type: Option<PaymentMethodsType>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub billing_details: Option<BillingDetails>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub card: Option<PaymentSourceParam<'a>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expand: Option<Vec<&'a str>>,
}

#[derive(Default, Serialize, Debug, PartialEq)]
pub struct PaymentMethodsListParams<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub customer: Option<&'a str>,
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub payment_method_type: Option<PaymentMethodsType>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ending_before: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub starting_after: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expand: Option<Vec<&'a str>>,
}

impl PaymentMethods {
    pub fn create<B: serde::Serialize>(client: &Client, param: B) -> crate::Result<Self> {
        client.post(UrlPath::PaymentMethods, vec![], param)
    }

    pub fn retrieve(client: &Client, id: &str) -> crate::Result<Self> {
        client.get(UrlPath::PaymentMethods, vec![id], serde_json::Map::new())
    }

    pub fn update<B: serde::Serialize>(client: &Client, id: &str, param: B) -> crate::Result<Self> {
        client.post(UrlPath::PaymentMethods, vec![id], param)
    }

    pub fn list<B: serde::Serialize>(client: &Client, param: B) -> crate::Result<List<Self>> {
        client.get(UrlPath::PaymentMethods, vec![], param)
    }

    pub fn attach<B: serde::Serialize>(client: &Client, id: &str, param: B) -> crate::Result<Self> {
        client.post(UrlPath::PaymentMethods, vec![id, "attach"], param)
    }

    pub fn detach<B: serde::Serialize>(client: &Client, id: &str, param: B) -> crate::Result<Self> {
        client.post(UrlPath::PaymentMethods, vec![id, "attach"], param)
    }
}