2

I was trying to implement a webhook in laravel.

I have created access token and created webhook endpoint also.

my webhook end point is like,https://www.example.com/gocardless.php

and my route is like,

Route::get('/gocardless.php', 
'\App\Http\Controllers\gocardlessController@remote')->name('remote');

Controller code like,

class gocardlessController extends Controller
 {


  public function remote(Request $request)
  {

 $token ="token";

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

 $headers = getallheaders();


 $provided_signature = $headers["Webhook-Signature"];
 $calculated_signature = hash_hmac("sha256",$raw_payload,$token);
 if ($provided_signature == $calculated_signature) {

  $payload = json_decode($raw_payload, true);
   }
   }
   } 

But when i clik on send test webhook in gocardless account,they are given "405 no method found" as responce.

How i can solve this?

Reshma
  • 189
  • 2
  • 5
  • 18

2 Answers2

3

The HTTP 405 error you're seeing indicates that your Laravel application doesn't know how to handle the method of the incoming request.

GoCardless webhooks use the POST method to send you a request with a JSON body, but the route you've written is for handling a GET request (Route::get). To resolve this, you should define a route for POST requests to the endpoint which will receive webhooks.

jpn
  • 786
  • 6
  • 20
  • I have changed it with (Route::post).But now it throws other error like "500 internal server error" – Reshma Dec 11 '17 at 11:31
  • @Reshma your code is likely raising an exception which you are not handling. You should catch that exception and inspect it to see what the actual error is (at a guess, it's probably not related to Laravel or GoCardless!) http://php.net/manual/en/language.exceptions.php is the PHP documentation for exception handling, which should be a good starting point for this topic. – jpn Dec 11 '17 at 14:50
2

A few remarks and fixes

Remark

Why do you include the "ugly" .php extension in your route, there is no need for that

Fix

Change your route (in web.php) to

Route::get('gocardless', 'gocardlessController@remote');

Remark

I also see you start your controller name with lowercase, this is not common practise

Fix

Don't forget to add these lines in your controller at the top

namespace App\Http\Controllers; // declare right namespace

use Illuminate\Http\Request; // Hint which Request class to use below

For the body: that you really have to write yourself and return the data as json for example

online Thomas
  • 8,864
  • 6
  • 44
  • 85