#billymoonz

1 messages · Page 1 of 1 (latest)

open grailBOT
hearty sluice
#

Hello! Let's keep everything in this thread:

import { headers } from "next/headers"
import { db } from "@/lib/db"
import { stripe } from "@/lib/stripe"
import {NextRequest} from "next/server.js";

export async function POST(req: NextRequest) {
    const body = await req.text();
    const signingSecret = process.env.STRIPE_WEBHOOK;
    const signature = headers().get("stripe-signature")

    console.log(body);
    console.log(signingSecret);
    console.log(signature);

    let event;

    try {
        event = stripe.webhooks.constructEvent(
            body,
            signature,
            signingSecret
        )
    } catch (error) {
        return new Response(null, { status: 400 })
    }

    const database = db();
    const session = event.data.object

    switch(event.type) {
        case 'payment_intent.succeeded':
            const intent = event.data.object;
            console.log(intent);
            const amount = parseFloat((intent.amount / 100).toString());
            const charge = intent.charges.data[intent.charges.data.length - 1];
            return new Response(JSON.stringify(intent), { status: 200 })
        default:
            return new Response(null, { status: 400 })
    }
}
winged river
#

Apologies

hearty sluice
#

Do you have a question related to this?

winged river
#

In relation to the above: I keep getting the error in the image attached. I've copied this code from a project I used previously to take payments so I do not see why this is occurring.

hearty sluice
#

It's likely your web server is redirecting before your code is even run. Are you sure the URL is the exact URL that leads to that code? For example, a common issue is when people have a URL like https://example.com/webhooks/ but the web server wants to redirect to https://example.com/webhooks (no trailing slash) or https://www.example.com/webhooks/ (added www).

winged river
#

I will try a few variations and get back to you, it's a relevant point you bring up

hearty sluice
#

You can try using curl -v to determine the issue.

#

Like curl -v <your webhook URL>

#

That will show you the Location: header where it's trying to redirect to.

winged river
#

I added www. and receive the above, thanks for your kind assistance.

hearty sluice
#

Ah, so that worked?

winged river
#

Yeah, got a 500 internal error so got some debugging to do

#

Thanks again, you can close this thread

hearty sluice
#

This thread will remain open for a little while in case you have any other questions, but will be closed after it's idle for a bit. 🙂