Stripe Webhooks: Invoice.paid vs Checkout.Session.Completed - django

I use Stripes' webhooks and want to get notified, if the customer successfully "paid the bill". I came across two webhooks, which in my opinion both could do the job:
Webhook "invoice.paid" - According to Stripe doc: Occurs whenever an invoice payment attempt succeeds or an invoice is marked as paid out-of-band.
Webhook "checkout.session.completed" - According to Stripe doc: Occurs when a Checkout Session has been successfully completed.
My questions are:
I don't understand the second part of the "invoice.paid" webhook: "invoice is marked as paid out-of-band" -> What does "out-of-band" mean? Is this to be considered a successful payment?
Regarding "checkout.session.complete" -> This can also occur, if payment fails - correct?
Which webhooks shall I consider (or are there other webhooks) to see the status "customer paid the bill successfully"?
What is more, I don't really know if disputes should be considered as successful payments or not: On one hand, I get a invoice.paid webhook, on the other hand, I get a charge.dispute.created webhook. geeezus...
I appreciate your help! Thanks.

I don't understand the second part of the "invoice.paid" webhook: "invoice is marked as paid out-of-band" -> What does "out-of-band" mean? Is this to be considered a successful payment?
This is specifically referring to marking an invoice paid out of band (ie, the customer paid you outside of Stripe and you want to mark the Stripe invoice paid without collecting a payment). This will not involve an actual payment, but does transition the invoice to status=paid so this event fires.
Regarding "checkout.session.complete" -> This can also occur, if payment fails - correct?
This event signals only that the Checkout session is complete. Depending on the mode use for Checkout, this may or may not involve a payment. If an immediate payment is expected, the session will only complete if that payment is successful. For example mode=setup or mode=subscription with a free trial will not involve an immediate payment. A subscription with trial, though, will create a $0 invoice and fire invoice.paid.
Which webhooks shall I consider (or are there other webhooks) to see
the status "customer paid the bill successfully"?
This depends on what you mean by "paid" and "bill". If you mean specifically for invoices (whether related to subscriptions or not), then invoice.paid is a good choice. You can then filter for amounts greater than $0 etc to further constrain was "paid" means.
What is more, I don't really know if disputes should be considered as
successful payments or not: On one hand, I get a invoice.paid webhook,
on the other hand, I get a charge.dispute.created webhook.
Disputes are not payments, and should be an entirely separate discussion. You can only have a dispute after a payment. Suggest starting by reading the docs on disputes.
To summarize: What are you really trying to do? These events are related and sometimes overlap, but not always. It highly depends on what you're doing.

What's going on?
When you create a checkout session it will have an id, which you'll store in your database next to the user who started the checkout session.
When you receive an invoice.paid webhook event, it does not have any link back to the checkout session! (so you'll know someone paid, but you won't know who paid!)
checkout.session.completed solves this because it contains the id of the checkout session and the stripe customer id, which allows you to link the two, so you basically have a mapping from your customer ids to stripe's customer ids.
So simply grab the customer id from the checkout.session.completed event and store it in your database next to the relevant user, that way you'll be able to tell which one of your users is paying you when you receive an invoice.paid event!
How can this be implemented?
When a checkout session is started, store the checkout session id next to the user who started the session so you can look it up later
When you see checkout.session.completed, look at the accompanying JSON and take the stripe customer number and store it in your database (e.g. a column like stripe_id in users table). To figure out which of your users it's for, use the checkout session id to look it up in your database (i.e. the data you stored in step 1)
Now that you have the stripe customer id stored in your users table, whenever you see invoice.paid, look at the accompanying JSON, take the stripe customer number, look it up in your users table to find who paid, and update the expiry date of their subscription to 1 month into the future.
That's it!
Also good to know
Both checkout.session.completed and invoice.paid events are triggered when someone new subscribes, and only invoice.paid is triggered each month thereafter (presuming the user had enough funds and didn't cancel)
You can get to the stripe customer number in both webhook events like so (this is ruby, but should be similar with js or python):
payload = request.body.read
data = JSON.parse(payload, symbolize_names: true)
data.object.customer
=> "cus_Lvyv721cJGpYB1"

Related

Authorize.Net Query on recurring billing

We have been trying to integrate authorize.net payment gateway in one of our clients project based on Asp.net web API. We have few queries that we came across while implementing Recurring Planning scenarios.
Query 1
We checked the API’s for Creating Subscription, Getting Subscription, Updating Subscription. However once we have created subscription, is there any way we can update the amount in the subscription.
Let’s say for example.
We have a created a subscription for our user for 50$ amount on 01st Jan 2021 with 30 days interval.
And on 15th Jan 2021, our user wishes to purchase 1 more license which will cost him 10$ more.
Hence can we increase his billing cycle of subscription by updating the subscription?
We checked in Update Subscription API, & it is only allowing to update credit card info hence is there any way to update amount.
Query 2
Is there any way to implement Autorenewal, hence when a user wishes he/she can set auto renewal on/off for recurring billing.
Query 3
If there is any way to switch off auto renewal of recurring billing, then is there any link that we can generate & send them through which they can pay there next due.
Query 1: You cannot update a subscription amount. If the amount needs to change you either need to cancel the current subscription and create a new one for the new amount (being sure to prorate credit from the previous subscription payment) or use CIM to manage your subscription service which allows you to charge against their card at your discretion but requires you to also manage the subscription yourself.
Query 2: Not through Authorize.Net. If you want a subscription to start or end you need to explicitly do so through their API.
Query 3: Not through Authorize.Net. That application logic and, once again, you would be responsible for managing.
I'm assuming you are using or are aware of the API provided for Authorize.net here: https://github.com/AuthorizeNet/sdk-dotnet/tree/master/Authorize.NET/Api/Controllers
Query 1: As of now, there is a way to update the amount for a given subscription. You can use ARBSubscriptionType class. There is an amount property there you can set. Then you can create the request ARBUpdateSubscriptionRequest, passing in the ARBSubscriptionType class and the subscription Id.
Note: You might have to handle pro-rating.
Query 2: There isn't a built in renewal feature in Authorize.Net as far as I know. It seems like you could potentially update the totalOccurrences by some amount to act as a "renewal", when technically its an extension of the subscription. The method in which you check when to update, either a Modulo operation or a date check is up to you. You can use paymentScheduleType class to update totalOccurrences, passing it along to a ARBUpdateSubscriptionRequest.
Query 3: Authorize.Net does not have any in house link generation.

Authorize.Net capture settlements

In Authorize.Net is there a way to identify or trigger even when a settlement is a success? I need to update my order in the database so if any webhook for this would be great, does anyone know where I can found more details about it?
There is no webhook for when a settlement occurs. But, since they occur at exactly the same time each night and rarely, if ever, fail, you could just set up an automated job to update your database at the settlement time each night.
You can also parse the credit card settlement report email if want a trigger that is specific to a settlement occurring.
If you want to confirm the payment is settled you can always make an API call to get the transaction status. getTransactionDetailsRequest has a value called transactionStatus which will say "settledSuccessfully" is it was settled.
Or call getUnsettledTransactionListRequest and see if there are any unsettled transactions. If not, then you know they all settled successfully.

How to get a transaction details using invoice number?

We are using authorize.net for payments in our checkout but in some cases we are not getting any response from authorize.net so we are unable to store transaction details in our database and also customers are being charged more than once. So to resolve this we are planning to get the transaction details before sending the payment but we don't have transaction id in our side, so we need a API to get the transaction details using invoice number.
I have searched lot in the API documentation but couldn't able to find it, so any reference might be helpful.
You cannot retrieve transaction information through their API with an invoice number. If you know the dates, and other helpful information about these missing transactions, you can use their Transaction Reporting API to get those day's transaction and retrieve the necessary information that way.
One way to avoid this in the future is to use either Silent Post1 or their new Webhooks API to get notified whenever a payment is made (and any other event you specify).
1 I am the author of that article.

How to get user count by status of facebook events (attending, Invited, etc..)

Is there any way to get the user count by status of facebook events? Facebook does this on the event page (the left columng with each status has a count next to it), but could not find any documentation on how to do the same. It needs to work for small or large events. For small events, I can easily get the list of users and do a quick count. But for events with over 1000 users, the previous method is too slow and not acceptable.
I don't think there's a better way than count every list of users as explained below.
You can which users are 'attending' an event by issuing an HTTP GET to /EVENT_ID/attending
You can which users have replied 'maybe' to an event by issuing an HTTP GET to /EVENT_ID/maybe
You can which users are declined an event (i.e. responded 'no') by issuing an HTTP GET to /EVENT_ID/declined
You can which users have not replied to an event by issuing an HTTP GET to /EVENT_ID/noreply
Taken from https://developers.facebook.com/docs/reference/api/event/
UPDATE 28-Jun:
As of today, Facebook added new fields to the Event FQL table that allows you to do exactly what you want.
From the developers blog:
We've added the following fields to the event FQL table to make it
easier to get the counts of users RSVP-ed to an event:
all_members_count
attending_count
unsure_count
declined_count
not_replied_count
They're pretty much self-explanatory.

How to send customerId (x_cust_id) to authorize.net ARB?

I know, this is [normally] an RTFM question, except, well, we're talking about Authorize.net so the FM is hardly R-able.
I noticed when looking at the all the fields that get sent during the Silent Post, Authorize.net returns an item called "x_cust_id". It is not addressed in their sample code, and I can't find out where in the XML document that field would get posted. I'd like to use it!
So, how exactly do I add "customer Id" when I create a new ARB subscription?
Furthermore, is there any documentation that actually shows all the information I can send to ARB? I don't even know what my options are!
Silent Post != ARB. They are two different things. Silent Post is triggered whenever a transaction is processed by Authorize.Net. They send the results of each transaction to a script on your server notifying it of the results of the transaction. This allows you to manage subscriptions that fail programatically. It is very similar to Paypal's IPN. x_cust_id will be populated from the <customer/><id/> field in the ARBCreateSubscriptionRequest API call.
FYI, this is explained on pages 20 and 21 of the ARB guide.