Exemple #1
0
        static void Main(string[] args)
        {
            // Set your credentials and point to an API environment
            // Remember to set $MERCHANT_ID$ and $PASSWORD$ to your account's values
            // More about authentication here https://developers.klarna.com/api/#authentication
            Console.WriteLine("Setting up authentication against environment: {0:G}", KlarnaEnvironment.TestingEurope);
            var klarna = new Klarna.Rest.Core.Klarna("$MERCHANT_ID", "$PASSWORD$", KlarnaEnvironment.TestingEurope);

            Console.WriteLine("Constructing order body");
            // Prepare an order by constructing its body
            var order = new Klarna.Rest.Core.Model.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
                    }
                },
                CheckoutOptions = new CheckoutOptions
                {
                    AllowSeparateShippingAddress = false,
                    ColorButton            = "#FF9900",
                    ColorButtonText        = "#FF9900",
                    ColorCheckbox          = "#FF9900",
                    ColorCheckboxCheckmark = "#FF9900",
                    ColorHeader            = "#FF9900",
                    ColorLink            = "#FF9900",
                    DateOfBirthMandatory = false,
                    ShippingDetails      = "bar",
                    AllowedCustomerTypes = new List <string>()
                    {
                        "person", "organization"
                    }
                }
            };

            // Proceed with operating against the Klarna Checkout API
            try
            {
                // Create the order
                Console.WriteLine("Calling Klarna API to create order");
                var createdOrder = klarna.Checkout.CreateOrder(order).Result;
                Console.WriteLine("Klarna API returned created order with ID {0:G}", createdOrder.OrderId);

                // Superfluous, just to show you that you can retrieve a created order after the fact
                var fetchedOrder = klarna.Checkout.GetOrder(createdOrder.OrderId).Result;
                Console.WriteLine("Fetching order with ID {0:G} from Klarna API", createdOrder.OrderId);

                // Modify the created order by adding an item
                Console.WriteLine("Modifying order by adding a new OrderLine item");
                fetchedOrder.OrderLines.Add(new OrderLine
                {
                    Type                = OrderLineType.physical,
                    Name                = "Foo2",
                    Quantity            = 1,
                    UnitPrice           = 10000,
                    TaxRate             = 2500,
                    TotalAmount         = 10000,
                    TotalTaxAmount      = 2000,
                    TotalDiscountAmount = 0,
                });
                // Update the amounts appropriately taking into account all Orderlines
                fetchedOrder.OrderAmount    = 20000;
                fetchedOrder.OrderTaxAmount = 4000;

                // Update the order via calling the Klarna Checkout API
                Console.WriteLine("Update order by calling the Klarna API with modified order details");
                var updatedOrder = klarna.Checkout.UpdateOrder(fetchedOrder).Result;
                Console.WriteLine("Klarna API responded with updated order details for order with ID {0:G}", updatedOrder.OrderId);
            }
            catch (AggregateException ae)
            {
                foreach (var e in ae.InnerExceptions)
                {
                    if (e is ApiException)
                    {
                        var apiException = (ApiException)e;
                        Console.WriteLine("Status code: " + apiException.StatusCode);
                        Console.WriteLine("Error: " + string.Join("; ", apiException.ErrorMessage.ErrorMessages));
                    }
                    else
                    {
                        // Rethrow any other exception or process it
                        Console.WriteLine(e.Message);
                        throw;
                    }
                }
            }
        }
