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
97d69a1a3a
commit
1306849b21
15 changed files with 756 additions and 0 deletions
62
tests/Feature/CreditPricingServiceTest.php
Normal file
62
tests/Feature/CreditPricingServiceTest.php
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
use App\Enums\Tier;
|
||||
use App\Models\Plan;
|
||||
use App\Models\User;
|
||||
use App\Services\Billing\CreditPricingService;
|
||||
|
||||
beforeEach(function (): void {
|
||||
$this->pricing = app(CreditPricingService::class);
|
||||
});
|
||||
|
||||
test('extra-pm credits follow the tier staffel', function () {
|
||||
expect($this->pricing->extraPmCredits(Tier::Einzel))->toBe(19);
|
||||
expect($this->pricing->extraPmCredits(Tier::Starter))->toBe(15);
|
||||
expect($this->pricing->extraPmCredits(Tier::Business))->toBe(12);
|
||||
expect($this->pricing->extraPmCredits(Tier::Pro))->toBe(10);
|
||||
expect($this->pricing->extraPmCredits(Tier::Agency))->toBe(8);
|
||||
});
|
||||
|
||||
test('a user without a plan is on the Einzel tier and pays the full extra-pm rate', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
expect($user->currentTier())->toBe(Tier::Einzel);
|
||||
expect($this->pricing->extraPmCreditsFor($user))->toBe(19);
|
||||
});
|
||||
|
||||
test('a subscriber inherits the tier of the plan slug', function () {
|
||||
$user = User::factory()->create();
|
||||
$plan = Plan::factory()->create([
|
||||
'slug' => 'business',
|
||||
'stripe_price_id_monthly' => 'price_biz_'.fake()->unique()->randomNumber(6),
|
||||
]);
|
||||
subscribeUserToPlan($user, $plan);
|
||||
|
||||
expect($user->currentTier())->toBe(Tier::Business);
|
||||
expect($this->pricing->extraPmCreditsFor($user))->toBe(12);
|
||||
});
|
||||
|
||||
test('boost credits follow the duration staffel and reject unknown durations', function () {
|
||||
expect($this->pricing->boostCredits(7))->toBe(12);
|
||||
expect($this->pricing->boostCredits(14))->toBe(20);
|
||||
expect($this->pricing->boostCredits(30))->toBe(35);
|
||||
expect($this->pricing->boostOptions())->toBe([7 => 12, 14 => 20, 30 => 35]);
|
||||
|
||||
expect(fn () => $this->pricing->boostCredits(99))
|
||||
->toThrow(InvalidArgumentException::class);
|
||||
});
|
||||
|
||||
test('flat prices match the decision update', function () {
|
||||
expect($this->pricing->proofPdfCredits())->toBe(3);
|
||||
expect($this->pricing->depublishCredits())->toBe(25);
|
||||
});
|
||||
|
||||
test('review free quota follows the tier staffel', function () {
|
||||
expect($this->pricing->reviewFreeQuota(Tier::Einzel))->toBe(4);
|
||||
expect($this->pricing->reviewFreeQuota(Tier::Starter))->toBe(12);
|
||||
expect($this->pricing->reviewFreeQuota(Tier::Business))->toBe(30);
|
||||
expect($this->pricing->reviewFreeQuota(Tier::Pro))->toBe(60);
|
||||
expect($this->pricing->reviewFreeQuota(Tier::Agency))->toBe(120);
|
||||
expect($this->pricing->reviewDailyLimit())->toBe(10);
|
||||
expect($this->pricing->reviewOverflowCost())->toBe(1);
|
||||
});
|
||||
67
tests/Feature/CreditWalletServiceTest.php
Normal file
67
tests/Feature/CreditWalletServiceTest.php
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
use App\Enums\CreditTransactionType;
|
||||
use App\Exceptions\InsufficientCreditsException;
|
||||
use App\Models\User;
|
||||
use App\Services\Billing\CreditWalletService;
|
||||
|
||||
beforeEach(function (): void {
|
||||
$this->service = app(CreditWalletService::class);
|
||||
});
|
||||
|
||||
test('a credit raises the balance and writes a ledger row', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$tx = $this->service->credit($user, 20, CreditTransactionType::Topup, 'Paket 20');
|
||||
|
||||
expect($this->service->balance($user))->toBe(20);
|
||||
expect($tx->amount_credits)->toBe(20);
|
||||
expect($tx->balance_after)->toBe(20);
|
||||
expect($tx->type)->toBe(CreditTransactionType::Topup);
|
||||
});
|
||||
|
||||
test('a debit lowers the balance and records the balance after', function () {
|
||||
$user = User::factory()->create();
|
||||
$this->service->credit($user, 20);
|
||||
|
||||
$tx = $this->service->debit($user, 12, 'Extra-PM');
|
||||
|
||||
expect($this->service->balance($user))->toBe(8);
|
||||
expect($tx->amount_credits)->toBe(-12);
|
||||
expect($tx->balance_after)->toBe(8);
|
||||
expect($tx->type)->toBe(CreditTransactionType::Spend);
|
||||
});
|
||||
|
||||
test('debiting more than the balance throws and keeps the balance untouched', function () {
|
||||
$user = User::factory()->create();
|
||||
$this->service->credit($user, 8);
|
||||
|
||||
$threw = null;
|
||||
try {
|
||||
$this->service->debit($user, 15);
|
||||
} catch (InsufficientCreditsException $e) {
|
||||
$threw = $e;
|
||||
}
|
||||
|
||||
expect($threw)->toBeInstanceOf(InsufficientCreditsException::class);
|
||||
expect($threw->shortfall())->toBe(7);
|
||||
expect($this->service->balance($user))->toBe(8);
|
||||
expect($user->creditTransactions()->count())->toBe(1); // nur die Aufladung
|
||||
});
|
||||
|
||||
test('balance is zero and canAfford is false without a wallet', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
expect($this->service->balance($user))->toBe(0);
|
||||
expect($this->service->canAfford($user, 1))->toBeFalse();
|
||||
});
|
||||
|
||||
test('a refund credits back a prior spend', function () {
|
||||
$user = User::factory()->create();
|
||||
$this->service->credit($user, 30);
|
||||
$this->service->debit($user, 25, 'Depublizieren');
|
||||
|
||||
$this->service->credit($user, 25, CreditTransactionType::Refund, 'Widerruf Depublizieren');
|
||||
|
||||
expect($this->service->balance($user))->toBe(30);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue