Billing Phase 2: Stripe Checkout, Kundenportal und Webhook-Verarbeitung
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>
This commit is contained in:
+64
-7
@@ -3,25 +3,48 @@
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/bootstrap.php';
|
||||
require_once __DIR__ . '/stripe.php';
|
||||
|
||||
/**
|
||||
* Single source of truth for the pricing tiers, matching preise.php and
|
||||
* agb.php. max_participants = null means "no upper limit defined, contact
|
||||
* us" (the "auf Anfrage" tier).
|
||||
* us" (the "auf Anfrage" tier). stripe_lookup_key is null for tiers that
|
||||
* have no Stripe Price (free and enterprise are never checked out through
|
||||
* Stripe: free needs no payment, enterprise is arranged manually).
|
||||
*
|
||||
* @return array<string, array{label: string, max_participants: ?int, price_cents: int}>
|
||||
* @return array<string, array{label: string, max_participants: ?int, price_cents: int, stripe_lookup_key: ?string}>
|
||||
*/
|
||||
function billing_plans(): array
|
||||
{
|
||||
return [
|
||||
'free' => ['label' => 'Kostenlos', 'max_participants' => 10, 'price_cents' => 0],
|
||||
'basic' => ['label' => 'Basic', 'max_participants' => 25, 'price_cents' => 399],
|
||||
'plus' => ['label' => 'Plus', 'max_participants' => 50, 'price_cents' => 799],
|
||||
'pro' => ['label' => 'Pro', 'max_participants' => 150, 'price_cents' => 1299],
|
||||
'enterprise' => ['label' => 'Enterprise (auf Anfrage)', 'max_participants' => null, 'price_cents' => 0],
|
||||
'free' => ['label' => 'Kostenlos', 'max_participants' => 10, 'price_cents' => 0, 'stripe_lookup_key' => null],
|
||||
'basic' => ['label' => 'Basic', 'max_participants' => 25, 'price_cents' => 399, 'stripe_lookup_key' => 'kaffeeliste_basic'],
|
||||
'plus' => ['label' => 'Plus', 'max_participants' => 50, 'price_cents' => 799, 'stripe_lookup_key' => 'kaffeeliste_plus'],
|
||||
'pro' => ['label' => 'Pro', 'max_participants' => 150, 'price_cents' => 1299, 'stripe_lookup_key' => 'kaffeeliste_pro'],
|
||||
'enterprise' => ['label' => 'Enterprise (auf Anfrage)', 'max_participants' => null, 'price_cents' => 0, 'stripe_lookup_key' => null],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the Stripe Price id for a plan by its lookup_key. Returns null
|
||||
* for plans without a Stripe price (free, enterprise) or on API failure.
|
||||
*/
|
||||
function billing_stripe_price_id(string $planCode): ?string
|
||||
{
|
||||
$plans = billing_plans();
|
||||
$lookupKey = $plans[$planCode]['stripe_lookup_key'] ?? null;
|
||||
if ($lookupKey === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$result = stripe_request('GET', 'prices', ['lookup_keys' => [$lookupKey], 'active' => 'true']);
|
||||
if ($result['ok'] && !empty($result['data']['data'][0]['id'])) {
|
||||
return (string)$result['data']['data'][0]['id'];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the cheapest plan whose participant limit still covers
|
||||
* $activeParticipantCount. Used both to show customers which plan they
|
||||
@@ -72,6 +95,40 @@ function billing_fetch_or_init(PDO $pdo, int $tenantId): array
|
||||
return $row;
|
||||
}
|
||||
|
||||
function billing_update(PDO $pdo, int $tenantId, array $fields): void
|
||||
{
|
||||
billing_fetch_or_init($pdo, $tenantId);
|
||||
|
||||
$setClauses = [];
|
||||
$params = [];
|
||||
foreach ($fields as $column => $value) {
|
||||
$setClauses[] = "{$column} = ?";
|
||||
$params[] = $value;
|
||||
}
|
||||
$params[] = $tenantId;
|
||||
|
||||
$pdo->prepare('UPDATE tenant_billing SET ' . implode(', ', $setClauses) . ' WHERE tenant_id = ?')
|
||||
->execute($params);
|
||||
}
|
||||
|
||||
function billing_find_tenant_id_by_stripe_customer(PDO $pdo, string $stripeCustomerId): ?int
|
||||
{
|
||||
$stmt = $pdo->prepare('SELECT tenant_id FROM tenant_billing WHERE stripe_customer_id = ?');
|
||||
$stmt->execute([$stripeCustomerId]);
|
||||
$tenantId = $stmt->fetchColumn();
|
||||
|
||||
return $tenantId !== false ? (int)$tenantId : null;
|
||||
}
|
||||
|
||||
function billing_find_tenant_id_by_stripe_subscription(PDO $pdo, string $stripeSubscriptionId): ?int
|
||||
{
|
||||
$stmt = $pdo->prepare('SELECT tenant_id FROM tenant_billing WHERE stripe_subscription_id = ?');
|
||||
$stmt->execute([$stripeSubscriptionId]);
|
||||
$tenantId = $stmt->fetchColumn();
|
||||
|
||||
return $tenantId !== false ? (int)$tenantId : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the tenant's currently booked plan against what their active
|
||||
* participant count actually requires. Purely informational until the
|
||||
|
||||
Reference in New Issue
Block a user