presseportale/app/Services/Billing/ProofPdfService.php
Kevin Adametz b255d90721 Optimierung 3.5: Kleinere Zentralisierungen (VAT, findByEmail, Waehrung, Provisioning)
- VatResolver::normalizeCountryCode()/isEuCountry() als Single Source of Truth;
  resolve(), VatIdValidationService und StripeCheckoutService nutzen jetzt die
  zentrale EU-Pruefung statt eigener config-Lookups.
- User::findByEmail() buendelt die LOWER(email)-Lookups (ContactAccessService,
  SocialAuthService). Bleibt bewusst case-insensitiv, da E-Mails nicht
  normalisiert gespeichert werden; der indexfreundliche Direktvergleich
  braucht erst eine Schreib-Normalisierung (offen, dokumentiert).
- config('billing.default_currency') ersetzt 8 verstreute 'EUR'/'eur'-Literale
  (Stripe-Calls via strtolower). Vereinheitlicht nebenbei die zuvor uneinheitlich
  gespeicherte SinglePurchase.currency.
- Neuer CustomerAccountProvisioner::provisionVerifiedCustomer() ist die
  gemeinsame Provisioning-Methode (verifiziert + aktiv + customer-Rolle) fuer
  ContactAccessService und SocialAuthService.
- Tests: UserFindByEmailTest, erweiterte VatResolverTest.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 11:14:05 +00:00

92 lines
2.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Services\Billing;
use App\Enums\PressReleaseStatus;
use App\Enums\SinglePurchaseStatus;
use App\Enums\SinglePurchaseType;
use App\Exceptions\ProofPdfNotAvailableException;
use App\Models\PressRelease;
use App\Models\SinglePurchase;
use App\Models\User;
use Illuminate\Support\Facades\DB;
/**
* Kauf des Veröffentlichungsnachweis-PDF laut Decision-Update §2.3: 3 Credits
* pauschal pro PM aus der Wallet. Der Kauf wird als bezahlter SinglePurchase
* (Typ ProofPdf) festgehalten — ein erneuter Download derselben PM ist danach
* kostenfrei (Impulskauf, keine Doppelbelastung).
*/
class ProofPdfService
{
public function __construct(
private readonly CreditPricingService $pricing,
private readonly CreditWalletService $wallet,
) {}
public function priceCredits(): int
{
return $this->pricing->proofPdfCredits();
}
/**
* Ein Nachweis ist nur für veröffentlichte Meldungen sinnvoll.
*/
public function isAvailable(PressRelease $pressRelease): bool
{
return $pressRelease->status === PressReleaseStatus::Published;
}
public function hasPurchased(User $user, PressRelease $pressRelease): bool
{
return $this->purchaseQuery($user, $pressRelease)->exists();
}
/**
* Kauft (oder liefert den bereits gekauften) Nachweis. Belastet die Wallet
* nur beim Erstkauf. Wirft ProofPdfNotAvailableException, wenn die PM nicht
* veröffentlicht ist, und InsufficientCreditsException ohne Deckung.
*/
public function purchase(User $user, PressRelease $pressRelease): SinglePurchase
{
if (! $this->isAvailable($pressRelease)) {
throw ProofPdfNotAvailableException::notPublished();
}
$existing = $this->purchaseQuery($user, $pressRelease)->first();
if ($existing) {
return $existing;
}
$credits = $this->priceCredits();
return DB::transaction(function () use ($user, $pressRelease, $credits): SinglePurchase {
$purchase = $user->singlePurchases()->create([
'type' => SinglePurchaseType::ProofPdf,
'status' => SinglePurchaseStatus::Paid,
'price_cents' => $credits * 100,
'currency' => config('billing.default_currency'),
'press_release_id' => $pressRelease->id,
'paid_at' => now(),
]);
$this->wallet->debit(
$user,
$credits,
"Veröffentlichungsnachweis – PM #{$pressRelease->id}",
$purchase,
);
return $purchase;
});
}
private function purchaseQuery(User $user, PressRelease $pressRelease)
{
return $user->singlePurchases()
->where('type', SinglePurchaseType::ProofPdf->value)
->where('status', SinglePurchaseStatus::Paid->value)
->where('press_release_id', $pressRelease->id);
}
}