use crate::config::{Client, Response};
use crate::ids::PriceId;
use crate::params::{
Expand, Expandable, IdOrCreate, List, Metadata, Object, RangeQuery, Timestamp,
};
use crate::resources::{CreateProduct, Currency, Product, UpTo};
use serde_derive::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Price {
pub id: PriceId,
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub billing_scheme: Option<PriceBillingScheme>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created: Option<Timestamp>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<Currency>,
#[serde(default)]
pub deleted: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub livemode: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub lookup_key: Option<String>,
#[serde(default)]
pub metadata: Metadata,
#[serde(skip_serializing_if = "Option::is_none")]
pub nickname: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub product: Option<Expandable<Product>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub recurring: Option<Recurring>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tiers: Option<Vec<PriceTier>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tiers_mode: Option<PriceTiersMode>,
#[serde(skip_serializing_if = "Option::is_none")]
pub transform_quantity: Option<TransformQuantity>,
#[serde(rename = "type")]
#[serde(skip_serializing_if = "Option::is_none")]
pub type_: Option<PriceType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit_amount: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit_amount_decimal: Option<String>,
}
impl Price {
pub fn list(client: &Client, params: ListPrices<'_>) -> Response<List<Price>> {
client.get_query("/prices", ¶ms)
}
pub fn create(client: &Client, params: CreatePrice<'_>) -> Response<Price> {
client.post_form("/prices", ¶ms)
}
pub fn retrieve(client: &Client, id: &PriceId, expand: &[&str]) -> Response<Price> {
client.get_query(&format!("/prices/{}", id), &Expand { expand })
}
pub fn update(client: &Client, id: &PriceId, params: UpdatePrice<'_>) -> Response<Price> {
client.post_form(&format!("/prices/{}", id), ¶ms)
}
}
impl Object for Price {
type Id = PriceId;
fn id(&self) -> Self::Id {
self.id.clone()
}
fn object(&self) -> &'static str {
"price"
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PriceTier {
#[serde(skip_serializing_if = "Option::is_none")]
pub flat_amount: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub flat_amount_decimal: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit_amount: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit_amount_decimal: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub up_to: Option<i64>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Recurring {
#[serde(skip_serializing_if = "Option::is_none")]
pub aggregate_usage: Option<RecurringAggregateUsage>,
pub interval: RecurringInterval,
pub interval_count: u64,
pub usage_type: RecurringUsageType,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TransformQuantity {
pub divide_by: i64,
pub round: TransformQuantityRound,
}
#[derive(Clone, Debug, Serialize)]
pub struct CreatePrice<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub billing_scheme: Option<PriceBillingScheme>,
pub currency: Currency,
#[serde(skip_serializing_if = "Expand::is_empty")]
pub expand: &'a [&'a str],
#[serde(skip_serializing_if = "Option::is_none")]
pub lookup_key: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<Metadata>,
#[serde(skip_serializing_if = "Option::is_none")]
pub nickname: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub product: Option<IdOrCreate<'a, CreateProduct<'a>>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub product_data: Option<CreatePriceProductData>,
#[serde(skip_serializing_if = "Option::is_none")]
pub recurring: Option<CreatePriceRecurring>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tiers: Option<Vec<CreatePriceTiers>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tiers_mode: Option<PriceTiersMode>,
#[serde(skip_serializing_if = "Option::is_none")]
pub transfer_lookup_key: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub transform_quantity: Option<CreatePriceTransformQuantity>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit_amount: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit_amount_decimal: Option<&'a str>,
}
impl<'a> CreatePrice<'a> {
pub fn new(currency: Currency) -> Self {
CreatePrice {
active: Default::default(),
billing_scheme: Default::default(),
currency,
expand: Default::default(),
lookup_key: Default::default(),
metadata: Default::default(),
nickname: Default::default(),
product: Default::default(),
product_data: Default::default(),
recurring: Default::default(),
tiers: Default::default(),
tiers_mode: Default::default(),
transfer_lookup_key: Default::default(),
transform_quantity: Default::default(),
unit_amount: Default::default(),
unit_amount_decimal: Default::default(),
}
}
}
#[derive(Clone, Debug, Serialize, Default)]
pub struct ListPrices<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created: Option<RangeQuery<Timestamp>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<Currency>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ending_before: Option<PriceId>,
#[serde(skip_serializing_if = "Expand::is_empty")]
pub expand: &'a [&'a str],
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub lookup_keys: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub product: Option<IdOrCreate<'a, CreateProduct<'a>>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub recurring: Option<ListPricesRecurring>,
#[serde(skip_serializing_if = "Option::is_none")]
pub starting_after: Option<PriceId>,
#[serde(rename = "type")]
#[serde(skip_serializing_if = "Option::is_none")]
pub type_: Option<PriceType>,
}
impl<'a> ListPrices<'a> {
pub fn new() -> Self {
ListPrices {
active: Default::default(),
created: Default::default(),
currency: Default::default(),
ending_before: Default::default(),
expand: Default::default(),
limit: Default::default(),
lookup_keys: Default::default(),
product: Default::default(),
recurring: Default::default(),
starting_after: Default::default(),
type_: Default::default(),
}
}
}
#[derive(Clone, Debug, Serialize, Default)]
pub struct UpdatePrice<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
#[serde(skip_serializing_if = "Expand::is_empty")]
pub expand: &'a [&'a str],
#[serde(skip_serializing_if = "Option::is_none")]
pub lookup_key: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<Metadata>,
#[serde(skip_serializing_if = "Option::is_none")]
pub nickname: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub transfer_lookup_key: Option<bool>,
}
impl<'a> UpdatePrice<'a> {
pub fn new() -> Self {
UpdatePrice {
active: Default::default(),
expand: Default::default(),
lookup_key: Default::default(),
metadata: Default::default(),
nickname: Default::default(),
transfer_lookup_key: Default::default(),
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CreatePriceProductData {
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(default)]
pub metadata: Metadata,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub statement_descriptor: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit_label: Option<String>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CreatePriceRecurring {
#[serde(skip_serializing_if = "Option::is_none")]
pub aggregate_usage: Option<CreatePriceRecurringAggregateUsage>,
pub interval: CreatePriceRecurringInterval,
#[serde(skip_serializing_if = "Option::is_none")]
pub interval_count: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub usage_type: Option<CreatePriceRecurringUsageType>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CreatePriceTiers {
#[serde(skip_serializing_if = "Option::is_none")]
pub flat_amount: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub flat_amount_decimal: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit_amount: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub unit_amount_decimal: Option<String>,
pub up_to: Option<UpTo>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CreatePriceTransformQuantity {
pub divide_by: i64,
pub round: CreatePriceTransformQuantityRound,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ListPricesRecurring {
#[serde(skip_serializing_if = "Option::is_none")]
pub interval: Option<ListPricesRecurringInterval>,
#[serde(skip_serializing_if = "Option::is_none")]
pub usage_type: Option<ListPricesRecurringUsageType>,
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum CreatePriceRecurringAggregateUsage {
LastDuringPeriod,
LastEver,
Max,
Sum,
}
impl CreatePriceRecurringAggregateUsage {
pub fn as_str(self) -> &'static str {
match self {
CreatePriceRecurringAggregateUsage::LastDuringPeriod => "last_during_period",
CreatePriceRecurringAggregateUsage::LastEver => "last_ever",
CreatePriceRecurringAggregateUsage::Max => "max",
CreatePriceRecurringAggregateUsage::Sum => "sum",
}
}
}
impl AsRef<str> for CreatePriceRecurringAggregateUsage {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for CreatePriceRecurringAggregateUsage {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum CreatePriceRecurringInterval {
Day,
Month,
Week,
Year,
}
impl CreatePriceRecurringInterval {
pub fn as_str(self) -> &'static str {
match self {
CreatePriceRecurringInterval::Day => "day",
CreatePriceRecurringInterval::Month => "month",
CreatePriceRecurringInterval::Week => "week",
CreatePriceRecurringInterval::Year => "year",
}
}
}
impl AsRef<str> for CreatePriceRecurringInterval {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for CreatePriceRecurringInterval {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum CreatePriceRecurringUsageType {
Licensed,
Metered,
}
impl CreatePriceRecurringUsageType {
pub fn as_str(self) -> &'static str {
match self {
CreatePriceRecurringUsageType::Licensed => "licensed",
CreatePriceRecurringUsageType::Metered => "metered",
}
}
}
impl AsRef<str> for CreatePriceRecurringUsageType {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for CreatePriceRecurringUsageType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum CreatePriceTransformQuantityRound {
Down,
Up,
}
impl CreatePriceTransformQuantityRound {
pub fn as_str(self) -> &'static str {
match self {
CreatePriceTransformQuantityRound::Down => "down",
CreatePriceTransformQuantityRound::Up => "up",
}
}
}
impl AsRef<str> for CreatePriceTransformQuantityRound {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for CreatePriceTransformQuantityRound {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ListPricesRecurringInterval {
Day,
Month,
Week,
Year,
}
impl ListPricesRecurringInterval {
pub fn as_str(self) -> &'static str {
match self {
ListPricesRecurringInterval::Day => "day",
ListPricesRecurringInterval::Month => "month",
ListPricesRecurringInterval::Week => "week",
ListPricesRecurringInterval::Year => "year",
}
}
}
impl AsRef<str> for ListPricesRecurringInterval {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for ListPricesRecurringInterval {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ListPricesRecurringUsageType {
Licensed,
Metered,
}
impl ListPricesRecurringUsageType {
pub fn as_str(self) -> &'static str {
match self {
ListPricesRecurringUsageType::Licensed => "licensed",
ListPricesRecurringUsageType::Metered => "metered",
}
}
}
impl AsRef<str> for ListPricesRecurringUsageType {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for ListPricesRecurringUsageType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum PriceBillingScheme {
PerUnit,
Tiered,
}
impl PriceBillingScheme {
pub fn as_str(self) -> &'static str {
match self {
PriceBillingScheme::PerUnit => "per_unit",
PriceBillingScheme::Tiered => "tiered",
}
}
}
impl AsRef<str> for PriceBillingScheme {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for PriceBillingScheme {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum PriceTiersMode {
Graduated,
Volume,
}
impl PriceTiersMode {
pub fn as_str(self) -> &'static str {
match self {
PriceTiersMode::Graduated => "graduated",
PriceTiersMode::Volume => "volume",
}
}
}
impl AsRef<str> for PriceTiersMode {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for PriceTiersMode {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum PriceType {
OneTime,
Recurring,
}
impl PriceType {
pub fn as_str(self) -> &'static str {
match self {
PriceType::OneTime => "one_time",
PriceType::Recurring => "recurring",
}
}
}
impl AsRef<str> for PriceType {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for PriceType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum RecurringAggregateUsage {
LastDuringPeriod,
LastEver,
Max,
Sum,
}
impl RecurringAggregateUsage {
pub fn as_str(self) -> &'static str {
match self {
RecurringAggregateUsage::LastDuringPeriod => "last_during_period",
RecurringAggregateUsage::LastEver => "last_ever",
RecurringAggregateUsage::Max => "max",
RecurringAggregateUsage::Sum => "sum",
}
}
}
impl AsRef<str> for RecurringAggregateUsage {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for RecurringAggregateUsage {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum RecurringInterval {
Day,
Month,
Week,
Year,
}
impl RecurringInterval {
pub fn as_str(self) -> &'static str {
match self {
RecurringInterval::Day => "day",
RecurringInterval::Month => "month",
RecurringInterval::Week => "week",
RecurringInterval::Year => "year",
}
}
}
impl AsRef<str> for RecurringInterval {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for RecurringInterval {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum RecurringUsageType {
Licensed,
Metered,
}
impl RecurringUsageType {
pub fn as_str(self) -> &'static str {
match self {
RecurringUsageType::Licensed => "licensed",
RecurringUsageType::Metered => "metered",
}
}
}
impl AsRef<str> for RecurringUsageType {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for RecurringUsageType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum TransformQuantityRound {
Down,
Up,
}
impl TransformQuantityRound {
pub fn as_str(self) -> &'static str {
match self {
TransformQuantityRound::Down => "down",
TransformQuantityRound::Up => "up",
}
}
}
impl AsRef<str> for TransformQuantityRound {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for TransformQuantityRound {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}