CI Feature / test (push) Successful in 1m53s
Neuer Zahlungsweg PAYMENT_DRIVER=stripe: Checkout Session über die Stripe-API (Http-Client, kein SDK), Freischaltung nur durch die signierte Meldung an POST /stripe/webhook (Signatur und Zeittoleranz geprüft, Betrag und Währung gegen die Bestellung, wiederholte Meldung wirkungslos). Nur Testschlüssel (sk_test_), nie in Produktion; Standard bleibt der Testweg. Die Freischaltung nutzt denselben Weg wie die Testzahlung (Checkout::settle). Die Schaltfläche „Widerrufen“ ist im Stripe-Modus ausgeblendet, weil sie kein Geld zurückbucht. Betrieb: docs/betrieb/DEPLOYMENT.md. Bestandstests geändert: ShopProductPageTest und CmsSectionRenderingTest (2 Stellen) nutzten „stripe“ als Beispiel für einen nicht freigegebenen Zahlungsweg. Stripe ist jetzt freigegeben, die Tests nehmen den unbekannten Treiber „unbekannt“; die Assertions bleiben unverändert. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
146 lines
5.2 KiB
PHP
146 lines
5.2 KiB
PHP
<?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.');
|
|
}
|
|
}
|