This example demonstrates a payment retry process using Upstash Workflow. The following example handles retrying a payment, sending emails, and suspending accounts.

Use Case

Our workflow will:

  1. Attempt to process a payment
  2. Retry the payment if it fails with a 24-hour delay
  3. If the payment succeeds:
    • Unsuspend the user’s account if it was suspended
    • Send an invoice email
  4. If the payment fails after 3 retries:
    • Suspend the user’s account

Code Example

Code Breakdown

1. Charge Customer

We attempt to charge the customer:

If we haven’t put a try-catch block here, the workflow would still have retried the step. Hovewer, because we want to run custom logic when the payment fails, we catch the error here.

2. Retry Payment

We try to charge the customer 3 times with a 24-hour delay between each attempt:

3. If Payment Succeeds

3.1. Unsuspend User

We check if the user is suspended and unsuspend them if they are:

3.2. Send Invoice Email

We send an invoice we got from the payment step to the user:

One of the biggest advantages of using Upstash Workflow is that you have access to the result of the previous steps. This allows you to pass data between steps without having to store it in a database.

4. If Payment Fails After 3 Retries

4.1. Suspend User

If the payment fails after 3 retries, we suspend the user and send them an email to notify them: