#nikivi
1 messages · Page 1 of 1 (latest)
What event type?
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
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
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
Ok give me a sec
Can you paste the checkoutsession id here?
the one that starts with cs_test_
That looks like a different id than the one in the picture above
The one in this image: #1111737262489743420 message
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
Please just paste that id
Or have you cleared your console and can't access it anymore?
cs_test_a1NcPcNfVKI8BoZsxQetKTLaU6Ba6K873xsO8bnXeuPipVPhW8867Ppe8q
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.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
But really recommend just using invoice.paid
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
case "invoice.paid":
// @ts-ignore
const paidInvoice = event.data.object
ok doing this
Unless there's a specific field on the checkout session object you need, then invoice.paid would probably be better anyway
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
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 cant find price
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
ie. one of these: https://stripe.com/docs/api/invoices/line_item
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
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
That's the latest: https://stripe.com/docs/api/versioning
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
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 🙏
cant find it here
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.
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
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
yes
You could set it on the checkout session's invoice instead here: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-invoice_creation-invoice_data-metadata
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 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
Oh you're creating subscription checkout sessions
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
Ok that param is for one-time payments
so what do we do
Agh ok
You could go back to checkout_session.completed + retrieving the invoice via the api
If you really need that metadata
Well you could also just put the price in the metadata too
But that's kinda hacky
Up to you
Ok cool. That'll prevent an additional api request