#Kryze

1 messages ยท Page 1 of 1 (latest)

alpine impBOT
amber wren
#

Hi there!

flat mulch
#

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 ?

broken moss
#

๐Ÿ‘‹ taking over for my colleague. Let me catch up.

flat mulch
#

Thanks

broken moss
#

I'm not sure I follow the question

#

would you mind elaborating more?

flat mulch
#

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 ?

broken moss
#

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

flat mulch
#

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)

broken moss
flat mulch
#

I don't understand

broken moss
#

what exactly?

flat mulch
#

I still need to reference a Price (Stripe) Id in line_items

#

So I need to create a Stripe::Price.Create no ?

broken moss
#

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

flat mulch
#

Do you have maybe an exemple to show me ?

broken moss
#

what language are you using?

#

did you checkout the links I sent you already?

flat mulch
#

English is fine, yes I've look it up and I think I understood but I'm unsure how to create the object

broken moss
#

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

flat mulch
#

price_data: {
currency: 'eur',
unit_amount:10000,
product_data: {
name: @article.titre,
images: [rails_blob_url(@article.photo_principale)],
},
},

#

Like this ?

broken moss
#

yep

flat mulch
#

Thanks you a lot !

#

It work !