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

use crate::resources::common::path::UrlPath;
use crate::resources::core::balance::BalanceTransaction;
use crate::util::{List, RangeQuery, Expandable};
use crate::Client;
use std::collections::HashMap;
use crate::resources::issuing::cards::IssuingCard;
use crate::resources::issuing::transactions::Transactions;
use crate::resources::issuing::cardholders::CardHolders;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Authorizations {
    pub id: String,
    pub object: Object,
    pub approved: bool,
    pub authorization_method: AuthorizationMethod,
    pub authorized_amount: i64,
    pub authorized_currency: Currency,
    pub balance_transactions: Vec<BalanceTransaction>,
    pub card: Option<IssuingCard>,
    pub cardholder: Option<Expandable<CardHolders>>,
    pub created: i64,
    pub held_amount: i64,
    pub held_currency: Currency,
    pub is_held_amount_controllable: bool,
    pub livemode: bool,
    pub merchant_data: MerchantData,
    pub metadata: HashMap<String, String>,
    pub pending_authorized_amount: i64,
    pub pending_held_amount: i64,
    pub request_history: Vec<RequestHistory>,
    pub status: AuthorizationStatus,
    pub transactions: Option<Vec<Transactions>>,
    pub verification_data: VerificationData,
    pub wallet_provider: Option<WalletProvider>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all="snake_case")]
pub enum WalletProvider {
    ApplePay,
    GooglePay,
    SamsungPay
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum AuthorizationMethod {
    KeyedIn,
    Swipe,
    Chip,
    Contactless,
    Online,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct MerchantData {
    pub category: MerchantCategories,
    pub city: String,
    pub country: String,
    pub name: String,
    pub network_id: String,
    pub postal_code: String,
    pub state: String,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct RequestHistory {
    pub approved: bool,
    pub authorized_amount: i64,
    pub authorized_currency: Currency,
    pub created: i64,
    pub held_amount: i64,
    pub held_currency: Currency,
    pub reason: RequestReason,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum RequestReason {
    AuthorizationControls,
    CardActive,
    CardInactive,
    InsufficientFunds,
    AccountComplianceDisabled,
    AccountInactive,
    SuspectedFraud,
    WebhookApproved,
    WebhookDeclined,
    WebhookTimeout,
    ForcedCardAuthenticationFailed,
    ForcedCardExpired,
    ForcedCardLost,
    ForcedCardStolen,
    ForcedDoNotHonor,
    ForcedIncorrectPin,
    ForcedInsufficientFunds,
    ForcedInvalidAccountNumber,
    ForcedInvalidTransaction,
    ForcedSuspectedFraud,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct VerificationData {
    pub address_line1_check: VerificationCheck,
    pub address_zip_check: VerificationCheck,
    pub cvc_check: VerificationCheck,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum VerificationCheck {
    Match,
    Mismatch,
    NotProvided,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum AuthorizationStatus {
    Pending,
    Reversed,
    Closed,
}

#[derive(Debug, Default, Serialize, PartialEq)]
pub struct AuthorizationsParam<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<&'a str, &'a str>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub held_amount: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expand: Option<Vec<&'a str>>,
}

#[derive(Debug, Default, Serialize, PartialEq)]
pub struct AuthorizationsListParam<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub card: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cardholder: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<AuthorizationStatus>,
    #[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 created: Option<RangeQuery>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expand: Option<Vec<&'a str>>,
}

impl Authorizations {

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

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

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

    pub fn decline<B: serde::Serialize>(client: &Client, id: &str) -> crate::Result<Self> {
        client.post(
            UrlPath::Authorizations,
            vec![id, "decline"],
            serde_json::Map::new(),
        )
    }

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

}