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

use crate::resources::common::path::UrlPath;
use crate::resources::core::charges::{ShippingDetails, Charge};
use crate::util::{List, Expandable};
use crate::{Client};
use std::collections::HashMap;
use crate::resources::orders::sku::Sku;
use crate::resources::billing::discounts::Discount;
use crate::resources::core::customer::Customer;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Order {
    pub id: String,
    pub object: Object,
    pub amount: i64,
    pub amount_returned: Option<i64>,
    pub application: Option<String>,
    pub application_fee: Option<i64>,
    pub charge: Option<Expandable<Charge>>,
    pub created: i64,
    pub currency: Currency,
    pub customer: Option<Expandable<Customer>>,
    pub email: Option<String>,
    pub items: Vec<OrderItem>,
    pub livemode: bool,
    pub metadata: HashMap<String, String>,
    pub returns: List<OrderReturn>,
    pub selected_shipping_method: Option<String>,
    pub shipping: ShippingDetails,
    pub shipping_methods: Vec<ShippingMethods>,
    pub status: OrderStatus,
    pub status_transitions: OrderTransitions,
    pub updated: i64,
    pub upstream_id: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct DeliveryEstimate {
    #[serde(rename = "type")]
    pub delivery_type: DeliveryType,
    pub latest: Option<String>,
    pub earliest: Option<String>,
    pub date: Option<String>,
}

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

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct ShippingMethods {
    pub id: String,
    pub amount: i64,
    pub currency: Currency,
    pub delivery_estimate: Option<DeliveryEstimate>,
    pub description: String,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct OrderTransitions {
    pub canceled: i64,
    pub fulfiled: Option<i64>,
    pub paid: i64,
    pub returned: Option<i64>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct OrderReturn {
    pub id: String,
    pub object: Object,
    pub amount: i64,
    pub created: i64,
    pub currency: Currency,
    pub items: Vec<OrderItem>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct OrderItem {
    pub object: Option<Object>,
    pub amount: Option<i64>,
    pub currency: Option<Currency>,
    pub description: Option<String>,
    pub parent: Option<Expandable<OrderItemParent>>,
    pub quantity: Option<i64>,
    #[serde(rename = "type")]
    pub item_type: Option<ItemType>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(untagged)]
pub enum OrderItemParent {
    Sku(Sku),
    Discount(Discount)
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ItemType {
    Sku,
    Tax,
    Shipping,
    Discount,
    #[serde(other, skip_serializing)]
    Unknown,
}

impl Default for ItemType {
    fn default() -> ItemType {
        ItemType::Unknown
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum OrderStatus {
    Created,
    Paid,
    Canceled,
    Fulfilled,
    Returned,
}

#[derive(Default, Serialize, Debug, PartialEq)]
pub struct OrderParam<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub currency: Option<Currency>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub coupon: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub customer: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<&'a str>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub items: Option<OrderItem>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<&'a str, &'a str>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shipping: Option<ShippingDetails>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expand: Option<Vec<&'a str>>,
}

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

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

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

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

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

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