Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using InitializeTransaction() and Finalize() results variationContext being reset to default #505

Closed
MichaelNielsenDK opened this issue May 8, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@MichaelNielsenDK
Copy link

When doing something like this

var testVariationContextBefore = _variationContextAccessor.VariationContext?.Culture;

_umbracoCommerceApi.Uow.Execute(uow =>
{
var order = _umbracoCommerceApi.GetCurrentOrder(storeId)
						.AsWritable(uow);

//Finalize order
order.InitializeTransaction();
order.Finalize(order.TotalPrice, Guid.NewGuid().ToString("N"), PaymentStatus.Authorized);

_umbracoCommerceApi.SaveOrder(order);

    uow.Complete();
});

var testVariationContextAfter = _variationContextAccessor.VariationContext?.Culture;

And being on e.g. an english variant, the testVariationContextBefore variable will have a value of en, but testVariationContextAfter will have a value of da, as danish is the default variant.

Something is happening in InitializeTransaction() and/or Finalize() so variationContext is set to default.

If any redirection happen after finalizing, for example to a receipt page, the user will be redirected to the incorrect variant.

A temporary work around is fetch the variationContext prior to calling InitializeTransaction() and Finalize(), and then set it before redirections, so something like this

var currentVariationContext = _variationContextAccessor.VariationContext?.Culture;

_umbracoCommerceApi.Uow.Execute(uow =>
{
var order = _umbracoCommerceApi.GetCurrentOrder(storeId)
						.AsWritable(uow);

//Finalize order
order.InitializeTransaction();
order.Finalize(order.TotalPrice, Guid.NewGuid().ToString("N"), PaymentStatus.Authorized);

_umbracoCommerceApi.SaveOrder(order);

    uow.Complete();
});

_variationContextAccessor.VariationContext = new VariationContext(currentVariationContext);

return RedirectToUmbracoPage(receiptPage);

Umbraco 13.1.1
Umbraco Commerce 13.1.3

@MichaelNielsenDK MichaelNielsenDK added the bug Something isn't working label May 8, 2024
@mattbrailsford
Copy link
Contributor

I don't seem to be able to replicate this, do you have any minimal replication steps?

I've tried the following code in an API controller and at the end of this code block, the variation context is still en-US at the end even though I've set my installs default language to Norwegian.

variationContextAccessor.VariationContext = new VariationContext("en-US");

var testVariationContextBefore = variationContextAccessor.VariationContext?.Culture;

api.Uow.Execute((uow) =>
{
    var storeId = Guid.Parse("b1e61994-b83b-420a-903e-63a7a15942dc");
    var order = api.GetOrCreateCurrentOrder(storeId).AsWritable(uow);

    // Add product
    order.AddProduct("4c4d0aa4-5d5d-4ee1-bca9-357e0a6fbb4a", 1);

    // Set customer details
    var countryId = Guid.Parse("af697207-d370-4aee-824c-15711d43a9f2");
    order.SetProperties(new Dictionary<string, string>
    {
        { Constants.Properties.Customer.EmailPropertyAlias, "[email protected]" },
        { "marketingOptIn", "0" },
        { Constants.Properties.Customer.FirstNamePropertyAlias, "Test" },
        { Constants.Properties.Customer.LastNamePropertyAlias, "Customer" },
        { "billingAddressLine1", "10 Test Road" },
        { "billingCity", "Test City" },
        { "billingZipCode", "TS3 3ST" },
        { "billingTelephone", "0123456789" },
        { "shippingSameAsBilling", "1" }
    })
    .SetPaymentCountryRegion(countryId, null)
    .SetShippingCountryRegion(countryId, null);

    // Set shipping method
    var shippingMethodId = Guid.Parse("2ecb73ed-1b13-4ca4-8502-c1c4a8df533d");
    order.SetShippingMethod(shippingMethodId);

    // Set payment method
    var paymentMethodId = Guid.Parse("e35677ac-a544-45a0-ba4a-a78dd43dbaf2");
    order.SetPaymentMethod(paymentMethodId);

    // Recalculate order
    order.Recalculate();

    // Finalize order
    order.InitializeTransaction();
    order.Finalize(order.TransactionAmount.Value, Guid.NewGuid().ToString("N"), PaymentStatus.Authorized);

    api.SaveOrder(order);

    uow.Complete();
});

var testVariationContextAfter = variationContextAccessor.VariationContext?.Culture;

@mattbrailsford
Copy link
Contributor

I'm going to close this as can't replicate for now, but feel free to add more replication steps if you have any and I'll re-open the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants