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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// ======================================
// This file was automatically generated.
// ======================================

use crate::config::{Client, Response};
use crate::ids::{CouponId, CustomerId, OrderId};
use crate::params::{Expand, Expandable, List, Metadata, Object, RangeQuery, Timestamp};
use crate::resources::{
    Charge, Currency, Customer, OrderItem, OrderReturn, Shipping, ShippingParams,
};
use serde_derive::{Deserialize, Serialize};

/// The resource representing a Stripe "Order".
///
/// For more details see [https://stripe.com/docs/api/orders/object](https://stripe.com/docs/api/orders/object).
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Order {
    /// Unique identifier for the object.
    pub id: OrderId,

    /// A positive integer in the smallest currency unit (that is, 100 cents for $1.00, or 1 for ¥1, Japanese Yen being a zero-decimal currency) representing the total amount for the order.
    pub amount: i64,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub amount_returned: Option<i64>,

    /// ID of the Connect Application that created the order.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub application: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub application_fee: Option<i64>,

    /// The ID of the payment used to pay for the order.
    ///
    /// Present if the order status is `paid`, `fulfilled`, or `refunded`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub charge: Option<Expandable<Charge>>,

    /// Time at which the object was created.
    ///
    /// Measured in seconds since the Unix epoch.
    pub created: Timestamp,

    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
    ///
    /// Must be a [supported currency](https://stripe.com/docs/currencies).
    pub currency: Currency,

    /// The customer used for the order.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub customer: Option<Expandable<Customer>>,

    /// The email address of the customer placing the order.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub external_coupon_code: Option<String>,

    /// List of items constituting the order.
    ///
    /// An order can have up to 25 items.
    pub items: Vec<OrderItem>,

    /// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
    pub livemode: bool,

    /// Set of key-value pairs that you can attach to an object.
    ///
    /// This can be useful for storing additional information about the object in a structured format.
    pub metadata: Metadata,

    #[serde(default)]
    pub returns: List<OrderReturn>,

    /// The shipping method that is currently selected for this order, if any.
    ///
    /// If present, it is equal to one of the `id`s of shipping methods in the `shipping_methods` array.
    /// At order creation time, if there are multiple shipping methods, Stripe will automatically selected the first method.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selected_shipping_method: Option<String>,

    /// The shipping address for the order.
    ///
    /// Present if the order is for goods to be shipped.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shipping: Option<Shipping>,

    /// A list of supported shipping methods for this order.
    ///
    /// The desired shipping method can be specified either by updating the order, or when paying it.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shipping_methods: Option<Vec<ShippingMethod>>,

    /// Current order status.
    ///
    /// One of `created`, `paid`, `canceled`, `fulfilled`, or `returned`.
    /// More details in the [Orders Guide](https://stripe.com/docs/orders/guide#understanding-order-statuses).
    pub status: OrderStatus,

    /// The timestamps at which the order status was updated.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status_transitions: Option<StatusTransitions>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated: Option<Timestamp>,

    /// The user's order ID if it is different from the Stripe order ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub upstream_id: Option<String>,
}

impl Order {
    /// Returns a list of your orders.
    ///
    /// The orders are returned sorted by creation date, with the most recently created orders appearing first.
    pub fn list(client: &Client, params: ListOrders<'_>) -> Response<List<Order>> {
        client.get_query("/orders", &params)
    }

    /// Creates a new order object.
    pub fn create(client: &Client, params: CreateOrder<'_>) -> Response<Order> {
        client.post_form("/orders", &params)
    }

    /// Retrieves the details of an existing order.
    ///
    /// Supply the unique order ID from either an order creation request or the order list, and Stripe will return the corresponding order information.
    pub fn retrieve(client: &Client, id: &OrderId, expand: &[&str]) -> Response<Order> {
        client.get_query(&format!("/orders/{}", id), &Expand { expand })
    }

    /// Updates the specific order by setting the values of the parameters passed.
    ///
    /// Any parameters not provided will be left unchanged.
    pub fn update(client: &Client, id: &OrderId, params: UpdateOrder<'_>) -> Response<Order> {
        client.post_form(&format!("/orders/{}", id), &params)
    }
}

impl Object for Order {
    type Id = OrderId;
    fn id(&self) -> Self::Id {
        self.id.clone()
    }
    fn object(&self) -> &'static str {
        "order"
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ShippingMethod {
    /// A positive integer in the smallest currency unit (that is, 100 cents for $1.00, or 1 for ¥1, Japanese Yen being a zero-decimal currency) representing the total amount for the line item.
    pub amount: i64,

    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
    ///
    /// Must be a [supported currency](https://stripe.com/docs/currencies).
    pub currency: Currency,

    /// The estimated delivery date for the given shipping method.
    ///
    /// Can be either a specific date or a range.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub delivery_estimate: Option<DeliveryEstimate>,

    /// An arbitrary string attached to the object.
    ///
    /// Often useful for displaying to users.
    pub description: String,

    /// Unique identifier for the object.
    pub id: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DeliveryEstimate {
    /// If `type` is `"exact"`, `date` will be the expected delivery date in the format YYYY-MM-DD.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub date: Option<String>,

    /// If `type` is `"range"`, `earliest` will be be the earliest delivery date in the format YYYY-MM-DD.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub earliest: Option<String>,

    /// If `type` is `"range"`, `latest` will be the latest delivery date in the format YYYY-MM-DD.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub latest: Option<String>,

    /// The type of estimate.
    ///
    /// Must be either `"range"` or `"exact"`.
    #[serde(rename = "type")]
    pub type_: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct StatusTransitions {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub canceled: Option<Timestamp>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub fulfiled: Option<Timestamp>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub paid: Option<Timestamp>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub returned: Option<Timestamp>,
}

/// The parameters for `Order::create`.
#[derive(Clone, Debug, Serialize)]
pub struct CreateOrder<'a> {
    /// A coupon code that represents a discount to be applied to this order.
    ///
    /// Must be one-time duration and in same currency as the order.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coupon: Option<CouponId>,

    /// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase.
    ///
    /// Must be a [supported currency](https://stripe.com/docs/currencies).
    pub currency: Currency,

    /// The ID of an existing customer to use for this order.
    ///
    /// If provided, the customer email and shipping address will be used to create the order.
    /// Subsequently, the customer will also be charged to pay the order.
    /// If `email` or `shipping` are also provided, they will override the values retrieved from the customer object.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub customer: Option<CustomerId>,

    /// The email address of the customer placing the order.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<&'a str>,

    /// Specifies which fields in the response should be expanded.
    #[serde(skip_serializing_if = "Expand::is_empty")]
    pub expand: &'a [&'a str],

    /// List of items constituting the order.
    ///
    /// An order can have up to 25 items.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub items: Option<Vec<OrderItemParams>>,

    /// A set of key-value pairs that you can attach to an order object.
    ///
    /// Limited to 500 characters.
    /// Metadata can be useful for storing additional information about the order in a structured format.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Metadata>,

    /// Shipping address for the order.
    ///
    /// Required if any of the SKUs are for products that have `shippable` set to true.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shipping: Option<ShippingParams>,
}

impl<'a> CreateOrder<'a> {
    pub fn new(currency: Currency) -> Self {
        CreateOrder {
            coupon: Default::default(),
            currency,
            customer: Default::default(),
            email: Default::default(),
            expand: Default::default(),
            items: Default::default(),
            metadata: Default::default(),
            shipping: Default::default(),
        }
    }
}

/// The parameters for `Order::list`.
#[derive(Clone, Debug, Serialize, Default)]
pub struct ListOrders<'a> {
    /// Date this order was created.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created: Option<RangeQuery<Timestamp>>,

    /// Only return orders for the given customer.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub customer: Option<CustomerId>,

    /// A cursor for use in pagination.
    ///
    /// `ending_before` is an object ID that defines your place in the list.
    /// For instance, if you make a list request and receive 100 objects, starting with `obj_bar`, your subsequent call can include `ending_before=obj_bar` in order to fetch the previous page of the list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ending_before: Option<OrderId>,

    /// Specifies which fields in the response should be expanded.
    #[serde(skip_serializing_if = "Expand::is_empty")]
    pub expand: &'a [&'a str],

    /// Only return orders with the given IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ids: Option<Vec<String>>,

    /// A limit on the number of objects to be returned.
    ///
    /// Limit can range between 1 and 100, and the default is 10.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u64>,

    /// A cursor for use in pagination.
    ///
    /// `starting_after` is an object ID that defines your place in the list.
    /// For instance, if you make a list request and receive 100 objects, ending with `obj_foo`, your subsequent call can include `starting_after=obj_foo` in order to fetch the next page of the list.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub starting_after: Option<OrderId>,

    /// Only return orders that have the given status.
    ///
    /// One of `created`, `paid`, `fulfilled`, or `refunded`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<OrderStatusFilter>,

    /// Filter orders based on when they were paid, fulfilled, canceled, or returned.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status_transitions: Option<ListOrdersStatusTransitions>,

    /// Only return orders with the given upstream order IDs.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub upstream_ids: Option<Vec<String>>,
}

