billing: Rechnungsversand — PDF im Anhang der Zahlungsmail, optionale Rechnungs-E-Mail, Stripe-Rechnungs-PDF direkt aus dem Kundenbereich
Kundenwunsch 06.10.2026: Ein Kunde kam über die Stripe-Rechnungsseite nicht an seine Rechnung und fragte nach Belegen und einer Rechnungs-E-Mail. - Profil ▸ Rechnungsadresse ▸ Rechnungsversand: Schalter „Rechnungs-PDF an die Konto-E-Mail" (Standard an), optionale „E-Mail für Rechnungen" mit Schalter „Rechnungen als PDF an diese Adresse senden" (Standard an). Neue Spalten an billing_addresses (klein, ohne FULLTEXT). - InvoiceDelivery entscheidet Empfänger und Anhang; gleiche Adresse wie das Konto ergibt eine Mail mit PDF. - Zahlungsbestätigung (STR) und Bestandskunden-Rechnung/-Erinnerung (MAN) folgen der Einstellung; die Fassung an die Rechnungs-E-Mail nennt das Konto, ohne Blindkopie und ohne Login-Button. - StripeInvoicePdf holt das Stripe-PDF einmal und legt es lokal ab (storage/app/private/invoices/stripe); Mailversand wiederholt bei Stripe-Ausfall (5 Versuche). - Rechnungsseite: „PDF öffnen" aus pressekonto, „Beleg bei Stripe" für den Zahlungsbeleg auf der Stripe-Seite. Suite 2.340 grün, einer übersprungen; Larastan und Pint ohne Meldung. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
da9411bdab
commit
6d03d8de39
18 changed files with 695 additions and 30 deletions
38
app/Http/Controllers/StripeInvoicePdfController.php
Normal file
38
app/Http/Controllers/StripeInvoicePdfController.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Invoice;
|
||||
use App\Services\Billing\StripeInvoicePdf;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* PDF einer STR-Rechnung direkt aus pressekonto (06.10.2026). Bis dahin gab
|
||||
* es nur die Weiterleitung auf die bei Stripe gehostete Rechnungsseite —
|
||||
* ein Kunde kam darüber nicht an seine Rechnung („blockiert"). Das PDF
|
||||
* kommt einmal von Stripe und liegt danach lokal (StripeInvoicePdf).
|
||||
*/
|
||||
class StripeInvoicePdfController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request, Invoice $invoice, StripeInvoicePdf $pdf): Response
|
||||
{
|
||||
abort_unless($invoice->user_id === $request->user()->id, 404);
|
||||
abort_unless(filled($invoice->stripe_invoice_id), 404);
|
||||
|
||||
try {
|
||||
return $pdf->inlineResponse($invoice);
|
||||
} catch (\Throwable $exception) {
|
||||
Log::channel('billing')->warning('Stripe-Rechnungs-PDF nicht abrufbar.', [
|
||||
'invoice_id' => $invoice->id,
|
||||
'stripe_invoice_id' => $invoice->stripe_invoice_id,
|
||||
'error' => $exception->getMessage(),
|
||||
]);
|
||||
|
||||
return redirect()
|
||||
->route('me.invoices.index')
|
||||
->with('invoice-error', __('Das Rechnungs-PDF ist gerade nicht abrufbar. Bitte versuchen Sie es später erneut.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -22,6 +22,7 @@ use App\Models\User;
|
|||
use App\Scopes\PortalScope;
|
||||
use App\Services\ActivityLog;
|
||||
use App\Services\Billing\CreditTopupService;
|
||||
use App\Services\Billing\InvoiceDelivery;
|
||||
use App\Services\Billing\InvoiceNumberGenerator;
|
||||
use App\Services\Conversion\ConversionRecorder;
|
||||
use App\Services\PressRelease\CorrectionService;
|
||||
|
|
@ -294,8 +295,15 @@ class ProcessStripeWebhook
|
|||
// Zahlungsbestätigung an den Kunden — genau eine pro Beleg, da die
|
||||
// Spiegelung über stripe_invoice_id idempotent ist. Deckt alle
|
||||
// Buchungsarten ab (Tarif inkl. Verlängerung, Einzel-PM, Credits).
|
||||
// PDF-Anhang und Rechnungs-E-Mail nach Profileinstellung.
|
||||
$delivery = InvoiceDelivery::for($user);
|
||||
|
||||
if ($user->email) {
|
||||
Mail::to($user->email)->queue(new BookingPaymentConfirmed($user, $invoice));
|
||||
Mail::to($user->email)->queue(new BookingPaymentConfirmed($user, $invoice, attachPdf: $delivery->attachPdfForAccount));
|
||||
}
|
||||
|
||||
if ($delivery->invoiceEmail !== null) {
|
||||
Mail::to($delivery->invoiceEmail)->queue(new BookingPaymentConfirmed($user, $invoice, attachPdf: true, forInvoiceRecipient: true));
|
||||
}
|
||||
|
||||
$this->activityLog->record(
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@ namespace App\Mail;
|
|||
|
||||
use App\Models\Invoice;
|
||||
use App\Models\User;
|
||||
use App\Services\Billing\StripeInvoicePdf;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Mail\Attachment;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Mail\Mailables\Content;
|
||||
use Illuminate\Mail\Mailables\Envelope;
|
||||
|
|
@ -18,16 +20,28 @@ use Illuminate\Queue\SerializesModels;
|
|||
*
|
||||
* Transaktionsmail: bewusst ohne Opt-out (analog Kündigungs-/
|
||||
* Widerrufsbestätigungen, siehe EmailNotificationType-Doku).
|
||||
*
|
||||
* Rechnungsversand (06.10.2026): Mit `attachPdf` hängt das Stripe-PDF an;
|
||||
* `forInvoiceRecipient` ist die Fassung an die Rechnungs-E-Mail aus dem
|
||||
* Profil (siehe InvoiceDelivery). Das PDF wird erst beim Versand geholt —
|
||||
* ist Stripe nicht erreichbar, wiederholt die Queue den Versand.
|
||||
*/
|
||||
class BookingPaymentConfirmed extends Mailable implements ShouldQueue
|
||||
{
|
||||
use Queueable, SerializesModels;
|
||||
|
||||
public int $tries = 5;
|
||||
|
||||
/** @var list<int> */
|
||||
public array $backoff = [60, 300, 900, 3600];
|
||||
|
||||
public string $invoicesUrl;
|
||||
|
||||
public function __construct(
|
||||
public readonly User $user,
|
||||
public readonly Invoice $invoice,
|
||||
public readonly bool $attachPdf = false,
|
||||
public readonly bool $forInvoiceRecipient = false,
|
||||
) {
|
||||
$this->invoicesUrl = route('me.invoices.index');
|
||||
}
|
||||
|
|
@ -36,10 +50,14 @@ class BookingPaymentConfirmed extends Mailable implements ShouldQueue
|
|||
{
|
||||
// Blindkopie an das Rechts-/Service-Postfach: interner Beleg über
|
||||
// jede versendete Zahlungsbestätigung.
|
||||
$bcc = (string) config('legal.contact_email');
|
||||
// Die Fassung an die Rechnungs-E-Mail ist ein Duplikat derselben
|
||||
// Rechnung — eine Blindkopie davon braucht das Postfach nicht.
|
||||
$bcc = $this->forInvoiceRecipient ? '' : (string) config('legal.contact_email');
|
||||
|
||||
return new Envelope(
|
||||
subject: 'Zahlungsbestätigung für Ihre Buchung — Rechnung '.$this->invoice->number,
|
||||
subject: $this->forInvoiceRecipient
|
||||
? 'Rechnung '.$this->invoice->number.' — pressekonto'
|
||||
: 'Zahlungsbestätigung für Ihre Buchung — Rechnung '.$this->invoice->number,
|
||||
bcc: $bcc !== '' ? [$bcc] : [],
|
||||
);
|
||||
}
|
||||
|
|
@ -48,4 +66,21 @@ class BookingPaymentConfirmed extends Mailable implements ShouldQueue
|
|||
{
|
||||
return new Content(view: 'emails.billing.payment-confirmed');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, Attachment>
|
||||
*/
|
||||
public function attachments(): array
|
||||
{
|
||||
if (! $this->attachPdf || blank($this->invoice->stripe_invoice_id)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$pdf = app(StripeInvoicePdf::class);
|
||||
|
||||
return [
|
||||
Attachment::fromData(fn (): string => $pdf->contents($this->invoice), $pdf->filename($this->invoice))
|
||||
->withMime('application/pdf'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,10 @@ use Illuminate\Queue\SerializesModels;
|
|||
*
|
||||
* Transaktionsmail: bewusst ohne Opt-out. Die Bounce-Sperrliste greift
|
||||
* über `GateOutgoingMail`; der Service prüft sie zusätzlich vorab.
|
||||
*
|
||||
* Rechnungsversand (06.10.2026): `attachPdf` folgt der Profileinstellung
|
||||
* (Standard: an); `forInvoiceRecipient` ist die Fassung an die
|
||||
* Rechnungs-E-Mail (siehe InvoiceDelivery), immer mit PDF.
|
||||
*/
|
||||
class ManualInvoiceIssued extends Mailable implements ShouldQueue
|
||||
{
|
||||
|
|
@ -42,6 +46,8 @@ class ManualInvoiceIssued extends Mailable implements ShouldQueue
|
|||
public readonly User $user,
|
||||
public readonly Invoice $invoice,
|
||||
public readonly bool $isReminder = false,
|
||||
public readonly bool $attachPdf = true,
|
||||
public readonly bool $forInvoiceRecipient = false,
|
||||
) {
|
||||
$renderer = app(ManualInvoicePdfRenderer::class);
|
||||
|
||||
|
|
@ -54,7 +60,8 @@ class ManualInvoiceIssued extends Mailable implements ShouldQueue
|
|||
|
||||
public function envelope(): Envelope
|
||||
{
|
||||
$bcc = (string) config('legal.contact_email');
|
||||
// Die Fassung an die Rechnungs-E-Mail ist ein Duplikat — ohne Blindkopie.
|
||||
$bcc = $this->forInvoiceRecipient ? '' : (string) config('legal.contact_email');
|
||||
|
||||
$subject = $this->isReminder
|
||||
? 'Zahlungserinnerung — Rechnung '.$this->invoice->number
|
||||
|
|
@ -76,6 +83,10 @@ class ManualInvoiceIssued extends Mailable implements ShouldQueue
|
|||
*/
|
||||
public function attachments(): array
|
||||
{
|
||||
if (! $this->attachPdf) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$renderer = app(ManualInvoicePdfRenderer::class);
|
||||
|
||||
return [
|
||||
|
|
|
|||
|
|
@ -24,8 +24,22 @@ class BillingAddress extends Model
|
|||
'city',
|
||||
'country_code',
|
||||
'vat_id',
|
||||
'invoice_email',
|
||||
'invoice_pdf_to_account',
|
||||
'invoice_pdf_to_invoice_email',
|
||||
];
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'invoice_pdf_to_account' => 'boolean',
|
||||
'invoice_pdf_to_invoice_email' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<User, $this>
|
||||
*/
|
||||
|
|
|
|||
44
app/Services/Billing/InvoiceDelivery.php
Normal file
44
app/Services/Billing/InvoiceDelivery.php
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Billing;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
/**
|
||||
* Wer eine Rechnungsmail bekommt und ob das PDF anhängt (Kundenwunsch
|
||||
* 06.10.2026, Einstellung an der Rechnungsadresse im Profil).
|
||||
*
|
||||
* - Die Konto-Adresse bekommt die Rechnungsmail immer; das PDF hängt an,
|
||||
* solange der Kunde es nicht abgewählt hat (Standard: an).
|
||||
* - Eine optionale Rechnungs-E-Mail bekommt eine eigene Mail mit PDF,
|
||||
* wenn der Versand dorthin aktiviert ist (Standard: an, sobald eine
|
||||
* Adresse eingetragen ist).
|
||||
* - Ist die Rechnungs-E-Mail dieselbe wie die Konto-Adresse, geht keine
|
||||
* zweite Mail raus; die Konto-Mail trägt dann das PDF.
|
||||
*/
|
||||
final class InvoiceDelivery
|
||||
{
|
||||
public function __construct(
|
||||
public readonly bool $attachPdfForAccount,
|
||||
public readonly ?string $invoiceEmail,
|
||||
) {}
|
||||
|
||||
public static function for(User $user): self
|
||||
{
|
||||
$billingAddress = $user->billingAddress;
|
||||
|
||||
$attachPdfForAccount = $billingAddress->invoice_pdf_to_account ?? true;
|
||||
|
||||
$invoiceEmail = trim((string) $billingAddress?->invoice_email);
|
||||
|
||||
if ($invoiceEmail === '' || ! $billingAddress?->invoice_pdf_to_invoice_email) {
|
||||
return new self($attachPdfForAccount, null);
|
||||
}
|
||||
|
||||
if (strcasecmp($invoiceEmail, trim((string) $user->email)) === 0) {
|
||||
return new self(true, null);
|
||||
}
|
||||
|
||||
return new self($attachPdfForAccount, $invoiceEmail);
|
||||
}
|
||||
}
|
||||
|
|
@ -60,7 +60,7 @@ class ManualInvoiceDunningService
|
|||
'due_date' => $dueDate,
|
||||
]);
|
||||
|
||||
Mail::to($invoice->user)->queue(new ManualInvoiceIssued($invoice->user, $invoice));
|
||||
$this->queueInvoiceMails($invoice);
|
||||
|
||||
$this->activityLog->record(
|
||||
ActivityType::ManualInvoice,
|
||||
|
|
@ -103,7 +103,7 @@ class ManualInvoiceDunningService
|
|||
'due_date' => $dueDate,
|
||||
]);
|
||||
|
||||
Mail::to($invoice->user)->queue(new ManualInvoiceIssued($invoice->user, $invoice, isReminder: true));
|
||||
$this->queueInvoiceMails($invoice, isReminder: true);
|
||||
|
||||
$this->activityLog->record(
|
||||
ActivityType::ManualInvoice,
|
||||
|
|
@ -279,6 +279,32 @@ class ManualInvoiceDunningService
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Rechnungsmail an die Konto-Adresse (PDF nach Profileinstellung) und,
|
||||
* falls hinterlegt und aktiviert, an die Rechnungs-E-Mail (mit PDF).
|
||||
*/
|
||||
private function queueInvoiceMails(Invoice $invoice, bool $isReminder = false): void
|
||||
{
|
||||
$delivery = InvoiceDelivery::for($invoice->user);
|
||||
|
||||
Mail::to($invoice->user)->queue(new ManualInvoiceIssued(
|
||||
$invoice->user,
|
||||
$invoice,
|
||||
isReminder: $isReminder,
|
||||
attachPdf: $delivery->attachPdfForAccount,
|
||||
));
|
||||
|
||||
if ($delivery->invoiceEmail !== null) {
|
||||
Mail::to($delivery->invoiceEmail)->queue(new ManualInvoiceIssued(
|
||||
$invoice->user,
|
||||
$invoice,
|
||||
isReminder: $isReminder,
|
||||
attachPdf: true,
|
||||
forInvoiceRecipient: true,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
private function assertOpenManual(Invoice $invoice): void
|
||||
{
|
||||
if (! $invoice->isManual()) {
|
||||
|
|
|
|||
71
app/Services/Billing/StripeInvoicePdf.php
Normal file
71
app/Services/Billing/StripeInvoicePdf.php
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Billing;
|
||||
|
||||
use App\Models\Invoice;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Laravel\Cashier\Cashier;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* PDF einer STR-Rechnung, wie Stripe es erzeugt. Beim ersten Abruf von
|
||||
* Stripe geladen und lokal abgelegt — eine bezahlte Rechnung ändert sich
|
||||
* nicht mehr, und Mailanhang wie Download hängen danach nicht mehr an der
|
||||
* Erreichbarkeit von Stripe oder an ablaufenden Links der gehosteten
|
||||
* Rechnungsseite.
|
||||
*/
|
||||
class StripeInvoicePdf
|
||||
{
|
||||
public function contents(Invoice $invoice): string
|
||||
{
|
||||
if (blank($invoice->stripe_invoice_id)) {
|
||||
throw new RuntimeException(sprintf('Rechnung %s hat keine Stripe-Rechnung.', $invoice->number));
|
||||
}
|
||||
|
||||
$disk = Storage::disk('local');
|
||||
$path = $this->path($invoice);
|
||||
|
||||
if ($disk->exists($path)) {
|
||||
return (string) $disk->get($path);
|
||||
}
|
||||
|
||||
$pdfUrl = Cashier::stripe()->invoices->retrieve($invoice->stripe_invoice_id)->invoice_pdf;
|
||||
|
||||
if (blank($pdfUrl)) {
|
||||
throw new RuntimeException(sprintf('Stripe liefert für Rechnung %s kein PDF.', $invoice->number));
|
||||
}
|
||||
|
||||
$pdf = Http::timeout(30)->get($pdfUrl)->throw()->body();
|
||||
|
||||
if (! str_starts_with($pdf, '%PDF')) {
|
||||
throw new RuntimeException(sprintf('Stripe-PDF zu Rechnung %s ist kein PDF.', $invoice->number));
|
||||
}
|
||||
|
||||
if (! $disk->put($path, $pdf)) {
|
||||
report(new RuntimeException(sprintf('Stripe-PDF zu Rechnung %s konnte nicht abgelegt werden.', $invoice->number)));
|
||||
}
|
||||
|
||||
return $pdf;
|
||||
}
|
||||
|
||||
public function filename(Invoice $invoice): string
|
||||
{
|
||||
return 'Rechnung-'.$invoice->number.'.pdf';
|
||||
}
|
||||
|
||||
public function inlineResponse(Invoice $invoice): Response
|
||||
{
|
||||
return response($this->contents($invoice), Response::HTTP_OK, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="'.$this->filename($invoice).'"',
|
||||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||||
]);
|
||||
}
|
||||
|
||||
private function path(Invoice $invoice): string
|
||||
{
|
||||
return 'invoices/stripe/'.$invoice->number.'.pdf';
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue