diff --git a/src/Models/PaymentItem/PaymentItemContainer.php b/src/Models/PaymentItem/PaymentItemContainer.php index 14766bc..ff7336c 100644 --- a/src/Models/PaymentItem/PaymentItemContainer.php +++ b/src/Models/PaymentItem/PaymentItemContainer.php @@ -11,6 +11,8 @@ class PaymentItemContainer private bool $unreliable = false; + private bool $zeroPriceAllowed = false; + /** * Is true if container contains GenericPaymentItem, which means * some payment item type was not registered in PaymentItemContainerFactory @@ -26,6 +28,16 @@ public function setUnreliable(bool $unreliable = true): void $this->unreliable = $unreliable; } + public function isZeroPriceAllowed(): bool + { + return $this->zeroPriceAllowed; + } + + public function setZeroPriceAllowed(bool $zeroPriceAllowed = true): void + { + $this->zeroPriceAllowed = $zeroPriceAllowed; + } + public function addItem(PaymentItemInterface $item): self { $this->items[] = $item; diff --git a/src/Presenters/MethodsPresenter.php b/src/Presenters/MethodsPresenter.php index f78d772..92242e3 100644 --- a/src/Presenters/MethodsPresenter.php +++ b/src/Presenters/MethodsPresenter.php @@ -60,6 +60,7 @@ public function renderAdd(string $paymentGatewayCode, int $recurrentPaymentId = $paymentItemContainer = (new PaymentItemContainer())->addItem( new AuthorizationPaymentItem('authorization', $gateway->getAuthorizationAmount()) ); + $paymentItemContainer->setZeroPriceAllowed(); $countryResolution = $this->oneStopShop->resolveCountry( user: $userRow, diff --git a/src/Repositories/PaymentsRepository.php b/src/Repositories/PaymentsRepository.php index a5738e2..b4e0bea 100644 --- a/src/Repositories/PaymentsRepository.php +++ b/src/Repositories/PaymentsRepository.php @@ -142,8 +142,12 @@ final public function add( // It's not possible to generate payment amount based on payment items as postal fees of product module were // not refactored yet to separate payment item. Therefore custom "$amount" is still allowed. - if ($data['amount'] <= 0) { - throw new \Exception('attempt to create payment with zero or negative amount: ' . $data['amount']); + if ($data['amount'] < 0) { + throw new \Exception('attempt to create payment with negative amount: ' . $data['amount']); + } + + if ($data['amount'] === 0.0 && !$paymentItemContainer->isZeroPriceAllowed()) { + throw new \Exception('attempt to create payment with zero amount.'); } if ($subscriptionType) {