Order Cancellation

Handles the end-to-end cancellation of a previously placed vehicle order. It validates cancellation eligibility, enforces a brief cooling-off Wait period to allow in-flight production and finance operations to settle, then runs concurrent inventory and credit reversal checks using a Parallel state before releasing the order or failing the cancellation with a structured error.
{
  "Comment": "Vehicle order cancellation workflow — demonstrates Pass, Task, Choice, Wait, Parallel, Succeed, and Fail states. Validates cancellation eligibility, holds briefly for in-flight production and finance to settle, then runs concurrent inventory and credit reversal checks before completing or failing.",
  "StartAt": "SetCancellationMeta",
  "States": {
    "SetCancellationMeta": {
      "Type": "Pass",
      "Comment": "Stamp a cancellationId and initial status onto the input without invoking a Lambda.",
      "Parameters": {
        "orderId.$": "$.orderId",
        "customerId.$": "$.customerId",
        "dealerId.$": "$.dealerId",
        "modelCode.$": "$.modelCode",
        "vehiclePrice.$": "$.vehiclePrice",
        "trimLevel.$": "$.trimLevel",
        "color.$": "$.color",
        "addOns.$": "$.addOns",
        "homeDelivery.$": "$.homeDelivery",
        "deliveryAddress.$": "$.deliveryAddress",
        "orderDate.$": "$.orderDate",
        "customer.$": "$.customer",
        "cancellationReason.$": "$.cancellationReason",
        "cancellationId.$": "$$.Execution.Id",
        "cancellationStatus": "PENDING"
      },
      "Next": "ValidateCancellationEligibility"
    },
    "ValidateCancellationEligibility": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "${ValidateOrderFunctionArn}",
        "Payload.$": "$"
      },
      "ResultPath": "$.validation",
      "Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 3,
          "BackoffRate": 2
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "ResultPath": "$.error",
          "Next": "CancellationFailed"
        }
      ],
      "Next": "IsCancellationEligible"
    },
    "IsCancellationEligible": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.validation.Payload.isValid",
          "BooleanEquals": true,
          "Next": "ProductionSettlementHold"
        }
      ],
      "Default": "RejectCancellation"
    },
    "RejectCancellation": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "${CancelOrderFunctionArn}",
        "Payload": {
          "reason": "CANCELLATION_INELIGIBLE",
          "order.$": "$"
        }
      },
      "ResultPath": "$.rejection",
      "Next": "CancellationFailed"
    },
    "CancellationFailed": {
      "Type": "Fail",
      "Error": "OrderCancellationFailed",
      "Cause": "Order could not be cancelled"
    },
    "ProductionSettlementHold": {
      "Type": "Wait",
      "Comment": "Brief hold to allow any in-flight production scheduling and finance operations to settle before reversal proceeds.",
      "Seconds": 3,
      "Next": "InventoryAndCreditReversal"
    },
    "InventoryAndCreditReversal": {
      "Type": "Parallel",
      "Comment": "Run inventory release and credit reversal concurrently to minimise latency.",
      "Branches": [
        {
          "StartAt": "ReleaseInventory",
          "States": {
            "ReleaseInventory": {
              "Type": "Task",
              "Resource": "arn:aws:states:::lambda:invoke",
              "Parameters": {
                "FunctionName": "${CheckInventoryFunctionArn}",
                "Payload.$": "$"
              },
              "ResultPath": "$.inventoryRelease",
              "Retry": [
                {
                  "ErrorEquals": [
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException"
                  ],
                  "IntervalSeconds": 2,
                  "MaxAttempts": 2,
                  "BackoffRate": 2
                }
              ],
              "End": true
            }
          }
        },
        {
          "StartAt": "ReverseCreditHold",
          "States": {
            "ReverseCreditHold": {
              "Type": "Task",
              "Resource": "arn:aws:states:::lambda:invoke",
              "Parameters": {
                "FunctionName": "${RunCreditCheckFunctionArn}",
                "Payload.$": "$"
              },
              "ResultPath": "$.creditReversal",
              "Retry": [
                {
                  "ErrorEquals": [
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException"
                  ],
                  "IntervalSeconds": 2,
                  "MaxAttempts": 2,
                  "BackoffRate": 2
                }
              ],
              "End": true
            }
          }
        }
      ],
      "ResultPath": "$.reversalResults",
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "ResultPath": "$.error",
          "Next": "CancellationFailed"
        }
      ],
      "Next": "ConfirmCancellation"
    },
    "ConfirmCancellation": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "${CancelOrderFunctionArn}",
        "Payload": {
          "reason.$": "$.cancellationReason",
          "order.$": "$"
        }
      },
      "ResultPath": "$.cancellation",
      "Retry": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 3,
          "BackoffRate": 2
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "ResultPath": "$.error",
          "Next": "CancellationFailed"
        }
      ],
      "Next": "NotifyCustomerOfCancellation"
    },
    "NotifyCustomerOfCancellation": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "${NotifyCustomerFunctionArn}",
        "Payload": {
          "notificationReason": "ORDER_CANCELLED",
          "order.$": "$"
        }
      },
      "ResultPath": "$.notification",
      "Retry": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 2,
          "BackoffRate": 2
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "ResultPath": "$.error",
          "Next": "CancellationFailed"
        }
      ],
      "Next": "CancellationComplete"
    },
    "CancellationComplete": {
      "Type": "Succeed"
    }
  }
}
JSON
Expand
100%

Automotive teams can use patterns like this to build reliable, compliant, and scalable automation for payment systems and can test and refine these flows locally with Thrubit to reduce cloud cost and speed up iteration.