#aswinpy

1 messages · Page 1 of 1 (latest)

pearl tuskBOT
dark sandal
#

Are those values already stored in the database before the webhook comes in? I wouldn't expect any of the IDs to update if that's the case

latent wolf
#

SELECT id,email,stripe_customer_id,stripe_subscription_id,stripe_price_id,stripe_current_period_end FROM `User`;"id email stripe_customer_id stripe_subscription_id stripe_price_id stripe_current_period_end
kp_3d2c4db32e51480ba563d5116eb7209f abc@gmail.com
kp_43dc139d1041437f9fc2f21f7c09753a def@gmail.com" when sign up id and email generated, others are empty

dark sandal
#

So those values are null in the database?

latent wolf
#

yes

#

before and after the payment, nothing changes

pearl tuskBOT
dark sandal
#

Can you add a log line console.log(); in your code to confirm that the object has the attributes you're looking for?

latent wolf
#

something like stripeCustomerId?

dark sandal
#

I would just log the whole object so you can look through it and manually confirm that each attribute is there

latent wolf
#

i also never get a code both in email and phone

#

so i just cancel it

uncut cargo
#

Can you try typing in all 0's for that code?

#

000000

latent wolf
#

0 worked

#
import Stripe from 'stripe';
import { NextApiRequest, NextApiResponse } from 'next';
import { db } from '@/db';
const stripe = new Stripe(process.env.STRIPE_TEST_SECRET_KEY ?? '', {
  apiVersion: '2023-08-16',
});

export const config = {
  api: {
    bodyParser: false,
  },
};

const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
  if (req.method === 'POST') {
    const buf = await buffer(req);
    const sig = req.headers['stripe-signature'] as string | string[];

    let event: Stripe.Event;

    try {
      event = stripe.webhooks.constructEvent(
        buf.toString(),
        sig,
        process.env.STRIPE_WEBHOOK_SECRET ?? ''
      );
    } catch (err) {
      console.log(`Webhook Error: ${err?.message}`);
      return res.status(400).send(`Webhook Error: ${err?.message}`);
    }

    if (event.type === 'invoice.payment_succeeded') {
      const invoice = event.data.object as Stripe.Invoice;

      if (!invoice.customer || !invoice.subscription) {
        return res.status(400).send('Invalid Stripe invoice event');
      }

      const subscription = await stripe.subscriptions.retrieve(invoice.subscription as string);

      await db.user.update({
        where: {
          stripeCustomerId: invoice.customer as string,
        },
        data: {
          stripeSubscriptionId: subscription.id,
          stripePriceId: subscription.items.data[0].price.id,
          stripeCurrentPeriodEnd: new Date(subscription.current_period_end * 1000),
        },
      });
    }
    console.log('success');
    console.log("event", event);
    console.log("event.type", event.type);
    console.log("event.data.object", event.data.object);
    console.log("event.data.object.customer", event.data.object.customer);
    

    res.status(200).json({ received: true });
  } else {
    res.setHeader('Allow', 'POST');
    res.status(405).send('Method Not Allowed');
  }
};

export default webhookHandler;```
#

i console log but i do not see anything in the console

uncut cargo
#

Ah so it sounds like your server is not receiving our webhook events

#

Can you send me your account ID (acct_1234)?

latent wolf
#

acct_1MxuYKGBNL1lkOS1

uncut cargo
#

Are you running a CLI webhook session? I do not see any active webhook endpoints on your account for this event to be sentt o

latent wolf
#

i guess i am not doing that, i just pnpm dev my app, sign in and pay

uncut cargo
#

Is /webhooks also the name of your webhook endpoint in your server's code?

#

You may need to change that CLI command to match the endpoint on your server's code

latent wolf
uncut cargo
#

Is your code being triggered when you get those 500 errors?

#

Also do you see in your code what the URL for your webhook endpoint is?