Add PayPal support for add-on and gift voucher checkout

This commit is contained in:
Codex Agent
2026-02-04 14:54:40 +01:00
parent 7025418d9e
commit 17025df47b
24 changed files with 1599 additions and 34 deletions

View File

@@ -36,7 +36,7 @@ class GiftVoucherCheckoutController extends Controller
if (! $checkout['checkout_url']) {
throw ValidationException::withMessages([
'tier_key' => __('Unable to create Lemon Squeezy checkout.'),
'tier_key' => __('Unable to create checkout.'),
]);
}
@@ -51,19 +51,38 @@ class GiftVoucherCheckoutController extends Controller
'code' => ['nullable', 'string', 'required_without_all:checkout_id,order_id'],
]);
$voucherQuery = GiftVoucher::query();
$voucherQuery = GiftVoucher::query()
->where('status', '!=', GiftVoucher::STATUS_PENDING)
->where(function ($query) use ($data) {
$hasCondition = false;
if (! empty($data['checkout_id'])) {
$voucherQuery->where('lemonsqueezy_checkout_id', $data['checkout_id']);
}
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']);
});
if (! empty($data['order_id'])) {
$voucherQuery->orWhere('lemonsqueezy_order_id', $data['order_id']);
}
$hasCondition = true;
}
if (! empty($data['code'])) {
$voucherQuery->orWhere('code', strtoupper($data['code']));
}
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();

View File

