# Create order from code (Litium 8)

**URL:** <https://forum.litium.com/t/create-order-from-code-litium-8/2656>\
**Category:** Questions\
**Tags:** litium-8\
**Created:** [August 25, 2022, 10:28am UTC](https://forum.litium.com/t/create-order-from-code-litium-8/2656 "2022-08-25T10:28:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam.clettborn](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/adam.clettborn/32/956_2.png) [@adam.clettborn](https://forum.litium.com/u/adam.clettborn)\
**Post date:** [August 25, 2022, 10:28am UTC](https://forum.litium.com/t/create-order-from-code-litium-8/2656/1 "2022-08-25T10:28:34Z")

</div>

Hi, I asked [the same question a few months ago](https://forum.litium.com/t/create-order-from-code-l8/2590), and now I have to revisit the question.

The [same problem](https://forum.litium.com/t/create-order-from-code-l8/2590/4) still occurs.

The following error is logged from the payment app (DirectPay):

```auto
2022-08-25 10:19:18.0509 [App:01] [] [ERROR] [] Microsoft.AspNetCore.Server.Kestrel - Connection id "0HMK6ETCSDC50", Request id "0HMK6ETCSDC50:00000002": An unhandled exception was thrown by the application. System.ArgumentException: Payment(SystemId=ba42f6e4-9acf-4b61-9736-ff52953141d8) was not found. (Parameter 'paymentSystemId')
   at Litium.Connect.DirectPay.Services.PaymentService.CaptureAsync(Guid paymentSystemId, CaptureRequest capture) in /_/src/Litium.Connect.DirectPay/Services/PaymentService.cs:line 32
   at Litium.Connect.DirectPay.Controllers.PaymentsController.Capture(Guid paymentSystemId, CaptureRequest capture) in /_/src/Litium.Connect.DirectPay/Controllers/PaymentsController.cs:line 58
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

```

This error is pretty logical, as I just create the payment in Litium, I’m not manually sending anything to the payment app. Not really sure how to achieve this?

I’m using `PaymentService` to create the actual payment in Litium. Then I manually create an Init transaction and an Authorize transaction (using `TransactionService`), both connected to the created payment. Finally I link the payment to the order.

Litium version: 8.3.1

@NilsN

---

<div class="post-metadata">

**Author:** ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)\
**Post date:** [August 26, 2022, 8:59am UTC](https://forum.litium.com/t/create-order-from-code-litium-8/2656/2 "2022-08-26T08:59:26Z")

</div>

What is the use case for these orders? Are they offline orders being synced to Litium?

I’m not sure if you can ensure the payment being created in the app if you’re doing it this way.

Another way is to create the order by using a cart context. This will call the payment provider app and create the transaction.

```auto
var cartContext = await _cartContextFactory.CreateAsync(new CreateCartContextArgs
{
	ChannelSystemId = channel.SystemId,
	CountryCode = country.Id,
	CurrencyCode = currency.Id,
	MarketSystemId = channel.MarketSystemId.Value,
});

foreach (var variant in variants)
{
	await cartContext.AddOrUpdateItemAsync(new AddOrUpdateCartItemArgs
	{
		ArticleNumber = variant.Id,
		Quantity = 1,
	});
}

// must come after adding products to cart
await cartContext.TryInitializeCheckoutFlowAsync(() => new CheckoutFlowInfoArgs
{
	CheckoutFlowInfo = new CheckoutFlowInfo(),
});

await cartContext.AddOrUpdateCustomerInfoAsync(new AddOrUpdateCustomerInfoArgs
{
	CustomerInfo = new CustomerInfo
	{
		FirstName = "Test",
		LastName = "Test",
		Email = "test@example.com",
		CustomerType = CustomerType.Person,
		// PersonSystemId = ,
		// OrganizationSystemId = ,
	}
});

var address = new Address
{
	FirstName = "Test",
	LastName = "Test",
	Address1 = "Testgatan 1",
	Address2 = string.Empty,
	ZipCode = "12345",
	City = "Test",
	Country = "SE",
	Email = string.Empty,
	PhoneNumber = deliveryAddress.PhoneNumber ?? string.Empty,
	MobilePhone = deliveryAddress.PhoneNumber ?? string.Empty
};

await cartContext.AddOrUpdateDeliveryAddressAsync(address);
await cartContext.AddOrUpdateBillingAddressAsync(address);

await cartContext.SelectPaymentOptionAsync(new SelectPaymentOptionArgs
{
	PaymentOptionId = new ProviderOptionIdentifier("DirectPayment", "DirectPay"),
});

await cartContext.SelectShippingOptionAsync(new SelectShippingOptionArgs
{
	ShippingOptionId = new ProviderOptionIdentifier("DirectShipment", "standardPackage"),
});

await cartContext.ConfirmOrderAsync();

```

---

<div class="post-metadata">

**Author:** ![adam.clettborn](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/adam.clettborn/32/956_2.png) [@adam.clettborn](https://forum.litium.com/u/adam.clettborn)\
**Post date:** [August 26, 2022, 9:13am UTC](https://forum.litium.com/t/create-order-from-code-litium-8/2656/3 "2022-08-26T09:13:37Z")

</div>

Hi,

Yes exactly, these are orders that are coming from the customer’s ERP which they create manually.

Thanks, I will try the CartContext way!

---

<div class="post-metadata">

**Author:** ![adam.clettborn](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/adam.clettborn/32/956_2.png) [@adam.clettborn](https://forum.litium.com/u/adam.clettborn)\
**Post date:** [August 26, 2022, 12:46pm UTC](https://forum.litium.com/t/create-order-from-code-litium-8/2656/4 "2022-08-26T12:46:51Z")

</div>

I have some trouble when actually confirming the order, with an exception being thrown:

```auto
Object reference not set to an instance of an object.
   at Litium.Application.Sales.OrderRowExtensions.CalculateByUnitPriceIncludingVat(OrderRow orderRow, String currencyCode, RoundOffService roundOffService)
   at Litium.Accelerator.ValidationRules.ProductPricesHasNotChanged.<>c __DisplayClass7_0.<Validate>b__ 1(SalesOrderRow orderRow)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
   at Litium.Accelerator.ValidationRules.ProductPricesHasNotChanged.Validate(ValidateCartContextArgs entity, ValidationMode validationMode)
   at Litium.Validations.ValidationRuleBase`1.ValidateAsync(T entity, ValidationMode validationMode)
   at Litium.Validations.ValidationRuleBase`1.Litium.Validations.IValidationRule.ValidateAsync(Object entity, ValidationMode validationMode)
   at Litium.Application.Validations.ValidationServiceImpl.<ValidateEntityAsync>d__7`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Litium.Application.Validations.ValidationServiceImpl.<ValidateAsync>d__5`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Litium.Application.Sales.CartContextSessionServiceImpl.<ValidateAsync>d__48.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Litium.Application.Sales.CartContextSessionServiceImpl.<ConfirmOrderAsync>d__37.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Litium.Application.Sales.CartContextImpl.<ConfirmOrderAsync>d__47.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Litium.Accelerator.Integration.Services.OrderIntegrationService.<CreateUpdateLitiumOrder>d__16.MoveNext() in C:\CustomerProject\src\Litium.Accelerator\Integration\Services\OrderIntegrationService.cs:line 215

```

If I just place the order using `await cartContext.PlaceOrderAsync()` the order is created correctly, but the payment looks invalid? If I set `Confirm` to true in PlaceOrderArgs, the order is still created as Init.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/db7ec647411e7ba0fcff9583bd386bf930b08b35.png)

---

<div class="post-metadata">

**Author:** ![adam.clettborn](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/adam.clettborn/32/956_2.png) [@adam.clettborn](https://forum.litium.com/u/adam.clettborn)\
**Post date:** [August 26, 2022, 1:48pm UTC](https://forum.litium.com/t/create-order-from-code-litium-8/2656/5 "2022-08-26T13:48:02Z")

</div>

The problem was related to a validation that has been removed in 8.4+ [Removing the "sales.validation.productprices.haschanged" validation in the checkout](https://forum.litium.com/t/removing-the-sales-validation-productprices-haschanged-validation-in-the-checkout/2542)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)\
**Post date:** [September 23, 2022, 1:48pm UTC](https://forum.litium.com/t/create-order-from-code-litium-8/2656/6 "2022-09-23T13:48:33Z")

</div>

Closed on September 23, 2022, 1:48pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
