Bad Request from Creating a New Order with Klarna

797 Views Asked by At

I'm getting a Bad Request when I'm sending a POST request to the Klarna API to create a new order.

This is my code for sending the POST request:

Cart = new CartManager(_context, HttpContext.Session).GetCart();
Customer = new CustomerManager(HttpContext.Session).GetCustomer()
OrderViewModel order = new OrderViewModel();

order.Reference = DateTime.Now.ToOADate().ToString().Replace(",", string.Empty);
order.Cart = Cart;
order.Customer = Customer;

string url = ApiHelper.KlarnaApiClient.BaseAddress + "checkout/v3/orders";

KlarnaOrderModel klarnaOrderModel = new KlarnaOrderModel
{
    purchase_currency = "SEK",
    order_amount = (int)order.Cart.TotalCharge,
    order_lines = klarnaOrderLines
};

HttpResponseMessage response = await ApiHelper.KlarnaApiClient.PostAsJsonAsync(
        url, klarnaOrderModel);

response.EnsureSuccessStatusCode();

KlarnaOrderModel:

public class KlarnaOrderModel
{
public string purchase_country { get { return "SE"; } }
public string purchase_currency { get; set; }
public string locale { get { return "en-GB"; } }
public int order_amount { get; set; }
public int order_tax_amount { get { return 2500; } }
public List<KlarnaOrderLine> order_lines { get; set; }
public KlarnaMerchantUrls merchant_urls { get { return new Models.KlarnaMerchantUrls(); } }
}

KlarnaOrderLine:

public class KlarnaOrderLine
{
public string name { get; set; }
public int quantity { get; set; }
public int unit_price { get; set; }
public int tax_rate { get { return 2500; } }
public int total_amount { get { return unit_price * quantity; } }
public int total_tax_amount { get { return total_amount / 5 ; } }
}

KlarnaMerchantUrls:

public class KlarnaMerchantUrls
{
public string terms { get { return "https://localhost:44316/shop/terms"; } }
public string checkout { get { return "https://localhost:44316/shop/checkout"; } }
public string confirmation { get { return "https://localhost:44316/shop/checkout/confirmation"; } 
public string push { get { return "https://localhost:44316/shop/push"; } }
}

Here is a screenshot:

enter image description here

My code for initializing the API:

KlarnaApiClient = new HttpClient();

KlarnaApiClient.BaseAddress = new Uri("https://api.playground.klarna.com/");

KlarnaApiClient.DefaultRequestHeaders.Accept.Clear();
KlarnaApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
KlarnaApiClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"{MY KLARNA API KEY UID}:{MY KLARNA API KEY PASSWORD}")));
0

There are 0 best solutions below