0

So I am trying to implement a stripe webhook to listen to various events. Basically I have my php application running live, say on "http://example.com". I have installed the stripe CLI and have all the classes. I already have created a webhook on stripe dashboard. But when I test a webhook it gives me this error :

"Test webhook error: Unable to connect. Timed out connecting to remote host"

I am coding on PhpStorm from where I save the files and they get reflected on my website "abc.com". Here is my php file code

 require_once 'abc.com/httpdocs/includes/classes/stripe-sdk/vendor/stripe/stripe-php/configuration.php'; require_once 'abc.com/httpdocs/includes/classes/stripe-sdk/vendor/stripe/stripe-php/init.php'; $endpoint_secret = 'whsec_...'; $payload = file_get_contents('php://input'); $event = null; $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE']; try { $event = \Stripe\Webhook::constructEvent( $payload, $sig_header, $endpoint_secret ); } catch(\Stripe\Exception\SignatureVerificationException $e) { // Invalid signature echo '⚠️ Webhook error while validating signature.'; http_response_code(400); exit(); } // Handle the event switch ($event->type) { case 'account.updated': $paymentIntent = $event->data->object; break; default: // Unexpected event type error_log('Received unknown event type'); } http_response_code(200); 

I don't think I have to run the php server on a local host. And do I need to "stripe listen .."? Hoping to get an answer! Thanks in advance :) Let me know if you need any other information.

2 Answers 2

1

It looks like whatever is trying to connect to your webhook endpoint URL can’t reach said endpoint. Can you clarify the following…

If you are testing local code: Make sure your local PHP server is up and running and make a note of the port it is running on. Then, run stripe listen --forward-to http://localhost:<PHP server’s port>. From there you can trigger sample events from a new CLI window like so: stripe trigger invoice.payment_succeeded. Take a look at the docs for this here.

If you are setting up a live endpoint: I'd first recommend using something like Postman to verify that you can indeed reach the server where your webhook code is deployed. Then, I’d double check the entry you have for that webhook endpoint URL in the dashboard.

If you can pinpoint where you’re stuck, I can provide a more detailed answer.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi @codename_duches! Thanks for your time. Actually I have my domain running on EC2 server. I created a file there with the code mentioned above. When I go to my stripe dashboard to send a test event, it says "Unable to connect". I also tested it with the "listen" etc commands and it gave /404/ error. If you say I can provide the URL. Or if you agree we can have a video call meeting so I can share my screen! My email is "[email protected]" Awaiting your response.
@MUHAMMADZAIN Are you able to reach any endpoints on the server with Postman? This sounds like a deployment issue.
Hi! So it was the problem with my EC2 server permission. I had to add the stripe "ip's" to the Allowed Hosts! Thanks by the way.
1

So it was the problem with my EC2 server permission. I had to add the stripe "ip's" to the Allowed Hosts! That was it :)

2 Comments

hi Zain, how do you get stripe "ip's" and how do you set them in EC2?
@titan - you can find list of ip's here stripe.com/docs/ips - Secondly click the EC2 instance, go to the "Security" tab then click on the associated "Security groups". Then click the "Edit Inbound Rules" and paste that list of IP's there. You can easily find additional things on the internet :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.