Files
fotospiel-app/app/Http/Controllers/Api/Marketing/GiftVoucherCheckoutController.php

105 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Marketing;
use App\Http\Controllers\Controller;
use App\Models\GiftVoucher;
use App\Services\GiftVouchers\GiftVoucherCheckoutService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class GiftVoucherCheckoutController extends Controller
{
public function __construct(private readonly GiftVoucherCheckoutService $checkout) {}
public function tiers(): JsonResponse
{
return response()->json([
'data' => $this->checkout->tiers(),
]);
}
public function store(Request $request): JsonResponse
{
$data = $this->validate($request, [
'tier_key' => ['required', 'string'],
'purchaser_email' => ['required', 'email:rfc,dns'],
'recipient_email' => ['nullable', 'email:rfc,dns'],
'recipient_name' => ['nullable', 'string', 'max:191'],
'message' => ['nullable', 'string', 'max:500'],
'success_url' => ['nullable', 'url'],
'return_url' => ['nullable', 'url'],
]);
$checkout = $this->checkout->create($data);
if (! $checkout['checkout_url']) {
throw ValidationException::withMessages([
'tier_key' => __('Unable to create checkout.'),
]);
}
return response()->json($checkout);
}
public function show(Request $request): JsonResponse
{
$data = $request->validate([
'checkout_id' => ['nullable', 'string', 'required_without_all:order_id,code'],
'order_id' => ['nullable', 'string', 'required_without_all:checkout_id,code'],
'code' => ['nullable', 'string', 'required_without_all:checkout_id,order_id'],
]);
$voucherQuery = GiftVoucher::query()
->where('status', '!=', GiftVoucher::STATUS_PENDING)
->where(function ($query) use ($data) {
$hasCondition = false;
if (! empty($data['checkout_id'])) {
$query->where(function ($inner) use ($data) {
$inner->where('lemonsqueezy_checkout_id', $data['checkout_id'])
->orWhere('paypal_order_id', $data['checkout_id']);
});
$hasCondition = true;
}
if (! empty($data['order_id'])) {
$method = $hasCondition ? 'orWhere' : 'where';
$query->{$method}(function ($inner) use ($data) {
$inner->where('lemonsqueezy_order_id', $data['order_id'])
->orWhere('paypal_capture_id', $data['order_id'])
->orWhere('paypal_order_id', $data['order_id']);
});
$hasCondition = true;
}
if (! empty($data['code'])) {
$method = $hasCondition ? 'orWhere' : 'where';
$query->{$method}('code', strtoupper($data['code']));
}
});
$voucher = $voucherQuery->latest()->firstOrFail();
return response()->json([
'data' => [
'code' => $voucher->code,
'amount' => (float) $voucher->amount,
'currency' => $voucher->currency,
'expires_at' => optional($voucher->expires_at)->toIso8601String(),
'recipient_name' => $voucher->recipient_name,
'recipient_email' => $voucher->recipient_email,
'purchaser_email' => $voucher->purchaser_email,
'status' => $voucher->status,
'redeemed_at' => optional($voucher->redeemed_at)->toIso8601String(),
'refunded_at' => optional($voucher->refunded_at)->toIso8601String(),
],
]);
}
}