Authorize.Net Recurring Billing Events - webhooks

I have successfully deployed Authorize.net API(currently sandbox mode) for subscription purposes. I have also configured its webhooks that are also working. But I have a confusion that still exists even after a week on working with the said API.
The question is, when a subscription starts in case of recurring billing(in my scenario subscription is monthly) the event that is called is
net.authorize.customer.subscription.created
When a month passes on a subscription and next bill payment is made by the API, what event will get called? How can I capture Or to what event should I be listening to? . Is it going to be
net.authorize.customer.subscription.updated
Currently I have clicked yes on all the all the events that are there for the webhooks

The event will be a payment related event, not a subscription related event. Subscription related events only happen when you do something to a subscription (i.e. create, modify, or delete) not with it (make payment).
So you would look out for any of the following:
net.authorize.payment.capture.created
net.authorize.payment.fraud.approved
net.authorize.payment.fraud.declined
net.authorize.payment.fraud.held

Related

When does Stripe's invoice.paid webhook get delivered in relation to when the subscription starts?

We're currently using Subscription Schedules to help us downgrade a customer at the end of the current billing cycle. Once the customer has been downgraded, we want to call the release function to convert the Subscription Schedule back to a normal Subscription based on the downgraded plan they selected. We don't want to wait for this to happen automatically at the end of the schedule because of the way our system is currently implemented.
Our current thinking is that we can rely on Stripe's webhooks to fire a invoice.paid event to know when to call the release function. However, if the webhook was fired too early, the customer would end up being released on to the wrong plan.
So, our question is, is the invoice.paid event guaranteed to be delivered after the new subscription has started?
Thank you for your help
No, the order of webhook events is not guaranteed by Stripe. Instead the best thing to do would be to retrieve the subscription (or subscription schedule) directly after receiving the invoice.paid event to check its status before releasing it.

Testing Stripe webhooks that require data on localhost

The guide here says to monitor the invoice.paid event via webhook in order to provision a customers account when the payment is successful. How can I test fire the invoice.paid event with data? I need to be able to access the customer id and see what plan they purchased to provision it correctly.
I am able to test fire these events via the Stripe CLI, but I don't know how to fire them with mock data.
With the Stripe CLI you can trigger a invoice.payment_suceeded webhook event (which is almost exactly the same as invoice.paid):
stripe trigger invoice.payment_succeeded
That event will contain mock data. If you want to provide your own data, you'd have to mimic the event yourself. In that case you could do that by creating a new subscription in test mode, which will create an invoice and trigger the invoice.paid event.

At which stripe event should I update my db to grant subscriber access

I have a SaaS MERN app which is using Stripe for subscriptions. I'm using the new checkout feature of Stripe to handle all the payments. I have the stripe CLI installed to see the events that occur when a subscription is made. I have a few questions regarding the events that are emitted and how to handle them in my stripe webhook.
Which event below indicate that the payment was successful so that I can update my database.
Why is it that the payment_intent.succeeded occurs before payment_intent.created.
Why is a customer.subscription.updated event happening (I'm assuming it should just be the customer.subscription.created event that should occur on first purchase)
I use the customer.subscription.updated event in my webhook to allow customers to turn autoRenewal on and off. Because the customer.subscription.updated event is also shot off at initial payment when subscribing, how can I differentiate what to do as the same event is fired off in both cases.
This is not as important, but if anyone knows what that POST error is at the bottom, I would appreciate some insight. [EDIT] I fixed this by moving my res.status(200).json({received: true}) further up my code. Initially it was placed at the very end of my webhook, so I think it was timing out.
Thank you :)
If you specifically want to provision the subscription on successful payment, then you should listen for payment_intent.succeeded
Stripe doesn't guarantee the order of webhook events, it's possible to receive them out of order: https://stripe.com/docs/webhooks/best-practices#event-ordering
The subscription's status would be updated to show that the latest invoice was successfully paid, which triggers and update event
You probably want to use invoice.upcoming instead: https://stripe.com/docs/api/events/types?lang=java#event_types-invoice.upcoming
You'd have to debug this on your end to see where it came from. Looks like perhaps your local server wasn't running and so the Stripe CLI couldn't forward on the webhook event.

Which webhooks to look after in Stripe Checkout?

I'm implementing subscription with Stripe Checkout, but I have some questions that I couldn't get definitive answers for even from their support.
I have following scenario:
User clicks on a Subscribe button
User gets redirected to Stripe Checkout page (along with session token)
User successfully pays (and from now on is a Customer)
and here I get 14 webhook calls (each one is different event type):
checkout.session.completed
payment_method.attached
invoice.created
customer.updated
customer.subscription.created
customer.created (even though I used the same email - test mode, but still...)
invoice.finalized
invoice.updated
invoice.payment_succeeded
customer.subscription.updated
charge.succeeded
payment_intent.succeeded
payment_intent.created
invoice.updated
edit: I get all of them at once because I use Stripe CLI's listen feature and it probably shows all calls which normally wouldn't happen because they have to be defined in dashboard event by event. For instance if I create invoice in the dashboard and pay it then if no webhook is defined explicitly then my server won't know about anything
Until here it all works good. My concern is that:
if a Customer will be charged next month which webhook should I listen to? I need to differentiate between first-time Customer (because I create new account for that user) and existing Customer that I just need to note in database that this user is still active (or listen for event of subscription cancellation? subscription_schedule.canceled maybe?). One idea is to just listen to successful payment and upon that check if customer exists in database - if he does just update, if not then create account.
when I do another subscription payment using the very same email I get exactly the same webhooks (including customer.created which I think shouldn't be there) or am I missing something?
is there any possibility to double-charge a Customer by accident like I read in other Stripe implementations (see: idempotency)?
what are other things to take into account while implementing Stripe Checkout? I feel like their instructions are not reassuring that I've done everything that is considered good practice.
Flow that I want to achieve:
User successful payment (as above in points from 1 - 3). Already done!
Register new customer in database (Firebase in this case) and log him in automatically so after payment he has instant access.
As long as customer's card is charged he has access. While he cancels or card has no insufficient funds I need to have my server notified about this to downgrade access.

Is the customer.subscription.updated event raised when a subscription is renewed?

I'm confused about why Stripe's documentation suggests the customer.subscription.updated event is not fired when I believe it should:
A Stripe subscription object has the properties current_period_start and current_period_end which would be updated whenever the customer successfully pays a subscription's invoices ( https://stripe.com/docs/api#subscriptions )
The documentation for the customer.subscription.updated event states that it...
Occurs whenever a subscription changes. Examples would include switching from one plan to another, or switching status from trial to active.
...which would imply that the event would be raised if the current_period_start and current_period_end values change, but it doesn't affirm if it does or it does not in this case.
However this third-party webpage states that it is not raised when a successful renewal is performed ( https://www.masteringmodernpayments.com/stripe-webhook-event-cheatsheet#8 ).
But raising the event just makes sense...
And certainly if applications only needed to monitor a single event type (i.e. customer.subscription.updated) then it would greatly simplify program code without needing to also monitor invoice.payment_succeeded, invoice.created and charge.succeeded.
However the documentation for the Subscription Lifecycle ( https://stripe.com/docs/subscriptions/lifecycle ) makes absolutely no mention of the customer.subscription.updated event at all.
It just seems odd that such an appropriate event is not raised when it should be. The documentation also really doesn't say when the current_period_end and current_period_start values are updated either, which limits their utility.
So in my application, after receiving an invoice.payment_succeeded event, how can my program code determine when the customer's subscription period will end next?
I've verified that customer.subscription.updated is called when the billing period ends.
To do this I intercepted all webhooks that occur at the end of the period (FYI: I used an AWS Lambda function that receives the events from an AWS API Gateway, then puts the events on an SQS queue :))
I agree that the Stripe documentation for the customer.subscription.updated event could be clearer and could cover this use case by saying....
Occurs whenever a subscription changes. Examples would include
when the billing period ends and a new billing period begins, when
switching from one plan to another, or switching status from trial to
active.
(FYI: I would ignore the cheatsheet website altogether. They make only fleeting reference to customer.subscription.updated - In step 8 they are describing (poorly) the use case of "Create a customer with a plan without a trial" which wouldn't create a customer.subscription.updated event because this event only occurs when the subscription is updated, not when it is created. Where they do reference customer.subscription.updated is in context of Step 12 "Invoice charge attempt fails")
In defense of Stripes documentation regarding the lifecycle of a subscription, it does say "the following image shows the lifecycle of the most important events that occur" and I'd say customer.subscription.updated is not an important event in context of creating invoices and making payments.
Some details about how Stripe handles the end of a period:
In my test, Stripe raised the customer.subscription.updated event approximately 2 minutes after the timestamp in the current_period_end property on the subscription. The event has two data objects associated with it. The first is the subscription with the new values on it. The second is a previousAttributes object with the two previous current_period_start and current_period_end values.
Two events were generated simultaneously: customer.subscription.updated and invoice.created (this was the invoice for the period that had just elapsed).
Around an hour after the invoice was created, three events were generated simultaneously: invoice.payment_succeeded, charge.succeeded and invoice.updated.
How you treat the rollover of a billing period vs. the payment status of an invoice is really up to you, and very much depends on your application type. That's the beauty of the Stripe API (and it is a thing of beauty).
In my case, I treat the rollover of a billing period separately to the payment status of an invoice. My application cares about when the billing period rolls over and makes updates to usage based on this, but any payment failures generate alerts & are handled offline.
In summary, you can use customer.subscription.updated to know when the billing period has changed.
customer.subscription.updated is triggered when current_period_start and current_period_end change. These represent the billing period.
When invoice.payment_succeeded happens, it's there that you must update the information on your side (e.g.: subscription period):
https://stripe.com/docs/subscriptions/guide#step-3-sync-with-your-site
Also more info here: https://support.stripe.com/questions/what-events-can-i-see-when-a-subscription-is-renewed

Resources