presseportale/app/Console/Commands/SyncStripePlans.php
Kevin Adametz a574898ca9 billing: Stripe-Belege auf Deutsch und sprechende Produktnamen
Drei Stellschrauben für die Stripe-Rechnung (Befund Test-Buchung:
englischer Beleg, Position nur 'Starter'):
- stripePreferredLocales() am User: Stripe rendert Rechnung/Quittung
  in der Kontosprache (de-DE, bei language=en englisch)
- Checkout-Session mit locale in der Kontosprache statt Auto-Erkennung
- billing:sync-stripe-plans vergibt sprechende Produktnamen inkl.
  PM-Kontingent und zieht sie bei bereits verknüpften Plänen an den
  bestehenden Stripe-Produkten nach (Preise bleiben unverändert)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 15:25:07 +00:00

166 lines
5.8 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) {
// Preise sind unveränderlich, der Produktname nicht: Er wird
// bei jedem Lauf nachgezogen, damit Stripe-Belege den
// sprechenden Namen tragen (statt z. B. nur "Starter").
if ($dryRun) {
$this->line("[dry-run] {$plan->slug}: verknüpft — Produktname auf \"{$this->stripeProductName($plan)}\" aktualisieren.");
continue;
}
$stripe->products->update($plan->stripe_product_id, [
'name' => $this->stripeProductName($plan),
]);
$this->line("{$plan->slug}: verknüpft — Produktname aktualisiert.");
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' => $this->stripeProductName($plan),
'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}");
}
$this->syncSinglePmPrice($stripe, $dryRun);
return self::SUCCESS;
}
/**
* Legt das Einmal-Produkt „Einzel-Pressemitteilung" an (Netto-Preis aus
* billing.single_pm_price_cents). Die Price-ID landet bewusst in der ENV
* (STRIPE_PRICE_SINGLE_PM) statt in einer Tabelle — es gibt genau einen
* solchen Preis, und ohne ENV bleibt der Checkout deaktiviert.
*/
private function syncSinglePmPrice(object $stripe, bool $dryRun): void
{
if (config('billing.single_pm_stripe_price_id')) {
$this->line('einzel-pm: bereits verknüpft (STRIPE_PRICE_SINGLE_PM), übersprungen.');
return;
}
$amount = (int) config('billing.single_pm_price_cents');
if ($dryRun) {
$this->line(sprintf(
'[dry-run] einzel-pm: Einmal-Produkt + Preis %s € (netto) anlegen.',
number_format($amount / 100, 2, ',', '.'),
));
return;
}
$product = $stripe->products->create([
'name' => 'Einzel-Pressemitteilung',
'metadata' => ['purpose' => 'single_pm'],
]);
$price = $stripe->prices->create([
'product' => $product->id,
'currency' => strtolower((string) config('billing.default_currency')),
'unit_amount' => $amount,
'tax_behavior' => 'exclusive',
'metadata' => ['purpose' => 'single_pm'],
]);
$this->info("einzel-pm: {$product->id} · {$price->id}");
$this->warn("Bitte in die .env eintragen: STRIPE_PRICE_SINGLE_PM={$price->id}");
}
/**
* Sprechender Produktname für Stripe-Belege: Auf Rechnung und
* Checkout-Seite erscheint sonst nur der nackte Tarifname.
*/
private function stripeProductName(Plan $plan): string
{
return sprintf(
'Tarif %s — %d Pressemitteilungen/Monat (pressekonto)',
$plan->name,
$plan->press_release_quota,
);
}
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;
}
}