Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(next): owned types on requests #597

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions async-stripe-types/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,34 @@ macro_rules! def_id {
}
}

impl From<&str> for $struct_name {
#[inline]
fn from(text: &str) -> Self {
Self(smol_str::SmolStr::from(text))
}
}

impl From<$struct_name> for String {
#[inline]
fn from(id: $struct_name) -> Self {
id.0.to_string()
}
}

impl From<&$struct_name> for String {
#[inline]
fn from(id: &$struct_name) -> Self {
id.0.to_string()
}
}

impl From<&$struct_name> for $struct_name {
#[inline]
fn from(id: &$struct_name) -> Self {
id.clone()
}
}

#[cfg(feature = "deserialize")]
impl<'de> serde::Deserialize<'de> for $struct_name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
Expand Down
19 changes: 10 additions & 9 deletions examples/endpoints/src/checkout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,28 @@ use stripe_product::product::CreateProduct;
use stripe_types::{Currency, Expandable};

pub async fn run_checkout_session_example(client: &stripe::Client) -> Result<(), StripeError> {
let metadata =
std::collections::HashMap::from([(String::from("async-stripe"), String::from("true"))]);
let customer = CreateCustomer::new()
.name("Alexander Lyon")
.email("[email protected]")
.description("A fake customer that is used to illustrate the examples in async-stripe.")
.metadata(&metadata)
.metadata([(String::from("async-stripe"), String::from("true"))])
.send(client)
.await?;

println!("created a customer at https://dashboard.stripe.com/test/customers/{}", customer.id);

// create a new example product
let product = CreateProduct::new("T-Shirt").metadata(&metadata).send(client).await?;
let product = CreateProduct::new("T-Shirt")
.metadata([(String::from("async-stripe"), String::from("true"))])
.send(client)
.await?;

// and add a price for it in USD
let price = CreatePrice::new(Currency::USD)
.product(product.id.as_str())
.metadata(&metadata)
.metadata([(String::from("async-stripe"), String::from("true"))])
.unit_amount(1000)
.expand(&["product"])
.expand([String::from("product")])
.send(client)
.await?;

Expand All @@ -52,15 +53,15 @@ pub async fn run_checkout_session_example(client: &stripe::Client) -> Result<(),
// finally, create a checkout session for this product / price
let line_items = vec![CreateCheckoutSessionLineItems {
quantity: Some(3),
price: Some(&price.id),
price: Some(price.id.to_string()),
..Default::default()
}];
let checkout_session = CreateCheckoutSession::new()
.cancel_url("http://test.com/cancel")
.customer(customer.id.as_str())
.mode(CheckoutSessionMode::Payment)
.line_items(&line_items)
.expand(&["line_items", "line_items.data.price.product"])
.line_items(line_items)
.expand([String::from("line_items"), String::from("line_items.data.price.product")])
.send(client)
.await?;

Expand Down
2 changes: 1 addition & 1 deletion examples/endpoints/src/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub async fn run_connect_example(client: &stripe::Client) -> Result<(), StripeEr
.send(client)
.await?;

let link = CreateAccountLink::new(&account.id, CreateAccountLinkType::AccountOnboarding)
let link = CreateAccountLink::new(account.id, CreateAccountLinkType::AccountOnboarding)
.refresh_url("https://test.com/refresh")
.return_url("https://test.com/return")
.send(client)
Expand Down
6 changes: 2 additions & 4 deletions examples/endpoints/src/customer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ use stripe::{Client, StripeError};
use stripe_core::customer::{CreateCustomer, ListCustomer};

pub async fn run_customer_example(client: &Client) -> Result<(), StripeError> {
let meta =
std::collections::HashMap::from([(String::from("async-stripe"), String::from("true"))]);
let customer = CreateCustomer::new()
.name("Alexander Lyon")
.email("[email protected]")
.description("A fake customer that is used to illustrate the examples in async-stripe.")
.metadata(&meta)
.metadata([(String::from("async-stripe"), String::from("true"))])
.send(client)
.await?;

Expand All @@ -27,7 +25,7 @@ pub async fn run_customer_example(client: &Client) -> Result<(), StripeError> {
.name("Someone Else")
.email("[email protected]")
.description("A fake customer that is used to illustrate the examples in async-stripe.")
.metadata(&meta)
.metadata([(String::from("async-stripe"), String::from("true"))])
.send(client)
.await?;

Expand Down
19 changes: 8 additions & 11 deletions examples/endpoints/src/payment_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,21 @@ use stripe_payment::payment_method::{
use stripe_types::Currency;

pub async fn run_payment_intent_example(client: &Client) -> Result<(), StripeError> {
let meta =
std::collections::HashMap::from([(String::from("async-stripe"), String::from("true"))]);
let customer = CreateCustomer::new()
.name("Alexander Lyon")
.email("[email protected]")
.description("A fake customer that is used to illustrate the examples in async-stripe.")
.metadata(&meta)
.metadata([(String::from("async-stripe"), String::from("true"))])
.send(client)
.await?;

println!("created a customer at https://dashboard.stripe.com/test/customers/{}", customer.id);

// we create an intent to pay
let meta = [("color".to_string(), "red".to_string())].iter().cloned().collect();
let payment_intent = CreatePaymentIntent::new(1000, Currency::USD)
.payment_method_types(&["card"])
.payment_method_types([String::from("card")])
.statement_descriptor("Purchasing a new car")
.metadata(&meta)
.metadata([("color".to_string(), "red".to_string())])
.send(client)
.await?;

Expand All @@ -46,16 +43,16 @@ pub async fn run_payment_intent_example(client: &Client) -> Result<(), StripeErr
let payment_method = CreatePaymentMethod::new()
.type_(CreatePaymentMethodType::Card)
.card(CreatePaymentMethodCard::CardDetailsParams(CreatePaymentMethodCardDetailsParams {
number: "4000008260000000", // UK visa
number: String::from("4000008260000000"), // UK visa
exp_year: 2025,
exp_month: 1,
cvc: Some("123"),
cvc: Some(String::from("123")),
networks: None,
}))
.send(client)
.await?;

AttachPaymentMethod::new(&payment_method.id, &customer.id).send(client).await?;
AttachPaymentMethod::new(payment_method.id.clone(), &customer.id).send(client).await?;

println!(
"created a payment method with id {} and attached it to {}",
Expand All @@ -64,7 +61,7 @@ pub async fn run_payment_intent_example(client: &Client) -> Result<(), StripeErr
);

// lets update the payment intent with their details
let payment_intent = UpdatePaymentIntent::new(&payment_intent.id)
let payment_intent = UpdatePaymentIntent::new(payment_intent.id)
.payment_method(payment_method.id.as_str())
// this is not strictly required but good practice to ensure we have the right person
.customer(customer.id.as_str())
Expand All @@ -73,7 +70,7 @@ pub async fn run_payment_intent_example(client: &Client) -> Result<(), StripeErr

println!("updated payment intent with status '{}'", payment_intent.status);

let payment_intent = ConfirmPaymentIntent::new(&payment_intent.id).send(client).await?;
let payment_intent = ConfirmPaymentIntent::new(payment_intent.id).send(client).await?;
println!("completed payment intent with status {}", payment_intent.status);
Ok(())
}
13 changes: 7 additions & 6 deletions examples/endpoints/src/payment_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ use stripe_types::Currency;

pub async fn run_payment_link_example(client: &Client) -> Result<(), StripeError> {
// create a new example project
let meta =
std::collections::HashMap::from([(String::from("async-stripe"), String::from("true"))]);
let product = CreateProduct::new("T-Shirt").metadata(&meta).send(client).await?;
let product = CreateProduct::new("T-Shirt")
.metadata([(String::from("async-stripe"), String::from("true"))])
.send(client)
.await?;

// and add a price for it in USD
let price = CreatePrice::new(Currency::USD)
.product(product.id.as_str())
.metadata(&meta)
.metadata([(String::from("async-stripe"), String::from("true"))])
.unit_amount(1000)
.expand(&["product"])
.expand([String::from("product")])
.send(client)
.await?;

Expand All @@ -38,7 +39,7 @@ pub async fn run_payment_link_example(client: &Client) -> Result<(), StripeError
let payment_link = CreatePaymentLink::new(&[CreatePaymentLinkLineItems {
adjustable_quantity: None,
quantity: 3,
price: &price.id,
price: price.id.to_string(),
}])
.send(client)
.await?;
Expand Down
32 changes: 19 additions & 13 deletions examples/endpoints/src/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,27 @@ use stripe_product::product::CreateProduct;
use stripe_types::{Currency, Expandable};

pub async fn run_subscriptions_example(client: &Client) -> Result<(), StripeError> {
let meta =
std::collections::HashMap::from([(String::from("async-stripe"), String::from("true"))]);
let customer = CreateCustomer::new()
.name("Alexander Lyon")
.email("[email protected]")
.description("A fake customer that is used to illustrate the examples in async-stripe.")
.metadata(&meta)
.metadata([(String::from("async-stripe"), String::from("true"))])
.send(client)
.await?;

println!("created a customer at https://dashboard.stripe.com/test/customers/{}", customer.id);
let payment_method = CreatePaymentMethod::new()
.type_(CreatePaymentMethodType::Card)
.card(CreatePaymentMethodCard::CardDetailsParams(CreatePaymentMethodCardDetailsParams {
number: "4000008260000000", // UK visa
number: String::from("4000008260000000"), // UK visa
exp_year: 2025,
exp_month: 1,
cvc: Some("123"),
cvc: Some(String::from("123")),
networks: None,
}))
.send(client)
.await?;
AttachPaymentMethod::new(&payment_method.id, &customer.id).send(client).await?;
AttachPaymentMethod::new(payment_method.id.clone(), &customer.id).send(client).await?;

println!(
"created a payment method with id {} and attached it to {}",
Expand All @@ -49,16 +47,18 @@ pub async fn run_subscriptions_example(client: &Client) -> Result<(), StripeErro
);

// create a new example product
let product =
CreateProduct::new("Monthly T-Shirt Subscription").metadata(&meta).send(client).await?;
let product = CreateProduct::new("Monthly T-Shirt Subscription")
.metadata([(String::from("async-stripe"), String::from("true"))])
.send(client)
.await?;

// and add a price for it in USD
let price = CreatePrice::new(Currency::USD)
.product(&product.id)
.metadata(&meta)
.metadata([(String::from("async-stripe"), String::from("true"))])
.unit_amount(1000)
.recurring(CreatePriceRecurring::new(CreatePriceRecurringInterval::Month))
.expand(&["product"])
.expand([String::from("product")])
.send(client)
.await?;

Expand All @@ -69,11 +69,17 @@ pub async fn run_subscriptions_example(client: &Client) -> Result<(), StripeErro
price.currency,
);

let create_items = [CreateSubscriptionItems { price: Some(&price.id), ..Default::default() }];
let subscription = CreateSubscription::new(&customer.id)
.items(&create_items)
.items(vec![CreateSubscriptionItems {
price: Some(price.id.to_string()),
..Default::default()
}])
.default_payment_method(&payment_method.id)
.expand(&["items", "items.data.price.product", "schedule"])
.expand([
String::from("items"),
String::from("items.data.price.product"),
String::from("schedule"),
])
.send(client)
.await?;

Expand Down
Loading
Loading