configuring stripe webhook, Test webhook error: Unable to connect - php

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.

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.

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 :)

Related

Soap client stopped working

I have a website which frequently makes SOAP calls to a particular API. The site was working fine for a few months of time, however the SOAP functionality suddenly stopped working without any known reason, the error was "Cannot connect to host".
The WSDL service has not moved or shut down.
After this I updated plesk from version 12 to version 17. SOAP client is still not working but now its a different error:
SOAP-ERROR: Parsing WSDL: Couldn't load from (URL) : failed to load external entity (URL)
This is my SOAP call:
$opts = array('http'=>array('user_agent' => 'PHPSoapClient'));
$context = stream_context_create($opts);
libxml_disable_entity_loader(false);
$client = new SoapClient($url,array('stream_context' => $context,'cache_wsdl' => WSDL_CACHE_NONE));
Any possible solutions?
EDIT:
New information came in, so the API server had DNS problems, how can I restore my connection to the API?
Your php server is clearly not reaching out the soap server. This might help:
Try checking directly from php server if the service are reachable:
wget {url} > page.txt
nano (or equivalent) page.txt
Try using other program to consume the server, I suggest Soap Ui
On the php server try ping the service url and see if the IP is resolved.
Check if any other soap service is reachable, you can use this service
If this not help you coming up with a solution, post here the results to help others solving it.

socket_write(): unable to write to socket [10053]

I'm using whatsapp api with laravel 5.2
https://github.com/mgp25/Chat-API
And i got this error when i trying to send new message
socket_write(): unable to write to socket [10053]: An established connection was aborted by the software in your host machine.
Send Controller
$massage = "Thanks for subscribe";
Whatsapi::send($massage, function ($send) {
$user = User::Find(1);
$send->to($user->phone);
}
While following this tutorial
I was getting the same error in phperror.log which I have configured in my php.ini. Initially I thought it was due to some firewall issue, but it wasn't.
The problem was, I was running the client first and then the server. Client was probably not able to make a connection with the server when it ran for the first time.
So I resolved it by running the server first and then the client, which can now make a successful web socket connection.
EDIT : This error also comes up when we simply reload the client page without properly terminating the previous connection.

Ratchet: How to connect via PHP to it?

I am running a Ratchet Server
$app = new Ratchet\App( 'localhost', 8080 );
$app->route( '/api', new API );
$app->run();
It is very easy to connect via JavaScript (and send some messages to the server):
_connection = new WebSocket( "ws://localhost:8080" );
Now I want the same from PHP (connect and send messages), but I dont know how and I dont find any information in the documentation of Ratchet.
Is it somehow possible? I really need to send messages from PHP to the clients (JavaScript), when there is some event on the server.
Edit: I really just need to "fire&forget" some simple message. Nothing fancy.
It works a bit wrong.
After connecting to the server, you can send a message from the client to the server and from the server to the client.
In the Connection Interface is the "send" method.
And call onSubscribe or onPublish method you have $topic variable.
In the $topic is the "broadcasting" method.
You need call:
$conn->send($message); or $topic->brodcasting($message);
If you need send client message after action on site, use example on ratchet (we need install ZMQ)
It works like this:
You create server. This server recieve message from users and recieve message from ZMQ.
Your site publish on ZMQ message
Your server receive message from ZMQ and broadcasting on users
Regards Maxim

Wechat : Sandbox API config Failed

Hi please support to troubleshoot the below error in Wechat Sandbox API Config:
I am integrating Wechat API in PHP application. I have created a test account in Wechat Sandbox API. But when configuring the API with URL and token its shows "Unable to configure".
When checking the ajax response it throws : {"errcode":-106,"errmsg":"token check fail"}
Thank you.
I got the "Unable to configure" message when testing the Getting Started wiki sample too. Trying to reach wx_sample.php returned a 500 Internal Server Error.
It was fixed after changing the owners for my directory and file, after that the token could be set in the sandbox - 'API Config'.

Google Analytics API oauth exception "invalid_grant" with Service Account. Same code on two servers. Only one works

I'm querying the Analytics API via a Service Account.
I have written the code on the dev server and it works without issues.
When running the same code on the production server, it throws this:
Google_AuthException: Error refreshing the OAuth2 token, message: '{
"error" : "invalid_grant" }'
I've tried creating another Service account, and the behavior is the same.
The oAuth IETF draft (https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-31) says this about the error:
invalid_grant
The provided authorization grant (e.g. authorization
code, resource owner credentials) or refresh token is
invalid, expired, revoked, does not match the redirection
URI used in the authorization request, or was issued to
another client.
Here is the code I've written:
$GA_CLIENT_ID = 'XX.apps.googleusercontent.com';
$GA_APP_EMAIL = 'XX#developer.gserviceaccount.com';
$GA_APP_NAME = 'XX';
$GA_KEY_FILE = 'XX';
// create client object and set app name
$client = new Google_Client();
$client->setApplicationName($GA_APP_NAME); // name of your app
// set assertion credentials
$client->setAssertionCredentials(
new Google_AssertionCredentials(
$GA_APP_EMAIL, // email you added to GA
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents($GA_KEY_FILE) // keyfile you downloaded
));
// other settings
$client->setClientId($GA_CLIENT_ID); // from API console
$client->setAccessType('offline_access'); // this may be unnecessary?
// create service and get data
$service = new Google_AnalyticsService($client);
$result = $service->data_ga->get($ids, $startDate, $endDate, $metrics, $optParams);
return $result;
I've also tried a solution suggested here (https://groups.google.com/forum/?fromgroups#!topic/gs-discussion/3y_2XVE2q7U%5B1-25%5D) using authenticatedRequest() instead of Google_AnalyticsService:
$req = new Google_HttpRequest($apiUrl);
$resp = $client::getIo()->authenticatedRequest($req);
$result = json_decode($resp->getResponseBody(), true);
This alternative also works on the dev server, but not on the production one.
I am totally clueless on this one. Has anyone seen this/fixed it?
Thanks!
Apparently the problem was the system time being off. Worked by sync-ing via NTP with:
sudo ntpdate npt.ubuntu.com
sudo ntpdate pool.ntp.org
Edit
As #RafaSashi suggested below, the pool.ntp.org server is more reliable. Use that instead of ntp.ubuntu.com (which was the first working one I tried, thus the initial choice).
The invalid grant can also be caused if you use the wrong "ServiceAccountId". It should be the email associated with the client id in the service account client id in the google apis access page. You'd also have to add this user to the google analytics account that you're planning to access.
This tripped me up because I assumed the email address they were referring to was the email address of my google account, since I used the same google account to get api access as I do for google analytics. I know Vir's already figured his out, just thought I'd add this in case someone else comes across the same question and, like me, their computer seems to be in sync with NTP.
In addition to Valer's answer:
First, you’ll need to install NTP if it isn’t already installed. For Debian or Ubuntu, that would be this command:
sudo apt-get install ntp
For Redhat or CentOS, you’ll need to use this one:
yum install ntp
If the synchronization via npt.ubuntu.com doesn't work try :
sudo ntpdate pool.ntp.org
Resources
http://www.howtogeek.com/tips/how-to-sync-your-linux-server-time-with-network-time-servers-ntp/
https://www.digitalocean.com/community/tutorials/how-to-set-up-time-synchronization-on-ubuntu-12-04
There are two major reasons for invalid_grant error which you have to take care prior to the POST request for Refresh Token and Access Token.
Request header must contain "content-type: application/x-www-form-urlencoded"
Your request payload should be url encoded Form Data, don't send as json object.
RFC 6749 OAuth 2.0 defined invalid_grant as:
The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.
I found another good article, here you will find many other reasons for this error.
https://blog.timekit.io/google-oauth-invalid-grant-nightmare-and-how-to-fix-it-9f4efaf1da35
Google Playground is best tool which help you how to send request.
https://developers.google.com/oauthplayground

Categories