- CreditTopup-Gutschrift, MAN-Rechnung, Proof-PDF und Stripe-Webhook- Spiegelung sperren die betroffene Zeile innerhalb der Transaktion und prüfen die Idempotenz erst danach (verhindert Doppelgutschrift bei at-least-once-Webhooks, H4). - Gemeinsamer LocksModelForUpdate-Trait für die häufige Lock-by-Key-Form (CreditTopup, ManualInvoice). - Unique-Index auf invoices.stripe_invoice_id; Boost-Inventar über boost_slot_locks serialisiert, verwaiste Kategorie-Locks werden beim Löschen der Rubrik entfernt. Deployment: vor migrate prüfen, ob invoices.stripe_invoice_id bereits Duplikate enthält (die Unique-Migration bricht sonst bewusst ab). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
100 lines
3.3 KiB
PHP
100 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Billing;
|
|
|
|
use App\Enums\CreditTransactionType;
|
|
use App\Enums\SinglePurchaseStatus;
|
|
use App\Models\CreditTopup;
|
|
use App\Models\User;
|
|
use App\Services\Billing\Concerns\LocksModelForUpdate;
|
|
|
|
/**
|
|
* Wallet-Aufladung über Credit-Pakete (Stripe). Der Kauf wird als Pending-
|
|
* CreditTopup angelegt und nach Zahlungseingang (Webhook) der Wallet
|
|
* gutgeschrieben — idempotent über die `credited_at`-Marke.
|
|
*/
|
|
class CreditTopupService
|
|
{
|
|
use LocksModelForUpdate;
|
|
|
|
public function __construct(private readonly CreditWalletService $wallet) {}
|
|
|
|
/**
|
|
* Verfügbare Credit-Pakete aus der Config.
|
|
*
|
|
* @return list<array{key: string, price_cents: int, credits: int}>
|
|
*/
|
|
public function packs(): array
|
|
{
|
|
return array_values(config('credits.packs', []));
|
|
}
|
|
|
|
/**
|
|
* @return array{key: string, price_cents: int, credits: int}|null
|
|
*/
|
|
public function findPack(string $key): ?array
|
|
{
|
|
foreach ($this->packs() as $pack) {
|
|
if ($pack['key'] === $key) {
|
|
return $pack;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Legt einen Pending-Topup für ein Paket an (vor dem Stripe-Checkout).
|
|
*/
|
|
public function startTopup(User $user, string $packKey): CreditTopup
|
|
{
|
|
$pack = $this->findPack($packKey);
|
|
|
|
if (! $pack) {
|
|
throw new \InvalidArgumentException("Unbekanntes Credit-Paket: {$packKey}.");
|
|
}
|
|
|
|
return $user->creditTopups()->create([
|
|
'pack_key' => $pack['key'],
|
|
'credits' => $pack['credits'],
|
|
'price_cents' => $pack['price_cents'],
|
|
'currency' => config('billing.default_currency'),
|
|
'status' => SinglePurchaseStatus::Pending,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Schreibt einen bezahlten Topup der Wallet gut. Idempotent: bereits
|
|
* gutgeschriebene Topups (credited_at gesetzt) werden übersprungen.
|
|
*/
|
|
public function fulfill(CreditTopup $topup, ?string $sessionId = null, ?string $paymentIntentId = null): void
|
|
{
|
|
// Zeile sperren UND Idempotenz innerhalb der Transaktion prüfen: Stripe
|
|
// liefert Webhooks at-least-once (Original + Retry). Läge der Guard außen,
|
|
// könnten zwei parallele Zustellungen beide `credited_at === null` lesen
|
|
// und die Wallet doppelt gutschreiben. Der Lock serialisiert sie.
|
|
$this->withLockedByKey($topup, function (CreditTopup $locked) use ($topup, $sessionId, $paymentIntentId): void {
|
|
if ($locked->credited_at !== null) {
|
|
return;
|
|
}
|
|
|
|
$locked->update([
|
|
'status' => SinglePurchaseStatus::Paid,
|
|
'paid_at' => $locked->paid_at ?? now(),
|
|
'credited_at' => now(),
|
|
'stripe_checkout_session_id' => $sessionId ?? $locked->stripe_checkout_session_id,
|
|
'stripe_payment_intent_id' => $paymentIntentId ?? $locked->stripe_payment_intent_id,
|
|
]);
|
|
|
|
$this->wallet->credit(
|
|
$locked->user,
|
|
$locked->credits,
|
|
CreditTransactionType::Topup,
|
|
"Credit-Paket {$locked->pack_key}",
|
|
$locked,
|
|
);
|
|
|
|
$topup->setRawAttributes($locked->getAttributes());
|
|
});
|
|
}
|
|
}
|