app/stripe.php: minimaler REST-Client fuer die Stripe-API auf Basis von PHP-Streams statt eines vendorten SDKs - diese PHP-Installation hat keine curl-Extension, Streams sind zudem portabler und brauchen kein composer.json. - Fuer jeden bezahlten Tarif per API ein Stripe-Produkt mit monatlichem Preis angelegt, referenziert ueber einen stabilen lookup_key statt hartcodierter Price-ID; Erstellung ist idempotent. - abo-upgrade.php: erstellt eine Stripe-Checkout-Session fuer den gewaehlten Tarif, tenant_id/plan_code als Metadaten auf Session UND Subscription (damit spaetere Subscription-Events zuordenbar bleiben). - abo-portal.php: oeffnet das Stripe Customer Portal fuer bestehende Kunden (Zahlungsmittel/Kuendigung, ohne eigene UI dafuer). - stripe-webhook.php: verifiziert die Stripe-Signature per HMAC-SHA256 mit Zeitstempel-Toleranz, verarbeitet checkout.session.completed, customer.subscription.updated/.deleted, invoice.paid/.payment_failed und haelt tenant_billing aktuell. Bewusst kein CSRF-/Login-Check (Stripe ruft unauthentifiziert auf), stattdessen ausschliesslich Signaturpruefung als Echtheitsnachweis. - mandant-einstellungen.php: echter "Jetzt upgraden"-Button (Stripe Checkout) sowie "Zahlungsmethode verwalten/Abo kuendigen" (Customer Portal), sobald ein Stripe-Kunde existiert. Live getestet (Stripe-Testmodus, kein echtes Geld): voller Checkout-Flow bis zum echten Redirect auf checkout.stripe.com, Webhook-Verarbeitung durch selbst erzeugte, korrekt signierte Test-Events (da diese Dev-Umgebung keine oeffentlich erreichbare URL fuer echte Stripe- Zustellung hat) inklusive Ablehnung falscher Signaturen, Customer Portal mit echtem per API angelegtem Test-Kunden. Alle Regressionstests weiterhin gruen (36 Seiten HTTP-Smoke, Golden Master, M8-Isolation/ Rollenmatrix). Noch offen: echten Webhook-Endpunkt auf die Produktions-Domain eintragen, sobald diese feststeht; Wechsel auf Live-Keys; Dolibarr-Sync (Phase 3). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
105 lines
4.1 KiB
PHP
105 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/app/database.php';
|
|
require_once __DIR__ . '/app/billing.php';
|
|
require_once __DIR__ . '/app/audit.php';
|
|
|
|
// Kein app_require_csrf()/saas_require_login(): Stripe ruft diesen
|
|
// Endpunkt unauthentifiziert von aussen auf. Die Echtheit wird
|
|
// ausschliesslich ueber die Signaturpruefung sichergestellt.
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
exit('Nur POST erlaubt.');
|
|
}
|
|
|
|
$payload = file_get_contents('php://input') ?: '';
|
|
$signatureHeader = $_SERVER['HTTP_STRIPE_SIGNATURE'] ?? '';
|
|
|
|
if ($signatureHeader === '' || !stripe_verify_webhook_signature($payload, $signatureHeader, stripe_webhook_secret())) {
|
|
http_response_code(400);
|
|
exit('Ungültige Signatur.');
|
|
}
|
|
|
|
$event = stripe_parse_event($payload);
|
|
if ($event === null) {
|
|
http_response_code(400);
|
|
exit('Ungültiges Ereignis.');
|
|
}
|
|
|
|
$pdo = app_db_pdo();
|
|
$object = $event['data'];
|
|
|
|
switch ($event['type']) {
|
|
case 'checkout.session.completed':
|
|
$tenantId = isset($object['metadata']['tenant_id']) ? (int)$object['metadata']['tenant_id'] : null;
|
|
$planCode = (string)($object['metadata']['plan_code'] ?? '');
|
|
$knownPlans = billing_plans();
|
|
if ($tenantId !== null && isset($object['customer'], $object['subscription']) && isset($knownPlans[$planCode])) {
|
|
billing_update($pdo, $tenantId, [
|
|
'plan_code' => $planCode,
|
|
'subscription_status' => 'active',
|
|
'stripe_customer_id' => (string)$object['customer'],
|
|
'stripe_subscription_id' => (string)$object['subscription'],
|
|
]);
|
|
app_audit_log($pdo, $tenantId, null, 'billing.subscription_started', 'tenant', $tenantId, ['plan_code' => $planCode]);
|
|
}
|
|
break;
|
|
|
|
case 'customer.subscription.updated':
|
|
$tenantId = isset($object['metadata']['tenant_id']) ? (int)$object['metadata']['tenant_id'] : null;
|
|
$tenantId ??= billing_find_tenant_id_by_stripe_subscription($pdo, (string)($object['id'] ?? ''));
|
|
if ($tenantId !== null) {
|
|
$status = (string)($object['status'] ?? 'active');
|
|
$periodEnd = isset($object['current_period_end'])
|
|
? date('Y-m-d H:i:s', (int)$object['current_period_end'])
|
|
: null;
|
|
billing_update($pdo, $tenantId, [
|
|
'subscription_status' => $status,
|
|
'current_period_end' => $periodEnd,
|
|
]);
|
|
app_audit_log($pdo, $tenantId, null, 'billing.subscription_updated', 'tenant', $tenantId, ['status' => $status]);
|
|
}
|
|
break;
|
|
|
|
case 'customer.subscription.deleted':
|
|
$tenantId = isset($object['metadata']['tenant_id']) ? (int)$object['metadata']['tenant_id'] : null;
|
|
$tenantId ??= billing_find_tenant_id_by_stripe_subscription($pdo, (string)($object['id'] ?? ''));
|
|
if ($tenantId !== null) {
|
|
billing_update($pdo, $tenantId, [
|
|
'plan_code' => 'free',
|
|
'subscription_status' => 'canceled',
|
|
]);
|
|
app_audit_log($pdo, $tenantId, null, 'billing.subscription_canceled', 'tenant', $tenantId);
|
|
}
|
|
break;
|
|
|
|
case 'invoice.payment_failed':
|
|
$tenantId = billing_find_tenant_id_by_stripe_customer($pdo, (string)($object['customer'] ?? ''));
|
|
if ($tenantId !== null) {
|
|
billing_update($pdo, $tenantId, ['subscription_status' => 'past_due']);
|
|
app_audit_log($pdo, $tenantId, null, 'billing.payment_failed', 'tenant', $tenantId);
|
|
}
|
|
break;
|
|
|
|
case 'invoice.paid':
|
|
$tenantId = billing_find_tenant_id_by_stripe_customer($pdo, (string)($object['customer'] ?? ''));
|
|
if ($tenantId !== null) {
|
|
app_audit_log($pdo, $tenantId, null, 'billing.invoice_paid', 'tenant', $tenantId, [
|
|
'amount_paid_cents' => $object['amount_paid'] ?? null,
|
|
]);
|
|
// Dolibarr-Rechnungssynchronisation folgt in Phase 3.
|
|
}
|
|
break;
|
|
|
|
default:
|
|
// Unbehandelte Ereignistypen werden bewusst ignoriert, aber mit 200
|
|
// bestaetigt, damit Stripe sie nicht wiederholt zustellt.
|
|
break;
|
|
}
|
|
|
|
http_response_code(200);
|
|
echo 'ok';
|