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
357
358
359
360
361
362
363
use client::Client;
use error::{Error, ErrorCode};
use params::{Identifiable, List, Metadata, RangeQuery, Timestamp};
use resources::{Address, Currency, PaymentSource, PaymentSourceParams, Refund};
use serde_qs as qs;

/// The resource representing a Stripe charge object outcome.
///
/// For more details see [https://stripe.com/docs/api#charge_object-outcome](https://stripe.com/docs/api#charge_object-outcome)
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ChargeOutcome {
    #[serde(rename = "type")]
    pub outcome_type: OutcomeType,
    pub network_status: NetworkStatus,
    #[serde(default)]
    pub reason: Option<OutcomeReason>,
    #[serde(default)]
    pub risk_level: Option<RiskLevel>,
    #[serde(default)]
    pub seller_message: Option<String>,
    #[serde(default)]
    pub rule: Option<String>,
}

/// An enum representing the possible values of a `ChargeOutcome`'s `type` field.
///
/// For more details see [https://stripe.com/docs/api#charge_object-outcome-type](https://stripe.com/docs/api#charge_object-outcome-type)
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Copy, Eq)]
#[serde(rename_all = "snake_case")]
pub enum OutcomeType {
    Authorized,
    ManualReview,
    IssuerDeclined,
    Blocked,
    Invalid,
    #[serde(other)]
    Other,
}

/// An enum representing the possible values of a `ChargeOutcome`'s `network_status` field.
///
/// For more details see [https://stripe.com/docs/api#charge_object-outcome-network_status](https://stripe.com/docs/api#charge_object-outcome-network_status)
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Eq)]
#[serde(rename_all = "snake_case")]
pub enum NetworkStatus {
    ApprovedByNetwork,
    DeclinedByNetwork,
    NotSentToNetwork,
    ReversedAfterApproval,
    #[serde(other)]
    Other,
}

/// An enum representing the possible values of a `ChargeOutcome`'s `risk_level` field.
///
/// For more details see [https://stripe.com/docs/api#charge_object-outcome-risk_level](https://stripe.com/docs/api#charge_object-outcome-risk_level)
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RiskLevel {
    Normal,
    Elevated,
    Highest,
    NotAssessed,
    #[serde(other)]
    Unknown
}

/// An enum representing the possible values of a `ChargeOutcome`'s `reason` field.
///
/// For more details see [https://stripe.com/docs/api#charge_object-outcome-reason](https://stripe.com/docs/api#charge_object-outcome-reason)
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Eq)]
#[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,
    MerchantBlacklist,
    NewAccountInformationAvailable,
    NoActionTaken,
    NotPermitted,
    PickupCard,
    PinTryExceeded,
    ProcessingError,
    ReenterTransaction,
    RestrictedCard,
    RevocationOfAllAuthorizations,
    RevocationOfAuthorization,
    SecurityViolation,
    ServiceNotAllowed,
    StolenCard,
    StopPaymentOrder,
    TestmodeDecline,
    TransactionNotAllowed,
    TryAgainLater,
    WithdrawalCountLimitExceeded,
    #[serde(other)]
    Other,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct FraudDetails {
    pub user_report: Option<String>,
    #[serde(skip_serializing)]
    pub stripe_report: Option<String>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ShippingDetails {
    pub name: String,
    pub address: Address,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub carrier: Option<String>, // eg. Fedex, UPS, USPS
    #[serde(skip_serializing_if = "Option::is_none")]
    pub phone: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tracking_number: Option<String>,
}

/// The set of parameters that can be used when capturing a charge object.
///
/// For more details see (https://stripe.com/docs/api#charge_capture](https://stripe.com/docs/api#charge_capture).
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct CaptureParams<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amount: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub application_fee: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub receipt_email: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub statement_descriptor: Option<&'a str>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DestinationParams<'a> {
    pub account: &'a str,
    pub amount: u64,
}

/// The set of parameters that can be used when creating or updating a charge object.
///
/// For more details see [https://stripe.com/docs/api#create_charge](https://stripe.com/docs/api#create_charge)
/// and [https://stripe.com/docs/api#update_charge](https://stripe.com/docs/api#update_charge).
#[derive(Clone, Debug, Default, Serialize)]
pub struct ChargeParams<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amount: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub currency: Option<Currency>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub application_fee: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub capture: Option<bool>, // NOTE: if None, Stripe assumes true
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub destination: Option<DestinationParams<'a>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fraud_details: Option<FraudDetails>,
    #[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<Metadata>,
    #[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<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<PaymentSourceParams<'a>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub statement_descriptor: Option<&'a str>,
}

