Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

Fix fee calculations for free tickets (#500)

Dave Earley855f7a7c205d
diff --git a/backend/app/DomainObjects/OrderDomainObject.php b/backend/app/DomainObjects/OrderDomainObject.php
index 5782dd2af..528e4338f 100644
--- a/backend/app/DomainObjects/OrderDomainObject.php
+++ b/backend/app/DomainObjects/OrderDomainObject.php
@@ -11,6 +11,7 @@ use HiEvents\DomainObjects\Status\OrderStatus;
use HiEvents\Helper\AddressHelper;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
+use RuntimeException;
class OrderDomainObject extends Generated\OrderDomainObjectAbstract implements IsSortable, IsFilterable
{
@@ -229,6 +230,15 @@ class OrderDomainObject extends Generated\OrderDomainObjectAbstract implements I
return $this;
}
+ public function getTotalQuantity(): int
+ {
+ if ($this->getOrderItems() === null) {
+ throw new RuntimeException('Cannot calculate total quantity, order items are null');
+ }
+
+ return $this->getOrderItems()->sum(fn(OrderItemDomainObject $item) => $item->getQuantity());
+ }
+
public function getQuestionAndAnswerViews(): ?Collection
{
return $this->questionAndAnswerViews;
diff --git a/backend/app/Services/Domain/Order/MarkOrderAsPaidService.php b/backend/app/Services/Domain/Order/MarkOrderAsPaidService.php
index d9fd58882..bf7c5ac82 100644
--- a/backend/app/Services/Domain/Order/MarkOrderAsPaidService.php
+++ b/backend/app/Services/Domain/Order/MarkOrderAsPaidService.php
@@ -81,7 +81,9 @@ class MarkOrderAsPaidService
$this->updateOrderInvoice($orderId);
- $updatedOrder = $this->orderRepository->findById($orderId);
+ $updatedOrder = $this->orderRepository
+ ->loadRelation(OrderItemDomainObject::class)
+ ->findById($orderId);
$this->updateAttendeeStatuses($updatedOrder);
@@ -168,9 +170,8 @@ class MarkOrderAsPaidService
$this->orderApplicationFeeService->createOrderApplicationFee(
orderId: $updatedOrder->getId(),
applicationFeeAmountMinorUnit: $this->orderApplicationFeeCalculationService->calculateApplicationFee(
- $config,
- $updatedOrder->getTotalGross(),
- $event->getCurrency(),
+ accountConfiguration: $config,
+ order: $updatedOrder,
)->toMinorUnit(),
orderApplicationFeeStatus: OrderApplicationFeeStatus::AWAITING_PAYMENT,
paymentMethod: PaymentProviders::OFFLINE,
diff --git a/backend/app/Services/Domain/Order/OrderApplicationFeeCalculationService.php b/backend/app/Services/Domain/Order/OrderApplicationFeeCalculationService.php
index fa9371f23..b744e0eb1 100644
--- a/backend/app/Services/Domain/Order/OrderApplicationFeeCalculationService.php
+++ b/backend/app/Services/Domain/Order/OrderApplicationFeeCalculationService.php
@@ -4,6 +4,7 @@ namespace HiEvents\Services\Domain\Order;
use Brick\Money\Currency;
use HiEvents\DomainObjects\AccountConfigurationDomainObject;
+use HiEvents\DomainObjects\OrderDomainObject;
use HiEvents\Services\Infrastructure\CurrencyConversion\CurrencyConversionClientInterface;
use HiEvents\Values\MoneyValue;
use Illuminate\Config\Repository;
@@ -21,10 +22,12 @@ class OrderApplicationFeeCalculationService
public function calculateApplicationFee(
AccountConfigurationDomainObject $accountConfiguration,
- float $orderTotal,
- string $currency
+ OrderDomainObject $order,
): MoneyValue
{
+ $currency = $order->getCurrency();
+ $quantityPurchased = $this->getChargeableQuantityPurchased($order);
+
if (!$this->config->get('app.saas_mode_enabled')) {
return MoneyValue::zero($currency);
}
@@ -33,7 +36,7 @@ class OrderApplicationFeeCalculationService
$percentageFee = $accountConfiguration->getPercentageApplicationFee();
return MoneyValue::fromFloat(
- amount: $fixedFee->toFloat() + ($orderTotal * $percentageFee / 100),
+ amount: ($fixedFee->toFloat() * $quantityPurchased) + ($order->getTotalGross() * $percentageFee / 100),
currency: $currency
);
}
@@ -53,4 +56,16 @@ class OrderApplicationFeeCalculationService
amount: $accountConfiguration->getFixedApplicationFee()
);
}
+
+ private function getChargeableQuantityPurchased(OrderDomainObject $order): int
+ {
+ $quantityPurchased = 0;
+ foreach ($order->getOrderItems() as $item) {
+ if ($item->getPrice() > 0) {
+ $quantityPurchased += $item->getQuantity();
+ }
+ }
+
+ return $quantityPurchased;
+ }
}
diff --git a/backend/app/Services/Domain/Payment/Stripe/StripePaymentIntentCreationService.php b/backend/app/Services/Domain/Payment/Stripe/StripePaymentIntentCreationService.php
index effef6647..fc0e25366 100644
--- a/backend/app/Services/Domain/Payment/Stripe/StripePaymentIntentCreationService.php
+++ b/backend/app/Services/Domain/Payment/Stripe/StripePaymentIntentCreationService.php
@@ -64,8 +64,7 @@ class StripePaymentIntentCreationService
$applicationFee = $this->orderApplicationFeeCalculationService->calculateApplicationFee(
accountConfiguration: $paymentIntentDTO->account->getConfiguration(),
- orderTotal: $paymentIntentDTO->amount->toFloat(),
- currency: $paymentIntentDTO->currencyCode,
+ order: $paymentIntentDTO->order,
)->toMinorUnit();
$paymentIntent = $this->stripeClient->paymentIntents->create([
diff --git a/backend/tests/Unit/Services/Domain/Order/OrderApplicationFeeCalculationServiceTest.php b/backend/tests/Unit/Services/Domain/Order/OrderApplicationFeeCalculationServiceTest.php
new file mode 100644
index 000000000..3f20cd7a5
--- /dev/null
+++ b/backend/tests/Unit/Services/Domain/Order/OrderApplicationFeeCalculationServiceTest.php
@@ -0,0 +1,126 @@
+<?php
+
+namespace Tests\Unit\Services\Domain\Order;
+
+use HiEvents\DomainObjects\AccountConfigurationDomainObject;
+use HiEvents\DomainObjects\OrderDomainObject;
+use HiEvents\DomainObjects\OrderItemDomainObject;
+use HiEvents\Services\Domain\Order\OrderApplicationFeeCalculationService;
+use HiEvents\Services\Infrastructure\CurrencyConversion\CurrencyConversionClientInterface;
+use HiEvents\Values\MoneyValue;
+use Illuminate\Config\Repository;
+use PHPUnit\Framework\TestCase;
+
+class OrderApplicationFeeCalculationServiceTest extends TestCase
+{
+ private Repository $config;
+ private CurrencyConversionClientInterface $currencyConversionClient;
+ private OrderApplicationFeeCalculationService $service;
+
+ protected function setUp(): void
+ {
+ $this->config = $this->createMock(Repository::class);
+ $this->currencyConversionClient = $this->createMock(CurrencyConversionClientInterface::class);
+ $this->service = new OrderApplicationFeeCalculationService($this->config, $this->currencyConversionClient);
+ }
+
+ private function createOrderWithItems(array $items, string $currency = 'USD'): OrderDomainObject
+ {
+ $order = (new OrderDomainObject())
+ ->setCurrency($currency)
+ ->setOrderItems(collect($items));
+
+ // Calculate gross manually for test accuracy
+ $total = collect($items)->reduce(fn($carry, $item) => $carry + ($item->getPrice() * $item->getQuantity()), 0);
+ $order->setTotalGross($total);
+
+ return $order;
+ }
+
+ private function createItem(float $price, int $quantity): OrderItemDomainObject
+ {
+ $item = $this->createMock(OrderItemDomainObject::class);
+ $item->method('getPrice')->willReturn($price);
+ $item->method('getQuantity')->willReturn($quantity);
+ return $item;
+ }
+
+ private function createAccountConfig(float $fixedFee = 0, float $percentageFee = 0): AccountConfigurationDomainObject
+ {
+ $config = $this->getMockBuilder(AccountConfigurationDomainObject::class)
+ ->disableOriginalConstructor()
+ ->onlyMethods(['getFixedApplicationFee', 'getPercentageApplicationFee'])
+ ->getMock();
+
+ $config->method('getFixedApplicationFee')->willReturn($fixedFee);
+ $config->method('getPercentageApplicationFee')->willReturn($percentageFee);
+
+ return $config;
+ }
+
+ public function testNoFeeWhenSaasModeDisabled(): void
+ {
+ $this->config->method('get')->with('app.saas_mode_enabled')->willReturn(false);
+
+ $order = $this->createOrderWithItems([$this->createItem(10, 2)]);
+ $account = $this->createAccountConfig(1, 1);
+
+ $fee = $this->service->calculateApplicationFee($account, $order);
+
+ $this->assertEquals(0.0, $fee->toFloat());
+ }
+
+ public function testNoFeeForFreeOrder(): void
+ {
+ $this->config->method('get')->willReturn(true);
+
+ $order = $this->createOrderWithItems([$this->createItem(0, 3)]);
+ $account = $this->createAccountConfig(1, 1);
+
+ $fee = $this->service->calculateApplicationFee($account, $order);
+
+ $this->assertEquals(0.0, $fee->toFloat());
+ }
+
+ public function testFixedAndPercentageFeeSameCurrency(): void
+ {
+ $this->config->method('get')->willReturn(true);
+
+ $order = $this->createOrderWithItems([
+ $this->createItem(10, 1), // chargeable
+ $this->createItem(0, 5), // free
+ $this->createItem(20, 2), // chargeable
+ ]);
+
+ $account = $this->createAccountConfig(2.00, 1); // 2 USD fixed, 1% percentage
+
+ // 3 chargeable items × $2 fixed = $6
+ // $10 + $40 = $50 gross → 1% of $50 = $0.50
+ // Total = $6.50
+ $fee = $this->service->calculateApplicationFee($account, $order);
+
+ $this->assertEquals(6.50, $fee->toFloat());
+ }
+
+ public function testCurrencyConversionForFixedFee(): void
+ {
+ $this->config->method('get')->willReturn(true);
+
+ $order = $this->createOrderWithItems([
+ $this->createItem(15, 1), // chargeable
+ $this->createItem(0, 3), // free
+ ], 'EUR');
+
+ $account = $this->createAccountConfig(1.50, 20);
+
+ $this->currencyConversionClient->method('convert')
+ ->willReturn(MoneyValue::fromFloat(2.00, 'EUR')); // fixed fee per chargeable = €2.00
+
+ // 1 chargeable × €2 fixed = €2
+ // 15 × 20% = €3 percentage
+ // Total = €5
+ $fee = $this->service->calculateApplicationFee($account, $order);
+
+ $this->assertEquals(5.00, $fee->toFloat());
+ }
+}
diff --git a/docker/development/.env b/docker/development/.env
index 3245088b7..9d0a40931 100644
--- a/docker/development/.env
+++ b/docker/development/.env
@@ -5,7 +5,7 @@ APP_DEBUG=true
API_URL_CLIENT=https://localhost:8443/api
API_URL_SERVER=http://backend:8080
-STRIPE_PUBLIC_KEY=pk_test_XX
+STRIPE_PUBLIC_KEY=pk_test_xxx
FRONTEND_URL=https://localhost:8443
DB_CONNECTION=pgsql