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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use serde::{Deserialize, Serialize};

use crate::client::Client;
use crate::errors::Result;

#[derive(Deserialize, Debug, Clone)]
pub struct Institution {
    /// Unique identifier for the institution
    pub institution_id: String,
    /// The official name of the institution
    pub name: String,
    /// A list of the Plaid products supported by the institution
    /// Possible values: assets, auth, balance, identity, investments, liabilities, payment_initiation, transactions, credit_details, income, deposit_switch
    pub products: Vec<String>,
    /// A list of the country codes supported by the institution.
    /// Possible values: US, GB, ES, NL, FR, IE, CA
    pub country_codes: Vec<String>,
    /// The URL for the institution's website
    pub url: Option<String>,
    /// Hexadecimal representation of the primary color used by the institution
    pub primary_color: Option<String>,
    /// Base64 encoded representation of the institution's logo
    pub logo: Option<String>,
    /// A partial list of routing numbers associated with the institution. This list is provided for the purpose of looking up institutions by routing number. It is not comprehensive and should never be used as a complete list of routing numbers for an institution.
    pub routing_numbers: Option<Vec<String>>,
    /// Indicates that the institution has an OAuth login flow. This is primarily relevant to institutions with European country codes.
    pub oauth: bool,
}

#[derive(Serialize)]
struct GetInstitutionsRequest<'a> {
    client_id: &'a str,
    secret: &'a str,
    count: i32,
    offset: i32,
    country_codes: &'a [&'a str],
    #[serde(skip_serializing_if = "Option::is_none")]
    options: Option<GetInstitutionsOptions>,
}

#[derive(Serialize)]
pub struct GetInstitutionsOptions {
    /// Filter the Institutions based on which products they support.
    /// Possible values: assets, auth, balance, identity, investments, liabilities, payment_initiation, transactions, credit_details, income, deposit_switch
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub products: Vec<String>,
    /// Specify an array of routing numbers to filter institutions.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub routing_numbers: Vec<String>,
    /// Limit results to institutions with or without OAuth login flows. This is primarily relevant to institutions with European country codes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub oauth: Option<bool>,
    /// When true, return the institution's homepage URL, logo and primary brand color.
    pub include_optional_metadata: bool,
}

impl Default for GetInstitutionsOptions {
    fn default() -> Self {
        Self {
            products: Vec::new(),
            include_optional_metadata: false,
            oauth: None,
            routing_numbers: Vec::new(),
        }
    }
}

#[derive(Deserialize, Debug)]
pub struct GetInstitutionsResponse {
    /// A unique identifier for the request, which can be used for troubleshooting. This identifier, like all Plaid identifiers, is case sensitive.
    pub request_id: String,
    /// A list of Plaid Institution
    pub institutions: Vec<Institution>,
    /// The number of institutions returned
    pub total: i32,
}

#[derive(Serialize)]
struct GetInstitutionByIdRequest<'a> {
    client_id: &'a str,
    secret: &'a str,
    institution_id: &'a str,
    country_codes: &'a [&'a str],
    #[serde(skip_serializing_if = "Option::is_none")]
    options: Option<GetInstitutionByIdOptions>,
}

#[derive(Serialize)]
pub struct GetInstitutionByIdOptions {
    /// When true, return an institution's logo, brand color, and URL. When available, the bank's logo is returned as a base64 encoded 152x152 PNG, the brand color is in hexadecimal format. The default value is false.
    pub include_optional_metadata: bool,
    /// If true, the response will include status information about the institution. Default value is false.
    pub include_status: bool,
}

impl Default for GetInstitutionByIdOptions {
    fn default() -> Self {
        Self {
            include_optional_metadata: false,
            include_status: false,
        }
    }
}

#[derive(Deserialize, Debug)]
pub struct GetInstitutionByIdResponse {
    request_id: String,
    institution: Institution,
}

#[derive(Serialize)]
struct SearchInstitutionsRequest<'a> {
    client_id: &'a str,
    secret: &'a str,
    query: &'a str,
    country_codes: &'a [&'a str],
    products: &'a [&'a str],
    #[serde(skip_serializing_if = "Option::is_none")]
    options: Option<SearchInstitutionsOptions>,
}

#[derive(Serialize)]
pub struct SearchInstitutionsOptions {
    include_optional_metadata: bool,
    // account_filter:
    #[serde(skip_serializing_if = "Option::is_none")]
    oauth: Option<bool>,
}

impl Default for SearchInstitutionsOptions {
    fn default() -> Self {
        Self {
            include_optional_metadata: false,
            oauth: None,
        }
    }
}

#[derive(Deserialize, Debug)]
pub struct SearchInstitutionsResponse {
    request_id: String,
    institutions: Vec<Institution>,
}

impl Client {
    /// Get details of an institution.
    ///
    /// Returns a JSON response containing details on a specified financial institution currently supported by Plaid.
    ///
    /// * `institution_id` - The ID of the institution to get details about
    /// * `country_codes` - Specify an array of Plaid-supported country codes this institution supports, using the ISO-3166-1 alpha-2 country code standard. Possible values: US, GB, ES, NL, FR, IE, CA
    /// * `options` - Specifies optional parameters for /institutions/get_by_id.
    pub async fn get_institution_by_id(
        &self,
        institution_id: &str,
        country_codes: &[&str],
        options: Option<GetInstitutionByIdOptions>,
    ) -> Result<GetInstitutionByIdResponse> {
        self.send_request(
            "institutions/get_by_id",
            &GetInstitutionByIdRequest {
                client_id: &self.client_id,
                secret: &self.secret,
                institution_id,
                country_codes,
                options,
            },
        )
        .await
    }

