<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Controllers\Controller;
use App\Models\WebhookEndpoint;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
final class WebhookEndpointController extends Controller { public function index(Request $r){return ApiResponse::success(WebhookEndpoint::where('merchant_id',$r->user()->id)->get());} public function store(Request $r){$d=$r->validate(['url'=>'required|url:https|max:2048','events'=>'required|array|min:1','environment'=>'required|in:sandbox,live']);$secret='whsec_'.Str::random(48);$e=WebhookEndpoint::create(['merchant_id'=>$r->user()->id,'url'=>$d['url'],'secret_ciphertext'=>$secret,'events'=>$d['events'],'status'=>'active','environment'=>$d['environment']]);return ApiResponse::success(['endpoint'=>$e,'secret'=>$secret,'warning'=>'Store this signing secret now.'],201);} public function destroy(Request $r,WebhookEndpoint $webhookEndpoint){abort_unless($webhookEndpoint->merchant_id===$r->user()->id,404);$webhookEndpoint->update(['status'=>'disabled']);return response()->noContent();} }
