\Paynl\Config::setApiToken() to set the API token is required

We are using the Pay.nl SDK to work with pay.nl. We load the Pay.nl token and service id keys with the function:

public function __construct()
{
Config::setApiToken(env('PAY_TOKEN'));
Config::setServiceId(env('SERVICE_ID'));
}

in app/Services/PayPaymentService.php.

No API Token Set

Lately we ran into the issue with the keys not being loaded properly. We would get the error

'No APItoken is set, use \Paynl\Config::setApiToken() to set the API token' is required

With a simple

php artisan config:clear

this would be resolved. But why was this happening?

Background

When you read the Laravel documentation on the loading of configuration data you will see that the .env is used to load these and you know this. And when you do make changes there these have to be reloaded using:

php artisan config:clear

We also use:

php artisan config:cache

to preload the env variables post deployment by caching them with Deployer. And this tends to work well… except for the Pay.nl API token.

Preloading .env keys

These things mentioned earlier on are kind of clear, but do not explain our issue. Why is this happening? Why are these keys not loaded or preloaded? With env() we load .env variables so that should work. But then, it sometimes doesn’t. So then what is the issue.

Config Class

Now let’s look at the Config. It is a Pay.nl SDK Class used to load config data and located at vendor/paynl/sdk/src/Config.php. With it you can access static methods which set the token and service id number.

In the Pay.nl SDK config example they load these directly:

\Paynl\Config::setApiToken('111111');
\Paynl\Config::setServiceId('SL-1111-1111');
\Paynl\Config::setTokenCode('AT-1111-1111');
//optional: you can download it on https://curl.haxx.se/ca/cacert.pem
//\Paynl\Config::setCAInfoLocation('path/to/cacert.pem');
// Or you can skip verifyPeer
//\Paynl\Config::setVerifyPeer(false);

Env() vs Config()

At a Laracasts thread I read that we should only use env() in config files and otherwise use config(). And when I read the documentation again that is true it seems.  I also reread the warning on config caching:

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.

After deploys we do run php artisan config:cache of course and if then the env() tries to load stuff it fails and returns null. So we either need to replace env() by config() or live without config:cache. Former sounded like a plan. And so we did.

Solution

We now use:

Config::setApiToken(config('paynl.token'));
Config::setServiceId(config('paynl.id'));

which allow us to load these .env variables properly, even when we cache the .env file variables.

Jasper Frumau

Jasper has been working with web frameworks and applications such as Laravel, Magento and his favorite CMS WordPress including Roots Trellis and Sage for more than a decade. He helps customers with web design and online marketing. Services provided are web design, ecommerce, SEO, content marketing. When Jasper is not coding, marketing a website, reading about the web or dreaming the internet of things he plays with his son, travels or run a few blocks.