Magento 2 - Magento additional costs

For plugin versions 1.1.17 and higher.

In order to avoid discrepancies between the total order amount calculated based on the list of items in the cart and the total order amount sent in the field gross_amount_cents, caused most of the time by additional costs associated with the orders, the merchant needs to include these costs in the create order request. To do that our plugin provides an interface that can be mapped to custom implementation by the merchant.

<?php

namespace Mondu\Mondu\Helpers\AdditionalCosts;

use Magento\Quote\Model\Quote;

/**
 *     Map this interface onto your custom class if you have additional costs attached to payment methods ( in di.xml )
 *     also make sure your module is loaded after Mondu
 *     <preference for="Mondu\Mondu\Helpers\AdditionalCosts\AdditionalCostsInterface"
 *                 type="My\Module\Mondu\AdditionalCosts" />
 */

interface AdditionalCostsInterface
{
    /**
     * Returns additional costs associated with quote
     *
     * @param Quote $quote
     * @return int
     */
    public function getAdditionalCostsFromQuote(Quote $quote): int;
}

In order to tell our plugin to use a custom implementation of this interface the merchant needs to add a preference in their code ( di.xml ). Here My\Module is the namespace of merchants custom code.

<preference for="Mondu\Mondu\Helpers\AdditionalCosts\AdditionalCostsInterface"
                  type="My\Module\Mondu\AdditionalCosts" />

This is our plugin's implementation of AdditionalCosts, the merchant can use it as a template.

<?php

namespace Mondu\Mondu\Helpers\AdditionalCosts;

use Magento\Quote\Model\Quote;

class AdditionalCosts implements AdditionalCostsInterface
{
    /**
     * Returns additional costs associated with quote
     *
     * @param Quote $quote
     * @return int
     */
    public function getAdditionalCostsFromQuote(Quote $quote): int
    {
        if ($quote->getPaymentSurchargeAmount()) {
            return round($quote->getPaymentSurchargeAmount(), 2) ?? 0;
        }
        return 0;
    }
}

Please note that getAdditionalCostsFromQuote function should return the value in cents.