    /// Get details of all supported institutions.
    ///
    /// Returns a JSON response containing details on all financial institutions currently supported by Plaid. Because Plaid supports thousands of institutions, results are paginated.
    /// This data changes frequently. If you store it locally on your system, be sure to update it regularly.
    ///
    /// * `count` - The total number of Institutions to return.
    /// * `offset` - The number of Institutions to skip.
    /// * `country_codes` - Specify an array of Plaid-supported country codes this institution supports, using the ISO-3166-1 alpha-2 country code standard.
    /// * `options` - An optional object to filter /institutions/get results.
    pub async fn get_institutions(
        &self,
        count: i32,
        offset: i32,
        country_codes: &[&str],
        options: Option<GetInstitutionsOptions>,
    ) -> Result<GetInstitutionsResponse> {
        self.send_request(
            "institutions/get",
            &GetInstitutionsRequest {
                client_id: &self.client_id,
                secret: &self.secret,
                count,
                offset,
                country_codes,
                options,
            },
        )
        .await
    }

    /// Search institutions.
    ///
    /// Returns a JSON response containing details for institutions that match the query parameters, up to a maximum of ten institutions per query.
    ///
    /// * `query` - The search query. Institutions with names matching the query are returned
    /// * `products` - Filter the Institutions based on whether they support all products listed in products.
    /// * `country_codes` - Specify an array of Plaid-supported country codes this institution supports, using the ISO-3166-1 alpha-2 country code standard.
    /// * `options` - An optional object to filter /institutions/search results.
    pub async fn search_institutions(
        &self,
        query: &str,
        products: &[&str],
        country_codes: &[&str],
        options: Option<SearchInstitutionsOptions>,
    ) -> Result<SearchInstitutionsResponse> {
        self.send_request(
            "institutions/search",
            &SearchInstitutionsRequest {
                client_id: &self.client_id,
                secret: &self.secret,
                query,
                products,
                country_codes,
                options,
            },
        )
        .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::client::tests::{get_test_client, SANDBOX_INSTITUTION_QUERY};

    #[tokio::test]
    async fn test_get_institutions() {
        let client = get_test_client();

        let resp = client.get_institutions(2, 1, &["US"], None).await.unwrap();
        assert_eq!(resp.institutions.len(), 2);

        let resp = client
            .get_institutions(
                2,
                1,
                &["US"],
                Some(GetInstitutionsOptions {
                    include_optional_metadata: true,
                    ..Default::default()
                }),
            )
            .await
            .unwrap();
        assert_eq!(resp.institutions.len(), 2);
        for institution in &resp.institutions {
            assert_eq!(institution.url.is_some(), true);
            assert_ne!(institution.url.as_ref().unwrap().len(), 0);
        }

        let resp = client
            .get_institutions(
                2,
                1,
                &["GB"],
                Some(GetInstitutionsOptions {
                    oauth: Some(true),
                    ..Default::default()
                }),
            )
            .await
            .unwrap();
        assert_eq!(resp.institutions.len(), 2);

        let resp = client.get_institutions(2, 1, &[], None).await;
        assert_eq!(resp.is_err(), true);

        let resp = client
            .get_institutions(
                1,
                0,
                &["US"],
                Some(GetInstitutionsOptions {
                    routing_numbers: vec!["021200339".to_string(), "052001633".to_string()],
                    ..Default::default()
                }),
            )
            .await
            .unwrap();
        assert_eq!(resp.institutions.len(), 1);
    }

    #[tokio::test]
    async fn test_search_institutions() {
        let client = get_test_client();

        let resp = client
            .search_institutions(SANDBOX_INSTITUTION_QUERY, &["transactions"], &["US"], None)
            .await
            .unwrap();
        assert!(resp.institutions.len() > 0);

        let resp = client
            .search_institutions(
                SANDBOX_INSTITUTION_QUERY,
                &["transactions"],
                &["US"],
                Some(SearchInstitutionsOptions {
                    include_optional_metadata: true,
                    ..Default::default()
                }),
            )
            .await
            .unwrap();
        assert!(resp.institutions.len() > 0);
        for institution in &resp.institutions {
            assert_eq!(institution.url.is_some(), true);
            assert_ne!(institution.url.as_ref().unwrap().len(), 0);
        }

        let resp = client
            .search_institutions(SANDBOX_INSTITUTION_QUERY, &["transactions"], &[""], None)
            .await;
        assert_eq!(resp.is_err(), true);
    }

    #[tokio::test]
    async fn test_get_institutions_by_id() {
        let client = get_test_client();

        let resp = client
            .get_institution_by_id("ins_12", &["US"], None)
            .await
            .unwrap();
        assert!(resp.institution.products.len() > 0);

        let resp = client
            .get_institution_by_id(
                "ins_12",
                &["US"],
                Some(GetInstitutionByIdOptions {
                    include_optional_metadata: true,
                    ..Default::default()
                }),
            )
            .await
            .unwrap();
        assert!(resp.institution.products.len() > 0);
        assert_eq!(resp.institution.url.is_some(), true);
        assert_ne!(resp.institution.url.as_ref().unwrap().len(), 0);

        let resp = client.get_institution_by_id("ins_12", &[], None).await;
        assert_eq!(resp.is_err(), true);
    }
}