Add PayPal webhook handling
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Models\CheckoutSession;
|
||||
use App\Models\Package;
|
||||
use App\Services\Checkout\CheckoutAssignmentService;
|
||||
use App\Services\Checkout\CheckoutSessionService;
|
||||
use App\Services\Coupons\CouponRedemptionService;
|
||||
use App\Services\Coupons\CouponService;
|
||||
use App\Services\PayPal\Exceptions\PayPalException;
|
||||
use App\Services\PayPal\PayPalOrderService;
|
||||
@@ -24,6 +25,7 @@ class PayPalCheckoutController extends Controller
|
||||
private readonly CheckoutSessionService $sessions,
|
||||
private readonly CheckoutAssignmentService $assignment,
|
||||
private readonly CouponService $coupons,
|
||||
private readonly CouponRedemptionService $couponRedemptions,
|
||||
) {}
|
||||
|
||||
public function create(PayPalCheckoutRequest $request): JsonResponse
|
||||
@@ -61,7 +63,7 @@ class PayPalCheckoutController extends Controller
|
||||
$couponCode = Str::upper(trim((string) ($data['coupon_code'] ?? '')));
|
||||
|
||||
if ($couponCode !== '') {
|
||||
$preview = $this->coupons->preview($couponCode, $package, $tenant);
|
||||
$preview = $this->coupons->preview($couponCode, $package, $tenant, CheckoutSession::PROVIDER_PAYPAL);
|
||||
$this->sessions->applyCoupon($session, $preview['coupon'], $preview['pricing']);
|
||||
}
|
||||
|
||||
@@ -154,6 +156,7 @@ class PayPalCheckoutController extends Controller
|
||||
]);
|
||||
|
||||
$this->sessions->markFailed($session, 'paypal_capture_failed');
|
||||
$this->couponRedemptions->recordFailure($session, 'paypal_capture_failed');
|
||||
|
||||
return response()->json([
|
||||
'status' => CheckoutSession::STATUS_FAILED,
|
||||
@@ -191,6 +194,7 @@ class PayPalCheckoutController extends Controller
|
||||
]);
|
||||
|
||||
$this->sessions->markCompleted($session, now());
|
||||
$this->couponRedemptions->recordSuccess($session, $capture);
|
||||
|
||||
return response()->json([
|
||||
'status' => CheckoutSession::STATUS_COMPLETED,
|
||||
@@ -209,6 +213,7 @@ class PayPalCheckoutController extends Controller
|
||||
}
|
||||
|
||||
$this->sessions->markFailed($session, 'paypal_'.$status);
|
||||
$this->couponRedemptions->recordFailure($session, 'paypal_'.$status);
|
||||
|
||||
return response()->json([
|
||||
'status' => CheckoutSession::STATUS_FAILED,
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
||||
use App\Models\CheckoutSession;
|
||||
use App\Services\Checkout\CheckoutAssignmentService;
|
||||
use App\Services\Checkout\CheckoutSessionService;
|
||||
use App\Services\Coupons\CouponRedemptionService;
|
||||
use App\Services\PayPal\Exceptions\PayPalException;
|
||||
use App\Services\PayPal\PayPalOrderService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
@@ -18,6 +19,7 @@ class PayPalReturnController extends Controller
|
||||
private readonly PayPalOrderService $orders,
|
||||
private readonly CheckoutSessionService $sessions,
|
||||
private readonly CheckoutAssignmentService $assignment,
|
||||
private readonly CouponRedemptionService $couponRedemptions,
|
||||
) {}
|
||||
|
||||
public function __invoke(Request $request): RedirectResponse
|
||||
@@ -57,6 +59,7 @@ class PayPalReturnController extends Controller
|
||||
]);
|
||||
|
||||
$this->sessions->markFailed($session, 'paypal_capture_failed');
|
||||
$this->couponRedemptions->recordFailure($session, 'paypal_capture_failed');
|
||||
|
||||
return redirect()->to($this->resolveSafeRedirect($cancelUrl, $fallback));
|
||||
}
|
||||
@@ -89,11 +92,13 @@ class PayPalReturnController extends Controller
|
||||
]);
|
||||
|
||||
$this->sessions->markCompleted($session, now());
|
||||
$this->couponRedemptions->recordSuccess($session, $capture);
|
||||
|
||||
return redirect()->to($this->resolveSafeRedirect($successUrl, $fallback));
|
||||
}
|
||||
|
||||
$this->sessions->markFailed($session, 'paypal_'.$status);
|
||||
$this->couponRedemptions->recordFailure($session, 'paypal_'.$status);
|
||||
|
||||
return redirect()->to($this->resolveSafeRedirect($cancelUrl, $fallback));
|
||||
}
|
||||
|
||||
110
app/Http/Controllers/PayPalWebhookController.php
Normal file
110
app/Http/Controllers/PayPalWebhookController.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\Integrations\IntegrationWebhookRecorder;
|
||||
use App\Services\PayPal\PayPalWebhookService;
|
||||
use App\Services\PayPal\PayPalWebhookVerifier;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class PayPalWebhookController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PayPalWebhookVerifier $verifier,
|
||||
private readonly PayPalWebhookService $webhooks,
|
||||
private readonly IntegrationWebhookRecorder $recorder,
|
||||
) {}
|
||||
|
||||
public function handle(Request $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
$payload = $this->decodePayload($request);
|
||||
|
||||
if (! is_array($payload)) {
|
||||
return response()->json(['status' => 'ignored'], Response::HTTP_ACCEPTED);
|
||||
}
|
||||
|
||||
if (! $this->verifier->verify($request, $payload)) {
|
||||
Log::warning('PayPal webhook signature verification failed');
|
||||
|
||||
return response()->json(['status' => 'invalid'], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$eventType = $payload['event_type'] ?? null;
|
||||
$eventId = $payload['id'] ?? null;
|
||||
|
||||
$webhookEvent = $this->recorder->recordReceived(
|
||||
'paypal',
|
||||
is_string($eventId) ? $eventId : null,
|
||||
is_string($eventType) ? $eventType : null,
|
||||
);
|
||||
|
||||
$handled = is_string($eventType) ? $this->webhooks->handle($payload) : false;
|
||||
|
||||
Log::info('PayPal webhook processed', [
|
||||
'event_type' => $eventType,
|
||||
'handled' => $handled,
|
||||
]);
|
||||
|
||||
if ($handled) {
|
||||
$this->recorder->markProcessed($webhookEvent, ['handled' => true]);
|
||||
} else {
|
||||
$this->recorder->markIgnored($webhookEvent, ['handled' => false]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => $handled ? 'processed' : 'ignored',
|
||||
], $handled ? Response::HTTP_OK : Response::HTTP_ACCEPTED);
|
||||
} catch (\Throwable $exception) {
|
||||
$eventId = $this->captureWebhookException($exception);
|
||||
|
||||
Log::error('PayPal webhook processing failed', [
|
||||
'message' => $exception->getMessage(),
|
||||
'event_type' => (string) data_get($request->json()->all(), 'event_type'),
|
||||
'sentry_event_id' => $eventId,
|
||||
]);
|
||||
|
||||
if (isset($webhookEvent)) {
|
||||
$this->recorder->markFailed($webhookEvent, $exception->getMessage());
|
||||
}
|
||||
|
||||
return response()->json(['status' => 'error'], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
protected function decodePayload(Request $request): ?array
|
||||
{
|
||||
$payload = $request->getContent();
|
||||
|
||||
if (! is_string($payload) || $payload === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$decoded = json_decode($payload, true);
|
||||
|
||||
return is_array($decoded) ? $decoded : null;
|
||||
}
|
||||
|
||||
protected function captureWebhookException(\Throwable $exception): ?string
|
||||
{
|
||||
report($exception);
|
||||
|
||||
if (! app()->bound('sentry') || empty(config('sentry.dsn'))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$eventId = app('sentry')->captureException($exception);
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $eventId ? (string) $eventId : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user