Exemple #2
0
        public ActionResult Kco()
        {
            AddLogs("Creating Klarna API Client");
            var client = new Client(_username, _password, KlarnaEnvironment.TestingEurope);

            AddLogs("Prepare an order by constructing its body");
            var order = new CheckoutOrder
            {
                PurchaseCountry  = "gb",
                PurchaseCurrency = "gbp",
                Locale           = "en-gb",
                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,
                    }
                }
            };

            AddLogs("Proceed with operating against the Klarna Checkout API");
            try
            {
                AddLogs("Sending a request to Klarna API server");
                var createdOrder = client.Checkout.CreateOrder(order).Result;

                AddLogs("Getting the HTML snippet code ");
                ViewBag.HTMLSnippet = createdOrder.HtmlSnippet;
            }
            catch (AggregateException ae)
            {
                foreach (var e in ae.InnerExceptions)
                {
                    if (e is ApiException)
                    {
                        var apiException = (ApiException)e;
                        ViewBag.Error = "Status code: " + apiException.StatusCode + "; Error: "
                                        + string.Join("; ", apiException.ErrorMessage.ErrorMessages);
                        AddLogs($"Error happened {ViewBag.Error}");
                    }
                    else
                    {
                        ViewBag.Error = e.Message;
                        AddLogs($"Error happened {ViewBag.Error}");
                    }
                }
            }

            ViewBag.Logs = GetLogs().ToArray();
            AddLogs("Render the page template");

            return(View());
        }
        protected void Page_Load(object sender, EventArgs args)
        {
            PutHTMLLogs("Page is loading...");
            // Set your credentials and point to an API environment
            // Remember to set $MERCHANT_ID$ and $PASSWORD$ to your account's values
            // More about authentication here https://developers.klarna.com/api/#authentication
            var username = "******";
            var password = "******";

            PutHTMLLogs("Creating Klarna API Client");
            var client = new Client(username, password, KlarnaEnvironment.TestingEurope);

            PutHTMLLogs("Prepare an order by constructing its body");
            var order = new CheckoutOrder
            {
                PurchaseCountry  = "gb",
                PurchaseCurrency = "gbp",
                Locale           = "en-gb",
                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                = "Tomatoes",
                        Quantity            = 10,
                        QuantityUnit        = "kg",
                        UnitPrice           = 600,
                        TaxRate             = 2500,
                        TotalAmount         = 6000,
                        TotalTaxAmount      = 1200,
                        TotalDiscountAmount = 0,
                    },
                    new OrderLine
                    {
                        Type                = OrderLineType.physical,
                        Name                = "Bananas",
                        Quantity            = 1,
                        QuantityUnit        = "bag",
                        UnitPrice           = 5000,
                        TaxRate             = 2500,
                        TotalAmount         = 4000,
                        TotalTaxAmount      = 800,
                        TotalDiscountAmount = 1000
                    }
                }
            };

            PutHTMLLogs("Proceed with operating against the Klarna Checkout API");
            try
            {
                PutHTMLLogs("Sending a request to Klarna API server");
                // Due to the Web Forms limitations we cannot use Task.Result in the Main thread
                // https://blogs.msdn.microsoft.com/jpsanders/2017/08/28/asp-net-do-not-use-task-result-in-main-context/
                Task <CheckoutOrder> callTask = Task.Run(() => client.Checkout.CreateOrder(order));

                PutHTMLLogs("Waiting for the result");
                callTask.Wait();

                PutHTMLLogs("Show the HTML snippet code to the customer");
                HTMLSnippet.InnerHtml = callTask.Result.HtmlSnippet;
            }
            catch (AggregateException ae)
            {
                foreach (var e in ae.InnerExceptions)
                {
                    if (e is ApiException)
                    {
                        var apiException     = (ApiException)e;
                        var error            = "Status code: " + apiException.StatusCode;
                        var exceptionMessage = string.Join("; ", apiException.ErrorMessage.ErrorMessages);
                        Console.WriteLine(error, exceptionMessage);
                        PutHTMLLogs($"An Error occured: {error}");
                        HTMLSnippet.InnerHtml = exceptionMessage;
                    }
                    else
                    {
                        Console.WriteLine(e.Message);
                        PutHTMLLogs($"An Error occured: {e.Message}");
                    }
                }
            }
        }
