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>
This commit is contained in:
parent
5a9aab7012
commit
b63cd26326
15 changed files with 756 additions and 0 deletions
34
database/factories/CreditTransactionFactory.php
Normal file
34
database/factories/CreditTransactionFactory.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Enums\CreditTransactionType;
|
||||
use App\Models\CreditTransaction;
|
||||
use App\Models\CreditWallet;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<CreditTransaction>
|
||||
*/
|
||||
class CreditTransactionFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$amount = $this->faker->numberBetween(1, 50);
|
||||
|
||||
return [
|
||||
'credit_wallet_id' => CreditWallet::factory(),
|
||||
'user_id' => User::factory(),
|
||||
'amount_credits' => $amount,
|
||||
'balance_after' => $amount,
|
||||
'type' => CreditTransactionType::Topup,
|
||||
'description' => null,
|
||||
'reference_type' => null,
|
||||
'reference_id' => null,
|
||||
];
|
||||
}
|
||||
}
|
||||
29
database/factories/CreditWalletFactory.php
Normal file
29
database/factories/CreditWalletFactory.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\CreditWallet;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<CreditWallet>
|
||||
*/
|
||||
class CreditWalletFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'user_id' => User::factory(),
|
||||
'balance_credits' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
public function withBalance(int $credits): static
|
||||
{
|
||||
return $this->state(fn (): array => ['balance_credits' => $credits]);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
/**
|
||||
* Credit-Wallet laut Decision-Update (Rev. 4): eine Wallet pro User, ein
|
||||
* tier-abhängiger Preis. 1 Credit = 1 €. Der Saldo ist die Summe aller
|
||||
* Buchungen in `credit_transactions` und wird hier denormalisiert geführt,
|
||||
* damit Gate-Checks ohne Aggregat-Query auskommen.
|
||||
*/
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('credit_wallets', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->unique()->constrained()->cascadeOnDelete();
|
||||
$table->integer('balance_credits')->default(0);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('credit_wallets');
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
/**
|
||||
* Append-only Ledger der Credit-Wallet. Jede Auf-/Entladung ist eine Zeile
|
||||
* mit vorzeichenbehaftetem `amount_credits` (+ = Gutschrift, − = Verbrauch)
|
||||
* und dem resultierenden `balance_after` für lückenlose Nachvollziehbarkeit.
|
||||
* `reference` verweist polymorph auf den auslösenden Vorgang (SinglePurchase,
|
||||
* Boost, PressRelease …).
|
||||
*/
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('credit_transactions', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('credit_wallet_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
$table->integer('amount_credits');
|
||||
$table->integer('balance_after');
|
||||
$table->string('type');
|
||||
$table->string('description')->nullable();
|
||||
$table->nullableMorphs('reference');
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['user_id', 'created_at']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('credit_transactions');
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue