1

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 \Magento\Quote\Model\Quote $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?

5
  • This is really strange logic convert each line item to int. Imagine A = 0.01 and B = 0.99. Total = 1.00, (int)0.01 + (int)0.99 = 0. Total with this logic can be different from Magento Total. Commented Aug 13, 2022 at 20:17
  • Did you find a solution to this?
    – codedge
    Commented Sep 29, 2022 at 14:19
  • Yes, I found. Today I don't have free time to describe what I did, but I can do it tomorrow morning @codedge Commented Sep 29, 2022 at 15:23
  • That would be great. Thanks @DavidYoung
    – codedge
    Commented Sep 29, 2022 at 19:50
  • @codedge you can check out my answer to this problem, I didn't describe it in code snippets because for me this is an exceptional problem that hasn't happened anywhere else. Commented Sep 30, 2022 at 12:25

3 Answers 3

0

I found a solution to this problem.

In my case, this was due to the fact that I have a very strong modification of the current types of products: configurable and bundle, in particular.

Because of these modifications, I was getting this behavior because the pricing was changed and the price was affected by the logic that applies to my modified products.

If you do not have modified products, there are no custom product types, then my case will not suit you, but it can lead to the right solution in search of a problem.

And so, I found the problem in the class: Klarna\Core\Model\Checkout\Orderline\Items, methods: collect and processItem, shouldSkip.

  1. In the method collect i had a problem that shouldSkip missed the products needed for pricing, therefore, I had to modify this method for the changed logic.
  2. In the method processItem I had to change the logic that relates to getting data like: total_amount, unit_price
  3. You need to check that in the processItem everything was correct so that the products that need to be skipped for pricing are skipped.

After these changes, checking in the file no longer gave an error: vendor/klarna/module-kp/Model/Api/Request/Builder.php

I want to reiterate, my case is an exception, I don't claim that this is the right way to fix the problem, but it worked for me.

2
  • Seems like a very special case, but thanks for explaining.
    – codedge
    Commented Sep 30, 2022 at 16:19
  • Yes it is @codedge Commented Oct 3, 2022 at 11:37
0

Klarna\Core\Model\Checkout\Orderline\Items;

private function processItem($product, $item, $qtyMultiplier)

Modify the processItem function

private function processItem($product, $item, $qtyMultiplier)
    {
 .....

    if ($this->klarnaConfig->isSeparateTaxLine($item->getStore())) {
      ......
    } else {
       .......
          $_item['total_amount'] = $this->helper->toApiFloat(
           ($item->getBaseRowTotal() - $item->getBaseDiscountAmount()) + $item->getBaseTaxAmount()
        );
        }
}
0

./bin/magento setup:config:set --deferred-total-calculating 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.