- Implement OfferOptionAdminTest to validate offer option management, including price display, activation, and cancellation. - Create OrganizationPurchaseTest to ensure correct handling of organization purchases, including access control and order processing. - Add ShopProductPageTest to verify shop product listings, search functionality, and product page details. - Introduce BuildsShop trait for seeding test data related to shop functionality. - Implement MoneyTest to validate money parsing and formatting, ensuring correct handling of euro amounts.
266 lines
9.7 KiB
PHP
266 lines
9.7 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Commerce;
|
|
|
|
use App\Domain\Catalog\DomainRuleException;
|
|
use App\Domain\Catalog\Offer;
|
|
use App\Domain\Catalog\OfferService;
|
|
use App\Domain\Catalog\OfferVersion;
|
|
use App\Domain\Entitlement\Acquisition;
|
|
use App\Domain\Entitlement\AcquisitionType;
|
|
use App\Domain\Entitlement\GrantIssuer;
|
|
use App\Domain\Entitlement\GrantStatus;
|
|
use App\Domain\Identity\AccountStatus;
|
|
use App\Domain\Identity\Party;
|
|
use App\Mail\OrderConfirmed;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class Checkout
|
|
{
|
|
public function __construct(
|
|
private readonly GrantIssuer $grants,
|
|
private readonly OfferService $offers,
|
|
private readonly OrderHolders $holders,
|
|
) {}
|
|
|
|
/**
|
|
* Einzelkauf einer Option als Person (bestehender Weg, ohne Warenkorb).
|
|
*/
|
|
public function place(User $user, Offer $offer): Order
|
|
{
|
|
$this->assertActive($user);
|
|
|
|
$version = $this->offers->currentActiveVersion($offer);
|
|
|
|
if ($version->grant_kind !== 'kauf' || $version->holder_kind !== 'person') {
|
|
throw new DomainRuleException('Dieses Angebot kann nicht als persönlicher Kauf bestellt werden.');
|
|
}
|
|
|
|
if ($version->price_cents === null) {
|
|
throw new DomainRuleException('Der Kauf braucht einen Preis an der Angebotsfassung.');
|
|
}
|
|
|
|
$holder = $this->holders->resolve($user, null);
|
|
|
|
return DB::transaction(fn (): Order => $this->createOrder($user, $holder, [[$offer, $version]]));
|
|
}
|
|
|
|
/**
|
|
* Bestellung aus dem Warenkorb, erst nach serverseitiger Prüfung. Die Prüfsumme bindet die Bestellung an die
|
|
* Übersicht, die der Käufer gesehen hat: Hat sich Preis, Fassung oder Bedingung seither geändert, entsteht keine
|
|
* Bestellung und der Käufer sieht zuerst die aktualisierte Übersicht. Positionen, deren Umfang der Inhaber bereits
|
|
* mit denselben Rechten besitzt, erzeugen keine Bestellposition.
|
|
*/
|
|
public function placeFromCart(User $user, Cart $cart, ?int $holderPartyId, string $digest): Order
|
|
{
|
|
$this->assertActive($user);
|
|
|
|
$order = DB::transaction(function () use ($user, $cart, $holderPartyId, $digest): Order {
|
|
// Optionen sperren, damit eine gleichzeitige Aktivierung die Prüfung nicht unterläuft.
|
|
Offer::query()->whereIn('id', array_keys($cart->items()))->orderBy('id')->lockForUpdate()->get();
|
|
$review = $cart->review($user, $holderPartyId);
|
|
|
|
if ($review->holder === null) {
|
|
throw new DomainRuleException('Für diesen Lizenzinhaber ist das Konto nicht bestellberechtigt.');
|
|
}
|
|
|
|
if (! hash_equals($review->digest(), $digest)) {
|
|
throw new StaleOverview('Die Übersicht hat sich geändert. Bitte prüfe die aktualisierte Bestellübersicht.');
|
|
}
|
|
|
|
if ($review->blocked()) {
|
|
throw new DomainRuleException('Entferne zuerst die Positionen, die nicht bestellt werden können.');
|
|
}
|
|
|
|
if ($review->empty()) {
|
|
throw new DomainRuleException('Es gibt nichts zu bestellen. Alle Positionen sind bereits erworben.');
|
|
}
|
|
|
|
$pairs = array_map(fn (CartLine $line): array => [$line->offer, $line->version], $review->orderable());
|
|
|
|
return $this->createOrder($user, $review->holder, $pairs);
|
|
});
|
|
|
|
$cart->clear();
|
|
|
|
return $order;
|
|
}
|
|
|
|
/**
|
|
* @param list<array{0: Offer, 1: OfferVersion}> $pairs
|
|
*/
|
|
private function createOrder(User $user, Party $holder, array $pairs): Order
|
|
{
|
|
$order = Order::query()->create([
|
|
'user_id' => $user->id,
|
|
'party_id' => $holder->id,
|
|
'status' => 'offen',
|
|
'holder_kind' => $holder->type->value,
|
|
'total_cents' => array_sum(array_map(fn (array $pair): int => (int) $pair[1]->price_cents, $pairs)),
|
|
'currency' => 'EUR',
|
|
]);
|
|
|
|
foreach ($pairs as [$offer, $version]) {
|
|
$line = $order->lines()->create([
|
|
'offer_version_id' => $version->id,
|
|
'offer_name' => $offer->name,
|
|
'price_cents' => $version->price_cents,
|
|
'currency' => 'EUR',
|
|
'term_rule' => $version->term_rule,
|
|
'update_promise' => $version->update_promise,
|
|
'holder_kind' => $version->holder_kind,
|
|
'tax_class' => $version->tax_class,
|
|
'grant_kind' => $version->grant_kind,
|
|
'holder_party_id' => $holder->id,
|
|
]);
|
|
$version->load('resolvedScopes.rights');
|
|
|
|
foreach ($version->resolvedScopes as $scope) {
|
|
$line->scopeItems()->create([
|
|
'license_article_id' => $scope->license_article_id,
|
|
'rights_profile_version_id' => $scope->rights_profile_version_id,
|
|
'rights_snapshot' => [
|
|
'purpose' => $scope->rights->purpose,
|
|
'capabilities' => $scope->rights->capabilities,
|
|
'options' => $scope->rights->options ?? [],
|
|
],
|
|
]);
|
|
}
|
|
}
|
|
|
|
return $order->load('lines.scopeItems');
|
|
}
|
|
|
|
/**
|
|
* Freischaltung nach bestätigter Testzahlung: alle Bestellpositionen aus ihrem festgehaltenen Umfang, in einer
|
|
* Transaktion. Wiederholte Bestätigung ist wirkungslos; ein Fehler hinterlässt weder Zahlung noch Rechte.
|
|
*/
|
|
public function confirmTestPayment(Order $order, string $reference): Order
|
|
{
|
|
$this->assertTestDriver();
|
|
$reference = trim($reference);
|
|
|
|
if ($reference === '') {
|
|
throw new DomainRuleException('Die Zahlungsreferenz fehlt.');
|
|
}
|
|
|
|
$paidNow = false;
|
|
$order = DB::transaction(function () use ($order, $reference, &$paidNow): Order {
|
|
$order = Order::query()->lockForUpdate()->findOrFail($order->id);
|
|
|
|
if ($order->status === 'bezahlt') {
|
|
return $order->load('lines', 'payments');
|
|
}
|
|
|
|
if ($order->status !== 'offen') {
|
|
throw new DomainRuleException('Diese Bestellung nimmt keine Zahlung mehr an.');
|
|
}
|
|
|
|
$order->payments()->create([
|
|
'provider' => 'test',
|
|
'status' => 'bestaetigt',
|
|
'provider_reference' => $reference,
|
|
]);
|
|
$order->status = 'bezahlt';
|
|
$order->save();
|
|
$paidNow = true;
|
|
$actor = User::query()->findOrFail($order->user_id);
|
|
|
|
foreach ($order->lines()->orderBy('id')->get() as $line) {
|
|
$holder = Party::query()->findOrFail($line->holder_party_id ?? $order->party_id);
|
|
$this->grants->issueFromOrderLine($line, $holder, $actor);
|
|
}
|
|
|
|
return $order->load('lines', 'payments');
|
|
});
|
|
|
|
if ($paidNow) {
|
|
Mail::to($order->user)->send(new OrderConfirmed($order));
|
|
}
|
|
|
|
return $order;
|
|
}
|
|
|
|
public function failTestPayment(Order $order, string $reference): Order
|
|
{
|
|
$this->assertTestDriver();
|
|
|
|
return DB::transaction(function () use ($order, $reference): Order {
|
|
$order = Order::query()->lockForUpdate()->findOrFail($order->id);
|
|
|
|
if ($order->status !== 'offen') {
|
|
return $order;
|
|
}
|
|
|
|
$order->payments()->create([
|
|
'provider' => 'test',
|
|
'status' => 'fehlgeschlagen',
|
|
'provider_reference' => $reference,
|
|
]);
|
|
$order->status = 'fehlgeschlagen';
|
|
$order->save();
|
|
|
|
return $order;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Vollerstattung: widerruft die Berechtigungen aller Positionen dieser Bestellung. Andere Erwerbsquellen
|
|
* (kostenlose Vergabe, frühere Käufe) bleiben unberührt.
|
|
*/
|
|
public function refund(Order $order, string $reason): Order
|
|
{
|
|
$reason = trim($reason);
|
|
|
|
if ($reason === '') {
|
|
throw new DomainRuleException('Eine Erstattung braucht eine Begründung.');
|
|
}
|
|
|
|
return DB::transaction(function () use ($order, $reason): Order {
|
|
$order = Order::query()->lockForUpdate()->findOrFail($order->id);
|
|
|
|
if ($order->status !== 'bezahlt') {
|
|
throw new DomainRuleException('Nur eine bezahlte Bestellung kann erstattet werden.');
|
|
}
|
|
|
|
foreach ($order->lines()->orderBy('id')->get() as $line) {
|
|
$acquisition = Acquisition::query()->where([
|
|
'type' => AcquisitionType::Bestellposition,
|
|
'reference_type' => 'order_line',
|
|
'reference_id' => $line->id,
|
|
])->first();
|
|
|
|
foreach ($acquisition?->grants ?? [] as $grant) {
|
|
if ($grant->status === GrantStatus::Aktiv) {
|
|
$grant->status = GrantStatus::Widerrufen;
|
|
$grant->status_reason = $reason;
|
|
$grant->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
$order->payments()->where('status', 'bestaetigt')->update(['status' => 'erstattet']);
|
|
$order->status = 'erstattet';
|
|
$order->save();
|
|
|
|
return $order->refresh();
|
|
});
|
|
}
|
|
|
|
private function assertActive(User $user): void
|
|
{
|
|
if ($user->status !== AccountStatus::Aktiv || $user->party_id === null) {
|
|
throw new DomainRuleException('Nur ein aktives Mitglied kann kaufen.');
|
|
}
|
|
}
|
|
|
|
private function assertTestDriver(): void
|
|
{
|
|
if (app()->isProduction() || config('services.payment.driver') !== 'test') {
|
|
throw new DomainRuleException('Der Testzahlungsweg ist nicht aktiv.');
|
|
}
|
|
}
|
|
}
|