#CipherDev17

1 messages · Page 1 of 1 (latest)

chilly sequoiaBOT
oak ginkgo
#

Hello

#

Let's use this thread

#

Also instead of posting pictures of code, go ahead and put it inbetween three backticks like this to format

vale patrol
#

Okay awesome, will do that

#

wait what do you mean by backticks? lol

#

{

#

<

oak ginkgo
#

`

vale patrol
#

gotcha

#
  const sig = request.headers['stripe-signature'];

  let event;
  let subscriptionSchedule;

  try {
    event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
  } catch (err) {
    response.status(400).send(`Webhook Error: ${err.message}`);
    return;
  }

  // Handle the event
  switch (event.type) {
    case 'subscription_schedule.aborted':
      subscriptionSchedule = event.data.object;
      // Then define and call a function to handle the event subscription_schedule.aborted
      break;
    case 'subscription_schedule.canceled':
      subscriptionSchedule = event.data.object;
      // Then define and call a function to handle the event subscription_schedule.canceled
      break;
    case 'subscription_schedule.completed':
      subscriptionSchedule = event.data.object;
      // Then define and call a function to handle the event subscription_schedule.completed
      break;
    case 'subscription_schedule.created':
      subscriptionSchedule = event.data.object;
      // Then define and call a function to handle the event subscription_schedule.created
      break;
    case 'subscription_schedule.expiring':
      subscriptionSchedule = event.data.object;
      // Then define and call a function to handle the event subscription_schedule.expiring
      break;
    // ... handle other event types
    default:
      console.log(`Unhandled event type ${event.type}`);
  }

  // Return a 200 response to acknowledge receipt of the event
  response.send();
});```
oak ginkgo
#

Thanks

#

How are you testing this endpoint?

#

Are you using the CLI?

#

Or you have this running over HTTPS?

#

Also in your code above I don't see you logging anything for any of those event types

vale patrol
#

Its currently in my test environment, and I was getting confused when reading into the CLI as my terminal (MacOS) is Zsh and not HomeBrew.

Regarding the console.log(), i removed it when I went to copy paste it in here. it was originally above the const sig = request.headers['stripe-signature'];

oak ginkgo
#

Okay so to test webhooks you either need to have your endpoint hosted over HTTPS (or use something like ngrok to tunnel) or you need to forward to your local endpoint using the CLI

#

Are you just trying to test locally right now?

vale patrol
#

Yes

oak ginkgo
#

Which shows you various ways to install the CLI

#

You can install on MacOS without homebrew

#

Basically you need the --forward-to flag to be able to send to your local endpoint

vale patrol
#

I went to the github page to install without homebrew and found these which are for Mac, how would i know which one to download?

#

Sorry I am new to this

#

But once I have it downloaded properly, I am sure I will be able to figure out the rest, its just the initial setup that gets me

oak ginkgo
#

Click "About This Mac" from top left

#

If you have an M1 or M2 then you should use arm

#

Else use the other option

#

If it is Intel use the X86

vale patrol
oak ginkgo
#

Yep

vale patrol
#

okay brb 🙂

oak ginkgo
#

Let me know what happens 🙂

vale patrol
#

Okay so here is the zip file and the file that came from unzipping it (first image)

I tried the command stripe login from my terminal within my project folder on vscode and get zsh: command not found: stripe

Not sure if this means anything but I tried to double click on the stripe package that I unzipped and got this (second image)

oak ginkgo
#

Try opening it that way

vale patrol
#

Okay I was able to open it that way and got to this in my terminal:

oak ginkgo
#

that looks good

#

can you stripe login

vale patrol
#

but when I go to my project and type stripe login I still get that error saying stripe is not found. I have restarted my vscode as well

oak ginkgo
#

Sounds like it isn't recognizing the path

vale patrol
#

How do I see the current path or set the path properly

oak ginkgo
#

Have you tried running it from where you extracted?

#

I'm also looking for how you check on the path

#

I don't remember

#

You may also need to do ./ stripe login

#

I thought homebrew works on zsh ?

#

Was there a reason you didn't want to use homebrew? That is the much easier route here

vale patrol
#

No reason at all, I was not aware that it works on Zsh too. I would much prefer the easier route! lol let me try

oak ginkgo
#

But yeah, if you can use homebrew it will be much easier

vale patrol
#

brew install stripe/stripe-cli/stripe did not work within my terminal, it said that "brew" was not found

vale patrol
oak ginkgo
#

I believe from your root

vale patrol
#

was able to complete the first command but not sure if the second one worked properly

oak ginkgo
#

What happens now when you try to install with homebrew?

vale patrol
#

love when things just work

#

So now stripe login within the root directory or in my project directory

oak ginkgo
#

Shouldn't matter now

vale patrol
#

this within my project directory on vscode

oak ginkgo
#

Huh

#

Can you try to close your vscode and re-open

#

Or open a new shell completely

vale patrol
oak ginkgo
#

Really think it should be accessible anywhere at this point

#

Yep that looks good

vale patrol
#

so that is within my root directory terminal, not vscode and not within my project directory

vale patrol
oak ginkgo
#

That's strange. It is accessible everywhere for me

vale patrol
oak ginkgo
#

Yep

vale patrol
#

stripe listen --forward-to localhost:4242/stripe_webhooks

oak ginkgo
#

Is that the route for your local endpoint?

vale patrol
#

should this still be stripe_webhooks or should it be the path in which I have in my server?

oak ginkgo
#

Looks like just /webhook according to the above

vale patrol
#

okay was just making sure

oak ginkgo
#

Yeah so it would be localhost:xxxx/webhook

#

Depending on the port

#

Got to step away but @plush spade can help further

vale patrol
#

Okay thank you so much for your help!!

#

Is there docs on handling events or is it literally just:

    case 'subscription_schedule.completed':
      subscriptionSchedule =  event.data.object;
      handleCompletedSubscription(event.data.object)
}```
#

and then I initialize the handleCompletedSubscription function and do whatever logic within that function

plush spade
vale patrol
#

Okay awesome so I was on the right track then

#

I cannot believe how helpful this dev community is

vale patrol
#

Do they have to be different functions?

#

this is for handling the event types

#

in the docs, there is a diff function for every type of event but I figured that i could take the event type in a single function and then go from there based on the value of event

plush spade
#

i'm not entirely certain what you mean by i could take the event type in a single function and then go from there based on the value of event? can you share a simple example?

vale patrol
#

here they call two different functions within the two cases

#

they are commented out, but the functions are different

#

In the image i sent, I have one single function at the bottom of the page that takes an "event" parameter.

Within each of the cases above that function, I entered the same function to handle the event in every case (not different functions like in the docs).

#

My Q is, do they have to be different functions that handle the different cases?

#

bc when I console.log(event) in my function at the bottom of the first screenshot I shared, I get nothing

plush spade
#

can you paste your code?

#

it's difficult for me to envision what you're referring to based off the description alone

vale patrol
#

or pasted here:

#
  const sig = request.headers['stripe-signature'];

  let event;

  try {
    event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
  } catch (err) {
    response.status(400).send(`Webhook Error: ${err.message}`);
    return;
  }

  // Handle the event
  switch (event.type) {
    case 'subscription_schedule.aborted':
      const subscriptionAborted = event.data.object;
      handleSubscriptionEvent(subscriptionAborted)
      // Then define and call a function to handle the event subscription_schedule.aborted
      break;
    case 'subscription_schedule.canceled':
      const subscriptionCancelled = event.data.object;
      handleSubscriptionEvent(subscriptionCancelled)
      // Then define and call a function to handle the event subscription_schedule.canceled
      break;
    case 'subscription_schedule.completed':
      const subscriptionCompleted = event.data.object;
      handleSubscriptionEvent(subscriptionCompleted)
      // Then define and call a function to handle the event subscription_schedule.completed
      break;
    case 'subscription_schedule.created':
      const subscriptionCreated = event.data.object;
      handleSubscriptionEvent(subscriptionCreated)
      // Then define and call a function to handle the event subscription_schedule.created
      break;
    case 'subscription_schedule.expiring':
      const subscriptionIsExpiring = event.data.object;
      handleSubscriptionEvent(subscriptionIsExpiring)
      // Then define and call a function to handle the event subscription_schedule.expiring
      break;
    // ... handle other event types
  }
  
  // Return a 200 response to acknowledge receipt of the event
  response.send();
});```
plush spade
#

where is this bit I have one single function at the bottom of the page that takes an "event" parameter.?

vale patrol
#
  console.log(event)
}```

Sorry, it was right below and didnt get copied.
plush spade
#

if you just want to log event, why not log it at the beginning? i.e. just before this line // Handle the event