Handling order confirmation
This article shows how to perform tasks such as sending a confirmation email and invalidating used voucher codes by using the state transitions when an order gets confirmed.
When an order is confirmed, it needs to be set to the order status Confirmed. Since the state transition engine for order states are customisable, this state might be named differently based on the project, but effectively its purpose is to mark the order as confirmed.
When an order is confirmed the usual practice is to perform the following tasks:
- Send confirmation e-mail.
- Notify ERP system to start order processing.
- Reduce stock balances (can be made through the ERP system, or just the stock balance in Litium is reduced using the Litium API).
- Notify campaign engine to remove voucher code (or reduce the voucher code frequency), so that same voucher code cannot be used again.
Order confirmed state entry action
All of the above tasks need to be performed just once and only once. So, the ideal place to execute these tasks is on the entry action to the order confirmed state. The following code fragment is from the Litium accelerator implementation, which performs the above tasks.
//Order Confirmed
var confirmed = new State<OrderCarrier>((short)OrderState.Confirmed, OrderState.Confirmed.ToString(),
(orderCarrier, currentState, token) =>
{
//Order confirmed entry action.
//now the order is confirmed, reduce the stock balances.
OrderUtilities.ReduceStockBalance(ModuleECommerce.Instance.Orders[orderCarrier.ID, token], ModuleECommerce.Instance.AdminToken);
//notify campaigns engine to take order confirmation actions.
ModuleECommerce.Instance.CampaignCalculator.HandleOrderConfirmation(orderCarrier, ModuleECommerce.Instance.AdminToken);
//Send order confirmation email.
IoC.Resolve<IMailService>().SendEmail(new OrderConfirmationEmail(orderCarrier.ID, orderCarrier.CustomerInfo.Address.Email), false);
//TODO: integration code to execute when an order is confirmed.
//....
},
null);
The following image ilustrates how the voucher code gets reduced when an order is confirmed.
