#NormanLove-webhooks

1 messages ยท Page 1 of 1 (latest)

crimson badge
#

Copy and pasting this here:

#

2022-01-11 08:15:05 --> customer.updated [evt_0KGosBBB6wVCS7vFh20p3Tg3]
2022-01-11 08:15:05 --> customer.subscription.created [evt_0KGosBBB6wVCS7vFOG8jqMkV]
2022-01-11 08:15:06 --> invoice.created [evt_0KGosBBB6wVCS7vFQMxeHAQA]
2022-01-11 08:15:06 [ERROR] Failed to POST: Post "http://localhost:5000/income/webhook.php": dial tcp [::1]:5000: connectex: No connection could be made because the target machine actively refused it.

2022-01-11 08:15:06 [ERROR] Failed to POST: Post "http://localhost:5000/income/webhook.php": dial tcp [::1]:5000: connectex: No connection could be made because the target machine actively refused it.

2022-01-11 08:15:07 [ERROR] Failed to POST: Post "http://localhost:5000/income/webhook.php": dial tcp [::1]:5000: connectex: No connection could be made because the target machine actively refused it.

2022-01-11 08:15:08 [ERROR] Failed to POST: Post "http://localhost:5000/income/webhook.php": dial tcp [::1]:5000: connectex: No connection could be made because the target machine actively refused it.

2022-01-11 08:15:08 [ERROR] Failed to POST: Post "http://localhost:5000/income/webhook.php": dial tcp [::1]:5000: connectex: No connection could be made because the target machine actively refused it.

2022-01-11 08:15:08 [ERROR] Failed to POST: Post "http://localhost:5000/income/webhook.php": dial tcp [::1]:5000: connectex: No connection could be made because the target machine actively refused it.

2022-01-11 08:15:21 --> customer.source.created [evt_0KGos8BB6wVCS7vFt1o6gvVY]
2022-01-11 08:15:25 [ERROR] Failed to POST: Post "http://localhost:5000/income/webhook.php": dial tcp [::1]:5000: connectex: No connection could be made because the target machine actively refused it.

earnest marsh
#

I turned off windows firewall and still got the error. I also opened port 5000 on my router and still got the error

#

hi im kinda new to discord ๐Ÿ™‚

crimson badge
#

Hello!

earnest marsh
#

any ideas?

#

setting up localhost webhooks always is a pain

#

there is no help for this on stripe docs.

crimson badge
#

This is something that you consistently see, right? It's not just flakiness

earnest marsh
#

yeah consistant

#

triggers are sent by stripe and rufesed by my computer i guess

#

I also have to run for 15 minutes but ill be back.

crimson badge
#

How are you confirming that the server is up and running at the correct port?

earnest marsh
#

good question, well the server is the stripe cli right? and we have it running in another terminal and its is receiving the triggers I send from another window, and then tries to connect to my localhost. but im not sure how all that works

#

I thought we just run the stripe cli and magic happens ๐Ÿ™‚

#

im running th stripe cli and its says in dashboard there is a stripe cli listening at the localhost:5000/income/webhooks.php

crimson badge
#

No, the Stripe CLI is not what's running the server - you need to actually start up your own server and the CLI is what routes the events to that endpoint

earnest marsh
#

like wamp?

#

WAMP is running and thats how I see my local dev app in browser, but its running to port 80

crimson badge
#

Yeah try point your CLI command to port 80 and see if that works

earnest marsh
#

stripe cli instructions say use 5000

crimson badge
#

That's just an example - not a requirement

earnest marsh
#

holy crap! dude I love you!

#

no more rusals

crimson badge
#

phew! glad you got it working!

earnest marsh
#

but now Im seeing errors in my code, which I chould be able to iron out. here is what I see

#

C:\Users\norma\Downloads>stripe listen --forward-to localhost:80/income/webhooks.php

Ready! You are using Stripe API Version [2020-08-27]. Your webhook signing secret is whsec_4t5bz2Fr85h6psqtB3HaTrhwIym5UnwH (^C to quit)
2022-01-11 09:31:39 --> payment_method.attached [evt_0KGq4GBB6wVCS7vFmMaiIctQ]
2022-01-11 09:31:39 <-- [404] POST http://localhost:80/income/webhooks.php [evt_0KGq4GBB6wVCS7vFmMaiIctQ]
2022-01-11 09:31:39 --> customer.source.created [evt_0KGq4HBB6wVCS7vFZ1T3PvgV]

#

I have wrong file name I believe

#

C:\Users\norma\Downloads>stripe listen --forward-to localhost:80/income/webhook.php