impl<'a> ListOrders<'a> {
    pub fn new() -> Self {
        ListOrders {
            created: Default::default(),
            customer: Default::default(),
            ending_before: Default::default(),
            expand: Default::default(),
            ids: Default::default(),
            limit: Default::default(),
            starting_after: Default::default(),
            status: Default::default(),
            status_transitions: Default::default(),
            upstream_ids: Default::default(),
        }
    }
}

/// The parameters for `Order::update`.
#[derive(Clone, Debug, Serialize, Default)]
pub struct UpdateOrder<'a> {
    /// A coupon code that represents a discount to be applied to this order.
    ///
    /// Must be one-time duration and in same currency as the order.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coupon: Option<CouponId>,

    /// Specifies which fields in the response should be expanded.
    #[serde(skip_serializing_if = "Expand::is_empty")]
    pub expand: &'a [&'a str],

    /// A set of key-value pairs that you can attach to a product object.
    ///
    /// It can be useful for storing additional information about the order in a structured format.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Metadata>,

    /// The shipping method to select for fulfilling this order.
    ///
    /// If specified, must be one of the `id`s of a shipping method in the `shipping_methods` array.
    /// If specified, will overwrite the existing selected shipping method, updating `items` as necessary.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selected_shipping_method: Option<&'a str>,

    /// Tracking information once the order has been fulfilled.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shipping: Option<ShippingParams>,

    /// Current order status.
    ///
    /// One of `created`, `paid`, `canceled`, `fulfilled`, or `returned`.
    /// More detail in the [Orders Guide](https://stripe.com/docs/orders/guide#understanding-order-statuses).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<OrderStatus>,
}

impl<'a> UpdateOrder<'a> {
    pub fn new() -> Self {
        UpdateOrder {
            coupon: Default::default(),
            expand: Default::default(),
            metadata: Default::default(),
            selected_shipping_method: Default::default(),
            shipping: Default::default(),
            status: Default::default(),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ListOrdersStatusTransitions {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub canceled: Option<RangeQuery<Timestamp>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub fulfilled: Option<RangeQuery<Timestamp>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub paid: Option<RangeQuery<Timestamp>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub returned: Option<RangeQuery<Timestamp>>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct OrderItemParams {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amount: Option<i64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub currency: Option<Currency>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub quantity: Option<u64>,

    #[serde(rename = "type")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub type_: Option<OrderItemParamsType>,
}

/// An enum representing the possible values of an `OrderItemParams`'s `type` field.
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum OrderItemParamsType {
    Discount,
    Shipping,
    Sku,
    Tax,
}

impl OrderItemParamsType {
    pub fn as_str(self) -> &'static str {
        match self {
            OrderItemParamsType::Discount => "discount",
            OrderItemParamsType::Shipping => "shipping",
            OrderItemParamsType::Sku => "sku",
            OrderItemParamsType::Tax => "tax",
        }
    }
}

impl AsRef<str> for OrderItemParamsType {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl std::fmt::Display for OrderItemParamsType {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.as_str().fmt(f)
    }
}

/// An enum representing the possible values of an `Order`'s `status` field.
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum OrderStatus {
    Canceled,
    Created,
    Fulfilled,
    Paid,
    Returned,
}

impl OrderStatus {
    pub fn as_str(self) -> &'static str {
        match self {
            OrderStatus::Canceled => "canceled",
            OrderStatus::Created => "created",
            OrderStatus::Fulfilled => "fulfilled",
            OrderStatus::Paid => "paid",
            OrderStatus::Returned => "returned",
        }
    }
}

impl AsRef<str> for OrderStatus {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl std::fmt::Display for OrderStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.as_str().fmt(f)
    }
}

/// An enum representing the possible values of an `ListOrders`'s `status` field.
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum OrderStatusFilter {
    Created,
    Fulfilled,
    Paid,
    Refunded,
}

impl OrderStatusFilter {
    pub fn as_str(self) -> &'static str {
        match self {
            OrderStatusFilter::Created => "created",
            OrderStatusFilter::Fulfilled => "fulfilled",
            OrderStatusFilter::Paid => "paid",
            OrderStatusFilter::Refunded => "refunded",
        }
    }
}

impl AsRef<str> for OrderStatusFilter {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl std::fmt::Display for OrderStatusFilter {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.as_str().fmt(f)
    }
}