101 lines
3.6 KiB
PHP
101 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Member;
|
|
|
|
use App\Domain\Catalog\DomainRuleException;
|
|
use App\Domain\Catalog\Offer;
|
|
use App\Domain\Catalog\OfferStatus;
|
|
use App\Domain\Commerce\Checkout;
|
|
use App\Domain\Commerce\Order;
|
|
use App\Domain\Entitlement\GrantIssuer;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class CheckoutController extends Controller
|
|
{
|
|
public function offers(): View
|
|
{
|
|
$offers = Offer::query()
|
|
->whereHas('versions', fn ($query) => $query->where('status', OfferStatus::Aktiv)->where('holder_kind', 'person'))
|
|
->with(['versions' => fn ($query) => $query->where('status', OfferStatus::Aktiv)])
|
|
->orderBy('name')
|
|
->get();
|
|
|
|
return view('member.offers', ['offers' => $offers]);
|
|
}
|
|
|
|
public function claim(Request $request, Offer $offer, GrantIssuer $issuer): RedirectResponse
|
|
{
|
|
try {
|
|
$issuer->claim($request->user(), $offer);
|
|
} catch (DomainRuleException $exception) {
|
|
return back()->withErrors(['offer' => $exception->getMessage()]);
|
|
}
|
|
|
|
return redirect()->route('member.library')->with('status', 'Das Angebot liegt in Ihrer Bibliothek.');
|
|
}
|
|
|
|
public function place(Request $request, Offer $offer, Checkout $checkout): RedirectResponse
|
|
{
|
|
try {
|
|
$order = $checkout->place($request->user(), $offer);
|
|
} catch (DomainRuleException $exception) {
|
|
return back()->withErrors(['offer' => $exception->getMessage()]);
|
|
}
|
|
|
|
return redirect()->route('member.order', $order);
|
|
}
|
|
|
|
public function show(Request $request, Order $order): View
|
|
{
|
|
abort_unless($order->user_id === $request->user()->id, 403);
|
|
$order->load('lines.offerVersion.offer', 'payments');
|
|
|
|
return view('member.order', [
|
|
'order' => $order,
|
|
'testPayment' => ! app()->isProduction() && config('services.payment.driver') === 'test',
|
|
]);
|
|
}
|
|
|
|
public function confirm(Request $request, Order $order, Checkout $checkout): RedirectResponse
|
|
{
|
|
abort_unless($order->user_id === $request->user()->id, 403);
|
|
|
|
try {
|
|
$checkout->confirmTestPayment($order, 'test-'.$order->id);
|
|
} catch (DomainRuleException $exception) {
|
|
return back()->withErrors(['payment' => $exception->getMessage()]);
|
|
}
|
|
|
|
return redirect()->route('member.library')->with('status', 'Die Testzahlung ist bestätigt. Die Inhalte sind in der Bibliothek.');
|
|
}
|
|
|
|
public function fail(Request $request, Order $order, Checkout $checkout): RedirectResponse
|
|
{
|
|
abort_unless($order->user_id === $request->user()->id, 403);
|
|
|
|
try {
|
|
$checkout->failTestPayment($order, 'test-fail-'.$order->id);
|
|
} catch (DomainRuleException $exception) {
|
|
return back()->withErrors(['payment' => $exception->getMessage()]);
|
|
}
|
|
|
|
return redirect()->route('member.order', $order)->with('status', 'Die Testzahlung wurde abgelehnt. Es wurde keine Berechtigung vergeben.');
|
|
}
|
|
|
|
public function refund(Request $request, Order $order, Checkout $checkout): RedirectResponse
|
|
{
|
|
abort_unless($order->user_id === $request->user()->id, 403);
|
|
|
|
try {
|
|
$checkout->refund($order, 'Widerruf durch das Mitglied im Testmodus');
|
|
} catch (DomainRuleException $exception) {
|
|
return back()->withErrors(['payment' => $exception->getMessage()]);
|
|
}
|
|
|
|
return redirect()->route('member.order', $order)->with('status', 'Die Bestellung ist erstattet. Die Berechtigungen sind widerrufen.');
|
|
}
|
|
}
|