Skip to content

Commit e33a196

Browse files
authored
Feature: Store VAT rate (#944)
1 parent eae4b93 commit e33a196

File tree

14 files changed

+127
-10
lines changed

14 files changed

+127
-10
lines changed

backend/app/DomainObjects/Generated/OrderPaymentPlatformFeeDomainObjectAbstract.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ abstract class OrderPaymentPlatformFeeDomainObjectAbstract extends \HiEvents\Dom
2525
final public const APPLICATION_FEE_NET_AMOUNT = 'application_fee_net_amount';
2626
final public const APPLICATION_FEE_VAT_AMOUNT = 'application_fee_vat_amount';
2727
final public const CHARGE_ID = 'charge_id';
28+
final public const APPLICATION_FEE_VAT_RATE = 'application_fee_vat_rate';
2829

2930
protected int $id;
3031
protected int $order_id;
@@ -41,6 +42,7 @@ abstract class OrderPaymentPlatformFeeDomainObjectAbstract extends \HiEvents\Dom
4142
protected ?float $application_fee_net_amount = null;
4243
protected ?float $application_fee_vat_amount = null;
4344
protected ?string $charge_id = null;
45+
protected ?float $application_fee_vat_rate = null;
4446

4547
public function toArray(): array
4648
{
@@ -60,6 +62,7 @@ public function toArray(): array
6062
'application_fee_net_amount' => $this->application_fee_net_amount ?? null,
6163
'application_fee_vat_amount' => $this->application_fee_vat_amount ?? null,
6264
'charge_id' => $this->charge_id ?? null,
65+
'application_fee_vat_rate' => $this->application_fee_vat_rate ?? null,
6366
];
6467
}
6568

@@ -227,4 +230,15 @@ public function getChargeId(): ?string
227230
{
228231
return $this->charge_id;
229232
}
233+
234+
public function setApplicationFeeVatRate(?float $application_fee_vat_rate): self
235+
{
236+
$this->application_fee_vat_rate = $application_fee_vat_rate;
237+
return $this;
238+
}
239+
240+
public function getApplicationFeeVatRate(): ?float
241+
{
242+
return $this->application_fee_vat_rate;
243+
}
230244
}

backend/app/DomainObjects/Generated/StripePaymentDomainObjectAbstract.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ abstract class StripePaymentDomainObjectAbstract extends \HiEvents\DomainObjects
3232
final public const PAYOUT_CURRENCY = 'payout_currency';
3333
final public const PAYOUT_EXCHANGE_RATE = 'payout_exchange_rate';
3434
final public const BALANCE_TRANSACTION_ID = 'balance_transaction_id';
35+
final public const APPLICATION_FEE_VAT_RATE = 'application_fee_vat_rate';
3536

3637
protected int $id;
3738
protected int $order_id;
@@ -48,13 +49,14 @@ abstract class StripePaymentDomainObjectAbstract extends \HiEvents\DomainObjects
4849
protected ?string $stripe_platform = null;
4950
protected ?int $application_fee_net = null;
5051
protected ?int $application_fee_vat = null;
51-
protected string $currency = 'USD';
52+
protected ?string $currency = null;
5253
protected ?string $payout_id = null;
5354
protected ?int $payout_stripe_fee = null;
5455
protected ?int $payout_net_amount = null;
5556
protected ?string $payout_currency = null;
5657
protected ?float $payout_exchange_rate = null;
5758
protected ?string $balance_transaction_id = null;
59+
protected ?float $application_fee_vat_rate = null;
5860

5961
public function toArray(): array
6062
{
@@ -81,6 +83,7 @@ public function toArray(): array
8183
'payout_currency' => $this->payout_currency ?? null,
8284
'payout_exchange_rate' => $this->payout_exchange_rate ?? null,
8385
'balance_transaction_id' => $this->balance_transaction_id ?? null,
86+
'application_fee_vat_rate' => $this->application_fee_vat_rate ?? null,
8487
];
8588
}
8689

@@ -249,13 +252,13 @@ public function getApplicationFeeVat(): ?int
249252
return $this->application_fee_vat;
250253
}
251254

252-
public function setCurrency(string $currency): self
255+
public function setCurrency(?string $currency): self
253256
{
254257
$this->currency = $currency;
255258
return $this;
256259
}
257260

