#nikivi_code

1 messages ยท Page 1 of 1 (latest)

wet roverBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! We typically respond in a few minutes, but in some cases we might need a bit more time (e.g., server's busy, you've got a complex question, etc.).

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1254792695243739257

๐Ÿ“ Have more to share? Add details, code, screenshots, videos, etc. below.

gaunt obsidian
#
import { cors } from '@elysiajs/cors'
import { Elysia, t } from 'elysia'
import { stripe } from '@/api/lib/stripe'

const app = new Elysia()
    .onError(({ error }) => {
        console.log(error, 'error')
        return new Response(error.toString())
    })
    .use(cors())
    .post(
        '/create-product',
        async ({ body }) => {
            const { productRoninId, productTitle, productDescription, productPrice } = body
            console.log(process.env.STRIPE_SECRET_KEY!, 'secret key')
            console.log('----')

            const product = await stripe.products.create({
                name: productTitle
                // description: productDescription,
                // default_price_data: {
                //     unit_amount: productPrice * 100,
                //     currency: 'usd'
                // }
            })
            console.log(product, 'stripe product')
        },
        {
            body: t.Object({
                productRoninId: t.String(),
                productTitle: t.String(),
                productDescription: t.String(),
                productPrice: t.Number()
            })
        }
    )
#

my full endpoint

#

@/api/lib/stripe looks like

#
import Stripe from 'stripe'

export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
    // https://github.com/stripe/stripe-node#configuration
    apiVersion: '2024-04-10'
})

export const STRIPE_WEBHOOK_SECRET = process.env.STRIPE_WEBHOOK_SECRET!

#

I essentially want to create a product on demand that will have a checkout page

#

I thought from docs

#

I could use above api for this

#

but getting

#

ok this actually works

#

and I get massive product back

#

but which field is the checkout url

#

to show to do payment

hoary spruceBOT
gaunt obsidian
#

because I want to do stripe checkout that has name, description, price filled in

#

is 200

plain storm
#

Hi there ๐Ÿ‘‹ I'm not sure where the response you're sharing is coming from, that doesn't look like the format for a response from our API.

Creating a Product does not provide you with a URL that you can share for customers to check out.

#

For the latter you either need to create a Checkout Session, where you can use price_data and product_data to create your Price and Product objects adhoc.

gaunt obsidian
#

I need to create session

plain storm
#

Or you'd create a Payment Link for the newly created Product.

gaunt obsidian
#

but the thing is that

#

this product can be anything

#

i.e. a product can have a title etc. that is custom

#

previous i only used stripe for saas

gaunt obsidian
#

and there i had products that users bought but here I have to make on demand

#

checkout sessions

#

that can be anything

gaunt obsidian
plain storm
#

Pretty sure that's the parameter you're looking for

gaunt obsidian
#

i.e. here

#

price_1MotwRLkdIwHu7ixYcPLm5uZ

#

i dont know this

#

the price can be arbitrary

#

if that makes sense

#

i.e. I pass create stripe product with name, description and price and I get back checkout session

#

and also productId as metadata passed to checkout session

#

then in web hook when session is complete, I make sure user owns that product for real

#

thats how I thought i'd do it

#

but with code above, it wants me to know price_1MotwRLkdIwHu7ixYcPLm5uZ

#

which I can't know as that price_1MotwRLkdIwHu7ixYcPLm5uZ is fixed to a price

#

perhaps I misunderstand things

#
            const session = await stripe.checkout.sessions.create({
                success_url: 'https://solbond.co/payment-success',
                line_items: [
                    {
                        price_data: {
                            currency: 'usd',
                            product_data: {
                                name: productTitle,
                                description: productDescription
                            },
                            unit_amount: Math.round(Number(productPrice) * 100)
                        },
                        quantity: 1
                    }
                ],
                mode: 'payment',
                metadata: {
                    productRoninId: productRoninId
                }
            })
#

sonnet said to do this potentially, will try now

#

ok i get back 200 and session back

#

how do I get checkoutSession from it

#

i do get events

#
            const checkoutSessionUrl = session.
#

thought I can get it from line_items

#

but its not on session

plain storm
#

I don't understand what the current question is. You say you get a session back, but don't know how to get the session from that?

gaunt obsidian
#

yes

#
        async ({ body }) => {
            const { productTitle, productDescription, productPrice, productRoninId } = body
            console.log(process.env.STRIPE_SECRET_KEY!, 'secret key')
            console.log('----')

            const session = await stripe.checkout.sessions.create({
                success_url: 'https://solbond.co/payment-success',
                line_items: [
                    {
                        price_data: {
                            currency: 'usd',
                            product_data: {
                                name: productTitle,
                                description: productDescription
                            },
                            unit_amount: Math.round(Number(productPrice) * 100)
                        },
                        quantity: 1
                    }
                ],
                mode: 'payment',
                metadata: {
                    productRoninId: productRoninId
                }
            })

plain storm
#

The response to the request is a Checkout Session object.

gaunt obsidian
#

this creates what I think is a product

plain storm
#

What are you not finding?

#

No, that creates a Checkout Session.

gaunt obsidian
#

yes

#

basically i need a checkout session tied to productId

#

and at that point

#

i need to get the session url

#

so when user presses

#

it will open stripe checkout

#

user pays there, and on web hook i update my own db to unlock product

#

if that makes sense

#

i still am confused how to get checkout url

#

i get big object back from await stripe.checkout.sessions.create

#

but it doesn't seem to have checkout url

#

apologize if its unclear, I can expand more

plain storm
gaunt obsidian
#

thank you that seems to work

#

will do web hooks now but will open new thread if stuck

#

appreciate the help a lot

plain storm
#

Any time!