Skip to content

Commit

Permalink
Add option to allow zero total price in payment container
Browse files Browse the repository at this point in the history
remp/crm#3300
  • Loading branch information
Matus Kalafut committed Sep 19, 2024
1 parent 302520c commit 47868f5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/Models/PaymentItem/PaymentItemContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Presenters/MethodsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions src/Repositories/PaymentsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 47868f5

Please sign in to comment.