I am looking for an elegant way to manage workflows and rollback when needed but the system is depending on various external services most steps of these workflows are triggering by external sources (like webhooks).
An example scenario for a subscription system:
- User sends a subscription request
- The subscription created on the database with
status=pending - Subscription request sent to Stripe
- (Stripe does some black magic -like credit card verification-)
- Stripe informs the results of subscription (succeeded or failed) to our app via an HTTP request (God knows when, maybe in 10 seconds, maybe in 10 hours)
- Update the subscription on the database
status=activeorstatus=failed - doSomeOtherImportantStuffWithSubscription()
Now, in this flow, if any of these steps fails for some reason (an exception thrown, database connection error, Stripe card verification error etc.) I want to rollback all the previous steps. But handling all this flow with a bunch of if-else statements is probably the worst idea, and I wonder if there is an elegant and standardized way to achieve this rollback mechanism.
I'm looking for a framework-agnostic solution but if it's important this is our tech stack:
- MoleculerJS (with TypeScript)
- Stripe API
- MySQL 5.7.26
UPDATE
An example of what I'm trying to achieve:
- User sends a subscription request
- The subscription created on the database with
status=pending - Subscription request sent to Stripe
- (Stripe does some black magic -like credit card verification-)
- Stripe informs the results of subscription (succeeded or failed) to our app via an HTTP request (God knows when, maybe in 10 seconds, maybe in 10 hours)
- Update the subscription on the database failed because of a connection error
- Cancel Subscription on Stripe (reverse of 5th step)
- Delete subscription request from the database (reverse of 2nd step)