Search code examples
phpshopping-cart

PHP Klarna checkout item price issue


I am not very good at programming, but now i am having a learning project for testing a simple php cart

This >> http://www.sanwebe.com/2013/06/creating-simple-shopping-cart-with-php/comment-page-1

And implementing klarna checkout with it.

This klarna >> https://developers.klarna.com/en/se+php/kco-v2/checkout-api

I get it to "work", but not to pass the prices to klarna.

It seems to be something wrong with the format of the price value?

Example: i put something in the basket worth 200.50 USD.

Since klarna uses values without dots and commas i used the following code to clear the $total value.

$extotal = number_format("$total","2","","");

And if i print the value out like this

echo '<strong>Total : '.$extotal.'</strong>  ';

It prints this value 20050 and the original price is 200.50

But when i simply use this value $extotal in klarnas php code like below it dosen't work.

$cart = array(
array(
    'reference' => '123456789',
    'name' => 'Klarna t-shirt',
    'quantity' => 1,
    'unit_price' => $extotal,
    'tax_rate' => 2500
),
array(
    'type' => 'shipping_fee',
    'reference' => 'SHIPPING',
    'name' => 'Shipping Fee',
    'quantity' => 1,
    'unit_price' => 1000,
    'tax_rate' => 2500
));

I get the following message in klarna checkout DIV

string(9) "API Error" array(3) { ["http_status_code"]=> int(400) ["http_status_message"]=> string(11) "Bad Request" ["internal_message"]=> string(32) "Bad format: cart.item.unit_price" }

Can someone give some guidance pls =)


Solution

  • Thanks to Marc B, i got it to work!

    $int = (int)$extotal;