Phase 9E (Abschluss): Checkout-Flows und Plan-Kontingent statt Quota-Stub

- Checkout-Backend: me.checkout.subscription (Tarif-Abo monatlich/jährlich)
  und me.checkout.single-pm (Einzel-PM 19 € netto, pending-Kauf mit
  Webhook-Erfüllung); StripeCheckoutService als mockbarer Stripe-Wrapper;
  Stripe Tax via Cashier::calculateTaxes() (Netto-Preise, USt-ID-Abfrage)
- Slot-Logik: Kontingent aus dem Tarif (plans.press_release_quota) plus
  bezahlte Einmalkäufe; Verbrauch bei Veröffentlichung zuerst aus dem
  Plan-Zähler, danach Einlösung des ältesten Einmalkaufs (consumed +
  PM-Verknüpfung); Grandfathered = unbegrenzt (Entscheidung 12.06.2026,
  Bestandsschutz); Stub-Spalte users.press_release_quota entfernt
- billing:sync-stripe-plans legt zusätzlich das Einzel-PM-Produkt an
  (STRIPE_PRICE_SINGLE_PM); Test-Mode-Sync gelaufen
- Buchungs-Seite: Rückmeldung nach Checkout (erfolg/abbruch/Guard-Hinweis)
- Tests: PressReleaseQuotaTest auf Plan-Semantik neu geschrieben,
  CheckoutFlowTest (8 Tests), Modal-/API-Tests angepasst; Suite 510 passed
- Doku: Billing-und-Rechnungskreise (Kontingent-Tabelle, Checkout-Routen,
  Webhook-Events, Stripe-CLI-Hinweis), PHASE-9-Plan 9E , Checkliste,
  STATUS-ABGLEICH, PROGRESS

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-06-12 12:10:32 +00:00
parent 38fab64e10
commit c8dc99c3c8
24 changed files with 775 additions and 100 deletions

View file