#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum SourceFilterType {
    All,
    AlipayAccount,
    BankAccount,
    BitcoinReceiver,
    Card,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SourceFilter {
    pub object: SourceFilterType,
}

impl SourceFilter {
    pub fn all() -> SourceFilter {
        SourceFilter {
            object: SourceFilterType::All,
        }
    }
    pub fn alipay() -> SourceFilter {
        SourceFilter {
            object: SourceFilterType::AlipayAccount,
        }
    }
    pub fn bank() -> SourceFilter {
        SourceFilter {
            object: SourceFilterType::BankAccount,
        }
    }
    pub fn bitcoin() -> SourceFilter {
        SourceFilter {
            object: SourceFilterType::BitcoinReceiver,
        }
    }
    pub fn card() -> SourceFilter {
        SourceFilter {
            object: SourceFilterType::Card,
        }
    }
}

/// The set of parameters that can be used when listing charges.
///
/// For more details see [https://stripe.com/docs/api#list_charges](https://stripe.com/docs/api#list_charges)
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ChargeListParams<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created: Option<RangeQuery<Timestamp>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub customer: Option<&'a str>,
    #[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<SourceFilter>,
    #[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>,
}

/// The resource representing a Stripe charge object.
///
/// For more details see [https://stripe.com/docs/api#charges](https://stripe.com/docs/api#charges).
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Charge {
    pub id: String,
    pub amount: u64,
    pub amount_refunded: u64,
    pub application: Option<String>,
    pub application_fee: Option<String>,
    pub balance_transaction: Option<String>,
    pub captured: bool,
    pub created: Timestamp,
    pub currency: Currency,
    pub customer: Option<String>,
    pub description: Option<String>,
    pub destination: Option<String>,
    pub dispute: Option<String>,
    pub failure_code: Option<ErrorCode>,
    pub failure_message: Option<String>,
    pub fraud_details: FraudDetails,
    pub invoice: Option<String>,
    pub livemode: bool,
    pub metadata: Metadata,
    pub on_behalf_of: Option<String>,
    pub order: Option<String>,
    pub outcome: Option<ChargeOutcome>,
    pub paid: bool,
    pub payment_intent: Option<String>,
    pub receipt_email: Option<String>,
    pub receipt_number: Option<String>,
    pub refunded: bool,
    pub refunds: List<Refund>,
    pub review: Option<String>,
    pub shipping: Option<ShippingDetails>,
    pub source: PaymentSource,
    pub source_transfer: Option<String>,
    pub statement_descriptor: Option<String>,
    pub status: ChargeStatus,
    pub transfer_group: Option<String>,
}

impl Identifiable for Charge {
    fn id(&self) -> &str {
        &self.id
    }
}

/// The resource representing a Stripe charge object status.
///
/// For more details see [https://stripe.com/docs/api#charge_object-status](https://stripe.com/docs/api#charge_object-status)
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone, Eq)]
pub enum ChargeStatus {
    #[serde(rename = "succeeded")]
    Succeeded,
    #[serde(rename = "pending")]
    Pending,
    #[serde(rename = "failed")]
    Failed,
}

impl Charge {
    /// Creates a new charge.
    ///
    /// For more details see [https://stripe.com/docs/api#create_charge](https://stripe.com/docs/api#create_charge).
    pub fn create(client: &Client, params: ChargeParams) -> Result<Charge, Error> {
        client.post("/charges", params)
    }

    /// Retrieves the details of a charge.
    ///
    /// For more details see [https://stripe.com/docs/api#retrieve_charge](https://stripe.com/docs/api#retrieve_charge).
    pub fn retrieve(client: &Client, charge_id: &str) -> Result<Charge, Error> {
        client.get(&format!("/charges/{}", charge_id))
    }

    /// Updates a charge's properties.
    ///
    /// For more details see [https://stripe.com/docs/api#update_charge](https://stripe.com/docs/api#update_charge).
    pub fn update(client: &Client, charge_id: &str, params: ChargeParams) -> Result<Charge, Error> {
        client.post(&format!("/charges/{}", charge_id), params)
    }

    /// Capture captures a previously created charge with capture set to false.
    ///
    /// For more details see [https://stripe.com/docs/api#charge_capture](https://stripe.com/docs/api#charge_capture).
    pub fn capture(
        client: &Client,
        charge_id: &str,
        params: CaptureParams,
    ) -> Result<Charge, Error> {
        client.post(&format!("/charges/{}/capture", charge_id), params)
    }

    /// List all charges.
    ///
    /// For more details see [https://stripe.com/docs/api#list_charges](https://stripe.com/docs/api#list_charges).
    pub fn list(client: &Client, params: ChargeListParams) -> Result<Vec<Charge>, Error> {
        client.get(&format!("/charges?{}", qs::to_string(&params)?))
    }
}