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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use crate::resources::common::address::Address;
use crate::resources::common::currency::Currency;
use crate::resources::common::object::Object;

use crate::resources::common::path::UrlPath;
use crate::resources::core::refunds::FailureReason;
use crate::resources::paymentmethods::bank::{BankAccount, BankAccountParam};
use crate::resources::paymentmethods::cards::{Card, CardParam};
use crate::{Client};
use std::collections::HashMap;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Source {
    pub id: String,
    pub object: Object,
    pub ach_credit_transfer: Option<AchCreditTransfer>,
    pub amount: Option<i32>,
    pub client_secret: String,
    pub code_verification: Option<CodeVerification>,
    pub created: i64,
    pub currency: Currency,
    pub customer: Option<String>,
    pub flow: SourceFlow,
    pub livemode: bool,
    pub metadata: HashMap<String, String>,
    pub owner: SourceOwner,
    pub receiver: Option<SourceReceiver>,
    pub redirect: Option<SourceRedirect>,
    pub statement_descriptor: Option<String>,
    pub status: SourceStatus,
    #[serde(rename = "type")]
    pub source_type: SourceType,
    pub usage: SourceUsage,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct AchCreditTransfer {
    pub account_number: String,
    pub routing_number: String,
    pub fingerprint: String,
    pub bank_name: String,
    pub swift_code: String,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct CodeVerification {
    pub attempts_remaining: i64,
    pub status: VerificationStatus,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum VerificationStatus {
    Pending,
    Succeeded,
    Failed,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct SourceRedirect {
    pub failure_reason: FailureReason,
    pub return_url: String,
    pub status: SourceRedirectStatus,
    pub url: String,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum RedirectFailureReason {
    UserAbort,
    Declined,
    ProcessingError,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum SourceRedirectStatus {
    Pending,
    Succeeded,
    NotRequired,
    Failed,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct SourceOwner {
    pub address: Option<Address>,
    pub email: Option<String>,
    pub name: Option<String>,
    pub phone: Option<String>,
    pub verified_address: Option<Address>,
    pub verified_email: Option<String>,
    pub verified_name: Option<String>,
    pub verified_phone: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct SourceReceiver {
    pub address: String,
    pub amount_charged: i32,
    pub amount_received: i32,
    pub amount_returned: i32,
    pub refund_attributes_method: String,
    pub refund_attributes_status: String,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum SourceFlow {
    Redirect,
    Receiver,
    CodeVerification,
    None,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum SourceUsage {
    Reusable,
    SingleUse,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum SourceStatus {
    Canceled,
    Chargeable,
    Consumed,
    Failed,
    Pending,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum SourceType {
    AchCreditTransfer,
    AchDebit,
    Alypay,
    Bancontact,
    Card,
    CardPresent,
    Eps,
    Giopay,
    Ideal,
    Multibanco,
    P24,
    PaperCheck,
    SepaCreditTransfer,
    SepaDebit,
    Sofort,
    ThreeDSecure,
    Wechat,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(untagged)]
pub enum PaymentSource {
    BankAccount(BankAccount),
    Card(Card),
}

#[derive(Serialize, Debug, PartialEq)]
#[serde(untagged)]
pub enum PaymentSourceParam<'a> {
    BankAccount(BankAccountParam<'a>),
    Card(CardParam<'a>),
    Token(&'a str),
}

#[derive(Default, Serialize, Debug, PartialEq)]
pub struct PaymentParam<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<PaymentSourceParam<'a>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, String>>,
}

#[derive(Default, Serialize, Debug, PartialEq)]
pub struct SourceParam<'a> {
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub source_type: Option<SourceType>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amount: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub currency: Option<Currency>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flow: Option<SourceFlow>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<&'a str, &'a str>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub owner: Option<SourceOwner>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub redirect: Option<SourceRedirectParam<'a>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub statement_descriptor: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_secret: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<SourceUsage>,
}

#[derive(Default, Serialize, Debug, PartialEq)]
pub struct SourceRedirectParam<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub return_url: Option<&'a str>,
}

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

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

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

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

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