Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions Model/Total/Quote/BuckarooFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ class BuckarooFee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
protected $logging;

/**
* @param ConfigProviderAccount $configProviderAccount
* @param ConfigProviderAccount $configProviderAccount
* @param ConfigProviderBuckarooFee $configProviderBuckarooFee
* @param Factory $configProviderMethodFactory
* @param PriceCurrencyInterface $priceCurrency
* @param Data $catalogHelper
* @param Factory $configProviderMethodFactory
* @param PriceCurrencyInterface $priceCurrency
* @param Data $catalogHelper
* @param PaymentGroupTransaction $groupTransaction
* @param Log $logging
* @param TaxModelCalculation $taxCalculation
*/
public function __construct(
ConfigProviderAccount $configProviderAccount,
Expand Down Expand Up @@ -128,10 +131,13 @@ public function collect(

$orderId = $quote->getReservedOrderId();

// Check if already paid amount is affecting the calculation
if ($this->groupTransaction->getAlreadyPaid($orderId) > 0) {
return $this;
// Optionally, you could log or debug here to ensure no early returns affect the fee calculation.
$this->logging->addDebug('Already paid detected, but continuing to add fee.');
}

// Ensure payment method is set correctly
$paymentMethod = $quote->getPayment()->getMethod();
if (!$paymentMethod || strpos($paymentMethod, 'buckaroo_magento2_') !== 0) {
return $this;
Expand All @@ -142,9 +148,8 @@ public function collect(
return $this;
}

$basePaymentFeeOLD = $this->getBaseFee($methodInstance, $quote);
$basePaymentFee = $total->getBaseBuckarooFeeInclTax() - $total->getBuckarooFeeBaseTaxAmount();

// Calculate the base payment fee using the getBaseFee method
$basePaymentFee = $this->getBaseFee($methodInstance, $quote);
if ($basePaymentFee < 0.01) {
return $this;
}
Expand Down Expand Up @@ -176,7 +181,7 @@ public function collect(
/**
* @noinspection PhpUndefinedMethodInspection
*/
// $total->setGrandTotal($total->getGrandTotal() + $paymentFee);
$total->setGrandTotal($total->getGrandTotal() + $paymentFee);

return $this;
}
Expand Down
40 changes: 28 additions & 12 deletions Model/Total/Quote/BuckarooFeeHyva.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
*/
namespace Buckaroo\Magento2\Model\Total\Quote;

use Magento\Catalog\Helper\Data;
use Buckaroo\Magento2\Logging\Log;
use Buckaroo\Magento2\Helper\PaymentGroupTransaction;
use Buckaroo\Magento2\Logging\Log;
use Buckaroo\Magento2\Model\Config\Source\TaxClass\Calculation;
use Buckaroo\Magento2\Model\ConfigProvider\Account as ConfigProviderAccount;
use Buckaroo\Magento2\Model\ConfigProvider\BuckarooFee as ConfigProviderBuckarooFee;
use Buckaroo\Magento2\Model\ConfigProvider\Method\Factory;
use Buckaroo\Magento2\Service\HyvaCheckoutConfig;
use Magento\Catalog\Helper\Data;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address\Total;
use Magento\Tax\Model\Calculation as TaxModelCalculation;
use Buckaroo\Magento2\Model\ConfigProvider\Method\Factory;
use Buckaroo\Magento2\Model\Config\Source\TaxClass\Calculation;
use Buckaroo\Magento2\Model\ConfigProvider\Account as ConfigProviderAccount;
use Buckaroo\Magento2\Model\ConfigProvider\BuckarooFee as ConfigProviderBuckarooFee;

class BuckarooFeeHyva extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
Expand All @@ -55,7 +56,7 @@ class BuckarooFeeHyva extends \Magento\Quote\Model\Quote\Address\Total\AbstractT
public $catalogHelper;

/**
* @var \Buckaroo\Magento2\Helper\PaymentGroupTransaction
* @var PaymentGroupTransaction
*/
public $groupTransaction;

Expand All @@ -70,11 +71,20 @@ class BuckarooFeeHyva extends \Magento\Quote\Model\Quote\Address\Total\AbstractT
protected $logging;

/**
* @param ConfigProviderAccount $configProviderAccount
* @var HyvaCheckoutConfig
*/
protected $configProvider;

/**
* @param ConfigProviderAccount $configProviderAccount
* @param ConfigProviderBuckarooFee $configProviderBuckarooFee
* @param Factory $configProviderMethodFactory
* @param PriceCurrencyInterface $priceCurrency
* @param Data $catalogHelper
* @param Factory $configProviderMethodFactory
* @param PriceCurrencyInterface $priceCurrency
* @param Data $catalogHelper
* @param PaymentGroupTransaction $groupTransaction
* @param Log $logging
* @param TaxModelCalculation $taxCalculation
* @param HyvaCheckoutConfig $configProvider
*/
public function __construct(
ConfigProviderAccount $configProviderAccount,
Expand All @@ -84,7 +94,8 @@ public function __construct(
Data $catalogHelper,
PaymentGroupTransaction $groupTransaction,
Log $logging,
TaxModelCalculation $taxCalculation
TaxModelCalculation $taxCalculation,
HyvaCheckoutConfig $configProvider,
) {
$this->setCode('buckaroo_fee_hyva');

Expand All @@ -97,6 +108,7 @@ public function __construct(
$this->groupTransaction = $groupTransaction;
$this->logging = $logging;
$this->taxCalculation = $taxCalculation;
$this->configProvider = $configProvider;
}

/**
Expand All @@ -114,6 +126,10 @@ public function collect(
\Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
\Magento\Quote\Model\Quote\Address\Total $total
) {
if (!$this->configProvider->isHyvaCheckoutEnabled()) {
return $this;
}

parent::collect($quote, $shippingAssignment, $total);

// Ensure that shipping assignment has items, otherwise skip processing.
Expand Down
73 changes: 73 additions & 0 deletions Service/HyvaCheckoutConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Buckaroo\Magento2\Service;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Store\Model\ScopeInterface;

class HyvaCheckoutConfig
{
const HYVA_CHECKOUT_TYPE_PATH = 'hyva_themes_checkout/general/checkout';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var LayoutInterface
*/
private $layout;

/**
* HyvaCheckoutConfig constructor.
*
* @param ScopeConfigInterface $scopeConfig
* @param LayoutInterface $layout
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
LayoutInterface $layout
) {
$this->scopeConfig = $scopeConfig;
$this->layout = $layout;
}

/**
* Check if Hyvä checkout is enabled based on configuration and layout handles.
*
* @return bool
*/
public function isHyvaCheckoutEnabled(): bool
{
return $this->isCheckoutConfiguredAsHyva() && $this->isHyvaLayoutActive();
}

/**
* Check if the checkout type is set to Hyvä in the configuration.
*
* @return bool
*/
private function isCheckoutConfiguredAsHyva(): bool
{
$checkoutType = $this->scopeConfig->getValue(
self::HYVA_CHECKOUT_TYPE_PATH,
ScopeInterface::SCOPE_STORE
);

// Adjust this condition based on how Hyvä checkout is configured in your setup
return $checkoutType === 'default';
}

/**
* Check if Hyvä-specific layout handles are active in the current layout.
*
* @return bool
*/
private function isHyvaLayoutActive(): bool
{
// Check if known Hyvä layout handles are present in the current layout update
return in_array('hyva_checkout_index_index', $this->layout->getUpdate()->getHandles());
}
}
Loading