presseportale/app/Console/Commands/SyncStripePlans.php
Kevin Adametz 38fab64e10 Phase 9E (Backbone): Stripe-Produkt-Sync und Webhook-Verarbeitung mit STR-Spiegelung
- billing:sync-stripe-plans: legt die Tarife als Netto-Produkte/Preise
  (tax_behavior exclusive, EUR) in Stripe an und pflegt die IDs zurueck
  nach plans; idempotent, --dry-run. Gegen Stripe Test-Mode ausgefuehrt —
  alle 4 Tiers verknuepft.
- ProcessStripeWebhook (Listener auf Cashier WebhookReceived):
  - invoice.payment_succeeded -> Spiegelung in den lokalen STR-Kreis
    (fortlaufende Nummer via InvoiceNumberGenerator, Adress-Snapshot
    bevorzugt aus dem Stripe-Payload inkl. lokaler USt-ID, Status paid,
    idempotent gegen doppelte Zustellung)
  - checkout.session.completed -> markiert den referenzierten
    single_purchases-Datensatz als bezahlt (Metadata single_purchase_id)
- CASHIER_CURRENCY=eur (+ Locale de_DE); Cashier-Webhook-Route aktiv
- Doku: Billing-Referenz §7 + Phase-9-Plan (9E-Backbone) aktualisiert

Offen fuer 9E-Rest: Checkout-Flows (Abo + Einmalkauf), Webhook-Endpoint
im Stripe-Dashboard + STRIPE_WEBHOOK_SECRET, Slot-Logik auf
Plan-Kontingent (fachliche Frage: Grandfathered = unbegrenzt?).

Tests: StripeWebhookProcessingTest (7, inkl. Event-Wiring).
Suite: 497 passed, 4 skipped. Pint clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 11:07:09 +00:00

96 lines
3.3 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Plan;
use Illuminate\Console\Command;
use Laravel\Cashier\Cashier;
/**
* Legt die Tarife aus `plans` als Produkte/Preise in Stripe an und pflegt
* die Stripe-IDs zurück (Phase 9E).
*
* Preise sind NETTO (`tax_behavior: exclusive`) — die Steuer wird im
* Checkout ergänzt (Entscheidung 12.06.2026). Idempotent: vorhandene
* IDs werden übersprungen; Preisänderungen erzeugen in Stripe bewusst
* neue Price-Objekte (Stripe-Preise sind unveränderlich), der alte Preis
* bleibt für Bestandsabos gültig.
*/
class SyncStripePlans extends Command
{
protected $signature = 'billing:sync-stripe-plans
{--dry-run : Nur anzeigen, was angelegt würde}';
protected $description = 'Synchronisiert die Tarife aus plans als Netto-Produkte/Preise nach Stripe';
public function handle(): int
{
if (! config('cashier.secret')) {
$this->error('STRIPE_SECRET ist nicht gesetzt.');
return self::FAILURE;
}
$dryRun = (bool) $this->option('dry-run');
$stripe = Cashier::stripe();
foreach (Plan::query()->active()->get() as $plan) {
if ($plan->stripe_product_id && $plan->stripe_price_id_monthly && $plan->stripe_price_id_yearly) {
$this->line("{$plan->slug}: vollständig verknüpft, übersprungen.");
continue;
}
if ($dryRun) {
$this->line(sprintf(
'[dry-run] %s: Produkt + Preise %s €/Monat, %s €/Jahr (netto) anlegen.',
$plan->slug,
number_format($plan->monthly_price_cents / 100, 2, ',', '.'),
number_format($plan->yearly_price_cents / 100, 2, ',', '.'),
));
continue;
}
$productId = $plan->stripe_product_id;
if (! $productId) {
$product = $stripe->products->create([
'name' => $plan->name,
'metadata' => ['plan_slug' => $plan->slug],
]);
$productId = $product->id;
}
$monthlyId = $plan->stripe_price_id_monthly
?: $this->createPrice($stripe, $productId, $plan, $plan->monthly_price_cents, 'month');
$yearlyId = $plan->stripe_price_id_yearly
?: $this->createPrice($stripe, $productId, $plan, $plan->yearly_price_cents, 'year');
$plan->update([
'stripe_product_id' => $productId,
'stripe_price_id_monthly' => $monthlyId,
'stripe_price_id_yearly' => $yearlyId,
]);
$this->info("{$plan->slug}: {$productId} · monatlich {$monthlyId} · jährlich {$yearlyId}");
}
return self::SUCCESS;
}
private function createPrice(object $stripe, string $productId, Plan $plan, int $unitAmount, string $interval): string
{
$price = $stripe->prices->create([
'product' => $productId,
'currency' => strtolower($plan->currency),
'unit_amount' => $unitAmount,
'tax_behavior' => 'exclusive',
'recurring' => ['interval' => $interval],
'metadata' => ['plan_slug' => $plan->slug],
]);
return $price->id;
}
}