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
use crate::resources::common::address::Address;
use crate::resources::common::currency::Currency;
use crate::resources::common::object::Object;
use crate::resources::core::refunds::Refund;
use crate::resources::paymentmethods::source::{PaymentSource, PaymentSourceParam};
use crate::util::{List, RangeQuery, Expandable};
use crate::{Client, ErrorCode};
use std::collections::HashMap;

use crate::resources::common::path::UrlPath;
use crate::resources::paymentmethods::paymentmethods::PaymentMethodsDetails;
use crate::resources::core::balance::BalanceTransaction;
use crate::resources::connect::applicationfees::ApplicationFees;
use crate::resources::core::customer::Customer;
use crate::resources::core::disputes::Dispute;
use crate::resources::billing::invoices::Invoice;
use crate::resources::orders::order::Order;
use crate::resources::fraud::review::Reviews;
use crate::resources::connect::transfers::Transfer;
use crate::resources::core::paymentintents::TransferData;
use crate::resources::connect::account::Account;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Charge {
    pub id: String,
    pub object: Object,
    pub amount: i64,
    pub amount_refunded: i64,
    pub application: Option<String>,
    pub application_fee: Option<Expandable<ApplicationFees>>,
    pub application_fee_amount: Option<i32>,
    pub balance_transaction: Option<Expandable<BalanceTransaction>>,
    pub billing_details: BillingDetails,
    pub captured: bool,
    pub created: i64,
    pub currency: Currency,
    pub customer: Option<Expandable<Customer>>,
    pub description: Option<String>,
    pub destination: Option<String>,
    pub dispute: Option<Expandable<Dispute>>,
    pub failure_code: Option<ErrorCode>,
    pub failure_message: Option<String>,
    pub fraud_details: FraudDetails,
    pub invoice: Option<Expandable<Invoice>>,
    pub livemode: bool,
    pub metadata: HashMap<String, String>,
    pub on_behalf_of: Option<Expandable<Account>>,
    pub order: Option<Expandable<Order>>,
    pub outcome: Option<Outcome>,
    pub paid: bool,
    pub payment_intent: Option<String>,
    pub payment_method: Option<String>,
    pub payment_method_details: PaymentMethodsDetails,
    pub receipt_email: Option<String>,
    pub receipt_number: Option<String>,
    pub receipt_url: String,
    pub refunded: bool,
    pub refunds: List<Refund>,
    pub review: Option<Expandable<Reviews>>,
    pub shipping: Option<ShippingDetails>,
    pub source: Option<PaymentSource>,
    pub source_transfer: Option<Expandable<Transfer>>,
    pub statement_descriptor: Option<String>,
    pub status: ChargeStatus,
    pub transfer: Option<Expandable<Transfer>>,
    pub transfer_data: Option<TransferData>,
    pub transfer_group: Option<String>,
}

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

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum PreferredLanguage {
    EN,
    DE,
    FR,
    NL,
}

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

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Outcome {
    #[serde(rename = "type")]
    pub outcome_type: OutcomeType,
    pub network_status: NetworkStatus,
    pub reason: Option<OutcomeReason>,
    pub risk_level: RiskLevel,
    pub risk_score: i64,
    pub seller_message: Option<String>,
    pub rule: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct FraudDetails {
    pub user_report: Option<UserReport>,
    pub stripe_report: Option<StripeReport>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum UserReport {
    Safe,
    Fraudulent,
}

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

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct ShippingDetails {
    pub name: String,
    pub address: Address,
    pub carrier: Option<String>,
    pub phone: Option<String>,
    pub tracking_number: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum OutcomeType {
    Authorized,
    ManualReview,
    IssuerDeclined,
    Blocked,
    Invalid,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum NetworkStatus {
    ApprovedByNetwork,
    DeclinedByNetwork,
    NotSentToNetwork,
    ReversedAfterApproval,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum RiskLevel {
    Normal,
    Elevated,
    Highest,
    NotAssessed,
    Unknown,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum OutcomeReason {
    ApprovedWithID,
    CallIssuer,
    CardNotSupported,
    CardVelocityExceeded,
    CurrencyNotSupported,
    DoNotHonor,
    DoNotTryAgain,
    DuplicateTransaction,
    ExpiredCard,
    Fraudulent,
    GenericDecline,
    IncorrectNumber,
    IncorrectCVC,
    IncorrectPIN,
    IncorrectZip,
    InsufficientFunds,
    InvalidAccount,
    InvalidAmount,
    InvalidCVC,
    InvalidExpiryYear,
    InvalidNumber,
    InvalidPin,
    IssuerNotAvailable,
    LostCard,
    NewAccountInformationAvailable,
    NoActionTaken,
    NotPermitted,
    PickupCard,
    PinTryExceeded,
    ProcessingError,
    ReenterTransaction,
    RestrictedCard,
    RevocationOfAllAuthorization,
    RevocationOfAuthorization,
    SecurityViolation,
    ServiceNotAllowed,
    StolenCard,
    StopPaymentOrder,
    TestmodeDeclined,
    TransactionNotAllowed,
    TryAgainLater,
    WithrawalCountLimitExceeded,
}

#[derive(Default, Serialize, Debug, PartialEq)]
pub struct ChargeParams<'a> {
    #[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 application_fee: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub capture: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub destination: Option<&'a str>, //TODO
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transfer_group: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub on_behalf_of: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub receipt_email: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shipping: Option<ShippingDetails>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub customer: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<PaymentSourceParam<'a>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub statement_descriptor: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expand: Option<Vec<&'a str>>,
    //Used for updates
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fraud_details: Option<FraudDetails>,
}

#[derive(Default, Serialize, Debug, PartialEq)]
pub struct ChargeListParam<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub customer: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created: Option<RangeQuery>,
    #[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 source: Option<ChargeSourceListParam>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub starting_after: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transfer_group: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expand: Option<Vec<&'a str>>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct ChargeSourceListParam {
    pub object: ChargeSourceObject,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ChargeSourceObject {
    All,
    AlipayAccount,
    BankAccount,
    Card,
}

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

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

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

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

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