Skip to content

Commit b9ff550

Browse files
Activate the payment fee for all Mollie payment methods
1 parent 8b4a62d commit b9ff550

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2085
-459
lines changed

Config.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ class Config
1414
const GENERAL_INVOICE_NOTIFY = 'payment/mollie_general/invoice_notify';
1515
const XML_PATH_STATUS_PENDING_BANKTRANSFER = 'payment/mollie_methods_banktransfer/order_status_pending';
1616
const XML_PATH_STATUS_NEW_PAYMENT_LINK = 'payment/mollie_methods_paymentlink/order_status_new';
17-
const PAYMENT_KLARNAPAYLATER_PAYMENT_SURCHARGE = 'payment/mollie_methods_klarnapaylater/payment_surcharge';
18-
const PAYMENT_KLARNAPAYLATER_PAYMENT_SURCHARGE_TAX_CLASS = 'payment/mollie_methods_klarnapaylater/payment_surcharge_tax_class';
19-
const PAYMENT_KLARNASLICEIT_PAYMENT_SURCHARGE = 'payment/mollie_methods_klarnasliceit/payment_surcharge';
20-
const PAYMENT_KLARNASLICEIT_PAYMENT_SURCHARGE_TAX_CLASS = 'payment/mollie_methods_klarnasliceit/payment_surcharge_tax_class';
17+
const PAYMENT_METHOD_PAYMENT_SURCHARGE_TYPE = 'payment/mollie_methods_%s/payment_surcharge_type';
18+
const PAYMENT_METHOD_PAYMENT_SURCHARGE_FIXED_AMOUNT = 'payment/mollie_methods_%s/payment_surcharge_fixed_amount';
19+
const PAYMENT_METHOD_PAYMENT_SURCHARGE_PERCENTAGE = 'payment/mollie_methods_%s/payment_surcharge_percentage';
20+
const PAYMENT_METHOD_PAYMENT_SURCHARGE_LIMIT = 'payment/mollie_methods_%s/payment_surcharge_limit';
21+
const PAYMENT_METHOD_PAYMENT_SURCHARGE_TAX_CLASS = 'payment/mollie_methods_%s/payment_surcharge_tax_class';
2122
const PAYMENT_PAYMENTLINK_ALLOW_MARK_AS_PAID = 'payment/mollie_methods_paymentlink/allow_mark_as_paid';
2223

2324
/**
@@ -79,39 +80,53 @@ public function statusNewPaymentLink($storeId = null)
7980
}
8081

8182
/**
83+
* @param string $method
8284
* @param int|null $storeId
8385
* @return string
8486
*/
85-
public function klarnaPaylaterPaymentSurcharge($storeId = null)
87+
public function paymentSurchargeType($method, $storeId = null)
8688
{
87-
return $this->getPath(static::PAYMENT_KLARNAPAYLATER_PAYMENT_SURCHARGE, $storeId);
89+
return $this->getPath($this->addMethodToPath(static::PAYMENT_METHOD_PAYMENT_SURCHARGE_TYPE, $method), $storeId);
8890
}
8991

9092
/**
93+
* @param string $method
9194
* @param int|null $storeId
9295
* @return string
9396
*/
94-
public function klarnaPaylaterPaymentSurchargeTaxClass($storeId = null)
97+
public function paymentSurchargeFixedAmount($method, $storeId = null)
9598
{
96-
return $this->getPath(static::PAYMENT_KLARNAPAYLATER_PAYMENT_SURCHARGE_TAX_CLASS, $storeId);
99+
return $this->getPath($this->addMethodToPath(static::PAYMENT_METHOD_PAYMENT_SURCHARGE_FIXED_AMOUNT, $method), $storeId);
97100
}
98101

99102
/**
103+
* @param string $method
100104
* @param int|null $storeId
101105
* @return string
102106
*/
103-
public function klarnaSliceitPaymentSurcharge($storeId = null)
107+
public function paymentSurchargePercentage($method, $storeId = null)
104108
{
105-
return $this->getPath(static::PAYMENT_KLARNASLICEIT_PAYMENT_SURCHARGE, $storeId);
109+
return $this->getPath($this->addMethodToPath(static::PAYMENT_METHOD_PAYMENT_SURCHARGE_PERCENTAGE, $method), $storeId);
106110
}
107111

