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

@@ -0,0 +1,166 @@
<?php
namespace App\Services\PayPal;
use App\Models\EventPackageAddon;
use App\Services\Addons\EventAddonPurchaseService;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class PayPalAddonWebhookService
{
public function __construct(private readonly EventAddonPurchaseService $addons) {}
/**
* @param array<string, mixed> $event
*/
public function handle(array $event): bool
{
$eventType = $event['event_type'] ?? null;
$resource = $event['resource'] ?? null;
if (! is_string($eventType) || ! is_array($resource)) {
return false;
}
$normalized = strtoupper($eventType);
if (! in_array($normalized, [
'PAYMENT.CAPTURE.COMPLETED',
'PAYMENT.CAPTURE.PENDING',
'PAYMENT.CAPTURE.DENIED',
'PAYMENT.CAPTURE.REFUNDED',
'CHECKOUT.ORDER.COMPLETED',
'CHECKOUT.ORDER.VOIDED',
], true)) {
return false;
}
$orderId = $this->resolveOrderId($normalized, $resource);
if (! $orderId) {
return false;
}
$addon = EventPackageAddon::query()
->where('checkout_id', $orderId)
->first();
if (! $addon) {
return false;
}
$lock = Cache::lock('addon:webhook:paypal:'.$orderId, 30);
if (! $lock->get()) {
Log::info('[PayPalAddonWebhook] lock busy', [
'order_id' => $orderId,
'addon_id' => $addon->id,
]);
return true;
}
try {
if ($addon->status === 'completed' && $normalized === 'PAYMENT.CAPTURE.COMPLETED') {
return true;
}
if (in_array($normalized, ['PAYMENT.CAPTURE.COMPLETED', 'CHECKOUT.ORDER.COMPLETED'], true)) {
$captureId = $this->resolveCaptureId($resource, $normalized);
$totals = $this->resolveTotals($resource);
$this->addons->complete(
$addon,
$resource,
$captureId,
$orderId,
$totals['total'] ?? null,
$totals['currency'] ?? null,
[
'paypal_order_id' => $orderId,
'paypal_capture_id' => $captureId,
'paypal_status' => $resource['status'] ?? null,
'paypal_totals' => $totals ?: null,
'paypal_captured_at' => now()->toIso8601String(),
],
);
return true;
}
if (in_array($normalized, ['PAYMENT.CAPTURE.DENIED', 'PAYMENT.CAPTURE.REFUNDED', 'CHECKOUT.ORDER.VOIDED'], true)) {
$reason = match ($normalized) {
'PAYMENT.CAPTURE.DENIED' => 'paypal_capture_denied',
'PAYMENT.CAPTURE.REFUNDED' => 'paypal_refunded',
'CHECKOUT.ORDER.VOIDED' => 'paypal_voided',
default => 'paypal_failed',
};
$this->addons->fail($addon, $reason, $resource);
return true;
}
return false;
} finally {
$lock->release();
}
}
/**
* @param array<string, mixed> $resource
*/
protected function resolveOrderId(string $eventType, array $resource): ?string
{
if (str_starts_with($eventType, 'PAYMENT.CAPTURE.')) {
$relatedOrderId = Arr::get($resource, 'supplementary_data.related_ids.order_id');
return is_string($relatedOrderId) && $relatedOrderId !== '' ? $relatedOrderId : null;
}
$orderId = $resource['id'] ?? null;
return is_string($orderId) && $orderId !== '' ? $orderId : null;
}
/**
* @param array<string, mixed> $resource
*/
protected function resolveCaptureId(array $resource, string $eventType): ?string
{
if (str_starts_with($eventType, 'PAYMENT.CAPTURE.')) {
$captureId = $resource['id'] ?? null;
return is_string($captureId) && $captureId !== '' ? $captureId : null;
}
$captureId = Arr::get($resource, 'purchase_units.0.payments.captures.0.id');
return is_string($captureId) && $captureId !== '' ? $captureId : null;
}
/**
* @param array<string, mixed> $resource
* @return array{currency?: string, total?: float}
*/
protected function resolveTotals(array $resource): array
{
$amount = Arr::get($resource, 'amount')
?? Arr::get($resource, 'purchase_units.0.payments.captures.0.amount')
?? Arr::get($resource, '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,79 @@
<?php
namespace App\Services\PayPal;
use App\Models\GiftVoucher;
use App\Services\GiftVouchers\GiftVoucherService;
use Illuminate\Support\Arr;
class PayPalGiftVoucherWebhookService
{
public function __construct(private readonly GiftVoucherService $vouchers) {}
/**
* @param array<string, mixed> $event
*/
public function handle(array $event): bool
{
$eventType = $event['event_type'] ?? null;
$resource = $event['resource'] ?? null;
if (! is_string($eventType) || ! is_array($resource)) {
return false;
}
$normalized = strtoupper($eventType);
if (! in_array($normalized, [
'PAYMENT.CAPTURE.COMPLETED',
'PAYMENT.CAPTURE.REFUNDED',
'CHECKOUT.ORDER.COMPLETED',
], true)) {
return false;
}
$orderId = $this->resolveOrderId($normalized, $resource);
if (! $orderId) {
return false;
}
$voucher = GiftVoucher::query()
->where('paypal_order_id', $orderId)
->first();
if (! $voucher) {
return false;
}
if (in_array($normalized, ['PAYMENT.CAPTURE.COMPLETED', 'CHECKOUT.ORDER.COMPLETED'], true)) {
$this->vouchers->issueFromPayPal($voucher, $resource, $orderId);
return true;
}
if ($normalized === 'PAYMENT.CAPTURE.REFUNDED') {
$this->vouchers->markRefundedFromPayPal($voucher, $resource);
return true;
}
return false;
}
/**
* @param array<string, mixed> $resource
*/
protected function resolveOrderId(string $eventType, array $resource): ?string
{
if (str_starts_with($eventType, 'PAYMENT.CAPTURE.')) {
$relatedOrderId = Arr::get($resource, 'supplementary_data.related_ids.order_id');
return is_string($relatedOrderId) && $relatedOrderId !== '' ? $relatedOrderId : null;
}
$orderId = $resource['id'] ?? null;
return is_string($orderId) && $orderId !== '' ? $orderId : null;
}
}

View File

@@ -72,6 +72,61 @@ class PayPalOrderService
return $this->client->post('/v2/checkout/orders', $payload, $headers);
}
/**
* @param array{
* custom_id?: string|null,
* return_url?: string|null,
* cancel_url?: string|null,
* locale?: string|null,
* request_id?: string|null
* } $options
* @return array<string, mixed>
*/
public function createSimpleOrder(
string $referenceId,
string $description,
float $amount,
string $currency,
array $options = [],
): array {
$formattedAmount = $this->formatAmount($amount);
$currency = strtoupper($currency);
$purchaseUnit = array_filter([
'reference_id' => Str::limit($referenceId, 127, ''),
'description' => Str::limit($description, 127, ''),
'custom_id' => $options['custom_id'] ?? null,
'amount' => [
'currency_code' => $currency,
'value' => $formattedAmount,
],
], static fn ($value) => $value !== null && $value !== '');
$applicationContext = array_filter([
'brand_name' => config('app.name', 'Fotospiel'),
'landing_page' => 'NO_PREFERENCE',
'user_action' => 'PAY_NOW',
'shipping_preference' => 'NO_SHIPPING',
'locale' => $this->resolveLocale($options['locale'] ?? null),
'return_url' => $options['return_url'] ?? null,
'cancel_url' => $options['cancel_url'] ?? null,
], static fn ($value) => $value !== null && $value !== '');
$payload = [
'intent' => 'CAPTURE',
'purchase_units' => [$purchaseUnit],
'application_context' => $applicationContext,
];
$headers = [];
$requestId = $options['request_id'] ?? null;
if (is_string($requestId) && $requestId !== '') {
$headers['PayPal-Request-Id'] = $requestId;
}
return $this->client->post('/v2/checkout/orders', $payload, $headers);
}
/**
* @param array{request_id?: string|null} $options
* @return array<string, mixed>