Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

llevvi's avatar

Error while running tests in Laravel Webhook for Stripe

I am testing a webhook that Stripe uses to communicate with my Laravel application. I am using PHP Stripe Webhooks Tester for this and I'm following their official tutorial.

Here's my Test Case:

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use TeamTNT\Stripe;

class WebhooksControllerTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testWhenCustomerSubscriptionDeleted()
    {
      $tester = new TeamTNT\Stripe\WebhookTester('http:/localhost/stripe/webhook');
      $response = $tester->triggerEvent('customer.subscription.deleted');
      $this->assertEquals(200,$response->getStatusCode());

        //$this->assertTrue(true);
    }
}

When I run phpunit I get the following error:

PHPUnit 5.7.5 by Sebastian Bergmann and contributors.

SE                                                                  2 / 2 (100%)

Time: 228 ms, Memory: 6.00MB

There was 1 error:

1) WebhooksControllerTest::testWhenCustomerSubscriptionDeleted
GuzzleHttp\Exception\ClientException: Client error: `POST http://localhost/stripe/webhook` resulted in a `404 Not Found` response:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found (truncated...)


/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:111
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Middleware.php:65
/home/levi/SIGadmin/vendor/guzzlehttp/promises/src/Promise.php:203
/home/levi/SIGadmin/vendor/guzzlehttp/promises/src/Promise.php:156
/home/levi/SIGadmin/vendor/guzzlehttp/promises/src/TaskQueue.php:47
/home/levi/SIGadmin/vendor/guzzlehttp/promises/src/Promise.php:246
/home/levi/SIGadmin/vendor/guzzlehttp/promises/src/Promise.php:223
/home/levi/SIGadmin/vendor/guzzlehttp/promises/src/Promise.php:267
/home/levi/SIGadmin/vendor/guzzlehttp/promises/src/Promise.php:225
/home/levi/SIGadmin/vendor/guzzlehttp/promises/src/Promise.php:62
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Client.php:129
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Client.php:87
/home/levi/SIGadmin/vendor/teamtnt/php-stripe-webhook-tester/src/Stripe/WebhookTester.php:85
/home/levi/SIGadmin/tests/WebhooksControllerTest.php:24
/usr/share/php/PHPUnit/TextUI/Command.php:155
/usr/share/php/PHPUnit/TextUI/Command.php:106

FAILURES!
Tests: 2, Assertions: 0, Errors: 1, Skipped: 1.

Here's my route for this webhook:

Route::post('stripe/webhook', 'WebhooksController@handle');

And here's my actual webhook:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
class WebhooksController extends Controller
{
  public function handle()
  {
      $payload = request()->all();

      $method = $this->eventToMethod($payload['type']);

      if(method_exists($this, $method))
      {
        $this->$method($payload);
      }

      return response('Webhook Received');
      //return redirect('myaccount')->with('success', 'Account deactivated');
  }

  public function whenCustomerSubscriptionDeleted($payload)
  {
    User::byStripeId(
      $payload['data']['object']['customer']
    )->deactivate();
  }

  public function whenCustomerSubscriptionCreated($payload)
  {
    User::byStripeId(
      $payload['data']['object']['customer']
    )->addSubscriptionId($payload['data']['object']['id']);
  }

  public function eventToMethod($event)
  {
    return 'when' . studly_case(str_replace('.', '_', $event));
  }

}
0 likes
1 reply

Please or to participate in this conversation.