#arielbo-webhooks
1 messages · Page 1 of 1 (latest)
hello
I configure one endpoint
require_once('stripe-php/init.php');
\Stripe\Stripe::setApiKey('sk_test_xxxxx');
//include(get_template_directory().'/cpanel/stripe_secret_credential.php');
// This is your Stripe CLI webhook secret for testing your endpoint locally.
$endpoint_secret = 'xxxxxx';
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
// Invalid signature
http_response_code(400);
exit();
}
// Handle the event
switch ($event->type) {
case 'invoice.payment_succeeded':
$invoice = $event->data->object;
// ... handle other event types
default:
echo 'Received unknown event type ' . $event->type;
}
http_response_code(200);
I use this code in my endpoint on y server, I need to put in this code one order my database update the client service date
in wich point I do this?
I working in testing mode now
you do it inside the switch at the bottom
1/ get the event
2/ parse it
3/ based on the event type/content, action what you need to
let me test
// Handle the event
switch ($event->type) {
case 'invoice.payment_succeeded':
$invoice = $event->data->object;
$to = 'test@gmail.com';
$subj = 'Webhook stripe here ! '.date('Y-m-d ss').'';
$body = 'This is the body of the email';
wp_mail( $to, $subj, $body );
//... handle other event types
default:
echo 'Received unknown event type ' . $event->type;
}
like this ? I try to send an email after invoice is payment to test your advice
For example yes. I wouldn't do this honestly, I'd have my own classes that can receive events and handle them async, so I wouldn't hardcode a lot of lines of code to send an email straight in the body of that function
I assume you're just testing/debugging though so it seems fine
yes I try to test first, but not working for me
how long does the program usually take until the webhook works and sends me a notification ?
Is it immediate or does it take several minutes for the webbook to run?
?
it depends
you can track all of this as the developer
add logs to your code, look at the server's logs/your code locally
look at our CLI program https://stripe.com/cli
the email step should only come after you've successfully received an event and parsed it
I dont understad how can I do testing with your webhooks really
What's blocking you? It's just code on your server, so you have code that listens to invoice.payment_succeeded for example, and then you make that event happen, for example by creating a Subscription and the event would be sent to your server
If you are testing locally, you would use our CLI tool (linked above) that can receive events on your computer adn forwards them to your server
you can also use tools like ngrok https://ngrok.com/ to get a unique temporary URL for your local code and then have Stripe send events to it
you can also use curl or any equivalent on the command line to send a raw POST request to your own code
I want to test with my server, I try to but one success sale in test mode, all its good but the webhook not work for me
I'm sorry, but you just say "not work for me" with no info, it's really hard to help you without detailed information
I would not like to use cli but directly, make the successful purchase in and after the event does its job, I imagined it was that simple but as you can see I can't even send myself an email once the event happens. Excuse me I am completely lost in this of stripe webhooks with php
please could you guide me?
Stripe has no way to talk to your own computer and send it webhooks, it's impossible. You need a tool that receives events from Stripe while you develop locally which is what the CLI (or ngrok) offer.
Otherwise you need to push the code to your server while you debug so that you can configure a real URL to your webhook endpoint
I understand you and really thank your
for your explain
how can I install your cli ? in my server to listen events
let's take a small step back. What are you calling "my server" exactly?
Do you have a host provider and you push your code online on your server? Or do you test locally on your computer with localhost?
yes I have ssh
I have a hosting cloud server, and in this server I configure the enpoint like this: https://www.mydomain.com/stripe_webhook.php
I configure here https://dashboard.stripe.com/test/webhooks this endpoint with type: direct
okay so you don't need the CLI at all in that case
inside this https://www.mydomain.com/stripe_webhook.php I put the code this code 👆
you add the right webhook endpoint to uyour account in Test mode and then you should see the requests come in
as the developer you can read the logs for your server, see all incoming and outgoing connections and debug this
I receive this Response
400 ERR
cool, so that means you have a PHP error somewhere, now you can read the PHP error log and debug!
let me see
now work thank koopajah some issue with my server,
can I use this resend to test my endpoint?
or I need to use for some reason stripe CLI ?
yes the button should work fine!
thank you, so much for your help I develop now my webhook
CONGRATS
this is huge, webhooks are not easy to grasp/debug so I'm really glad you're making real progress 🙂
thanks to you dear friend
the logic was complicated to understand but then you can see that it is well raised
yay!
its really good
I can access to data quickly
Request
{
"id": "evt_1JiRbmHHulAmiNnDECGHg8eU",
"object": "event",
"api_version": "2020-08-27",
"created": 1633732326,
"data": {
"object": { ....
yep webhooks are really powerful
I need to differente one time payment to subscription
maybe you know if this "billing_reason": "subscription_create", when is one time payment what say ?
not sure what that could mean
yeah its magnific ! incredible to work with this . I will save a lot of time programming and integration analysis thanks to these webhooks
what do you call "one-time payment"
I have two types of services, some that are subscriptions (month to month) and others that are one-time payment services, and my database will enable each service in a different way, so I need some data in the response of the webhook that can differentiate this .
I think that when I receive the webhook then I will make my system work in these two ways with and without subscription.
do you use Invoicing for one-time payments? Because if not you won't get invoice.paid for those
payment_intent.succeeded is what you want in that case