Gib Paketen eine eigene Produktseite und ergänze Tests zu Nutzerkreis und Verkaufsstart

Ein eigenständig vermarktetes Paket (offers.standalone_bundle) erhält /pakete/{id}
mit seinem tatsächlichen Umfang, Shop-Kachel und Suche. Die Angebotsmaske kennt
Nutzerkreis und Anzahl Trainer. Zwei Bestandstests (CartTest, Vergleich der
Rechte und Erwerbsarten) legen die Vergleichsangebote jetzt als Einzellizenz an,
weil der Nutzerkreis zum identischen Umfang gehört; ihre Aussagen bleiben.
Eine Rechnungs- oder Stripe-Bestellung lässt sich nicht mit der Testzahlung bestätigen.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
LarsandClaude Sonnet 5.5 committed 2026-11-22 19:47:15 +01:00
1 parent 0154182f3b
commit 1ce2e1e51f
21 files changed
+997 -31

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
<?php
namespace Tests\Feature;
use App\Domain\Catalog\Bundle;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Catalog\Offer;
use App\Domain\Catalog\OfferService;
use App\Domain\Commerce\Cart;
use App\Domain\Commerce\CartLine;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Support\BuildsShop;
use Tests\TestCase;
/**
* Ein eigenständig vermarktetes Paket hat eine eigene Produktseite, die seinen tatsächlichen Umfang zeigt, statt auf
* der Seite eines fremden Programms zu hängen.
*/
class BundleProductPageTest extends TestCase
{
use BuildsShop;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seedShop();
}
private function bundle(): Bundle
{
return Bundle::query()->where('name', 'Starterpaket')->firstOrFail();
}
private function standaloneOffer(string $name, int $cents, string $scope = 'einzel', ?int $trainers = null): Offer
{
$version = $this->bundle()->versions()->firstOrFail();
$rights = app(OfferService::class)->currentActiveVersion($this->privateKonflikte())->lines()->first()->rights;
$offers = app(OfferService::class);
$offer = $offers->create($name);
$offers->assignProduct($offer, null, $name, true);
$offers->activate($offer, [['target' => $version, 'rights' => $rights]], [
'grant_kind' => 'kauf', 'price_cents' => $cents, 'holder_kind' => 'person', 'licence_scope' => $scope, 'trainer_count' => $trainers,
]);
return $offer->refresh();
}
public function test_the_package_page_shows_what_the_package_actually_contains(): void
{
$this->standaloneOffer('Starterpaket solo', 5500);
$this->get(route('public.package', $this->bundle()))
->assertOk()
->assertSee('Starterpaket')
->assertSee('Das ist im Paket enthalten')
->assertSee('Konflikte im Training')
->assertSee('Grenzen setzen im Verein')
->assertSee('Starterpaket für neue Trainer')
->assertSee('Ein Paket mit 3 Inhalten')
->assertSee('55,00 €')
->assertSee('Einzellizenz · 1 Trainer')
->assertSee('In den Warenkorb');
}
public function test_the_shop_lists_the_package_with_its_contents_and_finds_it_by_a_content_name(): void
{
$this->standaloneOffer('Starterpaket solo', 5500);
$this->get(route('shop'))->assertOk()
->assertSee(route('public.package', $this->bundle()), false)
->assertSee('Paket mit 3 Inhalten')
->assertSee('ab 55,00 €');
$this->get(route('shop', ['suche' => 'Grenzen setzen']))->assertSee(route('public.package', $this->bundle()), false);
$this->get(route('shop', ['suche' => 'gibtesnicht']))->assertDontSee(route('public.package', $this->bundle()), false);
}
public function test_the_package_offer_is_added_under_the_package_name_and_alternatives_need_a_switch(): void
{
$first = $this->standaloneOffer('Starterpaket Einzel', 5500);
$second = $this->standaloneOffer('Starterpaket Mehrfach', 9900, 'mehrfach', 3);
$tina = $this->tina();
$this->actingAs($tina)->post(route('cart.add', $first))->assertRedirect(route('public.package', $this->bundle()));
$this->post(route('cart.add', $second))->assertSessionHas('shop.switch', $second->id);
$cart = app(Cart::class);
$review = $cart->review($tina);
$this->assertSame(CartLine::OK, $review->lines[0]->state);
$this->assertSame('Starterpaket', $review->lines[0]->title());
$this->assertSame($first->id, $cart->alternativeTo($second)?->id);
$this->post(route('cart.switch', $second))->assertRedirect(route('cart'));
$this->get(route('cart'))->assertSee('Mehrfachlizenz · bis zu 3 Trainer')->assertDontSee('Starterpaket Einzel');
}
public function test_the_package_page_is_gone_without_a_purchasable_option(): void
{
$offer = $this->standaloneOffer('Starterpaket solo', 5500);
$this->get(route('public.package', $this->bundle()))->assertOk();
app(OfferService::class)->end($offer);
$this->get(route('public.package', $this->bundle()))->assertNotFound();
$this->get(route('shop'))->assertDontSee(route('public.package', $this->bundle()), false);
}
public function test_an_offer_with_an_own_package_page_consists_of_exactly_one_package_version(): void
{
$offers = app(OfferService::class);
$offer = $offers->create('Falsch');
$offers->assignProduct($offer, null, 'Falsch', true);
$rights = $offers->currentActiveVersion($this->privateKonflikte())->lines()->first()->rights;
$this->expectException(DomainRuleException::class);
$offers->activate($offer, [['target' => $this->article('shop-konflikte'), 'rights' => $rights]], ['grant_kind' => 'kauf', 'price_cents' => 1000]);
}
public function test_a_package_has_either_an_own_page_or_a_program_page(): void
{
$offer = app(OfferService::class)->create('Beides');
$this->expectException(DomainRuleException::class);
app(OfferService::class)->assignProduct($offer, $this->program('Konflikte im Training')->id, 'Beides', true);
}
public function test_the_package_page_does_not_exist_outside_the_shop(): void
{
$this->standaloneOffer('Starterpaket solo', 5500);
$this->app['env'] = 'production';
$this->get(route('public.package', $this->bundle()))->assertNotFound();
}
}
+3 -2
View File
@@ -3,6 +3,7 @@
namespace Tests\Feature;
use App\Domain\Catalog\CompositionVersionDisplayEvent;
use App\Domain\Catalog\LicenceScope;
use App\Domain\Catalog\Offer;
use App\Domain\Catalog\OfferService;
use App\Domain\Commerce\Cart;
@@ -293,7 +294,7 @@ public function test_the_rights_order_does_not_matter_when_comparing_owned_scope
]);
$this->ownByPurchase($tina, $this->privateGrenzen());
$same = $offers->create('Grenzen gleiche Rechte');
$offers->activate($same, [['target' => $this->article('shop-grenzen'), 'rights' => $reordered]], ['grant_kind' => 'kauf', 'price_cents' => 1900]);
$offers->activate($same, [['target' => $this->article('shop-grenzen'), 'rights' => $reordered]], ['grant_kind' => 'kauf', 'price_cents' => 1900, 'licence_scope' => LicenceScope::SINGLE]);
$cart = app(Cart::class);
$cart->add($same);
@@ -310,7 +311,7 @@ public function test_free_grants_and_manual_grants_count_as_owned_but_expired_or
'pdf' => ['ansehen', 'laden'],
]);
$free = $offers->create('Grenzen gratis');
$offers->activate($free, [['target' => $this->article('shop-grenzen'), 'rights' => $rights]], ['grant_kind' => 'auf_abruf']);
$offers->activate($free, [['target' => $this->article('shop-grenzen'), 'rights' => $rights]], ['grant_kind' => 'auf_abruf', 'licence_scope' => LicenceScope::SINGLE]);
$acquisition = app(GrantIssuer::class)->claim($tina, $free);
$cart = app(Cart::class);
+221
View File
@@ -0,0 +1,221 @@
<?php
namespace Tests\Feature;
use App\Domain\Catalog\ArticleService;
use App\Domain\Catalog\BundleService;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Catalog\LicenceScope;
use App\Domain\Catalog\Offer;
use App\Domain\Catalog\OfferService;
use App\Domain\Commerce\Cart;
use App\Domain\Commerce\CartLine;
use App\Domain\Commerce\Checkout;
use App\Domain\Entitlement\Acquisition;
use App\Domain\Entitlement\Grant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Support\BuildsShop;
use Tests\TestCase;
/**
* Einzel- und Mehrfachlizenz: Der Nutzerkreis gehört zur Lizenzoption, wird mit der Bestellung festgehalten und ist
* getrennt von Lizenzinhaber, Nutzungszweck, Preis und Downloadzugriff.
*/
class LicenceScopeTest extends TestCase
{
use BuildsShop;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seedShop();
}
/**
* Eine weitere Option mit denselben Positionen und Rechten wie die Quelle.
*
* @param array<string, mixed> $attributes
*/
private function variant(Offer $source, string $name, array $attributes): Offer
{
$active = app(OfferService::class)->currentActiveVersion($source);
$lines = $active->lines()->with('article', 'bundleVersion', 'rights')->get()->map(fn ($line): array => [
'target' => $line->article ?? $line->bundleVersion,
'rights' => $line->rights,
])->all();
$offer = app(OfferService::class)->create($name);
app(OfferService::class)->activate($offer, $lines, [
'grant_kind' => 'kauf',
'price_cents' => 4900,
'holder_kind' => $active->holder_kind,
...$attributes,
]);
$offer->update(['composition_id' => $source->composition_id]);
return $offer->refresh();
}
public function test_the_scope_is_validated_when_a_version_is_activated(): void
{
$source = $this->privateKonflikte();
foreach ([
['licence_scope' => 'einzel', 'trainer_count' => 3],
['licence_scope' => 'mehrfach', 'trainer_count' => 1],
['licence_scope' => 'mehrfach', 'trainer_count' => null],
['licence_scope' => 'mehrfach', 'trainer_count' => 100000],
['licence_scope' => null, 'trainer_count' => 4],
['licence_scope' => 'gruppe', 'trainer_count' => 2],
] as $attributes) {
try {
$this->variant($source, 'Ungültig', $attributes);
$this->fail('Ungültiger Nutzerkreis muss abgewiesen werden: '.json_encode($attributes));
} catch (DomainRuleException) {
$this->assertNull(Offer::query()->where('name', 'Ungültig')->first()?->versions()->where('status', 'aktiv')->first());
}
}
}
public function test_a_single_license_always_means_exactly_one_trainer(): void
{
$offer = $this->variant($this->privateKonflikte(), 'Einzel ohne Zahl', ['licence_scope' => 'einzel']);
$version = app(OfferService::class)->currentActiveVersion($offer);
$this->assertSame('einzel', $version->licence_scope);
$this->assertSame(1, $version->trainer_count);
}
public function test_the_scope_is_not_derived_from_purpose_price_or_discount(): void
{
$institutional = app(OfferService::class)->currentActiveVersion($this->clubKonflikte())->lines()->first()->rights;
$this->assertSame('institutionell', $institutional->purpose);
$single = $this->variant($this->clubKonflikte(), 'Institutionell, ein Trainer', ['licence_scope' => 'einzel', 'holder_kind' => 'organisation', 'price_cents' => 99900]);
$legacy = $this->variant($this->privateGrenzen(), 'Ohne Angabe', ['price_cents' => 1900]);
$this->assertSame('Einzellizenz · 1 Trainer', LicenceScope::label($single->versions()->first()->licence_scope, $single->versions()->first()->trainer_count));
$this->assertNull($legacy->versions()->first()->licence_scope, 'Ohne Angabe bleibt es ohne Angabe, auch bei anderem Preis oder Zweck.');
$this->assertNull($legacy->versions()->first()->trainer_count);
}
public function test_the_product_page_tells_single_and_multi_licenses_apart(): void
{
$page = $this->program('Konflikte im Training');
$this->variant($this->privateKonflikte(), 'Ohne Angabe', ['price_cents' => 1900]);
$this->get(route('public.program', $page))
->assertOk()
->assertSee('Nutzerkreis')
->assertSee('Einzellizenz · 1 Trainer')
->assertSee('Mehrfachlizenz · bis zu 5 Trainer')
->assertSee('Die Inhalte dürfen von einem Trainer eingesetzt werden.')
->assertSee('Die Inhalte dürfen von bis zu 5 Trainern eingesetzt werden.')
->assertSee('Nutzerkreis nicht festgelegt');
}
public function test_cart_checkout_and_order_show_the_scope_and_keep_the_agreed_one(): void
{
$tina = $this->tina();
$offer = $this->privateKonflikte();
$this->actingAs($tina)->post(route('cart.add', $offer));
$this->get(route('cart'))->assertSee('Einzellizenz · 1 Trainer');
$this->get(route('checkout'))->assertSee('Einzellizenz · 1 Trainer');
$order = $this->orderOf($tina, [$offer]);
$line = $order->lines->first();
$this->assertSame('einzel', $line->licence_scope);
$this->assertSame(1, $line->trainer_count);
app(Checkout::class)->confirmTestPayment($order, 'test-scope');
$conditions = Acquisition::query()->where('reference_type', 'order_line')->firstOrFail()->conditions;
$this->assertSame('einzel', $conditions['licence_scope']);
$this->assertSame(1, $conditions['trainer_count']);
// Eine spätere Fassung mit anderem Nutzerkreis ändert den gekauften Umfang nicht.
$lines = app(OfferService::class)->currentActiveVersion($offer)->lines()->with('article', 'rights')->get()->map(fn ($l): array => ['target' => $l->article, 'rights' => $l->rights])->all();
app(OfferService::class)->activate($offer, $lines, ['grant_kind' => 'kauf', 'price_cents' => 4900, 'licence_scope' => 'mehrfach', 'trainer_count' => 3]);
$this->assertSame('einzel', $line->fresh()->licence_scope);
$this->assertSame('einzel', Acquisition::query()->where('reference_type', 'order_line')->firstOrFail()->conditions['licence_scope']);
$this->get(route('member.order', $order))->assertOk()->assertSee('Einzellizenz · 1 Trainer')->assertDontSee('bis zu 3 Trainer');
}
public function test_a_club_purchase_keeps_the_club_as_holder_and_only_the_ordering_account_downloads(): void
{
$vera = $this->vera();
$order = $this->orderOf($vera, [$this->clubKonflikte()], $this->club()->id);
app(Checkout::class)->confirmTestPayment($order, 'test-club');
$order = $order->fresh(['lines']);
$this->assertSame($this->club()->id, $order->party_id, 'Lizenzinhaber ist der Verein.');
$this->assertSame($vera->id, $order->user_id, 'Das bestellende Konto bleibt davon getrennt festgehalten.');
$this->assertSame('mehrfach', $order->lines->first()->licence_scope);
$this->assertSame(5, $order->lines->first()->trainer_count);
$acquisition = Acquisition::query()->where('reference_type', 'order_line')->firstOrFail();
$this->assertSame($this->club()->id, $acquisition->party_id);
$this->assertSame($vera->id, $acquisition->acting_user_id);
$this->assertSame(5, $acquisition->conditions['trainer_count']);
$this->assertSame(1, Acquisition::query()->count(), 'Eine Mehrfachlizenz erzeugt zum Start keine weiteren Downloadkonten.');
$this->assertTrue(Grant::query()->usableBy($vera)->exists());
$this->assertFalse(Grant::query()->usableBy($this->max())->exists());
$this->assertFalse(Grant::query()->usableBy($this->tina())->exists());
$this->actingAs($vera)->get(route('member.order', $order))->assertOk()
->assertSee('Testverein Musterstadt e. V.')
->assertSee('Mehrfachlizenz · bis zu 5 Trainer');
}
public function test_an_owned_single_license_does_not_cover_a_multi_license_with_the_same_rights(): void
{
$tina = $this->tina();
$owned = $this->privateKonflikte();
$order = $this->orderOf($tina, [$owned]);
app(Checkout::class)->confirmTestPayment($order, 'test-own');
$multi = $this->variant($owned, 'Konflikte Mehrfach 3', ['licence_scope' => 'mehrfach', 'trainer_count' => 3]);
$single = $this->variant($owned, 'Konflikte Einzel nochmal', ['licence_scope' => 'einzel']);
$cart = app(Cart::class);
$cart->clear();
$cart->add($multi);
$this->assertSame(CartLine::OK, $cart->review($tina)->lines[0]->state, 'Eine Einzellizenz macht eine Mehrfachlizenz nicht zur „bereits erworben“.');
$cart->clear();
$cart->add($single);
$this->assertSame(CartLine::OWNED, $cart->review($tina)->lines[0]->state);
}
public function test_an_offer_without_published_content_cannot_be_activated(): void
{
$empty = app(ArticleService::class)->create('leer-ohne-stand');
$rights = app(OfferService::class)->currentActiveVersion($this->privateKonflikte())->lines()->first()->rights;
$offers = app(OfferService::class);
$offer = $offers->create('Ohne Inhalt');
try {
$offers->activate($offer, [['target' => $empty, 'rights' => $rights]], ['grant_kind' => 'kauf', 'price_cents' => 1000]);
$this->fail('Ein Artikel ohne veröffentlichten Stand darf nicht verkauft werden.');
} catch (DomainRuleException $exception) {
$this->assertStringContainsString('leer-ohne-stand', $exception->getMessage());
$this->assertStringContainsString('keinen veröffentlichten Stand', $exception->getMessage());
}
$bundles = app(BundleService::class);
$bundle = $bundles->publish($bundles->create('Leeres Paket'), [$empty]);
try {
$offers->activate($offer, [['target' => $bundle, 'rights' => $rights]], ['grant_kind' => 'kauf', 'price_cents' => 1000]);
$this->fail('Auch ein Paket mit einem Artikel ohne Stand darf nicht verkauft werden.');
} catch (DomainRuleException) {
$this->assertSame(0, $offer->versions()->count(), 'Es entsteht keine halbe Fassung.');
}
$offers->activate($offer, [['target' => $this->article('shop-konflikte'), 'rights' => $rights]], ['grant_kind' => 'kauf', 'price_cents' => 1000]);
$this->assertSame(1, $offer->versions()->count());
}
}
+344
View File
@@ -0,0 +1,344 @@
<?php
namespace Tests\Feature;
use App\Domain\Catalog\DomainRuleException;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageRevision;
use App\Domain\Commerce\Checkout;
use App\Domain\Commerce\Discount\DiscountCode;
use App\Domain\Commerce\Discount\DiscountService;
use App\Domain\Commerce\Order;
use App\Domain\Commerce\SalesTerms;
use App\Domain\Commerce\Shop;
use App\Domain\Commerce\Stripe\StripeGateway;
use App\Domain\Entitlement\Acquisition;
use App\Domain\Entitlement\Grant;
use App\Domain\Entitlement\GrantStatus;
use App\Mail\OrderConfirmed;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Client\Request as HttpRequest;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Tests\Support\BuildsShop;
use Tests\TestCase;
/**
* Verkaufsstart Deutschland: Pflichtangaben und Zustimmungen im Checkout, Kauf auf Rechnung für Vereine, Erstattung,
* Widerruf, Mail zur Vertragsbestätigung und die Produktivfreigabe (standardmäßig aus).
*/
class SalesStartTest extends TestCase
{
use BuildsShop;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seedShop();
$this->travelTo('2026-11-24 10:00:00');
}
private function publishLegalPage(string $path): void
{
$page = Page::query()->firstOrCreate(['path' => $path]);
$revision = PageRevision::query()->create(['page_id' => $page->id, 'blocks' => [], 'sections' => [], 'seo_title' => ltrim($path, '/')]);
$page->published_revision_id = $revision->id;
$page->save();
}
private function checkoutDigest(): string
{
$page = $this->get(route('checkout'))->assertOk();
preg_match('/name="digest" value="([a-f0-9]{64})"/', $page->getContent(), $match);
$this->assertNotEmpty($match, 'Die Bestellübersicht muss bestellbar sein.');
return $match[1];
}
public function test_the_checkout_shows_the_mandatory_information_and_the_order_button(): void
{
$this->actingAs($this->tina())->post(route('cart.add', $this->privateKonflikte()));
$this->publishLegalPage('/agb');
$this->get(route('checkout'))->assertOk()
->assertSee('Zahlungspflichtig bestellen')
->assertSee('Dein Vertrag')
->assertSee('Allgemeine Geschäftsbedingungen')
->assertSee('Widerrufsbelehrung (noch nicht veröffentlicht)')
->assertSee('Ich verlange ausdrücklich, dass mit der Ausführung des Vertrags vor Ablauf der Widerrufsfrist begonnen wird')
->assertSee('Mir ist bekannt, dass ich mein Widerrufsrecht verliere')
->assertSee('Testbetrieb')
->assertSee('Entwurf: Der Wortlaut der Zustimmungen ist noch nicht rechtlich geprüft.')
->assertSee('Es fehlen veröffentlichte Seiten')
->assertSee(url('/agb'), false);
}
public function test_an_order_needs_both_consents_and_keeps_the_proof(): void
{
$tina = $this->tina();
$this->actingAs($tina)->post(route('cart.add', $this->privateKonflikte()));
$digest = $this->checkoutDigest();
$this->post(route('checkout.place'), ['digest' => $digest])->assertSessionHasErrors('checkout');
$this->post(route('checkout.place'), ['digest' => $digest, 'zustimmung' => ['agb' => '1']])->assertSessionHasErrors('checkout');
$this->assertSame(0, Order::query()->count(), 'Ohne beide Zustimmungen entsteht keine Bestellung.');
$this->post(route('checkout.place'), ['digest' => $digest, ...$this->consentFields()])->assertRedirect();
$order = Order::query()->firstOrFail();
$this->assertSame($tina->id, $order->user_id);
$this->assertSame('2026-11-24 10:00:00', $order->terms_accepted_at->format('Y-m-d H:i:s'));
$this->assertSame(SalesTerms::VERSION, $order->terms_snapshot['version']);
$this->assertSame(SalesTerms::consents()['sofort'], $order->terms_snapshot['consents']['sofort']);
$this->assertSame(SalesTerms::consents()['agb'], $order->terms_snapshot['consents']['agb']);
$this->assertArrayHasKey('widerruf', $order->terms_snapshot['pages']);
}
public function test_the_confirmation_mail_repeats_scope_discount_and_consent(): void
{
Mail::fake();
app(DiscountService::class)->save(['code' => 'SOMMER10', 'label' => 'Sommer', 'kind' => 'prozent', 'percent' => 10, 'scope' => 'warenkorb', 'eligibility' => 'allgemein'], []);
$tina = $this->tina();
$cart = app(\App\Domain\Commerce\Cart::class);
$cart->add($this->privateKonflikte());
$cart->applyCode('SOMMER10', $tina);
$order = app(Checkout::class)->placeFromCart($tina, $cart, null, $cart->review($tina)->digest(), ['agb' => 1, 'sofort' => 1]);
app(Checkout::class)->confirmTestPayment($order, 'test-mail');
Mail::assertSent(OrderConfirmed::class, function (OrderConfirmed $mail): bool {
$text = $mail->render();
return str_contains($text, 'Vertragsbestätigung')
&& str_contains($text, 'Einzellizenz · 1 Trainer')
&& str_contains($text, 'Rabattcode SOMMER10')
&& str_contains($text, 'Gezahlter Betrag: 44,10 €')
&& str_contains($text, 'Mir ist bekannt, dass ich mein Widerrufsrecht verliere');
});
}
public function test_the_order_page_shows_discount_and_payable_amount_and_stripe_receives_the_discounted_amount(): void
{
config([
'services.payment.driver' => 'stripe', 'services.stripe.secret' => 'sk_test_nur_fuer_tests',
'services.stripe.webhook_secret' => 'whsec_x', 'services.stripe.api_base' => 'https://stripe.test',
]);
app(DiscountService::class)->save(['code' => 'SOMMER10', 'label' => 'Sommer', 'kind' => 'prozent', 'percent' => 10, 'scope' => 'warenkorb', 'eligibility' => 'allgemein'], []);
$tina = $this->tina();
$cart = app(\App\Domain\Commerce\Cart::class);
$cart->add($this->privateKonflikte());
$cart->add($this->privateGrenzen());
$cart->applyCode('SOMMER10', $tina);
$order = app(Checkout::class)->placeFromCart($tina, $cart, null, $cart->review($tina)->digest(), ['agb' => 1, 'sofort' => 1]);
Http::fake(['stripe.test/v1/checkout/sessions' => Http::response(['id' => 'cs_test_d', 'url' => 'https://checkout.stripe.test/c/pay/cs_test_d'])]);
$this->actingAs($tina)->get(route('member.order', $order))->assertOk()
->assertSee('Zu zahlen')->assertSee('70,20 €')->assertSee('Rabattcode')->assertSee('SOMMER10')->assertSee('−7,80 €');
$this->post(route('member.order.pay', $order))->assertRedirect('https://checkout.stripe.test/c/pay/cs_test_d');
Http::assertSent(function (HttpRequest $request): bool {
parse_str($request->body(), $body);
return (int) $body['line_items'][0]['price_data']['unit_amount'] === 4410
&& (int) $body['line_items'][1]['price_data']['unit_amount'] === 2610
&& str_contains((string) $body['line_items'][0]['price_data']['product_data']['description'], 'Listenpreis 49,00 €');
});
// Die Summe bei Stripe ist der gespeicherte Zahlbetrag; der Listenpreis wird nicht akzeptiert.
$checkout = app(Checkout::class);
try {
$checkout->confirmProviderPayment($order, 'stripe', 'cs_test_d', 7800, 'eur');
$this->fail('Der Listenpreis weicht vom Zahlbetrag ab.');
} catch (DomainRuleException) {
$this->assertSame('offen', $order->fresh()->status);
$this->assertSame(0, Acquisition::query()->count());
}
$checkout->confirmProviderPayment($order, 'stripe', 'cs_test_d', 7020, 'eur');
$checkout->confirmProviderPayment($order, 'stripe', 'cs_test_d', 7020, 'eur');
$this->assertSame('bezahlt', $order->fresh()->status);
$this->assertSame(2, Acquisition::query()->count(), 'Eine wiederholte Bestätigung schaltet nichts doppelt frei.');
}
public function test_a_club_can_order_on_invoice_and_only_the_operator_confirms_the_payment(): void
{
$vera = $this->vera();
$this->actingAs($vera)->post(route('cart.add', $this->clubKonflikte()));
$this->get(route('checkout'))->assertOk()->assertSee('Auf Rechnung (Überweisung, auf Anfrage)');
$this->post(route('checkout.place'), ['digest' => $this->checkoutDigest(), 'zahlungsweg' => 'rechnung', ...$this->consentFields()])->assertRedirect();
$order = Order::query()->firstOrFail();
$this->assertSame('rechnung', $order->payment_method);
$this->assertSame('offen', $order->status);
$this->assertSame($this->club()->id, $order->party_id);
$this->get(route('member.order', $order))->assertOk()
->assertSee('Wir erstellen die Rechnung manuell')
->assertDontSee('Testzahlung bestätigen')
->assertDontSee('Jetzt bezahlen');
$this->post(route('member.order.confirm', $order))->assertSessionHasErrors('payment');
$this->assertSame('offen', $order->fresh()->status, 'Das Mitglied kann den Zahlungseingang nicht selbst bestätigen.');
$this->assertFalse(Grant::query()->usableBy($vera)->exists());
app(Checkout::class)->confirmInvoicePayment($order, 'RE-2026-001', $this->tina());
app(Checkout::class)->confirmInvoicePayment($order, 'RE-2026-001', $this->tina());
$this->assertSame('bezahlt', $order->fresh()->status);
$this->assertSame('ueberweisung', $order->payments()->firstOrFail()->provider);
$this->assertSame(1, Acquisition::query()->count());
$this->assertTrue(Grant::query()->usableBy($vera)->exists());
$this->assertFalse(Grant::query()->usableBy($this->max())->exists());
}
public function test_a_private_buyer_cannot_order_on_invoice_and_a_test_order_cannot_be_confirmed_as_invoice(): void
{
$tina = $this->tina();
$this->actingAs($tina)->post(route('cart.add', $this->privateKonflikte()));
$this->get(route('checkout'))->assertOk()->assertDontSee('Auf Rechnung');
$this->post(route('checkout.place'), ['digest' => $this->checkoutDigest(), 'zahlungsweg' => 'rechnung', ...$this->consentFields()])->assertSessionHasErrors('checkout');
$this->assertSame(0, Order::query()->count());
$order = $this->orderOf($tina, [$this->privateKonflikte()]);
$this->expectException(DomainRuleException::class);
app(Checkout::class)->confirmInvoicePayment($order, 'RE-1', $this->vera());
}
public function test_a_paid_stripe_order_is_refunded_at_stripe_before_rights_are_revoked(): void
{
config(['services.payment.driver' => 'stripe', 'services.stripe.secret' => 'sk_test_nur_fuer_tests', 'services.stripe.api_base' => 'https://stripe.test']);
$order = $this->orderOf($this->tina(), [$this->privateKonflikte(), $this->privateGrenzen()]);
app(Checkout::class)->confirmProviderPayment($order, 'stripe', 'cs_test_r', 7800, 'eur');
$order->payments()->update(['provider_payment_ref' => 'pi_test_r']);
Http::fake(['stripe.test/v1/refunds' => Http::response(['id' => 're_1', 'status' => 'succeeded'])]);
app(Checkout::class)->refund($order, 'Widerruf geprüft');
Http::assertSent(function (HttpRequest $request) use ($order): bool {
parse_str($request->body(), $body);
return $request->url() === 'https://stripe.test/v1/refunds'
&& $body['payment_intent'] === 'pi_test_r'
&& (int) $body['amount'] === 7800
&& $request->header('Idempotency-Key')[0] === 'luf-refund-'.$order->id.'-'.$order->payments()->firstOrFail()->id;
});
$this->assertSame('erstattet', $order->fresh()->status);
$this->assertSame(0, Grant::query()->where('status', GrantStatus::Aktiv)->count());
}
public function test_a_failed_refund_at_stripe_revokes_nothing(): void
{
config(['services.payment.driver' => 'stripe', 'services.stripe.secret' => 'sk_test_nur_fuer_tests', 'services.stripe.api_base' => 'https://stripe.test']);
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
app(Checkout::class)->confirmProviderPayment($order, 'stripe', 'cs_test_f', 4900, 'eur');
$order->payments()->update(['provider_payment_ref' => 'pi_test_f']);
Http::fake(['stripe.test/v1/refunds' => Http::response(['error' => ['type' => 'invalid_request_error', 'message' => 'x']], 400)]);
try {
app(Checkout::class)->refund($order, 'Widerruf geprüft');
$this->fail('Ohne Rückzahlung bei Stripe darf nichts entzogen werden.');
} catch (DomainRuleException) {
$this->assertSame('bezahlt', $order->fresh()->status);
$this->assertSame(1, Grant::query()->where('status', GrantStatus::Aktiv)->count());
}
}
public function test_an_invoice_order_is_refunded_without_stripe_and_free_grants_stay(): void
{
$vera = $this->vera();
$order = $this->orderOf($vera, [$this->clubKonflikte()], $this->club()->id);
$order->update(['payment_method' => 'rechnung']);
app(Checkout::class)->confirmInvoicePayment($order, 'RE-9', $this->tina());
Http::fake();
app(Checkout::class)->refund($order, 'Storno nach Rücksprache');
Http::assertNothingSent();
$this->assertSame('erstattet', $order->fresh()->status);
$this->assertFalse(Grant::query()->usableBy($vera)->where('status', GrantStatus::Aktiv)->exists());
}
public function test_the_member_declares_a_withdrawal_and_the_operator_decides(): void
{
config(['services.payment.driver' => 'stripe', 'services.stripe.secret' => 'sk_test_nur_fuer_tests']);
$tina = $this->tina();
$order = $this->orderOf($tina, [$this->privateKonflikte()]);
app(Checkout::class)->confirmProviderPayment($order, 'stripe', 'cs_test_w', 4900, 'eur');
$this->actingAs($tina)->get(route('member.order', $order))->assertOk()->assertSee('Widerruf erklären')->assertDontSee('Widerrufen (Testzahlung)');
$this->post(route('member.order.refund', $order))->assertNotFound();
$this->post(route('member.order.withdraw', $order), ['note' => 'Passt nicht'])->assertRedirect(route('member.order', $order));
$this->assertNotNull($order->fresh()->withdrawal_requested_at);
$this->assertSame('Passt nicht', $order->fresh()->withdrawal_note);
$this->assertSame('bezahlt', $order->fresh()->status, 'Die Erklärung entzieht und erstattet nichts; der Betreiber prüft.');
$this->assertSame(1, Grant::query()->where('status', GrantStatus::Aktiv)->count());
$this->actingAs($this->max())->post(route('member.order.withdraw', $order))->assertForbidden();
}
public function test_the_shop_stays_off_in_production_unless_released_keyed_and_documented(): void
{
$this->app['env'] = 'production';
config(['services.payment.driver' => 'stripe', 'services.stripe.secret' => 'sk_live_nur_fuer_tests']);
$this->assertFalse(config('shop.live_release'), 'Die Freigabe ist standardmäßig aus.');
$this->assertFalse(Shop::enabled());
$this->assertFalse(Shop::stripe());
config(['shop.live_release' => true]);
$this->assertFalse(Shop::live(), 'Ohne veröffentlichte Rechtstexte bleibt der Shop aus.');
foreach (['/agb', '/lizenzbedingungen', '/widerruf', '/impressum', '/datenschutz'] as $path) {
$this->publishLegalPage($path);
}
$this->assertTrue(Shop::live());
$this->assertTrue(Shop::enabled());
$this->assertTrue(Shop::stripe());
config(['services.stripe.secret' => 'sk_test_nur_fuer_tests']);
$this->assertFalse(Shop::live(), 'Mit einem Testschlüssel ist Produktion nie live.');
config(['services.stripe.secret' => 'sk_live_nur_fuer_tests', 'services.payment.driver' => 'test']);
$this->assertFalse(Shop::live(), 'Der Testzahlungsweg gibt Produktion nie frei.');
config(['services.payment.driver' => 'stripe', 'shop.live_release' => false]);
$this->assertFalse(Shop::live());
}
public function test_stripe_refuses_a_live_key_outside_production_and_any_call_in_production_without_release(): void
{
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
config(['services.payment.driver' => 'stripe', 'services.stripe.secret' => 'sk_live_nur_fuer_tests']);
Http::fake();
try {
app(StripeGateway::class)->checkoutFor($order);
$this->fail('Ein Live-Schlüssel darf außerhalb der Produktion nicht verwendet werden.');
} catch (DomainRuleException $exception) {
$this->assertStringContainsString('Testmodus', $exception->getMessage());
}
$this->app['env'] = 'production';
try {
app(StripeGateway::class)->checkoutFor($order);
$this->fail('Ohne Produktivfreigabe geht kein Aufruf an Stripe.');
} catch (DomainRuleException $exception) {
$this->assertStringContainsString('nicht freigegeben', $exception->getMessage());
}
Http::assertNothingSent();
}
public function test_the_checkout_and_the_order_say_that_the_code_changes_the_price_not_the_scope(): void
{
$code = DiscountCode::query()->count();
$this->assertSame(0, $code);
$this->actingAs($this->tina())->post(route('cart.add', $this->privateKonflikte()));
$this->get(route('checkout'))->assertOk()->assertSee('Pro Bestellung gilt höchstens ein Code. Er ändert den Preis, nicht den Nutzungsumfang.');
}
}