Creating a Custom Checkout Flow
Learn how to build a custom checkout flow in Umbraco Commerce.
If you need a more custom checkout experience, you can build a custom checkout flow using the Umbraco Commerce API. This approach gives you full control over the checkout experience, allowing you to tailor it to your specific requirements.
Create a Checkout Surface Controller
To create a custom checkout flow, add a custom Surface Controller to handle the checkout process.
Create a new class and inherit from
Umbraco.Cms.Web.Website.Controllers.SurfaceController.Name the class
CheckoutSurfaceController.Add the following code to the class:
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Web.Website.Controllers;
using Umbraco.Commerce.Core.Api;
namespace Umbraco.Commerce.SwiftShop.Controllers;
public class CheckoutSurfaceController : SurfaceController
{
private readonly IUmbracoCommerceApi _commerceApi;
public CheckoutSurfaceController(
IUmbracoContextAccessor umbracoContextAccessor,
IUmbracoDatabaseFactory databaseFactory,
ServiceContext services,
AppCaches appCaches,
IProfilingLogger profilingLogger,
IPublishedUrlProvider publishedUrlProvider,
IUmbracoCommerceApi commerceApi)
: base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
{
_commerceApi = commerceApi;
}
// Add your checkout actions here
}Define the Checkout Steps
Before you can start building the checkout flow, you must define the steps the customer goes through. A typical checkout flow consists of the following steps:
Collecting Customer Information.
Selecting a Shipping Method.
Selecting a Payment Method.
Reviewing Order Details.
Showing an Order Confirmation.
To accommodate these steps, you must create a few new Document Types for each step (or one with multiple templates, one per step). Each Document Type will represent a step in the checkout flow.
Create Document Types for each of the steps above, adding only properties that make sense for your setup.

Collecting Customer Information
To collect customer information, you need to create an action method in our CheckoutSurfaceController. This should accept the details and update the order accordingly. The properties will be wrapped in a DTO class to pass it to the controller.
Create a new class and name it
UpdateOrderInformationDtousing the following properties.
Add the following action method to your
CheckoutSurfaceController.
Open the view for the
Collecting Customer Informationstep.Create a form that posts to the
UpdateOrderInformationaction method of theCheckoutSurfaceController, passing the customer information.
The customer can fill out their details and proceed to the next step in the checkout flow.

Selecting a Shipping Method
Create a new class and name it
UpdateShippingMethodDtousing the following properties.
Add the following action method to your
CheckoutSurfaceController.
Open the view for the
Selecting a Shipping Methodstep.Create a form that posts to the
UpdateOrderShippingMethodaction method of theCheckoutSurfaceController, passing the selected shipping method.
The customer can select a shipping method and proceed to the next step in the checkout flow.

Selecting a Payment Method
Create a new class and name it
UpdatePaymentMethodDtousing the following properties.
Add the following action method to your
CheckoutSurfaceController.
Open the view for the
Selecting a Payment MethodstepCreate a form that posts to the
UpdateOrderPaymentMethodaction method of theCheckoutSurfaceController, passing the selected payment method.
The customer can select a payment method and proceed to the next step in the checkout flow.

Reviewing Order Details
Open the view for the
Reviewing Order Detailsstep.Display the order details and provide a button to trigger capturing payment.
This is a unique step in the checkout flow as it doesn't post back to the CheckoutSurfaceController. Instead, it uses the BeginPaymentFormAsync method of the Html helper to render a payment method-specific form that triggers the payment capturing process.
The customer can review their order details and proceed to the payment gateway.

Capturing Payment
The payment capture screen is entirely dependent on the payment method being used. It is the responsibility of the associated payment provider to redirect and handle the payment process. The payment provider will redirect back to the order confirmation page on success, or to the cart page with an error if there is a problem.
For more information on how to implement a payment provider, see the Payment Providers documentation.
Showing an Order Confirmation
Open the view for the
Showing an Order Confirmationstep.Display the order confirmation details.
In the order confirmation, you must use GetCurrentFinalizedOrderAsync instead of the previously used GetCurrentOrderAsync. This is because the order will have been finalized during the payment processing step and the current cart order will be cleared.
The customer can view their order confirmation details.

At this stage, the customer should receive an email confirmation of their order.

Umbraco Commerce comes with a default order confirmation email template. This can be customized to suit your store's branding. See the Customizing Templates documentation for more information.
Extras
Redeeming a Coupon / Gift Card
The checkout flow is a common place to allow customers to redeem coupons or gift cards. This can be done by adding another step to the checkout flow where the customer can enter the code and apply it to their order.
Create a new class and name it
DiscountOrGiftCardCodeDtousing the following properties.
Add the following action methods to your
CheckoutSurfaceController.
Open the base view for your checkout flow.
Create a form that posts to the
ApplyDiscountOrGiftCardCodeaction method of theCheckoutSurfaceController, passing the code to redeem.
Show a list of applied discounts and gift cards with a link to remove them.
The customers can enter a discount or gift card code and apply it to their order.

Last updated
Was this helpful?