#nikivi

1 messages · Page 1 of 1 (latest)

prime violetBOT
hollow kettle
#

What event type?

strong owl
#

switch (event.type) { case "checkout.session.completed":

#

here

#
        const proYear = await stripe.checkout.sessions.create({
          success_url: process.env.STRIPE_SUCCESS_URL!,
          mode: "subscription",
          metadata: {
            userId: id,
          },
          line_items: [
            {
              quantity: 1,
              price: process.env.STRIPE_PRO_YEAR_SUBSCRIPTION!,
            },
          ],
        })
        return {
          stripeCheckoutUrl: proYear.url,
        }
#

is how i make the checkout

#
  switch (event.type) {
    case "checkout.session.completed":
      // @ts-ignore
      const checkoutSessionCompleted = event.data.object
#

trying to find in docs now

#

where in object it is

hollow kettle
strong owl
#

if its in object

#

is it possible to type this object

#

event.data.object

#
      const checkoutSessionCompleted = event.data.object

      const priceId = checkoutSessionCompleted.line_items.data.price.id
#

i guess its this

#

reading the docs

#

its unclear if any of the items is what type

#

const priceId = checkoutSessionCompleted.line_items[0].data.price.id

#

its most likely this

#

oh ok yea its array

#

should be that

#

types on that object would be so nice in typescript

#

ok im confused

#

so it means that line_items is not an array

#

what

#

if i log it

#

it doesn't show all the contents

#

it just ...

#

yea line_items are undefined

#
      console.log(checkoutSessionCompleted, "checkoutSessionCompleted")
      console.log(checkoutSessionCompleted.line_items, "line_items")
#

@hollow kettle

hollow kettle
#

Ok give me a sec

#

Can you paste the checkoutsession id here?

#

the one that starts with cs_test_

hollow kettle
#

That looks like a different id than the one in the picture above

strong owl
#

but it creates new ones every time

#

or maybe its old one as that was live

#

im doing test ones now

#

not sure what i should do

hollow kettle
#

Please just paste that id

#

Or have you cleared your console and can't access it anymore?

strong owl
#

cs_test_a1NcPcNfVKI8BoZsxQetKTLaU6Ba6K873xsO8bnXeuPipVPhW8867Ppe8q

hollow kettle
#

Ok looking

#

Oh my bad. I gave bad advice

#

You'd want to listen to invoice.paid instead if you need price

#

Looks like we don't send over line items for the checkout_session.completed event

#

If you really only want to use checkout_session.completed, then you'll need to actually make an api request to retrieve the session and expand line_items

#

By default we don't include it in the response. See: The line items purchased by the customer. This field is not included by default. To include it in the response, expand the line_items field.

#

But really recommend just using invoice.paid

strong owl
#

thats bad

#

or well

#

actually i guess i can

#

its just i need to do logic

#

based on succesful pay

#

so I can ignore checkout.session.completed

#

and just replace it with invoice paid

hollow kettle
#

invoice.paid is sufficient for successful payment

#

yep

strong owl
#
    case "invoice.paid":
      // @ts-ignore
      const paidInvoice = event.data.object
#

ok doing this

hollow kettle
#

Unless there's a specific field on the checkout session object you need, then invoice.paid would probably be better anyway

strong owl
#

yea i dont

#
    case "invoice.paid":
      // @ts-ignore
      const paidInvoice = event.data.object

      console.log(paidInvoice, "checkoutSessionCompleted")
      console.log(paidInvoice.line_items, "line_items")

      // const priceId = checkoutSessionCompleted.line_items[0].data.price.id
      // console.log(priceId, "priceId")

      if (paidInvoice.status === "complete") {
        const userId = paidInvoice.metadata.userId.trim()
        const subscription = await stripe.subscriptions.retrieve(
          paidInvoice.subscription
        )
        const endDateInUnix = subscription.current_period_end

        let date = new Date(endDateInUnix * 1000)

        let year = date.getFullYear()
        let month = ("0" + (date.getMonth() + 1)).slice(-2) // JS months start from 0
        let day = ("0" + date.getDate()).slice(-2)

        let endDate = `${year}-${month}-${day}`.trim()

        let updateUser = `
          mutation {
            userUpdate(
              by: {
                id: "${userId}"
              }
              input: {
                paidSubscriptionValidUntilDate: "${endDate}"
              }
            ) {
              user {
                paidSubscriptionValidUntilDate
              }
            }
          }
        `

        await fetch(c.env.KUSKUS_GRAFBASE_API_URL, {
          method: "POST",
          headers: {
            "x-api-key": c.env.KUSKUS_GRAFBASE_API_KEY,
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            query: updateUser,
          }),
        })
        return
      }
      break
#

is my code

#

i also have case "customer.subscription.updated":

#

but thats for later

#

need to read docs on that and test

#

i cant find price

hollow kettle
#

it's within lines

#

lines.data is an array

#

Each item in the array is an invoice line item where you can find the price

strong owl
#
  const stripe = new Stripe(c.env.KUSKUS_STRIPE_SECRET_KEY!, {
    apiVersion: "2022-11-15",
    typescript: true,
  })
#

btw i realise im using some old api version

#

i wonder if there is worth in updating

hollow kettle
strong owl
#

invoice object has no status?

#

i did a rename

#

so trying to fix/remove non used stuff

#

i guess i don't need to do this

#

ohh

#

i might have a problem

#

i pass meta data

#

which is very important to me

#

can i get that in invoice

#
      case "normalMonth":
        const normalMonth = await stripe.checkout.sessions.create({
          success_url: process.env.STRIPE_SUCCESS_URL!,
          mode: "subscription",
          metadata: {
            userId: id,
          },
          line_items: [
            {
              quantity: 1,
              price: process.env.STRIPE_NORMAL_MONTH_SUBSCRIPTION!,
            },
          ],
        })
#

userId

#

i need that id

#

hope its passed 🙏

#

Most updatable Stripe objects—including Account, Charge, Customer, PaymentIntent, Refund, Subscription, and Transfer

#

no invoice

#

so what should i do

@hollow kettle

#

in my eyes i guess i should use checkout.session.completed

#

and do api request to get the price id

#

unless i misunderstand something

hollow kettle
#

That's a lot of messages. Give me a bit to catch up

#

So looks like you're currently setting metadata on the checkout session itself

strong owl
#

yes

hollow kettle
strong owl
#

i thought it would be this

#

get this type error

#

oh ok i need enabled for some reason

#
          invoice_creation: {
            enabled: true,
            invoice_data: {
              metadata: {
                userId: id,
              },
            },
          },
#

ok nice will test

#

im confused @hollow kettle

#

i have a subscription indeed

hollow kettle
#

Oh you're creating subscription checkout sessions

strong owl
#
        const normalMonth = await stripe.checkout.sessions.create({
          success_url: process.env.STRIPE_SUCCESS_URL!,
          mode: "subscription",
          invoice_creation: {
            enabled: true,
            invoice_data: {
              metadata: {
                userId: id,
              },
            },
          },
          line_items: [
            {
              quantity: 1,
              price: process.env.STRIPE_NORMAL_MONTH_SUBSCRIPTION!,
            },
          ],
        })
#

yes

hollow kettle
#

Ok that param is for one-time payments

strong owl
#

so what do we do

hollow kettle
#

Agh ok

#

You could go back to checkout_session.completed + retrieving the invoice via the api

#

If you really need that metadata

strong owl
#

is there way to do it via api

#

no bare fetch

hollow kettle
#

Well you could also just put the price in the metadata too

#

But that's kinda hacky

#

Up to you

strong owl
#

oh

#

i dont mind that

#

thats smart actually

hollow kettle
#

Ok cool. That'll prevent an additional api request