Skip to content
This repository has been archived by the owner on Jun 2, 2020. It is now read-only.

CreateOrder dont working #43

Closed
AXS-RickardPettersson opened this issue Apr 3, 2019 · 5 comments
Closed

CreateOrder dont working #43

AXS-RickardPettersson opened this issue Apr 3, 2019 · 5 comments

Comments

@AXS-RickardPettersson
Copy link

Describe the bug
I have a ASP.NET MVC 4 project i trying to integrate Klarna Checkout in using the latest nuget package.

To try something i used the example of createing a checkout order that exist in the github respository and when its doing the first call with the code row:

client.Checkout.CreateOrder(order).Result;

Its stuck there, nothing happening its loading and loading, no timeout or anything...

Which service do you use
Klarna Checkout Rest API, kco_rest_dotnet

To Reproduce
Use your own example code CreateExample in https://github.com/klarna/kco_rest_dotnet/blob/v3.1/Klarna.Rest/Klarna.Rest.Core.Examples/CheckoutApi.cs

Expected behavior
I was expecting a result where i could get the OrderId or a error but dont getting anything.

Additional context
Visual Studio 2017
.Net Framework 4.6.1
ASP:NET MVC 4
Klarna Europe Test Environment

@generalengels
Copy link

I have same problem. There is no any response from Klarna.
klarna.Payment.CreateCreditSession(paymentSession) not working.

@DUAB-Johan
Copy link

Metoden är async vad jag kan se. Prova nedan

            Task<CheckoutOrder> callTask = Task.Run(() => klarna.Checkout.CreateOrder(order));
            // Wait finish
            callTask.Wait();
            // Get result                
            var createdOrder = callTask.Result;

@NiclasOhlsson
Copy link

Also having the same issue. Running the test code in a console application works fine but running that same code in a clean ASP.NET WebForms application freezes the application. The solution suggested by @DUAB-Johan does not solve the issue.

Generally it appears to be discouraged to run asynchronous methods in a synchronous way as done in the Klarna library (see for example: https://devblogs.microsoft.com/pfxteam/should-i-expose-synchronous-wrappers-for-asynchronous-methods/) and I am wondering if this is indeed the underlying issue; resulting in an application deadlock.

More disturbingly it would suggest Klarna has never actually tested their own code beyond the context of a console application (as is indeed the format of the provided sample project).

@NiclasOhlsson
Copy link

NiclasOhlsson commented Jun 12, 2019

Considering this code works (main method console application):

public static void Main()
        {
            var username = "user";
            var password = "password";

            var client = new Klarna(username, password, KlarnaEnvironment.TestingEurope);

            var order = new CheckoutOrder
            {
                PurchaseCountry = "se",
                PurchaseCurrency = "sek",
                Locale = "sv-se",
                OrderAmount = 10000,
                OrderTaxAmount = 2000,
                MerchantUrls = new CheckoutMerchantUrls
                {
                    Terms = "https://www.example.com/terms.html",
                    Checkout = "https://www.example.com/checkout.html",
                    Confirmation = "https://www.example.com/confirmation.html",
                    Push = "https://www.example.com/push.html"
                },
                OrderLines = new List<OrderLine>()
                {
                    new OrderLine
                    {
                        Type = OrderLineType.physical,
                        Name = "Foo",
                        Quantity = 1,
                        UnitPrice = 10000,
                        TaxRate = 2500,
                        TotalAmount = 10000,
                        TotalTaxAmount = 2000,
                        TotalDiscountAmount = 0,
                    }
                }
            };

            try
            {
                var createdOrder = client.Checkout.CreateOrder(order).Result;
                var orderId = createdOrder.OrderId;
                Console.WriteLine($"Order ID: {orderId}");
            }
            catch (AggregateException ae)
            {
            }
        }

…while this code doesn't (page load method, asp.net WebForms aspx page):

protected void Page_Load(object sender, EventArgs e)
        {
            var username = "user";
            var password = "password";

            var client = new Klarna(username, password, KlarnaEnvironment.TestingEurope);

            var order = new CheckoutOrder
            {
                PurchaseCountry = "se",
                PurchaseCurrency = "sek",
                Locale = "sv-se",
                OrderAmount = 10000,
                OrderTaxAmount = 2000,
                MerchantUrls = new CheckoutMerchantUrls
                {
                    Terms = "https://www.example.com/terms.html",
                    Checkout = "https://www.example.com/checkout.html",
                    Confirmation = "https://www.example.com/confirmation.html",
                    Push = "https://www.example.com/push.html"
                },
                OrderLines = new List<OrderLine>()
                {
                    new OrderLine
                    {
                        Type = OrderLineType.physical,
                        Name = "Foo",
                        Quantity = 1,
                        UnitPrice = 10000,
                        TaxRate = 2500,
                        TotalAmount = 10000,
                        TotalTaxAmount = 2000,
                        TotalDiscountAmount = 0,
                    }
                }
            };

            try
            {
                var createdOrder = client.Checkout.CreateOrder(order).Result;
                var orderId = createdOrder.OrderId;
                Console.WriteLine($"Order ID: {orderId}");
            }
            catch (AggregateException ae)
            {
            }
        }

@alexions
Copy link
Contributor

Thanks @DUAB-Johan for the solution.

Actually due to the Web Forms limitations you cannot use Task.Result in the Main thread
There is the nice article about this limitation: https://blogs.msdn.microsoft.com/jpsanders/2017/08/28/asp-net-do-not-use-task-result-in-main-context/

To make the things easier we created a nice example using WebForms to demonstrate how you can use the SDK together with the WebForms: https://github.com/klarna/kco_rest_dotnet/tree/v3.x/Klarna.Rest/SampleProjects/WebForms

Follow the link, clone the project and run the example. I hope this example will help you to resolve the issue.

Thanks everyone for the feedback!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants