#simo_webhooks

1 messages ¡ Page 1 of 1 (latest)

young grottoBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1262389320984297482

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

warm cave
#

hi there!

#

where you do see a 302 error? when forwarding an event with the Stripe CLI?

vale hare
#

yes, when I am connecting the user to stripe connect in my app

warm cave
#

302 status means there's a redirection. however Stripe webhooks don't support redirections. so you need to make sure the URL you set doesn't have a redirect.

vale hare
#

This is the route.ts invoked by the webhook

import { db } from '@/lib/db';
import { stripe } from '@/lib/stripe';
import { headers } from 'next/headers';

export async function POST(req: Request) {
  const body = await req.text();

  const signature = headers().get('Stripe-Signature') as string;

  let event;

  try {
    console.log('it works!');

    event = stripe.webhooks.constructEvent(
      body,
      signature,
      process.env.STRIPE_CONNECT_WEBHOOK_SECRET as string
    );
  } catch (error: unknown) {
    return new Response('webhook error', { status: 400 });
  }
  console.log('here works too: ', event);

  switch (event.type) {
    case 'account.updated': {
      const account = event.data.object;

      console.log('account', account);
      console.log('account id: ', account.id);
      console.log('account transfers: ', account.capabilities?.transfers);

      const data = await db.user.update({
        where: {
          connectedAccountId: account.id,
        },
        data: {
          stripeConnectedLinked:
            account.capabilities?.transfers === 'pending' ||
            account.capabilities?.transfers === 'inactive'
              ? false
              : true,
        },
      });
      break;
    }
    default: {
      console.log('unhandled event');
    }
  }

  return new Response(null, { status: 200 });
}
vale hare
#

because it invokes the route I show you here, and there are no redirects, could be something with nextauth and google?

warm cave
#

it's hard to tell where exactly is the issue. but yes there's a redirection hapepning somewhere. maybe in the way your server is configured?

young grottoBOT
vale hare
# warm cave it's hard to tell where exactly is the issue. but yes there's a redirection hape...

Maybe I found the redirection:

export async function CreateStripeAccountLink() {
  const user = await currentUser();

  if (!user) {
    throw new Error();
  }

  console.log('name: ' + user.name);

  const data = await db.user.findUnique({
    where: {
      id: user.id,
    },
    select: {
      connectedAccountId: true,
    },
  });

  //TODO: modificare il percorso da vercel a domain
  const accountLink = await stripe.accountLinks.create({
    account: data?.connectedAccountId as string,
    refresh_url:
      process.env.NODE_ENV === 'development'
        ? `http://localhost:3000/tutor/billing`
        : `https://smartscholars-git-stripeintegration-simonecapellis-projects.vercel.app/tutor/billing`,
    return_url:
      process.env.NODE_ENV === 'development'
        ? `http://localhost:3000/return/${data?.connectedAccountId}`
        : `https://smartscholars-git-stripeintegration-simonecapellis-projects.vercel.app/return/${data?.connectedAccountId}`,
    type: 'account_onboarding',
  });

  return redirect(accountLink.url);
}```
#

this is a function I call to connect the user to Stripe Connect, in actions.ts

twilit glade
#

Could be if it's called somewhere in your webhook code. You'll need to debug this yourself though. It's hard for me to tell just by looking at snippets of your code without access to your environment. This could be cause by many things (maybe you have another layer in front of your endpoint like a reverse proxy or something that's redirecting requests)

vale hare
twilit glade
#

yep. well you'd generally know if you have something in front of your endpoint

#

Are you using nginx or something?

#

Or any sort of middleware?

vale hare
#

I FIND IT

#

THANK YOU SO MUCH!!

#

it was the middleware

vale hare
#

so thank you so much again

twilit glade
#

Cool glad you got it working