#CipherDev17
1 messages · Page 1 of 1 (latest)
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
`
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();
});```
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
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'];
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?
Yes
Alright then take a look at https://stripe.com/docs/stripe-cli
Which shows you various ways to install the CLI
You can install on MacOS without homebrew
Then you will want to follow the steps here: https://stripe.com/docs/webhooks/test
Basically you need the --forward-to flag to be able to send to your local endpoint
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
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
Okay I downloaded this one and unzipped it, now I just login through the command line within my project and then follow the steps for the local webhook testing?
Yep
okay brb 🙂
Let me know what happens 🙂
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)
Ah you are going to have to go through https://support.apple.com/guide/mac-help/open-a-mac-app-from-an-unidentified-developer-mh40616/mac
Try opening it that way
Okay I was able to open it that way and got to this in my terminal:
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
Sounds like it isn't recognizing the path
How do I see the current path or set the path properly
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
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
Ah you also might need to do something like what is documented here: https://stackoverflow.com/questions/7117184/brew-installation-for-zsh
But yeah, if you can use homebrew it will be much easier
brew install stripe/stripe-cli/stripe did not work within my terminal, it said that "brew" was not found
Would these commands be ran in my projects folder or in the root directory
I believe from your root
was able to complete the first command but not sure if the second one worked properly
What happens now when you try to install with homebrew?
love when things just work
So now stripe login within the root directory or in my project directory
Shouldn't matter now
so that is within my root directory terminal, not vscode and not within my project directory
and I did restart vscode when I first did this
That's strange. It is accessible everywhere for me
So now I should still be able to go through these steps? (https://stripe.com/docs/webhooks/test)
Yep
stripe listen --forward-to localhost:4242/stripe_webhooks
Is that the route for your local endpoint?
should this still be stripe_webhooks or should it be the path in which I have in my server?
Looks like just /webhook according to the above
okay was just making sure
Yeah so it would be localhost:xxxx/webhook
Depending on the port
Got to step away but @plush spade can help further
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
as in generally how to handle events? you can take a look at https://stripe.com/docs/webhooks/quickstart
this one would probably be a good reference too : https://stripe.com/docs/billing/subscriptions/webhooks (although not specifically targetted towards Subscription Schedules)
Okay awesome so I was on the right track then
I cannot believe how helpful this dev community is
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
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?
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
can you paste your code?
it's difficult for me to envision what you're referring to based off the description alone
this image here
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();
});```
where is this bit I have one single function at the bottom of the page that takes an "event" parameter.?
console.log(event)
}```
Sorry, it was right below and didnt get copied.
if you just want to log event, why not log it at the beginning? i.e. just before this line // Handle the event