Reputation: 4516
I am conducting a simple localhost test using stripe CLI. When I execute the command:
stripe trigger invoice.payment_succeeded
My local webhook picks everything up perfectly and the test data they generate is sent to me. The only issue I have is when the code is deserialized:
if (stripeEvent.Type == "invoice.payment_succeeded")
{
Invoice successInvoice = (Invoice)stripeEvent.Data.Object;
...
The object stripeEvent (of type Invoice) does not have a subscription value for it, or a subscription id, for me to map back to what subscription the customer is under.
Sure, I can see the invoice amount, but I'd like to know more details now on this item. I was reading something about how Stripe will send over a successful invoice charge but may not initially include subscription details on it, but that concerns me since I want to know the associated subscription.
Any ideas? Am I looking at the wrong webhook event?
Upvotes: 0
Views: 673
Reputation: 4516
Luckily I just figured it out - if you are testing using the CLI, you need to first create a subscription i.e.
stripe trigger customer.subscription.created
and then once you do this, if you then execute your payment
stripe trigger invoice.payment_succeeded
Doing it in this order will then ensure the ID comes in (but not the whole subscription object). but that's ok - you can fetch the whole subscription using the ID like so:
if (successInvoice.SubscriptionId != null)
{
var service = new SubscriptionService();
var subscription = service.Get(successInvoice.SubscriptionId);
if (subscription != null)
{
var plan = subscription.Plan; //do stuff with plan
}
}
Upvotes: 1
Reputation: 5857
The Invoice object definitely has a reference to the Subscription: https://stripe.com/docs/api/invoices/object?lang=dotnet#invoice_object-subscription
It's true that the webhook event that describes the Invoice won't contain many details on the Subscription, but you can then either retrieve the Subscription by using the aforementioned ID or retrieve the Invoice from the API whilst expanding the Subscription.
Upvotes: 1