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
debf094cae
commit
a5bf33109e
18 changed files with 695 additions and 30 deletions
204
tests/Feature/Billing/InvoiceDeliveryTest.php
Normal file
204
tests/Feature/Billing/InvoiceDeliveryTest.php
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
<?php
|
||||
|
||||
use App\Enums\InvoiceStatus;
|
||||
use App\Mail\BookingPaymentConfirmed;
|
||||
use App\Models\BillingAddress;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\User;
|
||||
use App\Services\Billing\InvoiceDelivery;
|
||||
use App\Services\Billing\StripeInvoicePdf;
|
||||
use Database\Seeders\RolesAndPermissionsSeeder;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Livewire\Livewire;
|
||||
use Tests\TestCase;
|
||||
|
||||
function deliveryInvoice(User $user, array $overrides = []): Invoice
|
||||
{
|
||||
return Invoice::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'user_payment_id' => null,
|
||||
'number' => 'STR-00042',
|
||||
'status' => InvoiceStatus::Paid->value,
|
||||
'stripe_invoice_id' => 'in_test_42',
|
||||
...$overrides,
|
||||
]);
|
||||
}
|
||||
|
||||
test('without billing settings the account gets the pdf and no second address is used', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$delivery = InvoiceDelivery::for($user);
|
||||
|
||||
expect($delivery->attachPdfForAccount)->toBeTrue();
|
||||
expect($delivery->invoiceEmail)->toBeNull();
|
||||
});
|
||||
|
||||
test('an enabled invoice email is used and the account pdf follows its own switch', function () {
|
||||
$user = User::factory()->create();
|
||||
BillingAddress::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'invoice_email' => 'buchhaltung@example.com',
|
||||
'invoice_pdf_to_account' => false,
|
||||
'invoice_pdf_to_invoice_email' => true,
|
||||
]);
|
||||
|
||||
$delivery = InvoiceDelivery::for($user);
|
||||
|
||||
expect($delivery->attachPdfForAccount)->toBeFalse();
|
||||
expect($delivery->invoiceEmail)->toBe('buchhaltung@example.com');
|
||||
});
|
||||
|
||||
test('a disabled invoice email is not used', function () {
|
||||
$user = User::factory()->create();
|
||||
BillingAddress::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'invoice_email' => 'buchhaltung@example.com',
|
||||
'invoice_pdf_to_invoice_email' => false,
|
||||
]);
|
||||
|
||||
expect(InvoiceDelivery::for($user)->invoiceEmail)->toBeNull();
|
||||
});
|
||||
|
||||
test('an invoice email equal to the account email collapses into one mail with pdf', function () {
|
||||
$user = User::factory()->create(['email' => 'max@example.com']);
|
||||
BillingAddress::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'invoice_email' => 'MAX@example.com',
|
||||
'invoice_pdf_to_account' => false,
|
||||
'invoice_pdf_to_invoice_email' => true,
|
||||
]);
|
||||
|
||||
$delivery = InvoiceDelivery::for($user);
|
||||
|
||||
expect($delivery->attachPdfForAccount)->toBeTrue();
|
||||
expect($delivery->invoiceEmail)->toBeNull();
|
||||
});
|
||||
|
||||
test('the payment confirmation attaches the stripe pdf only when asked to', function () {
|
||||
$user = User::factory()->create();
|
||||
$invoice = deliveryInvoice($user);
|
||||
|
||||
$this->mock(StripeInvoicePdf::class, function ($mock) {
|
||||
$mock->shouldReceive('filename')->andReturn('Rechnung-STR-00042.pdf');
|
||||
$mock->shouldReceive('contents')->andReturn('%PDF-1.4 test');
|
||||
});
|
||||
|
||||
$withPdf = new BookingPaymentConfirmed($user, $invoice, attachPdf: true);
|
||||
$withPdf->assertHasAttachedData('%PDF-1.4 test', 'Rechnung-STR-00042.pdf', ['mime' => 'application/pdf']);
|
||||
$withPdf->assertSeeInHtml('als PDF im Anhang');
|
||||
|
||||
$withoutPdf = new BookingPaymentConfirmed($user, $invoice);
|
||||
expect($withoutPdf->attachments())->toBe([]);
|
||||
$withoutPdf->assertDontSeeInHtml('als PDF im Anhang');
|
||||
});
|
||||
|
||||
test('the copy for the invoice email names the account and skips the bcc and login button', function () {
|
||||
$user = User::factory()->create(['name' => 'Max Muster', 'email' => 'max@example.com']);
|
||||
$invoice = deliveryInvoice($user);
|
||||
|
||||
$this->mock(StripeInvoicePdf::class, function ($mock) {
|
||||
$mock->shouldReceive('filename')->andReturn('Rechnung-STR-00042.pdf');
|
||||
$mock->shouldReceive('contents')->andReturn('%PDF-1.4 test');
|
||||
});
|
||||
|
||||
$mail = new BookingPaymentConfirmed($user, $invoice, attachPdf: true, forInvoiceRecipient: true);
|
||||
|
||||
$mail->assertHasSubject('Rechnung STR-00042 — pressekonto');
|
||||
$mail->assertSeeInHtml('im Auftrag von Max Muster (max@example.com)');
|
||||
$mail->assertDontSeeInHtml('Rechnung im Konto ansehen');
|
||||
expect($mail->hasBcc((string) config('legal.contact_email')))->toBeFalse();
|
||||
});
|
||||
|
||||
test('a stored stripe pdf is served without asking stripe again', function () {
|
||||
Storage::fake('local');
|
||||
Storage::disk('local')->put('invoices/stripe/STR-00042.pdf', '%PDF-1.4 cached');
|
||||
|
||||
$invoice = deliveryInvoice(User::factory()->create());
|
||||
|
||||
expect(app(StripeInvoicePdf::class)->contents($invoice))->toBe('%PDF-1.4 cached');
|
||||
});
|
||||
|
||||
test('an invoice without stripe id has no stripe pdf', function () {
|
||||
$invoice = deliveryInvoice(User::factory()->create(), ['stripe_invoice_id' => null, 'number' => 'MAN-00001']);
|
||||
|
||||
expect(fn () => app(StripeInvoicePdf::class)->contents($invoice))->toThrow(RuntimeException::class);
|
||||
});
|
||||
|
||||
test('the owner opens the stripe invoice pdf inline, others get a 404', function () {
|
||||
/** @var TestCase $this */
|
||||
$this->seed(RolesAndPermissionsSeeder::class);
|
||||
Storage::fake('local');
|
||||
Storage::disk('local')->put('invoices/stripe/STR-00042.pdf', '%PDF-1.4 cached');
|
||||
|
||||
$customer = User::factory()->create(['is_active' => true]);
|
||||
$customer->assignRole('customer');
|
||||
$intruder = User::factory()->create(['is_active' => true]);
|
||||
$intruder->assignRole('customer');
|
||||
$invoice = deliveryInvoice($customer);
|
||||
|
||||
$this->actingAs($customer)
|
||||
->get(route('me.invoices.stripe-pdf', $invoice))
|
||||
->assertSuccessful()
|
||||
->assertHeader('Content-Type', 'application/pdf')
|
||||
->assertHeader('Content-Disposition', 'inline; filename="Rechnung-STR-00042.pdf"');
|
||||
|
||||
$this->actingAs($intruder)
|
||||
->get(route('me.invoices.stripe-pdf', $invoice))
|
||||
->assertNotFound();
|
||||
});
|
||||
|
||||
test('an unreachable stripe pdf sends the customer back with a notice', function () {
|
||||
/** @var TestCase $this */
|
||||
$this->seed(RolesAndPermissionsSeeder::class);
|
||||
|
||||
$customer = User::factory()->create(['is_active' => true]);
|
||||
$customer->assignRole('customer');
|
||||
$invoice = deliveryInvoice($customer);
|
||||
|
||||
$this->mock(StripeInvoicePdf::class, function ($mock) {
|
||||
$mock->shouldReceive('inlineResponse')->andThrow(new RuntimeException('Stripe down'));
|
||||
});
|
||||
|
||||
$this->actingAs($customer)
|
||||
->get(route('me.invoices.stripe-pdf', $invoice))
|
||||
->assertRedirect(route('me.invoices.index'))
|
||||
->assertSessionHas('invoice-error');
|
||||
});
|
||||
|
||||
test('the profile stores the invoice email and both pdf switches with the billing address', function () {
|
||||
/** @var TestCase $this */
|
||||
$customer = User::factory()->create(['is_active' => true]);
|
||||
|
||||
$this->actingAs($customer);
|
||||
|
||||
Livewire::test('customer.profile')
|
||||
->set('billingLastName', 'Mustermann')
|
||||
->set('billingAddress1', 'Musterstrasse 1')
|
||||
->set('billingPostalCode', '10115')
|
||||
->set('billingCity', 'Berlin')
|
||||
->set('billingCountryCode', 'DE')
|
||||
->set('invoiceEmail', 'buchhaltung@example.com')
|
||||
->assertSee('Rechnungen als PDF an diese Adresse senden')
|
||||
->set('invoicePdfToAccount', false)
|
||||
->set('invoicePdfToInvoiceEmail', true)
|
||||
->call('saveProfile')
|
||||
->assertHasNoErrors();
|
||||
|
||||
$billing = $customer->fresh()->billingAddress;
|
||||
|
||||
expect($billing->invoice_email)->toBe('buchhaltung@example.com');
|
||||
expect($billing->invoice_pdf_to_account)->toBeFalse();
|
||||
expect($billing->invoice_pdf_to_invoice_email)->toBeTrue();
|
||||
});
|
||||
|
||||
test('the invoice email must be valid and needs a complete billing address', function () {
|
||||
/** @var TestCase $this */
|
||||
$customer = User::factory()->create(['is_active' => true]);
|
||||
|
||||
$this->actingAs($customer);
|
||||
|
||||
Livewire::test('customer.profile')
|
||||
->set('invoiceEmail', 'keine-adresse')
|
||||
->call('saveProfile')
|
||||
->assertHasErrors(['invoiceEmail', 'billingLastName', 'billingAddress1']);
|
||||
});
|
||||
|
|
@ -7,6 +7,7 @@ use App\Exceptions\ManualInvoiceActionException;
|
|||
use App\Mail\GrandfatheringTerminated;
|
||||
use App\Mail\ManualInvoiceIssued;
|
||||
use App\Models\ActivityEvent;
|
||||
use App\Models\BillingAddress;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\InvoiceBillingAddress;
|
||||
use App\Models\LegacyInvoice;
|
||||
|
|
@ -98,6 +99,39 @@ test('sending an invoice mails the pdf, sets the due date from today and records
|
|||
expect($event->actor_label)->toBe('Admin Alma');
|
||||
});
|
||||
|
||||
test('an enabled invoice email gets its own copy with pdf, the account mail follows its switch', function () {
|
||||
[$invoice, , $customer] = dunningFixture();
|
||||
BillingAddress::factory()->create([
|
||||
'user_id' => $customer->id,
|
||||
'invoice_email' => 'buchhaltung@example.com',
|
||||
'invoice_pdf_to_account' => false,
|
||||
'invoice_pdf_to_invoice_email' => true,
|
||||
]);
|
||||
|
||||
app(ManualInvoiceDunningService::class)->send($invoice, dunningAdmin());
|
||||
|
||||
Mail::assertQueuedCount(2);
|
||||
Mail::assertQueued(ManualInvoiceIssued::class, function (ManualInvoiceIssued $mail) use ($customer): bool {
|
||||
if ($mail->forInvoiceRecipient) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$mail->assertSeeInHtml('in Ihrem Konto unter Rechnungen');
|
||||
|
||||
return $mail->hasTo($customer->email) && ! $mail->attachPdf && $mail->attachments() === [];
|
||||
});
|
||||
Mail::assertQueued(ManualInvoiceIssued::class, function (ManualInvoiceIssued $mail): bool {
|
||||
if (! $mail->forInvoiceRecipient) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$mail->assertSeeInHtml('Diese Adresse ist dort als Empfänger für Rechnungen hinterlegt');
|
||||
$mail->assertHasAttachedData(app(ManualInvoicePdfRenderer::class)->render($mail->invoice), 'Rechnung-MAN-00010.pdf', ['mime' => 'application/pdf']);
|
||||
|
||||
return $mail->hasTo('buchhaltung@example.com') && $mail->forInvoiceRecipient && ! $mail->hasBcc((string) config('legal.contact_email'));
|
||||
});
|
||||
});
|
||||
|
||||
test('a sent invoice cannot be sent twice but can be reminded', function () {
|
||||
[$invoice] = dunningFixture(['sent_at' => now()->subDays(20), 'due_date' => '2026-09-10']);
|
||||
$service = app(ManualInvoiceDunningService::class);
|
||||
|
|
|
|||
|
|
@ -10,11 +10,21 @@ use App\Models\BillingAddress;
|
|||
use App\Models\Invoice;
|
||||
use App\Models\SinglePurchase;
|
||||
use App\Models\User;
|
||||
use App\Services\Billing\StripeInvoicePdf;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
|
||||
// Mail-Assertions bereiten die Mail samt Anhängen vor — das Stripe-PDF
|
||||
// käme sonst live von Stripe.
|
||||
beforeEach(function (): void {
|
||||
$this->mock(StripeInvoicePdf::class, function ($mock): void {
|
||||
$mock->shouldReceive('filename')->andReturn('Rechnung.pdf');
|
||||
$mock->shouldReceive('contents')->andReturn('%PDF-1.4 test');
|
||||
});
|
||||
});
|
||||
|
||||
function stripeInvoicePayload(User $user, array $overrides = []): array
|
||||
{
|
||||
return [
|
||||
|
|
@ -184,6 +194,40 @@ test('a mirrored invoice queues a payment confirmation mail to the customer', fu
|
|||
});
|
||||
});
|
||||
|
||||
test('the account confirmation carries the pdf by default and goes to the invoice email when enabled', function () {
|
||||
Mail::fake();
|
||||
|
||||
$user = User::factory()->create(['stripe_id' => 'cus_test_1', 'email' => 'max@example.com']);
|
||||
BillingAddress::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'invoice_email' => 'buchhaltung@example.com',
|
||||
'invoice_pdf_to_account' => true,
|
||||
'invoice_pdf_to_invoice_email' => true,
|
||||
]);
|
||||
|
||||
app(ProcessStripeWebhook::class)->handle(new WebhookReceived(stripeInvoicePayload($user)));
|
||||
|
||||
Mail::assertQueuedCount(2);
|
||||
Mail::assertQueued(BookingPaymentConfirmed::class, fn (BookingPaymentConfirmed $mail): bool => $mail->hasTo('max@example.com')
|
||||
&& $mail->attachPdf
|
||||
&& ! $mail->forInvoiceRecipient);
|
||||
Mail::assertQueued(BookingPaymentConfirmed::class, fn (BookingPaymentConfirmed $mail): bool => $mail->hasTo('buchhaltung@example.com')
|
||||
&& $mail->attachPdf
|
||||
&& $mail->forInvoiceRecipient);
|
||||
});
|
||||
|
||||
test('a customer who switched the account pdf off gets the confirmation without attachment', function () {
|
||||
Mail::fake();
|
||||
|
||||
$user = User::factory()->create(['stripe_id' => 'cus_test_1']);
|
||||
BillingAddress::factory()->create(['user_id' => $user->id, 'invoice_pdf_to_account' => false]);
|
||||
|
||||
app(ProcessStripeWebhook::class)->handle(new WebhookReceived(stripeInvoicePayload($user)));
|
||||
|
||||
Mail::assertQueuedCount(1);
|
||||
Mail::assertQueued(BookingPaymentConfirmed::class, fn (BookingPaymentConfirmed $mail): bool => $mail->hasTo($user->email) && ! $mail->attachPdf);
|
||||
});
|
||||
|
||||
test('a replayed invoice webhook does not queue a second confirmation mail', function () {
|
||||
Mail::fake();
|
||||
|
||||
|
|
|
|||
|
|
@ -219,7 +219,9 @@ test('current STR and MAN invoices are listed with a stripe link where available
|
|||
->assertSee('STR-00042')
|
||||
->assertSee('MAN-00007')
|
||||
->assertDontSee('STR-99999')
|
||||
->assertSee('Bei Stripe öffnen')
|
||||
->assertSee('PDF öffnen')
|
||||
->assertSee(route('me.invoices.stripe-pdf', $strInvoice))
|
||||
->assertSee('Beleg bei Stripe')
|
||||
->assertSee(route('me.invoices.stripe', $strInvoice));
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue