Reputation: 14600
I'm using Stripe to add Connect accounts (bank accounts) for my users and have set up my server to listen to incoming webhooks. In my controller, I create a Stripe Connect account and then fill in my own StripeAccount entity with the newly created account data, like this:
// Stripe API call to create bank account
var service = new AccountService();
account = await service.CreateAsync(options);
// create a new entity in my db
var stripeAccount = new StripeAccount {
UserId = userFromRepo.Id,
stripeAccount.AccountId = account.Id;
// fill in other fields...
};
_unitOfWork.Repository<StripeAccount>().Add(stripeAccount);
var success = await _unitOfWork.Complete();
if (success) {
// send out email to user
}
Stripe immediately sends me a webhook AccountUpdated before my server has time to save the new entity to my db. Usually their timestamp is the same for AccountCreated and AccountUpdated events.
Question - So in my controller listening for webhooks, should I check for my entity to see if it's been created and if it doesn't exist send a status not found 404 response back to Stripe? And then wait for Stripe to send it again?
FYI - According to their documentation page regarding built in retries, they say just return a status 200, and they say - "If Stripe doesn’t quickly receive a 2xx response status code for an event, we mark the event as failed and stop trying to send it to your endpoint."
If anyone has any advice to give me regarding this I would be much appreciated!
Upvotes: 1
Views: 852
Reputation: 1981
A general rule of thumb that Stripe only cares whether you respond 200 to their webhook request. Doesn't matter if you return 404 or 500 to a webhook event, they will mark it as incomplete.
You seem to have a race condition between logic in listening to Stripe webhook and saving entity to your db. You can simply wait for the saving logic finished to start calling Stripe API (and trigger webhook event) or you will need a locking or queuing mechanism for handling them asynchronously.
Upvotes: 0