@@ -3,6 +3,7 @@
namespace App\Http\Controllers\Api\Tenant;
use App\Http\Controllers\Controller;
use App\Models\CheckoutSession;
use App\Services\Addons\EventAddonCatalog;
use Illuminate\Http\JsonResponse;
@@ -12,9 +13,25 @@ class EventAddonCatalogController extends Controller
public function index(): JsonResponse
{
$provider = config('package-addons.provider')
?? config('checkout.default_provider', CheckoutSession::PROVIDER_PAYPAL);
$addons = collect($this->catalog->all())
->filter(fn (array $addon) => ! empty($addon['variant_id']))
->map(fn (array $addon, string $key) => array_merge($addon, ['key' => $key]))
->map(function (array $addon, string $key) use ($provider): array {
$priceId = $provider === CheckoutSession::PROVIDER_PAYPAL
? ($addon['price'] ?? null ? 'paypal' : null)
: ($addon['variant_id'] ?? null);
return [
'key' => $key,
'label' => $addon['label'] ?? null,
'price_id' => $priceId,
'increments' => $addon['increments'] ?? [],
'price' => $addon['price'] ?? null,
'currency' => $addon['currency'] ?? 'EUR',
];
})
->filter(fn (array $addon) => ! empty($addon['price_id']))
->values()
->all();

View File

@@ -0,0 +1,150 @@
<?php
namespace App\Http\Controllers;
use App\Models\EventPackageAddon;
use App\Services\Addons\EventAddonPurchaseService;
use App\Services\PayPal\Exceptions\PayPalException;
use App\Services\PayPal\PayPalOrderService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class PayPalAddonReturnController extends Controller
{
public function __construct(
private readonly PayPalOrderService $orders,
private readonly EventAddonPurchaseService $addons,
) {}
public function __invoke(Request $request): RedirectResponse
{
$orderId = $this->resolveOrderId($request);
$fallback = $this->resolveFallbackUrl();
if (! $orderId) {
return redirect()->to($fallback);
}
$addon = EventPackageAddon::query()
->where('checkout_id', $orderId)
->first();
if (! $addon) {
return redirect()->to($fallback);
}
$successUrl = Arr::get($addon->metadata ?? [], 'paypal_success_url')
?? Arr::get($addon->metadata ?? [], 'success_url');
$cancelUrl = Arr::get($addon->metadata ?? [], 'paypal_cancel_url')
?? Arr::get($addon->metadata ?? [], 'cancel_url');
if ($addon->status === 'completed') {
return redirect()->to($this->resolveSafeRedirect($successUrl, $fallback));
}
try {
$capture = $this->orders->captureOrder($orderId, [
'request_id' => 'addon-'.$addon->id,
]);
} catch (PayPalException $exception) {
$this->addons->fail($addon, 'paypal_capture_failed', [
'message' => $exception->getMessage(),
'status' => $exception->status(),
]);
return redirect()->to($this->resolveSafeRedirect($cancelUrl, $fallback));
}
$captureId = $this->resolveCaptureId($capture);
$totals = $this->resolveTotals($capture);
$this->addons->complete(
$addon,
$capture,
$captureId,
$orderId,
$totals['total'] ?? null,
$totals['currency'] ?? null,
[
'paypal_order_id' => $orderId,
'paypal_capture_id' => $captureId,
'paypal_status' => $capture['status'] ?? null,
'paypal_totals' => $totals ?: null,
'paypal_captured_at' => now()->toIso8601String(),
],
);
return redirect()->to($this->resolveSafeRedirect($successUrl, $fallback));
}
protected function resolveOrderId(Request $request): ?string
{
$candidate = $request->query('token') ?? $request->query('order_id');
if (! is_string($candidate) || $candidate === '') {
return null;
}
return $candidate;
}
protected function resolveFallbackUrl(): string
{
return rtrim((string) config('app.url', url('/')), '/') ?: url('/');
}
protected function resolveSafeRedirect(?string $target, string $fallback): string
{
if (! $target) {
return $fallback;
}
if (Str::startsWith($target, ['/'])) {
return $target;
}
$appHost = parse_url($fallback, PHP_URL_HOST);
$targetHost = parse_url($target, PHP_URL_HOST);
if ($appHost && $targetHost && Str::lower($appHost) === Str::lower($targetHost)) {
return $target;
}
return $fallback;
}
/**
* @param array<string, mixed> $capture
*/
protected function resolveCaptureId(array $capture): ?string
{
$captureId = Arr::get($capture, 'purchase_units.0.payments.captures.0.id')
?? Arr::get($capture, 'id');
return is_string($captureId) && $captureId !== '' ? $captureId : null;
}
/**
* @param array<string, mixed> $capture
* @return array{currency?: string, total?: float}
*/
protected function resolveTotals(array $capture): array
{
$amount = Arr::get($capture, 'purchase_units.0.payments.captures.0.amount')
?? Arr::get($capture, 'purchase_units.0.amount');
if (! is_array($amount)) {
return [];
}
$currency = Arr::get($amount, 'currency_code');
$total = Arr::get($amount, 'value');
return array_filter([
'currency' => is_string($currency) ? strtoupper($currency) : null,
'total' => is_numeric($total) ? (float) $total : null,
], static fn ($value) => $value !== null);
}
}

View File

@@ -0,0 +1,129 @@
<?php
namespace App\Http\Controllers;
use App\Models\GiftVoucher;
use App\Services\GiftVouchers\GiftVoucherService;
use App\Services\PayPal\Exceptions\PayPalException;
use App\Services\PayPal\PayPalOrderService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class PayPalGiftVoucherReturnController extends Controller
{
public function __construct(
private readonly PayPalOrderService $orders,
private readonly GiftVoucherService $vouchers,
) {}
public function __invoke(Request $request): RedirectResponse
{
$orderId = $this->resolveOrderId($request);
$fallback = $this->resolveFallbackUrl();
if (! $orderId) {
return redirect()->to($fallback);
}
$voucher = GiftVoucher::query()
->where('paypal_order_id', $orderId)
->first();
if (! $voucher) {
return redirect()->to($fallback);
}
$successUrl = Arr::get($voucher->metadata ?? [], 'paypal_success_url')
?? Arr::get($voucher->metadata ?? [], 'success_url');
$cancelUrl = Arr::get($voucher->metadata ?? [], 'paypal_cancel_url')
?? Arr::get($voucher->metadata ?? [], 'return_url');
if (in_array($voucher->status, [GiftVoucher::STATUS_ISSUED, GiftVoucher::STATUS_REDEEMED], true)) {
return redirect()->to($this->resolveSafeRedirect($this->appendOrderId($successUrl, $orderId), $fallback));
}
try {
$capture = $this->orders->captureOrder($orderId, [
'request_id' => 'gift-voucher-'.$voucher->id,
]);
} catch (PayPalException $exception) {
return redirect()->to($this->resolveSafeRedirect($cancelUrl, $fallback));
}
$this->vouchers->issueFromPayPal($voucher, $capture, $orderId);
return redirect()->to($this->resolveSafeRedirect($this->appendOrderId($successUrl, $orderId), $fallback));
}
protected function resolveOrderId(Request $request): ?string
{
$candidate = $request->query('token') ?? $request->query('order_id');
if (! is_string($candidate) || $candidate === '') {
return null;
}
return $candidate;
}
protected function resolveFallbackUrl(): string
{
return rtrim((string) config('app.url', url('/')), '/') ?: url('/');
}
protected function resolveSafeRedirect(?string $target, string $fallback): string
{
if (! $target) {
return $fallback;
}
if (Str::startsWith($target, ['/'])) {
return $target;
}
$appHost = parse_url($fallback, PHP_URL_HOST);
$targetHost = parse_url($target, PHP_URL_HOST);
if ($appHost && $targetHost && Str::lower($appHost) === Str::lower($targetHost)) {
return $target;
}
return $fallback;
}
protected function appendOrderId(?string $url, string $orderId): ?string
{
if (! $url) {
return null;
}
$parts = parse_url($url);
if (! $parts) {
return $url;
}
$query = [];
if (! empty($parts['query'])) {
parse_str($parts['query'], $query);
}
if (! isset($query['order_id'])) {
$query['order_id'] = $orderId;
}
$scheme = $parts['scheme'] ?? null;
$host = $parts['host'] ?? null;
$port = isset($parts['port']) ? ':'.$parts['port'] : '';
$path = $parts['path'] ?? '';
$fragment = isset($parts['fragment']) ? '#'.$parts['fragment'] : '';
$queryString = $query ? '?'.http_build_query($query) : '';
if ($scheme && $host) {
return $scheme.'://'.$host.$port.$path.$queryString.$fragment;
}
return $path.$queryString.$fragment;
}
}

View File

@@ -3,6 +3,8 @@
namespace App\Http\Controllers;
use App\Services\Integrations\IntegrationWebhookRecorder;
use App\Services\PayPal\PayPalAddonWebhookService;
use App\Services\PayPal\PayPalGiftVoucherWebhookService;
use App\Services\PayPal\PayPalWebhookService;
use App\Services\PayPal\PayPalWebhookVerifier;
use Illuminate\Http\JsonResponse;
@@ -15,6 +17,8 @@ class PayPalWebhookController extends Controller
public function __construct(
private readonly PayPalWebhookVerifier $verifier,
private readonly PayPalWebhookService $webhooks,
private readonly PayPalAddonWebhookService $addonWebhooks,
private readonly PayPalGiftVoucherWebhookService $giftVoucherWebhooks,
private readonly IntegrationWebhookRecorder $recorder,
) {}
@@ -42,7 +46,13 @@ class PayPalWebhookController extends Controller
is_string($eventType) ? $eventType : null,
);
$handled = is_string($eventType) ? $this->webhooks->handle($payload) : false;
$handled = false;
if (is_string($eventType)) {
$handled = $this->webhooks->handle($payload) || $handled;
$handled = $this->addonWebhooks->handle($payload) || $handled;
$handled = $this->giftVoucherWebhooks->handle($payload) || $handled;
}
Log::info('PayPal webhook processed', [
'event_type' => $eventType,