Stripe webhooks on Yii2

I have to use Stripe webhooks (a Stripe listener that send events when a customer buys/proceed an operation on a product) to a php file on my website.

I decided to put this file, receiving Stripe webhooks, in my root folder.
The architecture of my project looks like this :


Considering this architecture, Stripe sends events to my webhook.php file.
In webhook.php I receive a message from the event then I check what is the type of the event (such as ‘customer.subscription.updated’ or ‘customer.subscription.deleted’).
Then on a switch case statement I handle these cases with functions I created like deleteSubscription() or updateSubscription().

But I need to access Yii’s functions to get my Stripe API key which I need to get from my database.
To be more precice I would need to do somthing like :
$stripeApiKey = $Yii::$app->settings->get('common', 'StripeApiKey')

But I consequently get the error “Class ‘Yii’ not found in …/webhook.php”
And if I add “use Yii;” I get “Warning: The use statement with non-compound name ‘Yii’ has no effect in”.

So my question is, have you got an idea of the good way to use Yii::$app in my webhook.php file ?

Thanks in advance for your answers.


i assume that webhook.php is the entrypoint (Bootstrap-File) of your App.

The Yii::$app Singleton is initialized on \yii\base\Application::__construct().
Therefore, you need to init an Yii-Application in order to use Yii::$app->foo.

Your webhook.php could look like this:

defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');

// you have to adjust this paths!
require '../vendor/autoload.php';
require '../vendor/yiisoft/yii2/Yii.php';

// you have to adjust DB-Settings!
// you have to adjust basePath!
$config = [
    'id' => 'stripeHooks',
    'name' => 'stripe hook handler-app',
    'basePath' => dirname(__DIR__),
    'language' => 'en-EN',
    'timeZone' => 'Europe/Berlin',
    'components' => [
        'db' => [
            'class'     => 'yii\db\Connection',
            'dsn'       => 'mysql:host=localhost;dbname=mydatabase',
            'username'  => 'myuser',
            'password'  => '123-lol',
            'charset'   => 'utf8',

(new \yii\web\Application($config))->run();

The Application will now try to resolve a Request to a valid Controller.
You have to operate all Actions in your Controllers.

1 Like

Hi @HenryVolkmer,
I actually already have an environment.php and a config.php file in my application directory.

The webhook.php file is used as an endpoint to stripe requests.

What I could do is put this file somewhere in my content/… directory but it would return me an http forbidden response.

Do I have to create another external app of the first one (as you did) and set another .env file to get my mysql parameters or do I have to handle it differently ?

I guess that maybe one way would be to move my webhook file into content and modify the .htaccess ?

Or maybe it has to do with a conflict between stripe and csrf tokens ?