31 lines
1.2 KiB
PHP
31 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Billing\PaymentGatewayManager;
|
|
use App\Models\BillingOrder;
|
|
use App\Services\BillingService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class PaymentWebhookController extends Controller
|
|
{
|
|
public function __invoke(string $gateway, Request $request, PaymentGatewayManager $gateways, BillingService $billing): JsonResponse
|
|
{
|
|
$driver = $gateways->driver($gateway);
|
|
$payload = $request->all();
|
|
abort_unless($driver->verifyWebhook($payload, $request->headers->all()), 401, 'Invalid payment signature.');
|
|
$externalId = $driver->externalIdFromWebhook($payload);
|
|
$order = BillingOrder::withoutGlobalScope('tenant')->where('gateway', $gateway)->where('external_id', $externalId)->firstOrFail();
|
|
abort_unless((int) ($payload['amount'] ?? 0) === (int) $order->total, 422, 'Payment amount mismatch.');
|
|
$status = $driver->statusFromWebhook($payload);
|
|
if ($status === 'paid') {
|
|
$billing->activate($order);
|
|
} elseif ($order->status === 'pending') {
|
|
$order->update(['status' => $status]);
|
|
}
|
|
|
|
return response()->json(['success' => true]);
|
|
}
|
|
}
|