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(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(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.'); } public function test_an_invoice_order_is_never_paid_by_card_even_when_stripe_is_the_driver(): void { config(['services.payment.driver' => 'stripe', 'services.stripe.secret' => 'sk_test_nur_fuer_tests', 'services.stripe.api_base' => 'https://stripe.test']); $vera = $this->vera(); $cart = app(Cart::class); $cart->add($this->clubKonflikte()); $order = app(Checkout::class)->placeFromCart($vera, $cart, $this->club()->id, $cart->review($vera, $this->club()->id)->digest(), ['agb' => 1, 'sofort' => 1], 'rechnung'); Http::fake(); $this->actingAs($vera)->get(route('member.order', $order))->assertOk()->assertDontSee('Jetzt bezahlen'); $this->post(route('member.order.pay', $order))->assertSessionHasErrors('payment'); Http::assertNothingSent(); } public function test_the_operator_who_confirmed_an_invoice_payment_is_recorded(): void { $order = $this->orderOf($this->vera(), [$this->clubKonflikte()], $this->club()->id); $order->update(['payment_method' => 'rechnung']); $operator = $this->tina(); app(Checkout::class)->confirmInvoicePayment($order, 'RE-77', $operator); $this->assertSame($operator->id, $order->payments()->firstOrFail()->confirmed_by); } public function test_in_live_mode_only_the_checkout_with_consents_creates_an_order(): void { $this->app['env'] = 'production'; config(['shop.live_release' => true, 'services.payment.driver' => 'stripe', 'services.stripe.secret' => 'sk_live_nur_fuer_tests']); foreach (['/agb', '/lizenzbedingungen', '/widerruf', '/impressum', '/datenschutz'] as $path) { $this->publishLegalPage($path); } $this->assertTrue(Shop::live()); $tina = $this->tina(); try { app(Checkout::class)->place($tina, $this->privateKonflikte()); $this->fail('Der alte Einzelkauf kennt keine Pflichtangaben und ist im Livebetrieb gesperrt.'); } catch (DomainRuleException) { $this->assertSame(0, Order::query()->count()); } $cart = app(Cart::class); $cart->add($this->privateKonflikte()); try { app(Checkout::class)->placeFromCart($tina, $cart, null, $cart->review($tina)->digest()); $this->fail('Ohne Zustimmungen entsteht im Livebetrieb keine Bestellung.'); } catch (DomainRuleException $exception) { $this->assertStringContainsString('Zustimmungen', $exception->getMessage()); $this->assertSame(0, Order::query()->count()); } $order = app(Checkout::class)->placeFromCart($tina, $cart, null, $cart->review($tina)->digest(), ['agb' => 1, 'sofort' => 1]); $this->assertNotNull($order->terms_accepted_at); } }