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
use crate::resources::common::currency::Currency;
use crate::resources::common::object::Object;

use crate::resources::common::path::UrlPath;
use crate::util::{Deleted, List, Expandable};
use crate::Client;
use std::collections::HashMap;
use crate::resources::connect::account::Account;
use crate::resources::core::customer::Customer;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct BankAccount {
    pub id: String,
    pub object: Object,
    pub account: Option<Expandable<Account>>,
    pub account_holder_name: String,
    pub account_holder_type: AccountHolderType,
    pub bank_name: String,
    pub country: String,
    pub customer: Option<Expandable<Customer>>,
    pub currency: Currency,
    pub default_for_currency: Option<bool>,
    pub fingerprint: String,
    pub last4: String,
    pub metadata: HashMap<String, String>,
    pub routing_number: String,
    pub status: BankStatus,
}

#[derive(Default, Serialize, Debug, PartialEq)]
pub struct BankAccountParam<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub object: Option<Object>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub account_number: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub country: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub currency: Option<Currency>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub account_holder_name: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub account_holder_type: Option<AccountHolderType>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub routing_number: Option<&'a str>,
    #[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>>,
}

//NOTE: Workaround to add an object name while leaving the rest "default"
impl<'a> BankAccountParam<'a> {
    pub fn default() -> Self {
        BankAccountParam {
            object: Some(Object::BankAccount),
            ..Default::default()
        }
    }
}

#[derive(Default, Serialize, Debug, PartialEq)]
pub struct BankAccountVerifyParam {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amounts: Option<(u32, u32)>,
}

#[derive(Serialize, Debug, PartialEq)]
pub struct BankListParams<'a> {
    #[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>>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum AccountHolderType {
    Individual,
    Company,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum BankStatus {
    New,
    Validated,
    Verified,
    VerificationFailed,
    Errored,
}

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

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

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

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

    pub fn delete(client: &Client, customer_id: &str, id: &str) -> crate::Result<Deleted> {
        client.delete(
            UrlPath::Customers,
            vec![customer_id, "sources", id],
            serde_json::Map::new(),
        )
    }

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