'configuring stripe webhook, Test webhook error: Unable to connect
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.
Solution 1:[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 :)
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | MUHAMMAD ZAIN |
Solution 2 | codename_duchess |