@ -3,6 +3,7 @@
use App\Enums\PressReleaseStatus;
use App\Models\Category;
use App\Models\Company;
use App\Models\Plan;
use App\Models\PressRelease;
use App\Models\PressReleaseStatusLog;
use App\Models\User;
@ -79,10 +80,16 @@ test('api submit responds 402 when the booking gate is enforced', function () {
test('api submit responds 422 when the monthly quota is exhausted', function () {
/** @var TestCase $this */
config()->set('billing.enforce_booking', true);
$user = User::factory()->create([
'press_release_quota' => 3,
'press_release_quota_used_this_month' => 3,
]);
$plan = Plan::factory()->create([
'press_release_quota' => 3,
'stripe_price_id_monthly' => 'price_test_m_submit',
]);
subscribeUserToPlan($user, $plan);
$pressRelease = PressRelease::factory()->create([
'user_id' => $user->id,
'status' => PressReleaseStatus::Draft->value,

View file

@ -0,0 +1,132 @@
<?php
use App\Enums\SinglePurchaseStatus;
use App\Enums\SinglePurchaseType;
use App\Models\Plan;
use App\Models\SinglePurchase;
use App\Models\User;
use App\Services\Billing\StripeCheckoutService;
use Database\Seeders\RolesAndPermissionsSeeder;
use Illuminate\Http\RedirectResponse;
use Laravel\Cashier\Checkout;
use Tests\TestCase;
beforeEach(function (): void {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
});
function checkoutTestCustomer(): User
{
$user = User::factory()->create();
$user->assignRole('customer');
return $user;
}
/**
* Stripe-Checkout-Stub: Der Controller gibt das Checkout-Objekt zurück,
* der Router ruft toResponse() wir leiten auf eine Fake-Stripe-URL um.
*/
function fakeStripeCheckout(): Checkout
{
$checkout = Mockery::mock(Checkout::class);
$checkout->shouldReceive('toResponse')
->andReturn(new RedirectResponse('https://checkout.stripe.com/c/pay/test'));
return $checkout;
}
test('guests are redirected to the login', function () {
/** @var TestCase $this */
$plan = Plan::factory()->create();
$this->get(route('me.checkout.subscription', ['planSlug' => $plan->slug, 'interval' => 'monthly']))
->assertRedirect();
});
test('an unknown plan or invalid interval responds 404', function () {
/** @var TestCase $this */
$this->actingAs(checkoutTestCustomer());
$this->get('/admin/me/checkout/abo/gibts-nicht/monthly')->assertNotFound();
$plan = Plan::factory()->create();
$this->get("/admin/me/checkout/abo/{$plan->slug}/weekly")->assertNotFound();
});
test('a plan without a synced stripe price redirects back with a notice', function () {
/** @var TestCase $this */
$plan = Plan::factory()->create(['stripe_price_id_monthly' => null]);
$this->actingAs(checkoutTestCustomer())
->get(route('me.checkout.subscription', ['planSlug' => $plan->slug, 'interval' => 'monthly']))
->assertRedirect(route('me.bookings.index'))
->assertSessionHas('checkout-notice');
});
test('an already subscribed user is redirected instead of double-booking', function () {
/** @var TestCase $this */
$user = checkoutTestCustomer();
$plan = Plan::factory()->create(['stripe_price_id_monthly' => 'price_test_m_1']);
subscribeUserToPlan($user, $plan);
$this->actingAs($user)
->get(route('me.checkout.subscription', ['planSlug' => $plan->slug, 'interval' => 'monthly']))
->assertRedirect(route('me.bookings.index'))
->assertSessionHas('checkout-notice');
});
test('the subscription checkout hands plan and interval to stripe', function () {
/** @var TestCase $this */
$user = checkoutTestCustomer();
$plan = Plan::factory()->create(['stripe_price_id_yearly' => 'price_test_y_1']);
$this->mock(StripeCheckoutService::class, function ($mock) use ($plan) {
$mock->shouldReceive('forSubscription')
->once()
->withArgs(fn (User $u, Plan $p, string $interval) => $p->is($plan) && $interval === 'yearly')
->andReturn(fakeStripeCheckout());
});
$this->actingAs($user)
->get(route('me.checkout.subscription', ['planSlug' => $plan->slug, 'interval' => 'yearly']))
->assertRedirect('https://checkout.stripe.com/c/pay/test');
});
test('the single pm checkout is disabled without a configured stripe price', function () {
/** @var TestCase $this */
config()->set('billing.single_pm_stripe_price_id', null);
$this->actingAs(checkoutTestCustomer())
->get(route('me.checkout.single-pm'))
->assertRedirect(route('me.bookings.index'))
->assertSessionHas('checkout-notice');
expect(SinglePurchase::count())->toBe(0);
});
test('the single pm checkout creates a pending purchase and hands it to stripe', function () {
/** @var TestCase $this */
config()->set('billing.single_pm_stripe_price_id', 'price_test_single_pm');
config()->set('billing.single_pm_price_cents', 1900);
$user = checkoutTestCustomer();
$this->mock(StripeCheckoutService::class, function ($mock) use ($user) {
$mock->shouldReceive('forSinglePurchase')
->once()
->withArgs(fn (User $u, SinglePurchase $p) => $u->is($user) && $p->exists)
->andReturn(fakeStripeCheckout());
});
$this->actingAs($user)
->get(route('me.checkout.single-pm'))
->assertRedirect('https://checkout.stripe.com/c/pay/test');
$purchase = SinglePurchase::sole();
expect($purchase->user_id)->toBe($user->id);
expect($purchase->type)->toBe(SinglePurchaseType::SinglePm);
expect($purchase->status)->toBe(SinglePurchaseStatus::Pending);
expect($purchase->price_cents)->toBe(1900);
});

View file

@ -1,6 +1,7 @@
<?php
use App\Enums\PressReleaseStatus;
use App\Models\Plan;
use Database\Seeders\RolesAndPermissionsSeeder;
use Illuminate\Support\Facades\Queue;
use Livewire\Volt\Volt as LivewireVolt;
@ -17,8 +18,17 @@ beforeEach(function (): void {
test('customer show renders the publish confirmation modal with legal note and quota', function () {
/** @var TestCase $this */
// Das Kontingent erscheint nur mit scharfem Launch-Schalter und Tarif —
// unbegrenzte User (Schalter aus, Bestandsschutz) sehen den Block nicht.
config()->set('billing.enforce_booking', true);
['customer' => $customer, 'pr' => $pr] = makeCustomerForShowPhase8a();
$customer->update(['press_release_quota' => 3, 'press_release_quota_used_this_month' => 1]);
$customer->update(['press_release_quota_used_this_month' => 1]);
$plan = Plan::factory()->create([
'press_release_quota' => 3,
'stripe_price_id_monthly' => 'price_test_m_modal',
]);
subscribeUserToPlan($customer, $plan);
$this->actingAs($customer);
LivewireVolt::test('customer.press-releases.show', ['id' => $pr->id])
@ -28,10 +38,21 @@ test('customer show renders the publish confirmation modal with legal note and q
->assertSee('2 / 3');
});
test('the quota block is hidden for users with an unlimited quota', function () {
/** @var TestCase $this */
['customer' => $customer, 'pr' => $pr] = makeCustomerForShowPhase8a();
$this->actingAs($customer);
// Launch-Schalter aus → Kontingent unbegrenzt → kein Quota-Block.
LivewireVolt::test('customer.press-releases.show', ['id' => $pr->id])
->assertSee('Pressemitteilung zur Prüfung einreichen')
->assertDontSee('PM-Kontingent diesen Monat');
});
test('submitting from the show modal moves the draft into review without consuming quota', function () {
/** @var TestCase $this */
['customer' => $customer, 'pr' => $pr] = makeCustomerForShowPhase8a();
$customer->update(['press_release_quota' => 3, 'press_release_quota_used_this_month' => 0]);
$customer->update(['press_release_quota_used_this_month' => 0]);
$this->actingAs($customer);
LivewireVolt::test('customer.press-releases.show', ['id' => $pr->id])

View file

@ -2,10 +2,15 @@
use App\Console\Commands\ResetMonthlyPressReleaseQuota;
use App\Enums\PressReleaseStatus;
use App\Enums\SinglePurchaseStatus;
use App\Enums\UserPaymentOptionStatus;
use App\Models\Category;
use App\Models\Company;
use App\Models\Plan;
use App\Models\PressRelease;
use App\Models\SinglePurchase;
use App\Models\User;
use App\Models\UserPaymentOption;
use App\Services\PressRelease\PressReleaseService;
use App\Services\PressRelease\QuotaExceededException;
use Database\Seeders\RolesAndPermissionsSeeder;
@ -15,6 +20,9 @@ use Tests\TestCase;
beforeEach(function (): void {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
// Das Kontingent greift erst mit dem Launch-Schalter (sonst unbegrenzt).
config()->set('billing.enforce_booking', true);
});
function quotaTestPressRelease(User $user, string $status = 'draft'): PressRelease
@ -31,25 +39,72 @@ function quotaTestPressRelease(User $user, string $status = 'draft'): PressRelea
]);
}
test('remaining quota reflects the used counter', function () {
function quotaTestSubscriber(int $planQuota = 3, int $used = 0): User
{
$user = User::factory()->create([
'press_release_quota' => 3,
'press_release_quota_used_this_month' => 1,
'press_release_quota_used_this_month' => $used,
]);
$user->assignRole('customer');
$plan = Plan::factory()->create([
'press_release_quota' => $planQuota,
'stripe_price_id_monthly' => 'price_test_m_'.fake()->unique()->randomNumber(6),
]);
subscribeUserToPlan($user, $plan);
return $user;
}
test('without the launch switch the quota is unlimited', function () {
config()->set('billing.enforce_booking', false);
$user = User::factory()->create();
expect($user->pressReleaseQuotaRemaining())->toBeNull();
expect($user->pressReleaseQuotaTotal())->toBeNull();
});
test('a subscriber inherits the monthly quota of the plan', function () {
$user = quotaTestSubscriber(planQuota: 3, used: 1);
expect($user->pressReleaseQuotaRemaining())->toBe(2);
expect($user->pressReleaseQuotaTotal())->toBe(3);
});
test('paid single purchases extend the quota', function () {
$user = quotaTestSubscriber(planQuota: 3, used: 3);
SinglePurchase::factory()->paid()->count(2)->create(['user_id' => $user->id]);
SinglePurchase::factory()->consumed()->create(['user_id' => $user->id]);
expect($user->pressReleaseQuotaRemaining())->toBe(2);
expect($user->pressReleaseQuotaTotal())->toBe(5);
});
test('a grandfathered legacy user has an unlimited quota', function () {
// Entscheidung 12.06.2026: Bestandsschutz — das Alt-Produkt sah
// unbegrenzte PMs vor, am Kontingent wird nichts umgestellt.
$user = User::factory()->create();
$user->assignRole('customer');
UserPaymentOption::factory()->create([
'user_id' => $user->id,
'status' => UserPaymentOptionStatus::Grandfathered->value,
]);
expect($user->pressReleaseQuotaRemaining())->toBeNull();
$pr = quotaTestPressRelease($user, 'review');
app(PressReleaseService::class)->publish($pr);
expect($pr->fresh()->status)->toBe(PressReleaseStatus::Published);
expect($user->fresh()->press_release_quota_used_this_month)->toBe(0);
});
test('submitting a press release does not consume a quota slot', function () {
// Decision-Update §3.2: Der Slot zählt erst bei Veröffentlichung runter.
Queue::fake();
$user = User::factory()->create([
'press_release_quota' => 3,
'press_release_quota_used_this_month' => 0,
]);
$user->assignRole('customer');
$user = quotaTestSubscriber();
$pr = quotaTestPressRelease($user);
app(PressReleaseService::class)->submitForReview($pr);
@ -58,13 +113,8 @@ test('submitting a press release does not consume a quota slot', function () {
expect($user->fresh()->press_release_quota_used_this_month)->toBe(0);
});
test('publishing consumes exactly one quota slot', function () {
$user = User::factory()->create([
'press_release_quota' => 3,
'press_release_quota_used_this_month' => 0,
]);
$user->assignRole('customer');
test('publishing consumes exactly one plan slot', function () {
$user = quotaTestSubscriber();
$pr = quotaTestPressRelease($user, 'review');
app(PressReleaseService::class)->publish($pr);
@ -74,12 +124,7 @@ test('publishing consumes exactly one quota slot', function () {
});
test('re-publishing after archive does not consume a second slot', function () {
$user = User::factory()->create([
'press_release_quota' => 3,
'press_release_quota_used_this_month' => 0,
]);
$user->assignRole('customer');
$user = quotaTestSubscriber();
$pr = quotaTestPressRelease($user, 'review');
$service = app(PressReleaseService::class);
@ -91,12 +136,7 @@ test('re-publishing after archive does not consume a second slot', function () {
});
test('a rejected press release does not consume a quota slot', function () {
$user = User::factory()->create([
'press_release_quota' => 3,
'press_release_quota_used_this_month' => 0,
]);
$user->assignRole('customer');
$user = quotaTestSubscriber();
$pr = quotaTestPressRelease($user, 'review');
app(PressReleaseService::class)->reject($pr, 'Unzulässiger Inhalt.', 'ki');
@ -105,15 +145,44 @@ test('a rejected press release does not consume a quota slot', function () {
expect($user->fresh()->press_release_quota_used_this_month)->toBe(0);
});
test('publishing past the plan quota consumes the oldest paid purchase', function () {
$user = quotaTestSubscriber(planQuota: 1, used: 1);
$older = SinglePurchase::factory()->paid()->create([
'user_id' => $user->id,
'paid_at' => now()->subDays(2),
]);
$newer = SinglePurchase::factory()->paid()->create([
'user_id' => $user->id,
'paid_at' => now()->subDay(),
]);
$pr = quotaTestPressRelease($user, 'review');
app(PressReleaseService::class)->publish($pr);
expect($user->fresh()->press_release_quota_used_this_month)->toBe(1);
expect($older->fresh()->status)->toBe(SinglePurchaseStatus::Consumed);
expect($older->fresh()->press_release_id)->toBe($pr->id);
expect($newer->fresh()->status)->toBe(SinglePurchaseStatus::Paid);
});
test('a purchase-only user consumes the purchase on publish', function () {
$user = User::factory()->create();
$user->assignRole('customer');
$purchase = SinglePurchase::factory()->paid()->create(['user_id' => $user->id]);
expect($user->pressReleaseQuotaRemaining())->toBe(1);
$pr = quotaTestPressRelease($user, 'review');
app(PressReleaseService::class)->publish($pr);
expect($purchase->fresh()->status)->toBe(SinglePurchaseStatus::Consumed);
expect($purchase->fresh()->consumed_at)->not->toBeNull();
});
test('submitting with an exhausted quota is blocked', function () {
Queue::fake();
$user = User::factory()->create([
'press_release_quota' => 3,
'press_release_quota_used_this_month' => 3,
]);
$user->assignRole('customer');
$user = quotaTestSubscriber(planQuota: 3, used: 3);
$pr = quotaTestPressRelease($user);
expect(fn () => app(PressReleaseService::class)->submitForReview($pr))
@ -123,6 +192,7 @@ test('submitting with an exhausted quota is blocked', function () {
});
test('monthly reset command zeroes the used counter', function () {
/** @var TestCase $this */
User::factory()->count(2)->create(['press_release_quota_used_this_month' => 2]);
$untouched = User::factory()->create(['press_release_quota_used_this_month' => 0]);

View file

@ -1,5 +1,7 @@
<?php
use App\Models\Plan;
use App\Models\User;
use App\Services\CurrentPortalContext;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
@ -54,3 +56,21 @@ function something()
{
// ..
}
/**
* Legt offline eine aktive Cashier-Abo-Zeile für den User an (kein
* Stripe-Aufruf) und verknüpft sie über die Preis-ID mit dem Tarif
* `User::currentPlan()` löst darüber auf.
*/
function subscribeUserToPlan(User $user, Plan $plan, string $interval = 'monthly'): void
{
$priceId = $interval === 'yearly' ? $plan->stripe_price_id_yearly : $plan->stripe_price_id_monthly;
$user->subscriptions()->create([
'type' => 'default',
'stripe_id' => 'sub_test_'.fake()->unique()->randomNumber(6),
'stripe_status' => 'active',
'stripe_price' => $priceId,
'quantity' => 1,
]);
}