#nikivi
1 messages · Page 1 of 1 (latest)
When you say "locally", is that to say you have this working for another endpoint?
Or is local the first place that you are testing and it is not working there?
i read through this
in my code
i set the env var
correctly
app.post("/learn-anything-bought", async (c: Context) => {
console.log(c.env.LA_STRIPE_WEBHOOK_SECRET!, "key..")
let event = c.req.body
const stripe = new Stripe(c.env.LA_STRIPE_SECRET_KEY!, {
apiVersion: "2023-08-16",
typescript: true,
})
i get No stripe-signature header value was provided
in the doc its not mentioned anywhere
Additionally, verify webhook signatures to confirm that received events are sent from Stripe. Stripe signs webhook events it sends to your endpoints by including a signature in each event’s Stripe-Signature header
only this
but its unclear what i should send in my curl
Oh you are sending a webhook event with curl?
I would recommend just sending events with stripe trigger from our CLI, you can calculate a webhook signature and send it in a header via curl but it would be a bit time consuming
ok let me try trigger
2023-10-13 16:12:40 --> price.created [evt_1O0m6W4soP2HpBfdzxu12qxJ]
2023-10-13 16:12:40 [ERROR] Failed to POST: Post "http://localhost:4242/learn-anything-bought": dial tcp [::1]:4242: connect: connection refused
i get this ones
stripe trigger checkout.session.completed
just ran this
as thats what i want to test
i think its because the port is wrong
but not sure how to change it
my server is on http://0.0.0.0:8787
or i should change server port
The port for that would be defined by the stripe listen command that it sound like you ran in another console
how can i pass metadata to it
const monthSubscription = await stripe.checkout.sessions.create({
success_url: process.env.STRIPE_SUCCESS_URL!,
mode: "subscription",
metadata: {
userEmail: args.userEmail
},
line_items: [
{
quantity: 1,
price: process.env.STRIPE_MONTH_SUBSCRIPTION!
}
]
})
in prod i do this
i want to test locally
by passing in userEmail
when I run stripe trigger checkout.session.completed
You can do this with the --add flag https://stripe.com/docs/cli/trigger#trigger-add
Figuring out what exactly you would type there
stripe trigger checkout.session.completed --add emailAddress:nikita@nikiv.dev
like this?
yea it didnt like that
Yeah, the format is way different
And it looks like we need to use the --override command for metadata on this event
It just worked for me with
stripe trigger checkout.session.completed --override checkout_session:metadata[userEmail]='person@example.com'
how can i emulate this
switch (event.type) {
case "checkout.session.completed":
// @ts-ignore
const checkoutSessionCompleted = event.data.object
console.log(checkoutSessionCompleted, "completed")
console.log(checkoutSessionCompleted.status, "status")
if (checkoutSessionCompleted.status === "complete") {
// const subscriptionType =
// checkoutSessionCompleted.metadata.subscriptionType.trim()
console.log(checkoutSessionCompleted.metadata, "metadata")
const email = checkoutSessionCompleted.metadata.userEmail.trim()
console.log(email, "email")
const subscription = await stripe.subscriptions.retrieve(
checkoutSessionCompleted.subscription,
)
const endDateInUnix = subscription.current_period_end
console.log(endDateInUnix, "end date in unix!")
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()
console.log(endDate, "end date")
essentially the event should come with a date in unix
for some reason locally this fails
i think in prod it would work
as i have a prod stripe project i took this code from
get Stripe: Argument "subscription_exposed_id" must be a string, but got: null (on API request to GET /v1/subscriptions/{subscription_exposed_id})
perhaps im doing something off
i basically need the date like 2023-06-20 until which the session was bought for
thought thats how you get it
I think it would be easiest to just make and complete a Checkout Session for a subscription and then resend the event for it https://stripe.com/docs/cli/events/resend
This error is happening because trigger is simulating a checkout session for a one time payment which is not what your code is expecting
oh yes
i remember doing that when i worked on the code
how can i get checkout session from cli
i dont get how to create a product in new ui
ok it was in sidebar
ok let me see
ok
there is a bug it seems
i see no logs
i should get a log with email
Can you send me the ID of the checkout session? I can check in to its events
It looks like we did send events for that session
Oh and I see them listed in your output in your screenshot
It is all the lines that say "Unhandled event type"
So it sounds like you need to add handlers in your code
yes events get sent
but
case "checkout.session.completed":
// @ts-ignore
const checkoutSessionCompleted = event.data.object
console.log(checkoutSessionCompleted, "completed")
console.log(checkoutSessionCompleted.status, "status")
this should run
and i should see logs
i dont
i need to debug if db query works after checkout session completed
right now i cant get to logs
It sounds like you need to debug your event handling code
Because currently your server is outputting "Unhandled event type"
So you are getting the events but your server says it does not know how to receive them
what does it mean to handle it
i thought this part handles it
I mean that is determined by your code
I am sorry but I can't review and that much code of yours
i am not receiving the event though
You will need to trace your code for this event handler
in the backend
the switch does not run
Right because your code is broken now and you need to debug it yourself
I wish I could help but that is too much code for me to go through. The best way forward is for you to track what your code does when it receives and event
Maybe look for where your code throws that "Unhandled event type" error and work backwards
ah
const email = checkoutSessionCompleted.metadata.userEmail.trim()
it fails here
can i send this userEmail
on this checkout thing
const monthSubscription = await stripe.checkout.sessions.create({
success_url: process.env.STRIPE_SUCCESS_URL!,
mode: "subscription",
metadata: {
userEmail: args.userEmail
},
its metadata
i guess the checkout sends no metadata
thus the failure
Object {
} metadata
yea it logs this