# Order Processing Pipelines

Order processing pipelines are workflows executed automatically when an order moves from one status to another. When an order status changes, the pipeline associated with the new status is executed and can be customized for business logic and integrations.

The way to customize the order processing pipelines differs slightly from regular pipelines. It happens on a dedicated pipeline builder and requires an alias identifier for each pipeline.

Ucommerce comes with the following order processing pipelines out of the box:

| Pipeline Alias   | Order Status Trigger   |
| ---------------- | ---------------------- |
| ToCompletedOrder | Order set as completed |
| ToCancelled      | Order set as cancelled |

## Implementing a Task

Tasks are implemented in the same way as other pipelines in Ucommerce by creating a new class that inherits from the interface \
`IPipelineTask<OrderProcessingInput, OrderProcessingOutput>`.\
See [Implementing a Task section on the Extending Pipelines page](https://dev.ucommerce.net/readme/extensions/extending-pipelines/..#implementing-a-task) for details.

```csharp
public class MyCustomOrderProcessingTask : IPipelineTask<OrderProcessingInput, OrderProcessingOutput>
{
    public CascadeMode CascadeMode => CascadeMode.Continue;

    public Task Execute(PipelineContext<OrderProcessingInput, OrderProcessingOutput> context, CancellationToken cancellationToken)
    {
        // Your custom logic.
    }
}
```

## Adding a Task

Adding a task is done through the `PaymentBuilder` returned by the `AddPayments` extension method on `IUcommerceBuilder`. It is recommended that you create an extension method to add your configuration.

```csharp
public static class CheckoutPipelineExtensions
{
    public static PaymentBuilder AddMyOrderProcessingExtensions(this PaymentBuilder builder)
    {
        //This will insert the task 'MyCustomOrderProcessingTask' just before 'DefaultToCompletedPipelineTask'
        builder
            .OrderProcessingPipelines
            .GetByAlias("ToCompletedOrder")
            .InsertBefore<MyCustomOrderProcessingTask>(typeof(DefaultToCompletedPipelineTask));

        //This will insert the task 'MyCustomOrderProcessingTask' just after 'DefaultToCompletedPipelineTask'
        builder
            .OrderProcessingPipelines
            .GetByAlias("ToCompletedOrder")
            .InsertAfter<MyCustomOrderProcessingTask>(typeof(DefaultToCompletedPipelineTask));

        //This will insert the task 'MyCustomOrderProcessingTask' as the first task of the pipeline
        builder
            .OrderProcessingPipelines
            .GetByAlias("ToCompletedOrder")
            .InsertFirst<MyCustomOrderProcessingTask>();

        //This will insert the task 'MyCustomOrderProcessingTask' as the last task of the pipeline
        builder
            .OrderProcessingPipelines
            .GetByAlias("ToCompletedOrder")
            .InsertLast<MyCustomOrderProcessingTask>();

        //This will remove the default task 'DefaultToCompletedPipelineTask' from the pipeline
        builder
            .OrderProcessingPipelines
            .GetByAlias("ToCompletedOrder")
            .Remove<DefaultToCompletedPipelineTask>();

        return builder;
    }
}
```

{% hint style="info" %}
Replace a task by placing your custom task before or after the default task, then remove the default task.
{% endhint %}

### Use the extension method to register tasks

```csharp
builder.Services.AddUcommerce(builder.Configuration)
    // ... register other Ucommerce components first
    .AddPayments() // We are extending the PaymentBuilder
    .AddMyOrderProcessingExtensions()
    // ...
    .Build();
```
