#arielbo-webhooks

1 messages · Page 1 of 1 (latest)

regal granite
#

Of course, what is your webhook question?

split plaza
#

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

gloomy stream
#

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

split plaza
#

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

gloomy stream
#

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

split plaza
#

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?

#

?

gloomy stream
#

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

#

the email step should only come after you've successfully received an event and parsed it

split plaza
#

I dont understad how can I do testing with your webhooks really

gloomy stream
#

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

split plaza
#

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

gloomy stream
#

I'm sorry, but you just say "not work for me" with no info, it's really hard to help you without detailed information

split plaza
#

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?

gloomy stream
#

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

split plaza
#

I understand you and really thank your

#

for your explain

#

how can I install your cli ? in my server to listen events

gloomy stream
#

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?

split plaza
#

yes I have ssh

gloomy stream
#

okay so you don't need the CLI at all in that case

split plaza
gloomy stream
#

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

split plaza
#

I receive this Response
400 ERR

gloomy stream
#

cool, so that means you have a PHP error somewhere, now you can read the PHP error log and debug!

split plaza
#

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 ?

gloomy stream
#

yes the button should work fine!

split plaza
#

thank you, so much for your help I develop now my webhook

gloomy stream
#

CONGRATS

#

this is huge, webhooks are not easy to grasp/debug so I'm really glad you're making real progress 🙂

split plaza
#

thanks to you dear friend

#

the logic was complicated to understand but then you can see that it is well raised

gloomy stream
#

yay!

split plaza
#

its really good

#

I can access to data quickly

#

Request
{
"id": "evt_1JiRbmHHulAmiNnDECGHg8eU",
"object": "event",
"api_version": "2020-08-27",
"created": 1633732326,
"data": {
"object": { ....

gloomy stream
#

yep webhooks are really powerful

split plaza
#

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 ?

gloomy stream
#

not sure what that could mean

split plaza
gloomy stream
#

what do you call "one-time payment"

split plaza
#

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.

gloomy stream
#

do you use Invoicing for one-time payments? Because if not you won't get invoice.paid for those

split plaza
#

you know

#

maybe another event

#

you are the boss

gloomy stream
#

payment_intent.succeeded is what you want in that case

split plaza
#

right !

#

I receive múltiple events y the same endpoint right?