square_api_client/api/cards_api.rs
1//! Use the Cards API to save a credit or debit card on file.
2//!
3//! You can use the CreateCard endpoint to save a credit or debit card to a Square account.
4//! Developers can integrate the Cards API in their application to let Square sellers:
5//!
6//! * Save a card that can be charged by any Square seller who uses your application. Your
7//! application specifies the organization access token in the `CreateCard` request.
8//! * Save a card that can be charged by a single Square seller. Your application specifies the
9//! access token of the specific seller account in the `CreateCard` request.
10//!
11//! The Cards API also supports other endpoints to manage the cards.
12
13use crate::{
14 config::Configuration,
15 http::client::HttpClient,
16 models::{
17 errors::ApiError, CreateCardRequest, CreateCardResponse, DisableCardResponse,
18 ListCardsParameters, ListCardsResponse, RetrieveCardResponse,
19 },
20};
21
22const DEFAULT_URI: &str = "/cards";
23
24/// Use the Cards API to save a credit or debit card on file.
25pub struct CardsApi {
26 /// App config information
27 config: Configuration,
28 /// HTTP Client for requests to the Cards API endpoints
29 client: HttpClient,
30}
31
32impl CardsApi {
33 /// Instantiates a new `CardsApi`
34 pub fn new(config: Configuration, client: HttpClient) -> Self {
35 Self { config, client }
36 }
37
38 /// Adds a card on file to an existing merchant.
39 pub async fn create_card(
40 &self,
41 body: &CreateCardRequest,
42 ) -> Result<CreateCardResponse, ApiError> {
43 let response = self.client.post(&self.url(), body).await?;
44
45 response.deserialize().await
46 }
47
48 /// Disables the card, preventing any further updates or charges.
49 ///
50 /// Disabling an already disabled card is allowed but has no effect.
51 pub async fn disable_card(&self, card_id: &str) -> Result<DisableCardResponse, ApiError> {
52 let url = format!("{}/{}/disable", &self.url(), card_id);
53 let response = self.client.empty_post(&url).await?;
54
55 response.deserialize().await
56 }
57
58 /// Retrieves a list of cards owned by the account making the request.
59 ///
60 /// A max of 25 cards will be returned.
61 pub async fn list_cards(
62 &self,
63 params: &ListCardsParameters,
64 ) -> Result<ListCardsResponse, ApiError> {
65 let url = format!("{}{}", &self.url(), params.to_query_string());
66 let response = self.client.get(&url).await?;
67
68 response.deserialize().await
69 }
70
71 /// Retrieves details for a specific Card.
72 pub async fn retrieve_card(&self, card_id: &str) -> Result<RetrieveCardResponse, ApiError> {
73 let url = format!("{}/{}", &self.url(), card_id);
74 let response = self.client.get(&url).await?;
75
76 response.deserialize().await
77 }
78
79 /// Constructs the basic entity URL including domain and entity path. Any additional path
80 /// elements (e.g. path parameters) will need to be appended to this URL.
81 fn url(&self) -> String {
82 format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
83 }
84}