#Kryze
1 messages ยท Page 1 of 1 (latest)
Hi there!
Why do you want to set the price as the default?
But here's how to set a default price for a product: https://stripe.com/docs/api/products/create#create_product-default_price_data
Unless I misunderstood something I do the following
When someone want to purchase something on my website I prepare a Checkout Session and I check if the Product exist in Stripe in case it was archived. I do the following
product = Stripe::Product.retrieve(db_product.stripe_id)
if(product.nil? || product.blank?)
product = Stripe::Product.create({
name: db_product.titre,
metadata:{db_id:db_product.id.to_s}
})
price = Stripe::Price.create({
product: product.id,
unit_amount: (db_product.prix * 100).to_i,
currency: 'eur'
})
tarticle = PrestationArticle.find_by(id: product.metadata["db_id"])
tarticle.stripe_id = product.id.to_s
tarticle.save
else
check_price = Stripe::Price.retrieve(
product.default_price.to_s,
)
unless(check_price.unit_amount == (db_product.prix * 100).to_i)
price = Stripe::Price.create({
product: product.id,
unit_amount: (db_product.prix * 100).to_i,
currency: 'eur'
})
Stripe::Product.update(
product.id,{default_price: price.id.to_s
})
else
price = check_price
end
....
Because I check if the price has been updated and I guess I should do what I'm doing if the price is different between Stripe and the DB no ?
๐ taking over for my colleague. Let me catch up.
Thanks
Let's imagine I have a product in my DB like this :
title : "My Product"
price : 100
currency : "eur"
When I'm creating it I create the same product in Stripe and I attach the price like this :
product: product.id,
unit_amount: (db_product.prix * 100).to_i,
currency: 'eur'
})
When a User buy a product I will create a Checkout Session, retrieve the product and check if it still exist in case someone has manually archived it on Stripe
When I do that I recreate a Product and a new Price as shown in the code
But I also check in case the Product has been changed in Stripe manually so I retrieve the price
check_price = Stripe::Price.retrieve(
product.default_price.to_s,
)
But the problem is when I create a new Product/Price it does not put it as the default price directly
Is there another solution ?
if you're managing all of your products/prices on your own db you can use instead of products/prices the price_data and price_data.product_data which you collect directly from your own db
So I don't need to create Product and Prices on Stripe ?
Do you have a documentation link about that ?
(I'll come back to the discussion in half an hour so feel free to check others requests in the meantime)
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
I don't understand
what exactly?
I still need to reference a Price (Stripe) Id in line_items
So I need to create a Stripe::Price.Create no ?
no not really
that's why I shared the price_data and price_data.product_data
you can create dynamic prices/products on the go for that Checkout Session
Do you have maybe an exemple to show me ?
English is fine, yes I've look it up and I think I understood but I'm unsure how to create the object
no I meant programming language ๐ฆ
this is part of the CheckoutSession create API
you don't have to create separate objects
it's just some params that you need to provide
so instead of providing price you would provide price_data
price_data: {
currency: 'eur',
unit_amount:10000,
product_data: {
name: @article.titre,
images: [rails_blob_url(@article.photo_principale)],
},
},
Like this ?
yep