presseportale/tests/Feature/Billing/CheckoutFlowTest.php
Kevin Adametz 6f9a68b63d T1neu: Unternehmer-Pflicht-Checkbox im Checkout, protokolliert mit AGB-Version
B2B-Gate (AGB v0.6 par. 1 Abs. 4): Das Buchungs-Modal verlangt vor jedem
Stripe-Checkout die Bestaetigung 'Ich bestelle in Ausuebung meiner
gewerblichen oder selbstaendigen beruflichen Taetigkeit (par. 14 BGB)'.

- booking_confirmations: user, Buchungstyp/-ref, AGB-Version, IP,
  confirmed_at (Nachweis laut Datenschutz Abschnitt 7)
- Checkout-Link erscheint erst nach der Checkbox; CheckoutController
  verlangt zusaetzlich serverseitig eine zeitnahe Bestaetigung (30 Min.)
  fuer Abo/Einzel-PM/Credit-Topup — Direktaufrufe laufen ins Leere
- config/legal.php als zentrale AGB-Versionsquelle (Seite + Protokoll,
  Uebergangsloesung bis zum legal_documents-System E3)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 16:17:59 +00:00

200 lines
7.4 KiB
PHP

<?php
use App\Enums\SinglePurchaseStatus;
use App\Enums\SinglePurchaseType;
use App\Models\BillingAddress;
use App\Models\BookingConfirmation;
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');
// Buchungs-Voraussetzung (12.06.2026): vollständige Rechnungsadresse.
BillingAddress::factory()->create(['user_id' => $user->id]);
// B2B-Gate (T1neu): Jeder Checkout verlangt eine zeitnah protokollierte
// Unternehmer-Bestätigung (Pflicht-Checkbox im Buchungs-Modal).
confirmEntrepreneur($user);
return $user;
}
function confirmEntrepreneur(User $user): BookingConfirmation
{
return BookingConfirmation::query()->create([
'user_id' => $user->id,
'booking_type' => 'subscription',
'booking_ref' => null,
'interval' => null,
'agb_version' => (string) config('legal.agb_version'),
'ip' => '127.0.0.1',
'confirmed_at' => now(),
]);
}
/**
* 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 checkout without a complete billing address redirects to the profile', function () {
/** @var TestCase $this */
$user = User::factory()->create();
$user->assignRole('customer');
$plan = Plan::factory()->create(['stripe_price_id_monthly' => 'price_test_m_addr']);
config()->set('billing.single_pm_stripe_price_id', 'price_test_single_pm');
$this->actingAs($user)
->get(route('me.checkout.subscription', ['planSlug' => $plan->slug, 'interval' => 'monthly']))
->assertRedirect(route('me.profile'))
->assertSessionHas('checkout-notice');
$this->actingAs($user)
->get(route('me.checkout.single-pm'))
->assertRedirect(route('me.profile'))
->assertSessionHas('checkout-notice');
expect(SinglePurchase::count())->toBe(0);
});
test('a checkout without a recent entrepreneur confirmation bounces back to the bookings page', function () {
/** @var TestCase $this */
// B2B-Gate (T1neu): Rechnungsadresse vorhanden, aber keine (bzw. nur eine
// abgelaufene) Unternehmer-Bestätigung → kein Stripe-Checkout.
$user = User::factory()->create();
$user->assignRole('customer');
BillingAddress::factory()->create(['user_id' => $user->id]);
$plan = Plan::factory()->create(['stripe_price_id_monthly' => 'price_test_m_b2b']);
config()->set('billing.single_pm_stripe_price_id', 'price_test_single_pm');
$this->actingAs($user)
->get(route('me.checkout.subscription', ['planSlug' => $plan->slug, 'interval' => 'monthly']))
->assertRedirect(route('me.bookings.index'))
->assertSessionHas('checkout-notice');
// Abgelaufene Bestätigung zählt nicht.
confirmEntrepreneur($user)->update(['confirmed_at' => now()->subMinutes(BookingConfirmation::VALID_MINUTES + 1)]);
$this->actingAs($user)
->get(route('me.checkout.single-pm'))
->assertRedirect(route('me.bookings.index'))
->assertSessionHas('checkout-notice');
expect(SinglePurchase::count())->toBe(0);
});
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);
});