Upstash Documentation

Payment Retries

2 min read

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:

Note

If we haven't put a try-catch block here, the workflow would have retried the step. However, 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:

Tip

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: