Search code examples
javascriptjsonaxiosklarna

basic auth axios - klarna checkout


I am implementing Klarna Checkout in my webapp. Backend using aws serverless. Frontend: react. I am stucked at creating a order. It works fine inside postman but I can't get basic auth working axios.

For testing purposes I am posting orders from my react app. It will be moved to the backend once I get it working.

My axios code looks like this.

axios
  .post(
    "https://api.playground.klarna.com/checkout/v3/orders",
    {
      purchase_country: "DE",
      purchase_currency: "EUR",
      locale: "de-DE",
      order_amount: 3,
      order_tax_amount: 0,
      order_lines: [
        {
          type: "digital",
          reference: "Test",
          name: "Test Subscription",
          quantity: 1,
          quantity_unit: "pcs",
          unit_price: 3,
          tax_rate: 0,
          total_amount: 3,
          total_discount_amount: 0,
          total_tax_amount: 0
        }
      ],
      merchant_urls: {
        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/api/push"
      }
    },
    {
      auth: {
        username: "xxx",
        password: "xxx"
      },
      headers: {
        "Content-Type": "application/json"
      }
    }
  )
  .then(result => console.dir(result));

I get a 401 error at OPTIONS.

enter image description here

The response I'm getting.

enter image description here

I have tried it with postman, which works fine.


Solution

  • To summarize the comments: Klarna's API cannot be called from browser only from serverside. I called the same code from my aws lambda and it worked fine.