#jojilede

1 messages ยท Page 1 of 1 (latest)

signal dustBOT
rugged radish
#

Hello

#

First, please redact your secret key above immediately

#

Even though it is your test key it gives access to your account

#

Recommend rolling it too

lapis laurel
#

i see

rugged radish
#

Next, let's back up and first talk about what you are trying to accomplish? Can you explain what you are trying to do?

lapis laurel
#

I want to charge the customer for an initial payment and subscribe them to a later date.

#

because they are renting properties and we want to take initial payment then schedule to start their subscription when their check-in starts.

rugged radish
#

Got it so you are first taking a one-off payment

lapis laurel
#

yes ๐Ÿ˜„

rugged radish
#

Then want a Subscription in the future

lapis laurel
#

yes again ๐Ÿ˜„

rugged radish
#

Then yes as far as I can tell your code looks good and a Sub Schedule is the right thing to use! Have you tried testing this yet?

lapis laurel
#

i can't see it being triggered ๐Ÿ˜ฟ

#

like this:

#

ah ignore the res.redirect()

rugged radish
#

Instead of a picture of code can you provide the code between three backticks like this

#

It is much easier for us to read and work with like that

lapis laurel
#

oh ok

#

const webhookHandler = async (req, res) => {
  if (req.method === 'POST') {
    const buf = await buffer(req);
    const signature = req.headers['stripe-signature'];
    const webhookSecret = process.env.STRIPE_WEBHOOK_SIGNING_SECRET;

    let event;

    try {
      if(!signature || !webhookSecret) return

      event = stripe.webhooks.constructEvent(buf, signature, webhookSecret);
      switch(event.type) {
        case 'checkout.session.expired':
          // Handle the checkout.session.expired event
          res.redirect('/');
          break;
        case 'charge.succeeded':
          // Handle the charge.succeeded event
          const subscriptionSchedule = await stripe.subscriptionSchedules.create({
            customer: 'cus_MBUbxhQStirO4q',
            start_date: 1673541069,
            end_behavior: 'release',
            phases: [
              {
                items: [
                  {
                    price: 'price_1MMwbTDTISXyBnp6jxGjMEQt',
                    quantity: 1,
                  },
                ],
                iterations: 12,
              },
            ],
          });
      }
    } catch(error){
      console.log(error)
      return res.status(400).send(`Webhook Error: ${error.message}`);
    }

    console.log('event', event)
    res.status(200).send()
  } else {
    res.setHeader('Allow', 'POST');
    res.status(405).end('Method Not Allowed');
  }
};

export default cors(webhookHandler)
rugged radish
#

Okay

#

And are you sure your webhook handler is getting hit at all?

#

Next step would be to add some logs in your handler

#

I'd add one right at the beginning of the endpoint and then another within your charge.succeeded case

lapis laurel
#

I'm using stripe vscode extension

#

so if my folder structure is like this:

#

I should forward it to localhost:3000/api/payment/webhooks right?

rugged radish
#

That should depend on what you are using for your Server and how your endpoint is set up in your project

lapis laurel
#

you know what? I'm just dumb I panic

#

it's a scheduled subscription so it should be on the scheduled tab! I'm so sorry!

rugged radish
#

Ah lol

#

No worries!

#

Glad it is working

lapis laurel
#

I'm sorry for wasting your time sir!

rugged radish
#

Not at all

#

We are here to help!

lapis laurel
#

thank you so much!