258-
public function getCurrency(): string
261+
public function getCurrency(): ?string
259262
{
260263
return $this->currency;
261264
}
@@ -325,4 +328,15 @@ public function getBalanceTransactionId(): ?string
325328
{
326329
return $this->balance_transaction_id;
327330
}
331+
332+
public function setApplicationFeeVatRate(?float $application_fee_vat_rate): self
333+
{
334+
$this->application_fee_vat_rate = $application_fee_vat_rate;
335+
return $this;
336+
}
337+
338+
public function getApplicationFeeVatRate(): ?float
339+
{
340+
return $this->application_fee_vat_rate;
341+
}
328342
}

backend/app/Models/StripePayment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function getCastMap(): array
1919
return [
2020
'last_error' => 'array',
2121
'payout_exchange_rate' => 'float',
22-
22+
'application_fee_vat_rate' => 'float',
2323
];
2424
}
2525

backend/app/Services/Application/Handlers/Order/Payment/Stripe/CreatePaymentIntentHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public function handle(string $orderShortId): CreatePaymentIntentResponseDTO
131131
StripePaymentDomainObjectAbstract::APPLICATION_FEE_GROSS => $applicationFeeData?->grossApplicationFee?->toMinorUnit() ?? 0,
132132
StripePaymentDomainObjectAbstract::APPLICATION_FEE_NET => $applicationFeeData?->netApplicationFee?->toMinorUnit() ?? 0,
133133
StripePaymentDomainObjectAbstract::APPLICATION_FEE_VAT => $applicationFeeData?->applicationFeeVatAmount?->toMinorUnit() ?? 0,
134+
StripePaymentDomainObjectAbstract::APPLICATION_FEE_VAT_RATE => $applicationFeeData?->applicationFeeVatRate,
134135
StripePaymentDomainObjectAbstract::CURRENCY => strtoupper($order->getCurrency()),
135136
StripePaymentDomainObjectAbstract::STRIPE_PLATFORM => $stripePlatform?->value,
136137
]);

backend/app/Services/Application/Handlers/Order/Payment/Stripe/IncomingWebhookHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function handle(StripeWebhookDTO $webhookDTO): void
7575
'data' => $event->data->object->toArray(),
7676
]);
7777

78-
// return;
78+
return;
7979
}
8080

8181
$this->logger->debug('Stripe event received: ' . $event->type, $event->data->object->toArray());

backend/app/Services/Domain/Order/OrderApplicationFeeCalculationService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ private function calculateFeeWithVat(
109109
return new ApplicationFeeValuesDTO(
110110
grossApplicationFee: $netApplicationFee,
111111
netApplicationFee: $netApplicationFee,
112+
applicationFeeVatRate: $vatRate,
112113
);
113114
}
114115

