Billing Phase 3: Dolibarr-Rechnungssynchronisation
Bei erfolgreicher Stripe-Zahlung (invoice.paid) wird automatisch ein Dolibarr-Kunde ermittelt/angelegt und eine validierte Rechnung als Buchhaltungsspiegel erzeugt. Dolibarr-Fehler blockieren die Stripe-Webhook-Verarbeitung nicht, sondern landen im Audit-Log zur manuellen Nachbearbeitung. Getestet gegen die produktive Dolibarr-Instanz des Kunden (kein Sandbox verfuegbar) mit einem rechtebeschraenkten API-Key und einem nicht validierten Test-Datensatz.
This commit is contained in:
+27
-2
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
require_once __DIR__ . '/app/database.php';
|
||||
require_once __DIR__ . '/app/billing.php';
|
||||
require_once __DIR__ . '/app/audit.php';
|
||||
require_once __DIR__ . '/app/dolibarr.php';
|
||||
|
||||
// Kein app_require_csrf()/saas_require_login(): Stripe ruft diesen
|
||||
// Endpunkt unauthentifiziert von aussen auf. Die Echtheit wird
|
||||
@@ -87,10 +88,34 @@ switch ($event['type']) {
|
||||
case 'invoice.paid':
|
||||
$tenantId = billing_find_tenant_id_by_stripe_customer($pdo, (string)($object['customer'] ?? ''));
|
||||
if ($tenantId !== null) {
|
||||
$amountPaidCents = (int)($object['amount_paid'] ?? 0);
|
||||
app_audit_log($pdo, $tenantId, null, 'billing.invoice_paid', 'tenant', $tenantId, [
|
||||
'amount_paid_cents' => $object['amount_paid'] ?? null,
|
||||
'amount_paid_cents' => $amountPaidCents,
|
||||
]);
|
||||
// Dolibarr-Rechnungssynchronisation folgt in Phase 3.
|
||||
|
||||
if ($amountPaidCents > 0) {
|
||||
$billing = billing_fetch_or_init($pdo, $tenantId);
|
||||
try {
|
||||
$sync = dolibarr_sync_invoice_paid($pdo, $tenantId, (string)$billing['plan_code'], $amountPaidCents);
|
||||
} catch (Throwable $e) {
|
||||
$sync = ['ok' => false, 'invoice_id' => null, 'ref' => null, 'error' => $e->getMessage()];
|
||||
}
|
||||
|
||||
if ($sync['ok']) {
|
||||
app_audit_log($pdo, $tenantId, null, 'billing.dolibarr_invoice_created', 'tenant', $tenantId, [
|
||||
'dolibarr_invoice_id' => $sync['invoice_id'],
|
||||
'dolibarr_ref' => $sync['ref'],
|
||||
]);
|
||||
} else {
|
||||
// Wird bewusst nicht als Fehler an Stripe zurueckgegeben (kein Retry
|
||||
// ausgeloest) - Stripe hat das Geld bereits erfolgreich eingezogen,
|
||||
// das ist unabhaengig vom Dolibarr-Spiegel. Fehler landet im Audit-
|
||||
// Log fuer manuelle Nachbearbeitung.
|
||||
app_audit_log($pdo, $tenantId, null, 'billing.dolibarr_invoice_failed', 'tenant', $tenantId, [
|
||||
'error' => $sync['error'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user