Next.js Discord

Discord Forum

Having issues with Stripe Webhooks in App Router

Unanswered
Russian Blue posted this in #help-forum
Open in Discord
Original message was deleted.

11 Replies

Russian Blue
Just adding the switch part becuase of character count
 // Handle the event
        switch (event.type) {
            case 'checkout.session.completed':
                const session = event.data.object;
                console.log('Checkout session completedddddddddddddddddddd:', session);

                // Extract Clerk user ID and Stripe customer ID from the metadata
                const clerkUserId = session.metadata?.clerkUserId;
                const stripeCustomerId = session.customer;
                console.log('Clerk user ID:', clerkUserId, 'Stripe Customer ID:', stripeCustomerId);

                if (clerkUserId && stripeCustomerId) {
                    try {
                        // Fetch the current user's metadata
                        const user = await clerkClient.users.getUser(clerkUserId);
                        const currentMetadata = user.publicMetadata || {};

                        // Merge the current metadata with the new metadata
                        const updatedMetadata = {
                            ...currentMetadata,
                            subscription: "active",
                            stripeCustomerId: stripeCustomerId
                        };

                        // Update the user's metadata with the merged metadata
                        await clerkClient.users.updateUserMetadata(clerkUserId, {
                            publicMetadata: updatedMetadata
                        });

                        console.log('User metadata updated successfully');
                    } catch (clerkError) {
                        console.error("Error updating Clerk user metadata:", clerkError);
                    }
                }
                break;
            // ... handle other event types
            default:
                console.log(`Unhandled event type ${event.type}`);
        }
I created exactly the same some days ago and it’s working fine. How do you construct the webhook event? @Russian Blue
Russian Blue
@B33fb0n3 I guess moreso my question is, in the code above, where can I see the output of:

console.log('Checkout session completedddddddddddddddddddd:', session); when Stripe sends me the checkout.session.completed event? I am handling the event itself fine but cannot see logs.

It does not show up in my terminal or console
Russian Blue
I feel like this should be simple but can't figure it out
you normally should be able to see them inside your vercel (or online other online provider) logs. The code itself works fine?
Russian Blue
The code itself it working fine, at least according to Stripe's CLI. Where would I see it just running on localhost though?
normally inside your IDE. If you also use webstorm, I can show you where 🙂
@Russian Blue found?
@Russian Blue ?
Russian Blue
It ended up having to do with api keys - all good!
ooh ok. If any message in this thread made it as the solution, then mark as solution