Automating Stripe test Webhook Setup with ngrok
Wait,...before you continue reading, I feel the idea is cool and like every modern problem, there could be possible solutions around them but yeah, I didn't budget that time. BECAUSE I THOUGHT IT WOULD BE A QUICK ONE. haha.
Let's go....
Problem statement
As someone who has been working with Stripe API heavily, there's this repetitive task I intended to automate which is setting up test webhook URLs generated by ngrok every time I start my codebase. Github repo
The 'tedious' flow:
- run ngrok http 8000
- copy the generated https URL
- login to Stripe dashboard
- create/edit a Stripe webhook
- add Stripe events to listen to
Automated version:
- signup and login to webhookautosyncer dot com then press one ultimate button (deploy stripe webhook)
In a more detailed way:
Auto deploy test webhooks to Stripe dashboard for Stripe payments.
🐥 No need to manually add test webhook endpoints (e.g. the ones generated with ngrok) in Stripe dashboard every time you restart your application.
🐥 Just start your app locally, a secured endpoint is automatically generated for you and it is automatically set in your Stripe dashboard.
🐥 It should just work in one click.
What should work, I mean, theoritically:
Once you have ngrok running in the background:
The script makes a GET request to http://localhost:4040/api/tunnels/command_line
Extracts the https URL:
$ngrokUrl = $response['public_url'];
Using Stripe webhook API, the webhook will be automatically deployed to users' Stripe dashboard.
$stripe = new StripeClient("sk_test_YOUR_STRIPE_TEST_KEY");
$result = $stripe->webhookEndpoints->create([
'enabled_events' => ['charge.succeeded', 'charge.failed'],
'url' => $ngrokUrl . "/api/v1/webhook/settle-customer",
]);
Well,... them challenges:
You can't remotely access people's CLI esp from the browser: If this was possible, that's a big security flaw so the principalities of software architecture and custodians of the internet made this impossible --which is a good thing tbh.
I could get the ngrok https URL via a localhost but I am unable to do that because, again, I can't make a GET request to localhost from a remote server.
Lastly, I could write a Node.js CLI tool that, when users install it (globally), this script sends me (my remote server) the ngrok https URL, which I will use to automatically setup a Stripe webhook. Also, I will need to authorize users with an API key kinda. That's like a couple of overheads that I did not budget time for.
Solutions (but, I will leave you to try 🤭 ):
-
Local App Setup:
- Build it as an application one can setup on their own PC.
Distribute the solution as a package.
Build it as a Laravel package
Conclusion:
I had fun digging the ngrok API docs and doing research, trying possible approaches but yeah,...here we are. It was a good one.
Can I have a word?
If you feel you can crack this thing, maybe using another approach, that's super cool. Do let us know!
I have a recent announcement you want to check out.
Check me out on GitHub. I got some open source projects going on there: https://github.com/jovialcore.
Cheers to hacking stuff. ⚒️ 🥂
Top comments (0)