presseportale/app/Services/Billing/CreditPricingService.php
Kevin Adametz b63cd26326 Credit-Wallet + Ledger + Tier-Preisableitung (Fundament)
Echte Credit-Wallet (1 Credit = 1 EUR) mit append-only Ledger als Basis fuer
die Credit-Oekonomie aus dem Decision-Update (Rev. 4):

- credit_wallets (denormalisierter Saldo) + credit_transactions (Ledger,
  vorzeichenbehaftet, balance_after, polymorphe reference)
- CreditWalletService: einziger Schreibpfad, atomar mit Row-Lock,
  InsufficientCreditsException mit shortfall fuer den Mini-Checkout
- Tier-Enum (Einzel/Starter/Business/Pro/Agency) + User::currentTier()
- CreditPricingService: tier-gestaffelte Ableitung aus config/credits.php
  (Extra-PM 19/15/12/10/8, Boost 12/20/35, PDF 3, Depublish 25, Pruef-Quota)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-17 14:16:43 +00:00

87 lines
2.1 KiB
PHP

<?php
namespace App\Services\Billing;
use App\Enums\Tier;
use App\Models\User;
/**
* Leitet die Credit-Preise laut Decision-Update (Rev. 4) zur Kaufzeit aus
* dem aktiven Tier ab — bewusst nicht statisch je Kauf gespeichert. Einzige
* Quelle der Preis-Wahrheit ist config/credits.php.
*/
class CreditPricingService
{
/**
* Extra-PM-Preis (Credits) für das angegebene Tier. Fällt auf den
* Einzel-Satz zurück, falls ein Tier nicht konfiguriert ist.
*/
public function extraPmCredits(Tier $tier): int
{
$table = config('credits.extra_pm', []);
return (int) ($table[$tier->value] ?? $table[Tier::Einzel->value]);
}
public function extraPmCreditsFor(User $user): int
{
return $this->extraPmCredits($user->currentTier());
}
/**
* Boost-Preis (Credits) für eine Laufzeit in Tagen.
*/
public function boostCredits(int $days): int
{
$table = config('credits.boost', []);
if (! isset($table[$days])) {
throw new \InvalidArgumentException("Unbekannte Boost-Laufzeit: {$days} Tage.");
}
return (int) $table[$days];
}
/**
* Verfügbare Boost-Laufzeiten als [Tage => Credits], aufsteigend.
*
* @return array<int, int>
*/
public function boostOptions(): array
{
$table = config('credits.boost', []);
ksort($table);
return $table;
}
public function proofPdfCredits(): int
{
return (int) config('credits.proof_pdf');
}
public function depublishCredits(): int
{
return (int) config('credits.depublish');
}
/**
* Freie Prüfungen pro Monat für das Tier (Prüfkontingent §4.3).
*/
public function reviewFreeQuota(Tier $tier): int
{
$table = config('credits.review.free_per_month', []);
return (int) ($table[$tier->value] ?? $table[Tier::Einzel->value]);
}
public function reviewDailyLimit(): int
{
return (int) config('credits.review.daily_limit');
}
public function reviewOverflowCost(): int
{
return (int) config('credits.review.overflow_cost');
}
}