Exemple #4
0
        public ActionResult Kp()
        {
            var client = new Client(_username, _password, KlarnaEnvironment.TestingEurope);

            try
            {
                var paymentSession = new PaymentCreditSession
                {
                    PurchaseCountry  = "GB",
                    PurchaseCurrency = "GBP",
                    Locale           = "en-GB",
                    OrderAmount      = 10000,
                    OrderTaxAmount   = 2000,
                    OrderLines       = new List <OrderLine>()
                    {
                        new OrderLine
                        {
                            Type                = OrderLineType.physical,
                            Name                = "Foo",
                            Quantity            = 1,
                            UnitPrice           = 10000,
                            TaxRate             = 2500,
                            TotalAmount         = 10000,
                            TotalTaxAmount      = 2000,
                            TotalDiscountAmount = 0,
                        }
                    },
                    MerchantReference1 = "StoreOrderId",
                    Options            = new PaymentOptions {
                        ColorButton = "#000000"
                    },
                    BillingAddress = new PaymentAddressInfo
                    {
                        GivenName     = "John",
                        FamilyName    = "Doe",
                        StreetAddress = "13 New Burlington St",
                        PostalCode    = "W13 3BG",
                        Country       = "GB",
                        Email         = "*****@*****.**",
                        City          = "London",
                        Phone         = "01895808221"
                    }
                };
                var creditSession = client.Payment.CreateCreditSession(paymentSession).Result;
                ViewBag.ClientToken = creditSession.ClientToken;
                ViewBag.Payments    = creditSession.PaymentMethodCategories;
            }
            catch (AggregateException ae)
            {
                foreach (var e in ae.InnerExceptions)
                {
                    if (e is ApiException)
                    {
                        var apiException = (ApiException)e;
                        ViewBag.Error = "Status code: " + apiException.StatusCode + "; Error: "
                                        + string.Join("; ", apiException.ErrorMessage.ErrorMessages);
                    }
                    else
                    {
                        ViewBag.Error = e.Message;
                    }
                }
            }

            return(View());
        }
Exemple #5
0
        static void Main(string[] args)
        {
            var klarna = new Klarna.Rest.Core.Klarna("K501001_a0dccbb272e5", "eJxoWRXrpLQ0JcIp", KlarnaEnvironment.TestingEurope);

            var order = new Klarna.Rest.Core.Model.CheckoutOrder
            {
                PurchaseCountry  = "se",
                PurchaseCurrency = "sek",
                Locale           = "sv-se",
                OrderAmount      = 10000,
                OrderTaxAmount   = 3000,
                MerchantUrls     = new CheckoutMerchantUrls
                {
                    Terms        = "https://www.estore.com/terms.html",
                    Checkout     = "https://www.estore.com/checkout.html",
                    Confirmation = "https://www.estore.com/confirmation.html",
                    Push         = "https://www.estore.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,
                    }
                },
                CheckoutOptions = new CheckoutOptions
                {
                    AllowSeparateShippingAddress = false,
                    ColorButton            = "#FF9900",
                    ColorButtonText        = "#FF9900",
                    ColorCheckbox          = "#FF9900",
                    ColorCheckboxCheckmark = "#FF9900",
                    ColorHeader            = "#FF9900",
                    ColorLink            = "#FF9900",
                    DateOfBirthMandatory = false,
                    ShippingDetails      = "bar",
                }
            };

            try
            {
                var createdOrder = klarna.Checkout.CreateOrder(order).Result;
            }
            catch (ApiException ex)
            {
                Console.WriteLine(ex);
            }


            var fetchedOrder = klarna.Checkout.GetOrder("56fb8a38-1e83-4dd7-a89f-9f297e7e95eb").Result;

            fetchedOrder.OrderLines.Add(new OrderLine
            {
                Type                = OrderLineType.physical,
                Name                = "Foo2",
                Quantity            = 1,
                UnitPrice           = 10000,
                TaxRate             = 2500,
                TotalAmount         = 10000,
                TotalTaxAmount      = 2000,
                TotalDiscountAmount = 0,
            });
            fetchedOrder.OrderAmount    = 20000;
            fetchedOrder.OrderTaxAmount = 2000;

            var updatedOrder = klarna.Checkout.UpdateOrder(fetchedOrder).Result;
        }