Skip to content

Magento 2 Klarna Error: ‘Order line totals do not total order_amount – 17311 != 17310’

When I add a configurable product to the map and go to the second checkout step and after loading it again I reload it, I get this error: ‘Order line totals do not total order_amount – 17311 != 17310’

This error is from the method validate that is in the file: vendor/klarna/module-kp/Model/Api/Request/Builder.php

Code:

    public function validate($requiredAttributes, $type)
{
    $missingAttributes = [];
    foreach ($requiredAttributes as $requiredAttribute) {
        if (null === $this->$requiredAttribute) {
            $missingAttributes[] = $requiredAttribute;
        }
        if (is_array($this->$requiredAttribute) && count($this->$requiredAttribute) === 0) {
            $missingAttributes[] = $requiredAttribute;
        }
    }
    if (!empty($missingAttributes)) {
        throw new KlarnaApiException(
            __(
                'Missing required attribute(s) on %1: "%2".',
                $type,
                implode(', ', $missingAttributes)
            )
        );
    }
    $total = 0;
    foreach ($this->orderlines as $orderLine) {
        $total += (int)$orderLine->getTotal();
    }

    if ($total !== $this->order_amount) {
        throw new KlarnaApiException(
            __('Order line totals do not total order_amount - %1 != %2', $total, $this->order_amount)
        );
    }

    return $this;
}

I get into the method validate from here vendor/klarna/module-kp/Model/Api/Builder/Kasper.php

Code:

    private function generateCreateUpdate()
{
    $requiredAttributes = [
        'purchase_country',
        'purchase_currency',
        'locale',
        'order_amount',
        'orderlines'
    ];

    /** @var MagentoQuoteModelQuote $quote */
    $quote = $this->getObject();
    $store = $quote->getStore();
    $options = array_map('trim', array_filter($this->configHelper->getCheckoutDesignConfig($store)));

    /**
     * Pre-fill customer details
     */
    $this->prefillAddresses($quote, $store);

    $address = $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();

    $tax = $address->getBaseTaxAmount();
    if ($this->configHelper->isFptEnabled($store) && !$this->configHelper->getDisplayInSubtotalFpt($store)) {
        $fptResult = $this->rate->getFptTax($quote);
        $tax += $fptResult['tax'];
    }

    $country = $address->getCountryId();
    if ($country === null) {
        $country = $this->directoryHelper->getDefaultCountry($store);
    }
    $this->requestBuilder->setPurchaseCountry($country)
        ->setPurchaseCurrency($quote->getBaseCurrencyCode())
        ->setLocale(str_replace('_', '-', $this->configHelper->getLocaleCode()))
        ->setOptions($options)
        ->setOrderAmount((int)$this->dataConverter->toApiFloat($address->getBaseGrandTotal()))
        ->addOrderlines($this->getOrderLines($quote->getStore()))
        ->setOrderTaxAmount((int)$this->dataConverter->toApiFloat($tax))
        ->setMerchantUrls($this->processMerchantUrls())
        ->validate($requiredAttributes, self::GENERATE_TYPE_CREATE);

    return $this;
}

My problem is that ($total !== $this->order_amount) = true
I always have a difference of 1.

But I can’t figure out why this is happening and how to fix it. Tell me, please, what could be the problem and how to fix it? If suddenly someone faced this behavior, how did you fix it?