backend/app/Services/Domain/Order/OrderPaymentPlatformFeeService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function createOrderPaymentPlatformFee(
2525
?string $chargeId = null,
2626
?int $applicationFeeNetAmountMinorUnit = null,
2727
?int $applicationFeeVatAmountMinorUnit = null,
28+
?float $applicationFeeVatRate = null,
2829
): void
2930
{
3031
$isZeroDecimalCurrency = Currency::isZeroDecimalCurrency($currency);
@@ -59,6 +60,7 @@ public function createOrderPaymentPlatformFee(
5960
OrderPaymentPlatformFeeDomainObjectAbstract::APPLICATION_FEE_GROSS_AMOUNT => $applicationFeeGrossAmount,
6061
OrderPaymentPlatformFeeDomainObjectAbstract::APPLICATION_FEE_NET_AMOUNT => $applicationFeeNetAmount,
6162
OrderPaymentPlatformFeeDomainObjectAbstract::APPLICATION_FEE_VAT_AMOUNT => $applicationFeeVatAmount,
63+
OrderPaymentPlatformFeeDomainObjectAbstract::APPLICATION_FEE_VAT_RATE => $applicationFeeVatRate,
6264
OrderPaymentPlatformFeeDomainObjectAbstract::CURRENCY => strtoupper($currency),
6365
OrderPaymentPlatformFeeDomainObjectAbstract::TRANSACTION_ID => $transactionId,
6466
OrderPaymentPlatformFeeDomainObjectAbstract::CHARGE_ID => $chargeId,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace HiEvents\Services\Domain\Payment\Stripe;
4+
5+
use Exception;
6+
7+
class NoStripeCountryCodeException extends Exception
8+
{
9+
10+
}

backend/app/Services/Domain/Payment/Stripe/StripeAccountSyncService.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace HiEvents\Services\Domain\Payment\Stripe;
44

55
use HiEvents\DomainObjects\AccountStripePlatformDomainObject;
6+
use HiEvents\DomainObjects\Enums\CountryCode;
67
use HiEvents\DomainObjects\Generated\AccountStripePlatformDomainObjectAbstract;
78
use HiEvents\DomainObjects\Generated\AccountVatSettingDomainObjectAbstract;
89
use HiEvents\Helper\Url;
@@ -60,6 +61,7 @@ public function syncStripeAccountStatus(
6061
/**
6162
* Force update account status when we know it should be complete
6263
* (e.g., from GetStripeConnectAccountsHandler when Stripe says complete but DB doesn't)
64+
* @throws NoStripeCountryCodeException
6365
*/
6466
public function markAccountAsComplete(
6567
AccountStripePlatformDomainObject $accountStripePlatform,
@@ -182,6 +184,9 @@ private function updateAccountCountryAndVerificationStatus(
182184
}
183185
}
184186

187+
/**
188+
* @throws NoStripeCountryCodeException
189+
*/
185190
private function createVatSettingIfMissing(AccountStripePlatformDomainObject $accountStripePlatform): void
186191
{
187192
if ($this->config->get('app.tax.eu_vat_handling_enabled') !== true) {
@@ -192,6 +197,29 @@ private function createVatSettingIfMissing(AccountStripePlatformDomainObject $ac
192197
return;
193198
}
194199

200+
$countryCode = $accountStripePlatform->getStripeAccountDetails()['country'];
201+
202+
if ($countryCode === null) {
203+
$this->logger->error('Stripe account country code is missing, cannot create VAT setting.', [
204+
'account_stripe_platform_id' => $accountStripePlatform->getId(),
205+
'account_id' => $accountStripePlatform->getAccountId(),
206+
]);
207+
208+
throw new NoStripeCountryCodeException('Stripe account country code is missing. cannot create VAT setting.',
209+
accountStripePlatformId: $accountStripePlatform->getId(),
210+
accountId: $accountStripePlatform->getAccountId()
211+
);
212+
}
213+
214+
if (!CountryCode::isEuCountry(CountryCode::from($countryCode))) {
215+
$this->logger->info('Account is not in an EU country, skipping VAT setting creation.', [
216+
'account_stripe_platform_id' => $accountStripePlatform->getId(),
217+
'account_id' => $accountStripePlatform->getAccountId(),
218+
'country_code' => $countryCode,
219+
]);
220+
return;
221+
}
222+
195223
$existingVatSetting = $this->vatSettingRepository->findFirstWhere([
196224
AccountVatSettingDomainObjectAbstract::ACCOUNT_ID => $accountStripePlatform->getAccountId(),
197225
]);
@@ -200,8 +228,7 @@ private function createVatSettingIfMissing(AccountStripePlatformDomainObject $ac
200228
$this->vatSettingRepository->create([
201229
AccountVatSettingDomainObjectAbstract::ACCOUNT_ID => $accountStripePlatform->getAccountId(),
202230
AccountVatSettingDomainObjectAbstract::VAT_VALIDATED => false,
203-
AccountVatSettingDomainObjectAbstract::VAT_COUNTRY_CODE => $accountStripePlatform
204-
->getStripeAccountDetails()['country'] ?? null,
231+
AccountVatSettingDomainObjectAbstract::VAT_COUNTRY_CODE => $countryCode,
205232
]);
206233
}
207234
}

backend/app/Services/Domain/Payment/Stripe/StripePaymentPlatformFeeExtractionService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public function extractAndStorePlatformFee(
111111
chargeId: $charge->id ?? null,
112112
applicationFeeNetAmountMinorUnit: $applicationFeeBreakdown['net'],
113113
applicationFeeVatAmountMinorUnit: $applicationFeeBreakdown['vat'],
114+
applicationFeeVatRate: $stripePayment->getApplicationFeeVatRate(),
114115
);
115116

116117
$this->logger->info(__('Platform fee stored successfully'), [

0 commit comments

Comments
 (0)