#iandk - subscription payment events
1 messages · Page 1 of 1 (latest)
For subscription payments we typically recommend listening to the invoice.paid event, that will trigger every time a subscription payment is made
I only need to run a function the first time the subscription is created and has a confirmed payment
That will trigger for every payment on a subscription, not just the first, though you can check the billing_reason property to see if it is the first payment or a renewal
Gotcha
Are you using Checkout or your own custom page with Elements?
checkout
In that case you can listen to checkout.session.completed which will only trigger after that first payment
thank you, just to confirm, does the customer.subscription.created even indicate that there has been a successful payment?
Yes, it will not get triggered unless the payment was successful
but thats what im using currently and it still gets triggered even if the payment fails
I just tested it
customer.subscription.created
That is a different event type
yea but I was talking about this?
Do you have the ID (evt_123) of a checkout.session.completed event from a failed payment?
Ah I see I misread your question
evt_1LTWuVCaKPzbNr2fD6q9YpIS
I'm currently listening to customer.subscription.created and this seems to trigger my function even if the payment did not succeed
You are correct that customer.subscription.created will always be triggered. The other event checkout.session.completed will only get triggered when the payment succeeds
I think we have been confused because customer.subscription.created and checkout.session.completed are very visually similar
okay, then I should switch to checkout.session.completed.
One more question, I need the subscription_id which at least seems to be included with the session.completed event, do you know if this is also the case with session.completed?
Yes it will be included in checkout.session.completed. The Checkout Session has a subscription property that will have the ID of the related subscription
thank you!
are you sure?
I cant see the subscription_id here
evt_1LTWebCaKPzbNr2fJU8HIGN4
never mind, my bad, I just didnt see it
Good to hear! Was worried for a moment, that should always be populated if the Checkout Session is for a subscription
one more question, is the checkout.session.completed event also sent to the local dev stripe listener?
I'm currently testing the changes on my local system, but the checkout.session.completed event seems to be missing
It should be. And this is just a webhook endpoint from running the stripe listen command?
yup
And you are getting other events as expected?
Can you send me the exact stripe listen command that you are using?
give me second, I'll give it another try
that's the output of the listen command
very strange, customer.subscription.created works, but when I change it to checkout.session.completed it no longer runs the function
Hi there 👋 taking over for @pastel phoenix
Give me a few minutes to get caught up.
thank you!
What command are you running to listen to that event?
stripe listen --forward-to localhost:8000/stripe/webhook
- a webserver php -S localhost:8000
Are you triggering the event via CLI?
i tried both, regular payment via stripe test and the cli, both didnt work
triggering e.g. customer.subscription.deleted via the cli works just fine, but checkout.session.completed never seems to reach my local webhook
for the production environment I could select which events to listen for, is there such a thing for the test modus?
Yeah, it's identical in both. I was just about to say, it looks like you might be mixing up live and test mode. For example, your local listen command is running on your test-mode data (e.g. listening for test-mode events), whereas the webhook endpoint you configured for <redacted>ode.com/stripe/webhook is only listening for live-mode events
yes, I did a few checkouts in the dev mode but my webhook never received an event.
I did use the correct environment
Do you have an example of a checkout.session.completed event that was triggered in test-mode, but never made it to your local CLI event listener?
evt_1LTXc3CaKPzbNr2fhKkq5nQp
It looks like that successfully sent. Stripe got a response back from your local machine saying that it was received. See bottom of the page here: https://dashboard.stripe.com/test/events/evt_1LTXc3CaKPzbNr2fhKkq5nQp
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
hmm I put a ray('here') in both my listener for customer.subscription.deleted as well as checkout.session.completed.
I get the expected output for the first one, but the second one never seems to be called
Can you post the code from you webhook handler that checks for the event type checkout.session.completed?
Does the code look the same for customer.subscription.deleted? I use case statements in mine and I'm not as familiar with PHP syntax as I would like.
yes, pretty much
I have absolutely no idea why this doesnt work, I mean the other evenlistener works just fine
and I was using the exact same code, just with subscription.created instead of checkout.session.completed
They're not in the same file? Have you confirmed that you even make it to that if statement when the event is fired?
they are in the same file
good idea, I just logged all events that my application received
ray($event->payload['type']);
so only those two events arrive at my application when I complete the checkout flow
this seems to be both the case for test and production
Hmmm, that's odd. Just a bit of a stab in the dark. Is the function terminating after the first if statement? Is there a reason not to use elseif?
okay I think I just figured out, laravel cashier seems to only expose specific events when using WebhookHandled instead of WebhookReceived
Is it working now?
still trying to figure out why laravel handels it this way, but I can confirm its not a problem on your sid e
sorry
It's all good! Glad you were able to dig up the solution nonetheless
okay one last question, I'm currently looking for a way to work around that
When creating a subscription i'm passing a "payID" as metadata, which seems to work just fine.
The event customer.subscription.updated also includes this payID, however I'd need the payID when listening for the checkout.session.completed but it seems like it has no metadata attached