Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9f7c645baf |
No files matched your search
@@ -140,6 +140,27 @@ private function createOrder(User $user, Party $holder, array $pairs): Order
|
||||
public function confirmTestPayment(Order $order, string $reference): Order
|
||||
{
|
||||
$this->assertTestDriver();
|
||||
|
||||
return $this->settle($order, 'test', $reference);
|
||||
}
|
||||
|
||||
/**
|
||||
* Zahlung eines Anbieters (Stripe): wie die Testzahlung, aber erst nach Prüfung von Betrag und Währung gegen die
|
||||
* Bestellung. Wiederholte Meldungen sind wirkungslos.
|
||||
*/
|
||||
public function confirmProviderPayment(Order $order, string $provider, string $reference, int $amountCents, string $currency): Order
|
||||
{
|
||||
$expected = (int) ($order->total_cents ?? $order->lines()->sum('price_cents'));
|
||||
|
||||
if ($amountCents !== $expected || strtoupper($currency) !== strtoupper((string) ($order->currency ?? 'EUR'))) {
|
||||
throw new DomainRuleException('Der gezahlte Betrag weicht von der Bestellung ab.');
|
||||
}
|
||||
|
||||
return $this->settle($order, $provider, $reference);
|
||||
}
|
||||
|
||||
private function settle(Order $order, string $provider, string $reference): Order
|
||||
{
|
||||
$reference = trim($reference);
|
||||
|
||||
if ($reference === '') {
|
||||
@@ -147,7 +168,7 @@ public function confirmTestPayment(Order $order, string $reference): Order
|
||||
}
|
||||
|
||||
$paidNow = false;
|
||||
$order = DB::transaction(function () use ($order, $reference, &$paidNow): Order {
|
||||
$order = DB::transaction(function () use ($order, $provider, $reference, &$paidNow): Order {
|
||||
$order = Order::query()->lockForUpdate()->findOrFail($order->id);
|
||||
|
||||
if ($order->status === 'bezahlt') {
|
||||
@@ -158,11 +179,11 @@ public function confirmTestPayment(Order $order, string $reference): Order
|
||||
throw new DomainRuleException('Diese Bestellung nimmt keine Zahlung mehr an.');
|
||||
}
|
||||
|
||||
$order->payments()->create([
|
||||
'provider' => 'test',
|
||||
'status' => 'bestaetigt',
|
||||
'provider_reference' => $reference,
|
||||
]);
|
||||
// Eine bei Stripe angelegte offene Zahlung mit derselben Referenz wird bestätigt statt doppelt geführt.
|
||||
$order->payments()->updateOrCreate(
|
||||
['provider_reference' => $reference],
|
||||
['provider' => $provider, 'status' => 'bestaetigt'],
|
||||
);
|
||||
$order->status = 'bezahlt';
|
||||
$order->save();
|
||||
$paidNow = true;
|
||||
|
||||
@@ -9,8 +9,19 @@
|
||||
*/
|
||||
final class Shop
|
||||
{
|
||||
public static function driver(): string
|
||||
{
|
||||
return (string) config('services.payment.driver');
|
||||
}
|
||||
|
||||
public static function enabled(): bool
|
||||
{
|
||||
return ! app()->isProduction() && config('services.payment.driver') === 'test';
|
||||
return ! app()->isProduction() && in_array(self::driver(), ['test', 'stripe'], true);
|
||||
}
|
||||
|
||||
/** Stripe-Kasse (Etappe 1: nur Testmodus, nie in Produktion). */
|
||||
public static function stripe(): bool
|
||||
{
|
||||
return ! app()->isProduction() && self::driver() === 'stripe';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Commerce\Stripe;
|
||||
|
||||
use App\Domain\Catalog\DomainRuleException;
|
||||
use App\Domain\Commerce\Order;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Http\Client\PendingRequest;
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Stripe-Kasse (Checkout Session) für die Testzahlung in Etappe 1. Ausschließlich Testschlüssel: Ein Live-Schlüssel
|
||||
* oder die Produktionsumgebung werden abgewiesen, bevor ein Aufruf an Stripe geht. Bezahlt wird bei Stripe; Kartendaten
|
||||
* berühren diese Anwendung nie. Die Freischaltung erfolgt allein über die signierte Meldung (StripeWebhook).
|
||||
*/
|
||||
class StripeGateway
|
||||
{
|
||||
/**
|
||||
* Legt für die Bestellung eine Checkout Session an oder liefert die noch offene wieder.
|
||||
*
|
||||
* @return array{id: string, url: string}
|
||||
*/
|
||||
public function checkoutFor(Order $order): array
|
||||
{
|
||||
$this->assertSandbox();
|
||||
$order->loadMissing('lines', 'user');
|
||||
|
||||
if ($order->status !== 'offen') {
|
||||
throw new DomainRuleException('Diese Bestellung nimmt keine Zahlung mehr an.');
|
||||
}
|
||||
|
||||
$open = $this->reusableSession($order);
|
||||
|
||||
if ($open !== null) {
|
||||
return $open;
|
||||
}
|
||||
|
||||
$attempt = $order->payments()->where('provider', 'stripe')->count() + 1;
|
||||
$params = [
|
||||
'mode' => 'payment',
|
||||
'client_reference_id' => (string) $order->id,
|
||||
'customer_email' => $order->user->email,
|
||||
'locale' => 'de',
|
||||
'success_url' => route('member.order', $order).'?zahlung=zurueck',
|
||||
'cancel_url' => route('member.order', $order).'?zahlung=abbruch',
|
||||
'metadata' => ['order_id' => (string) $order->id],
|
||||
'payment_intent_data' => ['metadata' => ['order_id' => (string) $order->id]],
|
||||
];
|
||||
|
||||
foreach ($order->lines->values() as $index => $line) {
|
||||
$params['line_items'][$index] = [
|
||||
'quantity' => 1,
|
||||
'price_data' => [
|
||||
'currency' => strtolower((string) ($line->currency ?? $order->currency ?? 'EUR')),
|
||||
'unit_amount' => (int) $line->price_cents,
|
||||
'product_data' => ['name' => filled($line->offer_name) ? (string) $line->offer_name : 'Lizenz'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$response = $this->request()
|
||||
->withHeaders(['Idempotency-Key' => 'luf-order-'.$order->id.'-'.$attempt])
|
||||
->post('/v1/checkout/sessions', $params);
|
||||
|
||||
$this->assertOk($response, 'angelegt');
|
||||
$id = (string) $response->json('id');
|
||||
$url = (string) $response->json('url');
|
||||
|
||||
if ($id === '' || $url === '') {
|
||||
throw new DomainRuleException('Stripe hat keine Kasse geliefert. Bitte versuche es später noch einmal.');
|
||||
}
|
||||
|
||||
$order->payments()->create(['provider' => 'stripe', 'status' => 'offen', 'provider_reference' => $id]);
|
||||
|
||||
return ['id' => $id, 'url' => $url];
|
||||
}
|
||||
|
||||
public function assertSandbox(): void
|
||||
{
|
||||
$secret = (string) config('services.stripe.secret');
|
||||
|
||||
if (app()->isProduction()) {
|
||||
throw new DomainRuleException('Stripe ist in Produktion nicht freigegeben.');
|
||||
}
|
||||
|
||||
if ($secret === '') {
|
||||
throw new DomainRuleException('Stripe ist nicht eingerichtet: Der Schlüssel fehlt.');
|
||||
}
|
||||
|
||||
if (! str_starts_with($secret, 'sk_test_') && ! str_starts_with($secret, 'rk_test_')) {
|
||||
throw new DomainRuleException('Etappe 1 erlaubt nur den Stripe-Testmodus (Schlüssel sk_test_).');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{id: string, url: string}|null
|
||||
*/
|
||||
private function reusableSession(Order $order): ?array
|
||||
{
|
||||
$payment = $order->payments()->where('provider', 'stripe')->where('status', 'offen')->latest('id')->first();
|
||||
|
||||
if ($payment === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = $this->request()->get('/v1/checkout/sessions/'.$payment->provider_reference);
|
||||
} catch (ConnectionException) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($response->successful() && $response->json('status') === 'open' && filled($response->json('url'))) {
|
||||
return ['id' => (string) $payment->provider_reference, 'url' => (string) $response->json('url')];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function request(): PendingRequest
|
||||
{
|
||||
return Http::baseUrl(rtrim((string) config('services.stripe.api_base'), '/'))
|
||||
->withToken((string) config('services.stripe.secret'))
|
||||
->asForm()
|
||||
->acceptJson()
|
||||
->timeout(15);
|
||||
}
|
||||
|
||||
private function assertOk(Response $response, string $what): void
|
||||
{
|
||||
if ($response->successful()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Nur Statuscode und Fehlertyp ins Log, nie Schlüssel oder Kundendaten.
|
||||
Log::warning('Stripe-Anfrage fehlgeschlagen', [
|
||||
'status' => $response->status(),
|
||||
'type' => $response->json('error.type'),
|
||||
'code' => $response->json('error.code'),
|
||||
]);
|
||||
|
||||
throw new DomainRuleException('Die Zahlung konnte bei Stripe nicht '.$what.' werden. Bitte versuche es später noch einmal.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Commerce\Stripe;
|
||||
|
||||
use App\Domain\Catalog\DomainRuleException;
|
||||
|
||||
/**
|
||||
* Prüft die Signatur einer Stripe-Meldung (Header „Stripe-Signature“: t=Zeit, v1=HMAC-SHA256 über „t.Nutzlast“).
|
||||
* Eine Meldung ohne gültige Signatur oder außerhalb der Zeittoleranz wird abgewiesen.
|
||||
*/
|
||||
class StripeSignature
|
||||
{
|
||||
public const TOLERANCE_SECONDS = 300;
|
||||
|
||||
public function verify(string $payload, ?string $header, ?int $now = null): void
|
||||
{
|
||||
$secret = (string) config('services.stripe.webhook_secret');
|
||||
|
||||
if ($secret === '') {
|
||||
throw new DomainRuleException('Das Webhook-Geheimnis ist nicht eingerichtet.');
|
||||
}
|
||||
|
||||
if ($header === null || trim($header) === '') {
|
||||
throw new DomainRuleException('Die Signatur fehlt.');
|
||||
}
|
||||
|
||||
$timestamp = null;
|
||||
$signatures = [];
|
||||
|
||||
foreach (explode(',', $header) as $part) {
|
||||
[$key, $value] = array_pad(explode('=', trim($part), 2), 2, '');
|
||||
|
||||
if ($key === 't') {
|
||||
$timestamp = ctype_digit($value) ? (int) $value : null;
|
||||
} elseif ($key === 'v1' && $value !== '') {
|
||||
$signatures[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
if ($timestamp === null || $signatures === []) {
|
||||
throw new DomainRuleException('Die Signatur ist unvollständig.');
|
||||
}
|
||||
|
||||
$expected = hash_hmac('sha256', $timestamp.'.'.$payload, $secret);
|
||||
$valid = false;
|
||||
|
||||
foreach ($signatures as $signature) {
|
||||
$valid = hash_equals($expected, $signature) || $valid;
|
||||
}
|
||||
|
||||
if (! $valid) {
|
||||
throw new DomainRuleException('Die Signatur stimmt nicht.');
|
||||
}
|
||||
|
||||
if (abs(($now ?? now()->getTimestamp()) - $timestamp) > self::TOLERANCE_SECONDS) {
|
||||
throw new DomainRuleException('Die Meldung ist zu alt.');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,9 @@
|
||||
use App\Domain\Commerce\Checkout;
|
||||
use App\Domain\Commerce\Order;
|
||||
use App\Domain\Commerce\OrderHolders;
|
||||
use App\Domain\Commerce\Shop;
|
||||
use App\Domain\Commerce\StaleOverview;
|
||||
use App\Domain\Commerce\Stripe\StripeGateway;
|
||||
use App\Domain\Entitlement\GrantIssuer;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
@@ -99,9 +101,26 @@ public function show(Request $request, Order $order): View
|
||||
return view('member.order', [
|
||||
'order' => $order,
|
||||
'testPayment' => ! app()->isProduction() && config('services.payment.driver') === 'test',
|
||||
'stripePayment' => Shop::stripe(),
|
||||
'zahlung' => $request->query('zahlung'),
|
||||
]);
|
||||
}
|
||||
|
||||
/** Leitet zur Stripe-Kasse (Testmodus). Freigeschaltet wird erst durch die signierte Meldung von Stripe. */
|
||||
public function pay(Request $request, Order $order, StripeGateway $stripe): RedirectResponse
|
||||
{
|
||||
abort_unless($order->user_id === $request->user()->id, 403);
|
||||
abort_unless(Shop::stripe(), 404);
|
||||
|
||||
try {
|
||||
$session = $stripe->checkoutFor($order);
|
||||
} catch (DomainRuleException $exception) {
|
||||
return back()->withErrors(['payment' => $exception->getMessage()]);
|
||||
}
|
||||
|
||||
return redirect()->away($session['url']);
|
||||
}
|
||||
|
||||
public function confirm(Request $request, Order $order, Checkout $checkout): RedirectResponse
|
||||
{
|
||||
abort_unless($order->user_id === $request->user()->id, 403);
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Domain\Catalog\DomainRuleException;
|
||||
use App\Domain\Commerce\Checkout;
|
||||
use App\Domain\Commerce\Order;
|
||||
use App\Domain\Commerce\Payment;
|
||||
use App\Domain\Commerce\Shop;
|
||||
use App\Domain\Commerce\Stripe\StripeSignature;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Empfängt die signierten Meldungen von Stripe (Etappe 1: Testmodus). Ohne gültige Signatur geschieht nichts. Die
|
||||
* Freischaltung nutzt denselben geprüften Weg wie die Testzahlung und ist bei Wiederholung wirkungslos.
|
||||
* Antworten: 404 wenn Stripe nicht aktiv ist, 400 bei ungültiger Signatur, sonst 200 (Stripe wiederholt sonst).
|
||||
*/
|
||||
class StripeWebhookController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request, StripeSignature $signature, Checkout $checkout): JsonResponse
|
||||
{
|
||||
abort_unless(Shop::stripe(), 404);
|
||||
|
||||
$payload = $request->getContent();
|
||||
|
||||
try {
|
||||
$signature->verify($payload, $request->header('Stripe-Signature'));
|
||||
} catch (DomainRuleException $exception) {
|
||||
Log::warning('Stripe-Meldung abgewiesen', ['grund' => $exception->getMessage()]);
|
||||
|
||||
return response()->json(['error' => 'Ungültige Signatur.'], 400);
|
||||
}
|
||||
|
||||
$event = json_decode($payload, true);
|
||||
|
||||
if (! is_array($event) || ! isset($event['type'])) {
|
||||
return response()->json(['error' => 'Ungültige Nutzlast.'], 400);
|
||||
}
|
||||
|
||||
$session = (array) ($event['data']['object'] ?? []);
|
||||
|
||||
return match ($event['type']) {
|
||||
'checkout.session.completed', 'checkout.session.async_payment_succeeded' => $this->paid($session, $checkout),
|
||||
'checkout.session.expired', 'checkout.session.async_payment_failed' => $this->notPaid($session, (string) $event['type']),
|
||||
default => response()->json(['received' => true, 'handled' => false]),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $session
|
||||
*/
|
||||
private function paid(array $session, Checkout $checkout): JsonResponse
|
||||
{
|
||||
if (($session['payment_status'] ?? null) !== 'paid') {
|
||||
return response()->json(['received' => true, 'handled' => false]);
|
||||
}
|
||||
|
||||
$order = $this->orderOf($session);
|
||||
$reference = (string) ($session['id'] ?? '');
|
||||
|
||||
if ($order === null || $reference === '') {
|
||||
Log::warning('Stripe-Zahlung ohne zugehörige Bestellung', ['session' => $reference]);
|
||||
|
||||
return response()->json(['received' => true, 'handled' => false]);
|
||||
}
|
||||
|
||||
try {
|
||||
$checkout->confirmProviderPayment(
|
||||
$order,
|
||||
'stripe',
|
||||
$reference,
|
||||
(int) ($session['amount_total'] ?? -1),
|
||||
(string) ($session['currency'] ?? ''),
|
||||
);
|
||||
} catch (DomainRuleException $exception) {
|
||||
// Dauerhafter Fehler (Betragsabweichung, Bestellung nicht mehr offen): nichts freischalten, Zahlung zur
|
||||
// Prüfung festhalten und 200 antworten, damit Stripe nicht endlos wiederholt.
|
||||
Log::warning('Stripe-Zahlung nicht übernommen', ['order' => $order->id, 'session' => $reference, 'grund' => $exception->getMessage()]);
|
||||
$order->payments()->updateOrCreate(['provider_reference' => $reference], ['provider' => 'stripe', 'status' => 'zu_pruefen']);
|
||||
|
||||
return response()->json(['received' => true, 'handled' => false]);
|
||||
}
|
||||
|
||||
return response()->json(['received' => true, 'handled' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $session
|
||||
*/
|
||||
private function notPaid(array $session, string $type): JsonResponse
|
||||
{
|
||||
$payment = Payment::query()->where('provider', 'stripe')->where('provider_reference', (string) ($session['id'] ?? ''))->first();
|
||||
|
||||
// Die Bestellung bleibt offen und lässt sich erneut bezahlen; nur diese Kasse gilt nicht mehr.
|
||||
if ($payment !== null && $payment->status === 'offen') {
|
||||
$payment->status = $type === 'checkout.session.expired' ? 'abgelaufen' : 'fehlgeschlagen';
|
||||
$payment->save();
|
||||
}
|
||||
|
||||
return response()->json(['received' => true, 'handled' => $payment !== null]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $session
|
||||
*/
|
||||
private function orderOf(array $session): ?Order
|
||||
{
|
||||
$id = $session['client_reference_id'] ?? ($session['metadata']['order_id'] ?? null);
|
||||
|
||||
return is_numeric($id) ? Order::query()->find((int) $id) : null;
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,8 @@
|
||||
)
|
||||
->withMiddleware(function (Middleware $middleware): void {
|
||||
$middleware->trustProxies(at: '*');
|
||||
// Stripe sendet ohne Sitzung und CSRF-Token; die Signatur der Meldung ersetzt beides.
|
||||
$middleware->validateCsrfTokens(except: ['stripe/webhook']);
|
||||
$middleware->prepend(UseStaffSessionCookie::class);
|
||||
$middleware->prepend(UseRequestRootUrl::class);
|
||||
$middleware->prepend(IgnoreEmptyForwardedHeaders::class);
|
||||
|
||||
@@ -29,9 +29,16 @@
|
||||
],
|
||||
|
||||
'payment' => [
|
||||
// test: Testweg ohne Anbieter. stripe: Stripe-Kasse, in Etappe 1 nur mit Testschlüsseln (sk_test_).
|
||||
'driver' => env('PAYMENT_DRIVER', 'test'),
|
||||
],
|
||||
|
||||
'stripe' => [
|
||||
'secret' => env('STRIPE_SECRET'),
|
||||
'webhook_secret' => env('STRIPE_WEBHOOK_SECRET'),
|
||||
'api_base' => env('STRIPE_API_BASE', 'https://api.stripe.com'),
|
||||
],
|
||||
|
||||
'slack' => [
|
||||
'notifications' => [
|
||||
'bot_user_oauth_token' => env('SLACK_BOT_USER_OAUTH_TOKEN'),
|
||||
|
||||
@@ -27,7 +27,10 @@ MAIL_MAILER=log
|
||||
MAIL_FROM_ADDRESS="noreply@example.invalid"
|
||||
MAIL_FROM_NAME="${APP_NAME}"
|
||||
|
||||
# test bleibt der lokale Testweg. Ein echter Zahlungsdienst ist noch nicht festgelegt.
|
||||
# test: lokaler Testweg ohne Anbieter. stripe: Stripe-Kasse, in Etappe 1 nur mit Testschlüsseln und nie in Produktion.
|
||||
# Die Schlüssel stehen nur in /etc/luf/<umgebung>.env auf dem Server, nie im Repo.
|
||||
PAYMENT_DRIVER=test
|
||||
# STRIPE_SECRET=sk_test_... (Entwickler > API-Schlüssel, Testmodus)
|
||||
# STRIPE_WEBHOOK_SECRET=whsec_... (Entwickler > Webhooks > Endpunkt > Signiergeheimnis)
|
||||
|
||||
# Medienpfade setzt Compose auf den Host-Mount (/srv/luf/<env>/media/...). Hier nichts eintragen.
|
||||
@@ -27,6 +27,30 @@ ## Env-Datei ändern (zum Beispiel Mail, `APP_URL`)
|
||||
|
||||
**nginx findet die neue App selbst (seit 21.11.2026).** Der Web-Container löst den Namen `app` über Dockers DNS zur Laufzeit auf (`resolver 127.0.0.11` in `docker/nginx/default.conf`). Vorher war die Adresse beim Start von nginx festgeschrieben: Ein erneuter Lauf mit unverändertem Code erzeugte die App neu, nicht aber den Web-Container, und Prod antwortete mit 502, bis `luf-prod-web-1` neu gestartet wurde (20.11.2026). Ein Test (`tests/Unit/NginxConfigTest.php`) hält die Konfiguration fest.
|
||||
|
||||
## Stripe-Sandbox (nur Test, Etappe 1)
|
||||
|
||||
Die Stripe-Kasse läuft ausschließlich im Testmodus und nie in Produktion. Sie ist nur aktiv, wenn `PAYMENT_DRIVER=stripe` gesetzt ist; sonst bleibt es beim Testweg (`test`). Ein Schlüssel ohne `sk_test_` (oder `rk_test_`) wird abgewiesen, in Produktion lehnt die Anwendung Stripe ganz ab. Kartendaten berühren die Anwendung nie, bezahlt wird bei Stripe.
|
||||
|
||||
**Ablauf:** Bestellung anlegen, „Jetzt bezahlen (Stripe-Testmodus)“, Weiterleitung zur Stripe-Kasse. Freigeschaltet wird erst durch die signierte Meldung von Stripe an `POST /stripe/webhook` (Ereignis `checkout.session.completed`). Betrag und Währung werden gegen die Bestellung geprüft; wiederholte Meldungen schalten nichts doppelt frei. Eine Zahlung mit falschem Betrag oder auf eine nicht mehr offene Bestellung schaltet nichts frei und steht als Zahlung mit Status `zu_pruefen` in der Bestellung.
|
||||
|
||||
**Einrichten (Test):**
|
||||
|
||||
1. In Stripe den Testmodus („Sandbox“) wählen. Unter Entwickler > API-Schlüssel den Geheimschlüssel `sk_test_…` kopieren.
|
||||
2. Unter Entwickler > Webhooks einen Endpunkt anlegen: URL `https://dev.laraundfelix.de/stripe/webhook`, Ereignisse `checkout.session.completed`, `checkout.session.expired`, `checkout.session.async_payment_succeeded`, `checkout.session.async_payment_failed`. Das Signiergeheimnis `whsec_…` des Endpunkts kopieren.
|
||||
3. Auf llm-node in `/etc/luf/test.env` eintragen (Rechte `640`, nichts davon ins Repo, in Commits, Logs oder Chat):
|
||||
|
||||
```
|
||||
PAYMENT_DRIVER=stripe
|
||||
STRIPE_SECRET=sk_test_...
|
||||
STRIPE_WEBHOOK_SECRET=whsec_...
|
||||
```
|
||||
|
||||
4. In Gitea den letzten Lauf von „Deploy Test“ erneut starten (siehe „Env-Datei ändern“). `docker exec luf-test-app-1 printenv PAYMENT_DRIVER` muss `stripe` zeigen.
|
||||
5. Testkauf mit der Stripe-Testkarte `4242 4242 4242 4242` (beliebiges künftiges Ablaufdatum, beliebige Prüfziffer). In Stripe unter Entwickler > Webhooks > Endpunkt > Ereignisse ist jede Zustellung samt Antwortcode zu sehen.
|
||||
|
||||
**Zurück zum Testweg:** `PAYMENT_DRIVER=test` setzen und neu ausrollen. Offene Stripe-Bestellungen bleiben offen.
|
||||
|
||||
**Grenzen:** Erstattungen laufen nicht über Stripe. Die Schaltfläche „Widerrufen“ ist im Stripe-Modus ausgeblendet; die Rückzahlung gehört zum Verkaufsstart (Live-Stripe, Belegprozess, Rückabwicklung). Steuer, Rechnung, Rabattcodes und Rechtstexte sind nicht Teil der Sandbox.
|
||||
## Trennung Test und Prod (beide auf llm-node)
|
||||
|
||||
| | Test | Prod |
|
||||
|
||||
@@ -19,6 +19,11 @@
|
||||
<div class="errors" role="alert">{{ $errors->first() }}</div>
|
||||
@endif
|
||||
<p class="state">Status: {{ $statusText }}</p>
|
||||
@if ($order->status === 'offen' && $zahlung === 'zurueck')
|
||||
<p class="state" role="status">Danke. Wir warten auf die Bestätigung von Stripe. Sobald sie da ist, steht die Bestellung auf „Bezahlt“. <a href="{{ route('member.order', $order) }}">Status neu laden</a></p>
|
||||
@elseif ($order->status === 'offen' && $zahlung === 'abbruch')
|
||||
<p class="state" role="status">Die Zahlung wurde abgebrochen. Es wurde nichts abgebucht. Du kannst erneut bezahlen.</p>
|
||||
@endif
|
||||
<dl class="facts">
|
||||
<div><dt>Lizenzinhaber</dt><dd>{{ $order->party?->display_name }}</dd></div>
|
||||
<div><dt>Bestellt von</dt><dd>{{ $order->user?->name }}</dd></div>
|
||||
@@ -32,6 +37,12 @@
|
||||
@endforeach
|
||||
</ul>
|
||||
<p class="state">Summe: {{ \App\Domain\Commerce\Money::format($order->total_cents ?? $order->lines->sum('price_cents')) }}</p>
|
||||
@if ($stripePayment && $order->status === 'offen')
|
||||
<form method="post" action="{{ route('member.order.pay', $order) }}">
|
||||
@csrf
|
||||
<button class="button" type="submit">Jetzt bezahlen (Stripe-Testmodus)</button>
|
||||
</form>
|
||||
@endif
|
||||
@if ($testPayment && $order->status === 'offen')
|
||||
<form method="post" action="{{ route('member.order.confirm', $order) }}">
|
||||
@csrf
|
||||
@@ -44,9 +55,11 @@
|
||||
@endif
|
||||
@if ($order->status === 'bezahlt')
|
||||
<p><a class="button" href="{{ route('member.library') }}">Zur Bibliothek</a></p>
|
||||
<form method="post" action="{{ route('member.order.refund', $order) }}">
|
||||
@csrf
|
||||
<button class="button button-quiet" type="submit">Widerrufen</button>
|
||||
</form>
|
||||
@if (! $stripePayment)
|
||||
<form method="post" action="{{ route('member.order.refund', $order) }}">
|
||||
@csrf
|
||||
<button class="button button-quiet" type="submit">Widerrufen</button>
|
||||
</form>
|
||||
@endif
|
||||
@endif
|
||||
@endsection
|
||||
@@ -14,6 +14,7 @@
|
||||
use App\Http\Controllers\PublicCatalogController;
|
||||
use App\Http\Controllers\PublicMediaController;
|
||||
use App\Http\Controllers\ShopController;
|
||||
use App\Http\Controllers\StripeWebhookController;
|
||||
use App\Http\Controllers\ThemeController;
|
||||
use App\Http\Controllers\WebsiteMediaController;
|
||||
use App\Http\Middleware\EnsureShopEnabled;
|
||||
@@ -42,6 +43,8 @@
|
||||
});
|
||||
});
|
||||
|
||||
Route::post('/stripe/webhook', StripeWebhookController::class)->name('stripe.webhook');
|
||||
|
||||
Route::middleware('guest')->group(function () {
|
||||
Route::get('/registrieren', [RegistrationController::class, 'create'])->name('register');
|
||||
Route::post('/registrieren', [RegistrationController::class, 'store']);
|
||||
@@ -121,6 +124,7 @@
|
||||
Route::post('/angebote/{offer}/abrufen', [CheckoutController::class, 'claim'])->name('offers.claim');
|
||||
Route::post('/angebote/{offer}/kaufen', [CheckoutController::class, 'place'])->name('offers.buy');
|
||||
Route::get('/bestellung/{order}', [CheckoutController::class, 'show'])->name('order');
|
||||
Route::post('/bestellung/{order}/bezahlen', [CheckoutController::class, 'pay'])->name('order.pay');
|
||||
Route::post('/bestellung/{order}/bestaetigen', [CheckoutController::class, 'confirm'])->name('order.confirm');
|
||||
Route::post('/bestellung/{order}/ablehnen', [CheckoutController::class, 'fail'])->name('order.fail');
|
||||
Route::post('/bestellung/{order}/widerrufen', [CheckoutController::class, 'refund'])->name('order.refund');
|
||||
|
||||
@@ -136,7 +136,7 @@ public function test_public_page_is_identical_for_guests_and_members(): void
|
||||
public function test_global_layout_has_header_navigation_mobile_menu_and_footer(): void
|
||||
{
|
||||
// Der Warenkorb gehört zum Shop (Etappe 1, nur Testzahlung). Ohne aktiven Shop erscheint kein Kaufknopf.
|
||||
config(['services.payment.driver' => 'stripe']);
|
||||
config(['services.payment.driver' => 'unbekannt']);
|
||||
$html = $this->get('/praevention')->assertOk()->getContent();
|
||||
|
||||
$this->assertStringContainsString('<a class="skip" href="#inhalt">', $html);
|
||||
@@ -161,7 +161,7 @@ public function test_cart_entry_appears_only_when_the_shop_is_enabled(): void
|
||||
// statt der früheren Vorbereitung über cms.cart.*.
|
||||
$this->get('/praevention')->assertSee('Warenkorb')->assertSee('href="'.route('cart').'"', false);
|
||||
|
||||
config(['services.payment.driver' => 'stripe']);
|
||||
config(['services.payment.driver' => 'unbekannt']);
|
||||
$this->get('/praevention')->assertDontSee('Warenkorb');
|
||||
}
|
||||
|
||||
|
||||
@@ -98,7 +98,8 @@ public function test_the_shop_exists_only_with_test_payment_and_never_in_product
|
||||
{
|
||||
$this->assertTrue(Shop::enabled());
|
||||
|
||||
config(['services.payment.driver' => 'stripe']);
|
||||
// Ein nicht freigegebener Zahlungsweg (Stripe ist seit der Sandbox-Anbindung freigegeben, siehe StripeSandboxTest).
|
||||
config(['services.payment.driver' => 'unbekannt']);
|
||||
$this->get(route('shop'))->assertNotFound();
|
||||
$this->get(route('cart'))->assertNotFound();
|
||||
$this->post(route('cart.add', $this->privateKonflikte()))->assertNotFound();
|
||||
|
||||
@@ -0,0 +1,314 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Domain\Commerce\Order;
|
||||
use App\Domain\Commerce\Shop;
|
||||
use App\Domain\Entitlement\Acquisition;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\Client\Request as HttpRequest;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Testing\TestResponse;
|
||||
use Tests\Support\BuildsShop;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Stripe-Sandbox (Etappe 1): Kasse über eine Checkout Session, Freischaltung nur durch die signierte Meldung.
|
||||
* Alle Aufrufe an Stripe sind gefälscht (Http::fake); es geht nie ein echter Aufruf hinaus.
|
||||
*/
|
||||
class StripeSandboxTest extends TestCase
|
||||
{
|
||||
use BuildsShop;
|
||||
use RefreshDatabase;
|
||||
|
||||
private const WEBHOOK_SECRET = 'whsec_testgeheimnis_nur_fuer_tests';
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->seedShop();
|
||||
config([
|
||||
'services.payment.driver' => 'stripe',
|
||||
'services.stripe.secret' => 'sk_test_nur_fuer_tests',
|
||||
'services.stripe.webhook_secret' => self::WEBHOOK_SECRET,
|
||||
'services.stripe.api_base' => 'https://stripe.test',
|
||||
]);
|
||||
$this->travelTo('2026-11-22 10:00:00');
|
||||
}
|
||||
|
||||
public function test_stripe_is_a_shop_driver_but_never_in_production(): void
|
||||
{
|
||||
$this->assertTrue(Shop::enabled());
|
||||
$this->assertTrue(Shop::stripe());
|
||||
|
||||
$this->app['env'] = 'production';
|
||||
$this->assertFalse(Shop::enabled());
|
||||
$this->assertFalse(Shop::stripe());
|
||||
}
|
||||
|
||||
public function test_the_order_page_offers_stripe_and_not_the_test_buttons(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
|
||||
$this->actingAs($this->tina())->get(route('member.order', $order))
|
||||
->assertOk()
|
||||
->assertSee('Jetzt bezahlen (Stripe-Testmodus)')
|
||||
->assertDontSee('Testzahlung bestätigen')
|
||||
->assertDontSee('Testzahlung ablehnen');
|
||||
}
|
||||
|
||||
public function test_paying_creates_a_checkout_session_with_the_order_amounts_and_redirects(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte(), $this->privateGrenzen()]);
|
||||
Http::fake(['stripe.test/v1/checkout/sessions' => Http::response(['id' => 'cs_test_1', 'url' => 'https://checkout.stripe.test/c/pay/cs_test_1'])]);
|
||||
|
||||
$this->actingAs($this->tina())->post(route('member.order.pay', $order))
|
||||
->assertRedirect('https://checkout.stripe.test/c/pay/cs_test_1');
|
||||
|
||||
Http::assertSent(function (HttpRequest $request) use ($order): bool {
|
||||
parse_str($request->body(), $body);
|
||||
|
||||
return $request->url() === 'https://stripe.test/v1/checkout/sessions'
|
||||
&& $request->hasHeader('Authorization', 'Bearer sk_test_nur_fuer_tests')
|
||||
&& $request->hasHeader('Idempotency-Key', 'luf-order-'.$order->id.'-1')
|
||||
&& $body['mode'] === 'payment'
|
||||
&& $body['client_reference_id'] === (string) $order->id
|
||||
&& $body['metadata']['order_id'] === (string) $order->id
|
||||
&& $body['line_items'][0]['price_data']['currency'] === 'eur'
|
||||
&& (int) $body['line_items'][0]['price_data']['unit_amount'] === 4900
|
||||
&& (int) $body['line_items'][1]['price_data']['unit_amount'] === 2900
|
||||
&& str_ends_with($body['success_url'], '?zahlung=zurueck')
|
||||
&& str_ends_with($body['cancel_url'], '?zahlung=abbruch');
|
||||
});
|
||||
$this->assertSame('offen', $order->payments()->where('provider', 'stripe')->value('status'));
|
||||
$this->assertSame('offen', $order->fresh()->status, 'Bezahlt wird erst durch die signierte Meldung.');
|
||||
}
|
||||
|
||||
public function test_an_open_session_is_reused(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
$order->payments()->create(['provider' => 'stripe', 'status' => 'offen', 'provider_reference' => 'cs_test_alt']);
|
||||
|
||||
Http::fake(['stripe.test/v1/checkout/sessions/cs_test_alt' => Http::response(['id' => 'cs_test_alt', 'status' => 'open', 'url' => 'https://checkout.stripe.test/alt'])]);
|
||||
$this->actingAs($this->tina())->post(route('member.order.pay', $order))->assertRedirect('https://checkout.stripe.test/alt');
|
||||
Http::assertNotSent(fn (HttpRequest $request): bool => $request->method() === 'POST');
|
||||
}
|
||||
|
||||
public function test_an_expired_session_is_replaced_by_a_new_one(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
$order->payments()->create(['provider' => 'stripe', 'status' => 'offen', 'provider_reference' => 'cs_test_alt']);
|
||||
|
||||
Http::fake([
|
||||
'stripe.test/v1/checkout/sessions/cs_test_alt' => Http::response(['id' => 'cs_test_alt', 'status' => 'expired']),
|
||||
'stripe.test/v1/checkout/sessions' => Http::response(['id' => 'cs_test_neu', 'url' => 'https://checkout.stripe.test/neu']),
|
||||
]);
|
||||
$this->actingAs($this->tina())->post(route('member.order.pay', $order))->assertRedirect('https://checkout.stripe.test/neu');
|
||||
Http::assertSent(fn (HttpRequest $request): bool => $request->hasHeader('Idempotency-Key', 'luf-order-'.$order->id.'-2'));
|
||||
}
|
||||
|
||||
public function test_only_the_buyer_pays_and_only_an_open_order(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
Http::fake();
|
||||
|
||||
$this->actingAs($this->max())->post(route('member.order.pay', $order))->assertForbidden();
|
||||
Http::assertNothingSent();
|
||||
|
||||
$order->forceFill(['status' => 'bezahlt'])->save();
|
||||
$this->actingAs($this->tina())->post(route('member.order.pay', $order))
|
||||
->assertSessionHasErrors('payment');
|
||||
Http::assertNothingSent();
|
||||
}
|
||||
|
||||
public function test_only_test_keys_reach_stripe(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
Http::fake();
|
||||
|
||||
foreach (['sk_live_abc', '', 'pk_test_abc'] as $secret) {
|
||||
config(['services.stripe.secret' => $secret]);
|
||||
$this->actingAs($this->tina())->post(route('member.order.pay', $order))->assertSessionHasErrors('payment');
|
||||
}
|
||||
|
||||
Http::assertNothingSent();
|
||||
}
|
||||
|
||||
public function test_a_stripe_error_leaves_the_order_open_and_shows_a_plain_message(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
Http::fake(['stripe.test/*' => Http::response(['error' => ['type' => 'api_error']], 500)]);
|
||||
|
||||
$this->actingAs($this->tina())->post(route('member.order.pay', $order))
|
||||
->assertSessionHasErrors(['payment' => 'Die Zahlung konnte bei Stripe nicht angelegt werden. Bitte versuche es später noch einmal.']);
|
||||
$this->assertSame('offen', $order->fresh()->status);
|
||||
$this->assertSame(0, $order->payments()->count());
|
||||
}
|
||||
|
||||
public function test_a_signed_paid_event_grants_every_line_once_even_when_repeated(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte(), $this->privateGrenzen()]);
|
||||
$payload = $this->sessionEvent('checkout.session.completed', $order, 'cs_test_bezahlt', 7800);
|
||||
|
||||
$this->webhook($payload)->assertOk()->assertJson(['handled' => true]);
|
||||
$this->webhook($payload)->assertOk();
|
||||
|
||||
$this->assertSame('bezahlt', $order->fresh()->status);
|
||||
$this->assertSame(2, Acquisition::query()->where('reference_type', 'order_line')->whereIn('reference_id', $order->lines()->pluck('id'))->count(), 'Je Position genau ein Erwerb.');
|
||||
$this->assertSame(1, $order->payments()->count());
|
||||
$payment = $order->payments()->first();
|
||||
$this->assertSame('stripe', $payment->provider);
|
||||
$this->assertSame('bestaetigt', $payment->status);
|
||||
$this->assertSame('cs_test_bezahlt', $payment->provider_reference);
|
||||
}
|
||||
|
||||
public function test_a_paid_event_confirms_the_open_payment_row_instead_of_adding_one(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
$order->payments()->create(['provider' => 'stripe', 'status' => 'offen', 'provider_reference' => 'cs_test_zeile']);
|
||||
|
||||
$this->webhook($this->sessionEvent('checkout.session.completed', $order, 'cs_test_zeile', 4900))->assertOk();
|
||||
|
||||
$this->assertSame(1, $order->payments()->count());
|
||||
$this->assertSame('bestaetigt', $order->payments()->first()->status);
|
||||
}
|
||||
|
||||
public function test_an_unsigned_wrongly_signed_or_stale_message_does_nothing(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
$payload = $this->sessionEvent('checkout.session.completed', $order, 'cs_test_x', 4900);
|
||||
$before = Acquisition::query()->count();
|
||||
|
||||
$this->call('POST', route('stripe.webhook'), [], [], [], ['CONTENT_TYPE' => 'application/json'], $payload)->assertStatus(400);
|
||||
$this->webhook($payload, 'sig-falsch')->assertStatus(400);
|
||||
$this->webhook($payload, 't='.now()->getTimestamp().',v1=abc')->assertStatus(400);
|
||||
$this->webhook($payload, $this->signature($payload, now()->subMinutes(10)->getTimestamp()))->assertStatus(400);
|
||||
$this->webhook($payload.' ', $this->signature($payload))->assertStatus(400);
|
||||
|
||||
$this->assertSame('offen', $order->fresh()->status);
|
||||
$this->assertSame($before, Acquisition::query()->count(), 'Es wird nichts freigeschaltet.');
|
||||
}
|
||||
|
||||
public function test_a_wrong_amount_or_currency_grants_nothing_and_is_marked_for_review(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
|
||||
$this->webhook($this->sessionEvent('checkout.session.completed', $order, 'cs_test_zuwenig', 100))->assertOk()->assertJson(['handled' => false]);
|
||||
$this->webhook($this->sessionEvent('checkout.session.completed', $order, 'cs_test_usd', 4900, 'usd'))->assertOk()->assertJson(['handled' => false]);
|
||||
|
||||
$this->assertSame('offen', $order->fresh()->status);
|
||||
$this->assertSame(['zu_pruefen', 'zu_pruefen'], $order->payments()->orderBy('id')->pluck('status')->all());
|
||||
}
|
||||
|
||||
public function test_an_unpaid_session_an_unknown_order_and_unknown_events_are_ignored(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
|
||||
$this->webhook($this->sessionEvent('checkout.session.completed', $order, 'cs_test_offen', 4900, 'eur', 'unpaid'))->assertOk()->assertJson(['handled' => false]);
|
||||
$this->webhook($this->event('checkout.session.completed', ['id' => 'cs_x', 'client_reference_id' => '999999', 'payment_status' => 'paid', 'amount_total' => 1, 'currency' => 'eur']))->assertOk()->assertJson(['handled' => false]);
|
||||
$this->webhook($this->event('customer.created', ['id' => 'cus_1']))->assertOk()->assertJson(['handled' => false]);
|
||||
|
||||
$this->assertSame('offen', $order->fresh()->status);
|
||||
}
|
||||
|
||||
public function test_an_expired_session_keeps_the_order_open_and_payable(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
$order->payments()->create(['provider' => 'stripe', 'status' => 'offen', 'provider_reference' => 'cs_test_abgelaufen']);
|
||||
|
||||
$this->webhook($this->sessionEvent('checkout.session.expired', $order, 'cs_test_abgelaufen', 4900, 'eur', 'unpaid'))->assertOk();
|
||||
|
||||
$this->assertSame('offen', $order->fresh()->status);
|
||||
$this->assertSame('abgelaufen', $order->payments()->first()->status);
|
||||
}
|
||||
|
||||
public function test_a_payment_for_an_order_that_is_no_longer_open_is_not_applied(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
$order->forceFill(['status' => 'fehlgeschlagen'])->save();
|
||||
|
||||
$this->webhook($this->sessionEvent('checkout.session.completed', $order, 'cs_test_spaet', 4900))->assertOk()->assertJson(['handled' => false]);
|
||||
|
||||
$this->assertSame('fehlgeschlagen', $order->fresh()->status);
|
||||
$this->assertSame('zu_pruefen', $order->payments()->first()->status);
|
||||
}
|
||||
|
||||
public function test_the_webhook_does_not_exist_outside_stripe_mode_or_in_production(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
$payload = $this->sessionEvent('checkout.session.completed', $order, 'cs_test_x', 4900);
|
||||
|
||||
config(['services.payment.driver' => 'test']);
|
||||
$this->webhook($payload)->assertNotFound();
|
||||
|
||||
config(['services.payment.driver' => 'stripe']);
|
||||
$this->app['env'] = 'production';
|
||||
$this->webhook($payload)->assertNotFound();
|
||||
$this->assertSame('offen', $order->fresh()->status);
|
||||
}
|
||||
|
||||
public function test_without_a_webhook_secret_every_message_is_refused(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
$payload = $this->sessionEvent('checkout.session.completed', $order, 'cs_test_x', 4900);
|
||||
$header = $this->signature($payload);
|
||||
config(['services.stripe.webhook_secret' => '']);
|
||||
|
||||
$this->webhook($payload, $header)->assertStatus(400);
|
||||
$this->assertSame('offen', $order->fresh()->status);
|
||||
}
|
||||
|
||||
public function test_the_return_page_explains_waiting_and_cancelling(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
|
||||
$this->actingAs($this->tina())->get(route('member.order', $order).'?zahlung=zurueck')->assertSee('Wir warten auf die Bestätigung von Stripe');
|
||||
$this->actingAs($this->tina())->get(route('member.order', $order).'?zahlung=abbruch')->assertSee('Es wurde nichts abgebucht');
|
||||
}
|
||||
|
||||
public function test_a_paid_order_offers_the_library_and_no_withdrawal_that_would_not_refund_money(): void
|
||||
{
|
||||
$order = $this->orderOf($this->tina(), [$this->privateKonflikte()]);
|
||||
$this->webhook($this->sessionEvent('checkout.session.completed', $order, 'cs_test_ok', 4900))->assertOk();
|
||||
|
||||
$this->actingAs($this->tina())->get(route('member.order', $order))
|
||||
->assertSee('Zur Bibliothek')
|
||||
->assertDontSee('Widerrufen');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $object
|
||||
*/
|
||||
private function event(string $type, array $object): string
|
||||
{
|
||||
return json_encode(['id' => 'evt_'.$type, 'object' => 'event', 'type' => $type, 'data' => ['object' => $object]], JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
private function sessionEvent(string $type, Order $order, string $sessionId, int $amount, string $currency = 'eur', string $paymentStatus = 'paid'): string
|
||||
{
|
||||
return $this->event($type, [
|
||||
'id' => $sessionId,
|
||||
'object' => 'checkout.session',
|
||||
'client_reference_id' => (string) $order->id,
|
||||
'payment_status' => $paymentStatus,
|
||||
'amount_total' => $amount,
|
||||
'currency' => $currency,
|
||||
'metadata' => ['order_id' => (string) $order->id],
|
||||
]);
|
||||
}
|
||||
|
||||
private function signature(string $payload, ?int $timestamp = null): string
|
||||
{
|
||||
$timestamp ??= now()->getTimestamp();
|
||||
|
||||
return 't='.$timestamp.',v1='.hash_hmac('sha256', $timestamp.'.'.$payload, self::WEBHOOK_SECRET);
|
||||
}
|
||||
|
||||
private function webhook(string $payload, ?string $signature = null): TestResponse
|
||||
{
|
||||
return $this->call('POST', route('stripe.webhook'), [], [], [], [
|
||||
'CONTENT_TYPE' => 'application/json',
|
||||
'HTTP_STRIPE_SIGNATURE' => $signature ?? $this->signature($payload),
|
||||
], $payload);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user