#iandk - subscription payment events

1 messages · Page 1 of 1 (latest)

pastel phoenix
#

For subscription payments we typically recommend listening to the invoice.paid event, that will trigger every time a subscription payment is made

ember fern
#

I only need to run a function the first time the subscription is created and has a confirmed payment

pastel phoenix
#

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?

ember fern
#

checkout

pastel phoenix
#

In that case you can listen to checkout.session.completed which will only trigger after that first payment

ember fern
#

thank you, just to confirm, does the customer.subscription.created even indicate that there has been a successful payment?

pastel phoenix
#

Yes, it will not get triggered unless the payment was successful

ember fern
#

but thats what im using currently and it still gets triggered even if the payment fails

#

I just tested it

#

customer.subscription.created

pastel phoenix
#

That is a different event type

ember fern
pastel phoenix
#

Do you have the ID (evt_123) of a checkout.session.completed event from a failed payment?

#

Ah I see I misread your question

ember fern
#

evt_1LTWuVCaKPzbNr2fD6q9YpIS

#

I'm currently listening to customer.subscription.created and this seems to trigger my function even if the payment did not succeed

pastel phoenix
#

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

ember fern
#

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?

pastel phoenix
#

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

ember fern
#

thank you!

#

are you sure?
I cant see the subscription_id here

evt_1LTWebCaKPzbNr2fJU8HIGN4

#

never mind, my bad, I just didnt see it

pastel phoenix
#

Good to hear! Was worried for a moment, that should always be populated if the Checkout Session is for a subscription

ember fern
#

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

pastel phoenix
#

It should be. And this is just a webhook endpoint from running the stripe listen command?

ember fern
#

yup

pastel phoenix
#

And you are getting other events as expected?

#

Can you send me the exact stripe listen command that you are using?

ember fern
#

give me second, I'll give it another try

#

very strange, customer.subscription.created works, but when I change it to checkout.session.completed it no longer runs the function

heavy void
#

Hi there 👋 taking over for @pastel phoenix

Give me a few minutes to get caught up.

ember fern
#

thank you!

heavy void
#

What command are you running to listen to that event?

ember fern
#

stripe listen --forward-to localhost:8000/stripe/webhook

#
  • a webserver php -S localhost:8000
heavy void
#

Are you triggering the event via CLI?

ember fern
#

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?

heavy void
#

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

ember fern
#

yes, I did a few checkouts in the dev mode but my webhook never received an event.

#

I did use the correct environment

heavy void
#

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?

ember fern
#

evt_1LTXc3CaKPzbNr2fhKkq5nQp

heavy void
ember fern
#

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

heavy void
#

Can you post the code from you webhook handler that checks for the event type checkout.session.completed?

ember fern
heavy void
#

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.

ember fern
#

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

heavy void
#

They're not in the same file? Have you confirmed that you even make it to that if statement when the event is fired?

ember fern
#

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

heavy void
#

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?

ember fern
#

okay I think I just figured out, laravel cashier seems to only expose specific events when using WebhookHandled instead of WebhookReceived

heavy void
#

Is it working now?

ember fern
#

still trying to figure out why laravel handels it this way, but I can confirm its not a problem on your sid e

#

sorry

heavy void
#

It's all good! Glad you were able to dig up the solution nonetheless

ember fern
#

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