108112
/**
113+
* @param string $method
109114
* @param int|null $storeId
110115
* @return string
111116
*/
112-
public function klarnaSliceitPaymentSurchargeTaxClass($storeId = null)
117+
public function paymentSurchargeLimit($method, $storeId = null)
113118
{
114-
return $this->getPath(static::PAYMENT_KLARNASLICEIT_PAYMENT_SURCHARGE_TAX_CLASS, $storeId);
119+
return $this->getPath($this->addMethodToPath(static::PAYMENT_METHOD_PAYMENT_SURCHARGE_LIMIT, $method), $storeId);
120+
}
121+
122+
/**
123+
* @param string $method
124+
* @param int|null $storeId
125+
* @return string
126+
*/
127+
public function paymentSurchargeTaxClass($method, $storeId = null)
128+
{
129+
return $this->getPath($this->addMethodToPath(static::PAYMENT_METHOD_PAYMENT_SURCHARGE_TAX_CLASS, $method), $storeId);
115130
}
116131

117132
/**
@@ -122,4 +137,16 @@ public function paymentlinkAllowMarkAsPaid($storeId = null)
122137
{
123138
return $this->getFlag(static::PAYMENT_PAYMENTLINK_ALLOW_MARK_AS_PAID, $storeId);
124139
}
140+
141+
/**
142+
* @param $method
143+
* @return string
144+
*/
145+
private function addMethodToPath($path, $method): string
146+
{
147+
return sprintf(
148+
$path,
149+
str_replace('mollie_methods_', '', $method)
150+
);
151+
}
125152
}

Exceptions/UnknownPaymentFeeType.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* Copyright Magmodules.eu. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Mollie\Payment\Exceptions;
8+
9+
class UnknownPaymentFeeType extends \Exception
10+
{
11+
12+
}

Model/Adminhtml/Backend/VerifiyPaymentFee.php

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,9 @@
1818

