@@ -0,0 +1,30 @@
|
||||
<?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]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user