<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use GoCardlessPro\Resources\Event;

class WebHookEventCheck extends Controller {

    public function event(Request $request) {

        $webhook_endpoint_secret = "-JW5gFGnB9vJ1kGPbma_OvL1Oqx-zLiwh5HLIhsZ";

        $request_body = file_get_contents('php://input');

        $headers = getallheaders();
        $signature_header = $headers["Webhook-Signature"];

        try {
            $events = gocardless()->environment()->parse($request_body, $signature_header, $webhook_endpoint_secret);
            echo json_encode($events);
            foreach ($events as $event) {
                // You can access each event in the webhook.
                echo($event->id);
            }

            header("HTTP/1.1 200 OK");
        } catch (GoCardlessPro\Core\Exception\InvalidSignatureException $e) {
            echo $e->getMessages();
            // The webhook doesn't appear to be genuinely from GoCardless, as the signature
            // included in the `Webhook-Signature` header doesn't match the one computed with
            // your webhook endpoint secret and the body
            header("HTTP/1.1 498 Invalid Token");
        }
    }

}