1919
class VerifiyPaymentFee extends Value
2020
{
21-
const MAXIMUM_PAYMENT_FEE_AMOUNT = 1.95;
22-
23-
/**
24-
* @var PriceCurrencyInterface
25-
*/
26-
private $priceCurrency;
27-
28-
public function __construct(
29-
Context $context,
30-
Registry $registry,
31-
ScopeConfigInterface $config,
32-
TypeListInterface $cacheTypeList,
33-
PriceCurrencyInterface $priceCurrency,
34-
AbstractResource $resource = null,
35-
AbstractDb $resourceCollection = null,
36-
array $data = []
37-
) {
38-
parent::__construct(
39-
$context,
40-
$registry,
41-
$config,
42-
$cacheTypeList,
43-
$resource,
44-
$resourceCollection,
45-
$data
46-
);
47-
48-
$this->priceCurrency = $priceCurrency;
49-
}
50-
5121
public function beforeSave()
5222
{
5323
$value = $this->getValue();
54-
$this->setValue(str_replace(',', '.', $value));
55-
56-
if ((double)$value > static::MAXIMUM_PAYMENT_FEE_AMOUNT) {
57-
$message = __(
58-
'Please make sure the payment surcharge does not exceed %1.',
59-
$this->priceCurrency->format(static::MAXIMUM_PAYMENT_FEE_AMOUNT)
60-
);
61-
62-
throw new ValidatorException($message);
63-
}
24+
$this->setValue(str_replace([',', '%'], ['.', ''], $value));
6425
}
6526
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright Magmodules.eu. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Mollie\Payment\Model\Adminhtml\Source;
8+
9+
use Magento\Framework\Data\OptionSourceInterface;
10+
11+
class PaymentFeeType implements OptionSourceInterface
12+
{
13+
const DISABLED = '';
14+
const PERCENTAGE = 'percentage';
15+
const FIXED_FEE = 'fixed_fee';
16+
const FIXED_FEE_AND_PERCENTAGE = 'fixed_fee_and_percentage';
17+
18+
/**
19+
* @inheritDoc
20+
*/
21+
public function toOptionArray()
22+
{
23+
return [
24+
[
25+
'value' => static::DISABLED,
26+
'label' => __('No'),
27+
],
28+
[
29+
'value' => static::PERCENTAGE,
30+
'label' => __('Percentage'),
31+
],
32+
[
33+
'value' => static::FIXED_FEE,
34+
'label' => __('Fixed Fee'),
35+
],
36+
[
37+
'value' => static::FIXED_FEE_AND_PERCENTAGE,
38+
'label' => __('Fixed Fee and Percentage'),
39+
],
40+
];
41+
}
42+
}

Model/PaymentFee/Quote/Address/Total/PaymentFee.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Quote\Model\Quote\Address\Total;
1313
use Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
1414
use Mollie\Payment\Service\Config\PaymentFee as PaymentFeeConfig;
15+
use Mollie\Payment\Service\PaymentFee\Calculate;
1516

1617
class PaymentFee extends AbstractTotal
1718
{
@@ -25,12 +26,19 @@ class PaymentFee extends AbstractTotal
2526
*/
2627
private $priceCurrency;
2728

29+
/**
30+
* @var Calculate
31+
*/
32+
private $calculate;
33+
2834
public function __construct(
2935
PaymentFeeConfig $paymentFeeConfig,
30-
PriceCurrencyInterface $priceCurrency
36+
PriceCurrencyInterface $priceCurrency,
37+
Calculate $calculate
3138
) {
3239
$this->paymentFeeConfig = $paymentFeeConfig;
3340
$this->priceCurrency = $priceCurrency;
41+
$this->calculate = $calculate;
3442
}
3543

3644
/**
@@ -43,15 +51,15 @@ public function collect(Quote $quote, ShippingAssignmentInterface $shippingAssig
4351
{
4452
parent::collect($quote, $shippingAssignment, $total);
4553

46-
if (!$shippingAssignment->getItems() || !$this->paymentFeeConfig->isAvailableForMethod($quote)) {
54+
if (!$shippingAssignment->getItems()) {
4755
return $this;
4856
}
4957

50-
$baseAmount = $this->paymentFeeConfig->excludingTax($quote);
51-
$amount = $this->priceCurrency->convert($baseAmount);
58+
$result = $this->calculate->forCart($quote, $total);
59+
$amount = $this->priceCurrency->convert($result->getAmount());
5260

5361
$total->setTotalAmount('mollie_payment_fee', $amount);
54-
$total->setBaseTotalAmount('mollie_payment_fee', $baseAmount);
62+
$total->setBaseTotalAmount('mollie_payment_fee', $result->getAmount());
5563

5664
$attributes = $quote->getExtensionAttributes();
5765

@@ -60,7 +68,7 @@ public function collect(Quote $quote, ShippingAssignmentInterface $shippingAssig
6068
}
6169

6270
$attributes->setMolliePaymentFee($amount);
63-
$attributes->setBaseMolliePaymentFee($amount);
71+
$attributes->setBaseMolliePaymentFee($result->getAmount());
6472

6573
return $this;
6674
}
@@ -72,14 +80,16 @@ public function collect(Quote $quote, ShippingAssignmentInterface $shippingAssig
7280
*/
7381
public function fetch(Quote $quote, Total $total)
7482
{
75-
if (!$this->paymentFeeConfig->isAvailableForMethod($quote)) {
83+
if (!$this->paymentFeeConfig->isAvailableForMethod($quote) || !$quote->getExtensionAttributes()) {
7684
return [];
7785
}
7886

87+
$extensionAttributes = $quote->getExtensionAttributes();
88+
7989
return [
8090
'code' => 'mollie_payment_fee',
8191
'title' => __('Payment Fee'),
82-
'value' => $this->paymentFeeConfig->includingTax($quote),
92+
'value' => $extensionAttributes->getMolliePaymentFee() + $extensionAttributes->getMolliePaymentFeeTax(),
8393
];
8494
}
8595

Model/PaymentFee/Quote/Address/Total/PaymentFeeTax.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Quote\Model\Quote\Address\Total;
1313
use Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
1414
use Mollie\Payment\Service\Config\PaymentFee as PaymentFeeConfig;
15+
use Mollie\Payment\Service\PaymentFee\Calculate;
1516

1617
class PaymentFeeTax extends AbstractTotal
1718
{
@@ -25,12 +26,19 @@ class PaymentFeeTax extends AbstractTotal
2526
*/
2627
private $priceCurrency;
2728

29+
/**
30+
* @var Calculate
31+
*/
32+
private $calculate;
33+
2834
public function __construct(
2935
PaymentFeeConfig $paymentFeeConfig,
30-
PriceCurrencyInterface $priceCurrency
36+
PriceCurrencyInterface $priceCurrency,
37+
Calculate $calculate
3138
) {
3239
$this->paymentFeeConfig = $paymentFeeConfig;
3340
$this->priceCurrency = $priceCurrency;
41+
$this->calculate = $calculate;
3442
}
3543

3644
/**
@@ -43,15 +51,15 @@ public function collect(Quote $quote, ShippingAssignmentInterface $shippingAssig
4351
{
4452
parent::collect($quote, $shippingAssignment, $total);
4553

46-
if (!$shippingAssignment->getItems() || !$this->paymentFeeConfig->isAvailableForMethod($quote)) {
54+
if (!$shippingAssignment->getItems()) {
4755
return $this;
4856
}
4957

50-
$baseAmount = $this->paymentFeeConfig->tax($quote);
51-
$amount = $this->priceCurrency->convert($baseAmount);
58+
$result = $this->calculate->forCart($quote, $total);
59+
$amount = $this->priceCurrency->convert($result->getTaxAmount());
5260

5361
$total->addTotalAmount('tax', $amount);
54-
$total->addBaseTotalAmount('tax', $baseAmount);
62+
$total->addBaseTotalAmount('tax', $result->getTaxAmount());
5563

5664
$extensionAttributes = $quote->getExtensionAttributes();
5765

@@ -60,7 +68,7 @@ public function collect(Quote $quote, ShippingAssignmentInterface $shippingAssig
6068
}
6169

6270
$extensionAttributes->setMolliePaymentFeeTax($amount);
63-
$extensionAttributes->setBaseMolliePaymentFeeTax($baseAmount);
71+
$extensionAttributes->setBaseMolliePaymentFeeTax($result->getTaxAmount());
6472

6573
return $this;
6674
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright Magmodules.eu. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Mollie\Payment\Observer\SalesQuoteSaveBefore;
8+
9+
10+
use Magento\Framework\Event\Observer;
11+
use Magento\Framework\Event\ObserverInterface;
12+
use Magento\Quote\Api\Data\CartInterface;
13+
14+
class SetPaymentFeeDataOnQuote implements ObserverInterface
15+
{
16+
/**
17+
* @inheritDoc
18+
*/
19+
public function execute(Observer $observer)
20+
{
21+
/** @var CartInterface $quote */
22+
$quote = $observer->getData('quote');
23+
24+
$extensionAttributes = $quote->getExtensionAttributes();
25+
if (!$extensionAttributes) {
26+
return;
27+
}
28+
29+
$quote->setData('mollie_payment_fee', $extensionAttributes->getMolliePaymentFee());
30+
$quote->setData('base_mollie_payment_fee', $extensionAttributes->getBaseMolliePaymentFee());
31+
$quote->setData('mollie_payment_fee_tax', $extensionAttributes->getMolliePaymentFeeTax());
32+
$quote->setData('base_mollie_payment_fee_tax', $extensionAttributes->getBaseMolliePaymentFeeTax());
33+
}
34+
}

0 commit comments

Comments
 (0)