Ready! You are using Stripe API Version [2020-08-27]. Your webhook signing secret is whsec_4t5bz2Fr85h6psqtB3HaTrhwIym5UnwH (^C to quit)
2022-01-11 09:35:24 --> payment_method.attached [evt_0KGq7uBB6wVCS7vFpqediwf7]
2022-01-11 09:35:24 <-- [200] POST http://localhost:80/income/webhook.php [evt_0KGq7uBB6wVCS7vFpqediwf7]
2022-01-11 09:35:24 --> customer.source.created [evt_0KGq7uBB6wVCS7vFkVIdFS9c]
2022-01-11 09:35:24 <-- [200] POST http://localhost:80/income/webhook.php [evt_0KGq7uBB6wVCS7vFkVIdFS9c]
2022-01-11 09:35:24 --> customer.created [evt_0KGq7uBB6wVCS7vFsh3eSRrA]
2022-01-11 09:35:24 <-- [200] POST http://localhost:80/income/webhook.php [evt_0KGq7uBB6wVCS7vFsh3eSRrA]
2022-01-11 09:35:25 --> product.created [evt_0KGq7uBB6wVCS7vFREPZf4g2]
2022-01-11 09:35:25 <-- [200] POST http://localhost:80/income/webhook.php [evt_0KGq7uBB6wVCS7vFREPZf4g2]
2022-01-11 09:35:29 --> plan.created [evt_0KGq7vBB6wVCS7vFkZ5iDbrN]

#

๐Ÿ™

crimson badge
#

happy to help (and sorry i had to delete that last message - it had your email in it and this is a public channel so we usually delete any PII)

earnest marsh
#

cool. nice day

#

are you like a admin who works for stripe support?

crimson badge
#

yeah, a team of us rotate out and staff this channel in shifts ๐Ÿ‘

earnest marsh
#

nice. I wish I knew about this place years ago. this is my second attempt to add subscriptions to my apps registration and last time it was too complicated, i could not get it done, so im trying again

#

do we leave this side channel open or what?

crimson badge
#

We typically close these threads after an hour of no response and if you have a new question you just ask in the main channel again and we create a new thread for you

earnest marsh
#

cool.

#

ok its hitting my webhook and im getting response code 200 and all, but I have the webhook coded to run some local code whe it detecs subscription created, but the code is not doing anything. here is my webhook.php

#

require 'vendor/autoload.php';

// This is your Stripe CLI webhook secret for testing your endpoint locally.
$endpoint_secret = 'whsec_4t5bz2Fr85h6psqtB3HaTrhwIym5UnwH';
// $endpoint_secret = 'whsec_W2lAZaT9yumHS9dtaBjkKtUHmuYzCcmu';
$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 'charge.failed':
$charge = $event->data->object;
case 'charge.succeeded':
$charge = $event->data->object;
case 'subscription_schedule.canceled':
$subscriptionSchedule = $event->data->object;
case 'subscription_schedule.created':
$subscriptionSchedule = $event->data->object;
//do othere stuff
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
$txt = "Subscription created\n";
fwrite($myfile, $txt);
$txt = "2nd line\n";
fwrite($myfile, $txt);
fclose($myfile);
http_response_code(144);

// ... handle other event types
default:
echo 'Received unknown event type ' . $event->type;
}

http_response_code(200);

#

code is directly from stripe docs

#

it should be editing my test.txt file and adding text to it, proving I can track new subsciptions and then run my local code, but text file is not being updated

crimson badge
#

Have you added logging to confirm that you're definitely reaching that point in your code? A log line right before $myfile = fopen("test.txt", "w") or die("Unable to open file!"); should do the trick

earnest marsh
#

I only know how to do console_log(); which logs in browser console I can see via chrome . and this is not in browser so wont show. How else can I setup a log?

#

but the code should trigger the currect switch right? and my code is in the right switch

#

code is from stripe

crimson badge
#

Doing basic debugging is really important even if the code comes from stripe - things can fail in unexpected ways depending on each person's individual setup

arctic nacelle
#

You'll need to establish logging on your server side, which looks like PHP, in order to catch and handle errors during development

earnest marsh
#

if I do run webhook.php directly in the browser, i do get errors ๐Ÿ™‚

#

not sure how to solve those, as the signeture should be the webhooks test one that came with code

arctic nacelle
#

Sure, but to debug that for handling a webhook event you'll need to write to s server log of some sort, there are several options put forward here for various ways to write and capture that depending on your environment: https://stackoverflow.com/questions/6079492/how-to-print-a-debug-log

#

Well thats likely because when you make a GET request from your browser you aren't setting the STRIPE-SIGNATURE header, so its not set when your code tries to access it. You won't be able to inspect things the way you're trying, you need server logging to review.

earnest marsh
#

Ok, I'll set that up. thanks a bunch. i have to run out for 2 hours and will try later. have a great day and thank you