2
votes

I created a custom service provider for paypal. When I try to access config from inside the provider, it returns null.

Below is the code I wrote.

public function register()
{
    $this->app->singleton(ApiContext::class, function($app){
        $paypalConf = $app['config']->get('paypal');
        $apiContext = new ApiContext(new OAuthTokenCredential(
            $paypalConf['client_id'],$paypalConf['client_secret']
        )); 
        $apiContext->setConfig($paypalConf['settings']);
        return $apiContext;
    });
}

But it throws following error

if (empty($credObj)) {
        throw new PayPalInvalidCredentialException("Credential not found for " .  ($userId ? $userId : " default user") .
        ". Please make sure your configuration/APIContext has credential information");
    }

"Credential not found for default user. Please make sure your configuration/APIContext has credential information

Which is certainly because my config file is returning null.

Here is my paypal.php

return [
'client_id' => env('PAYPAL_CLIENT_ID', ''),
'client_secret' => env('PAYPAL_SECRET', ''),
'settings' => [
    'mode' => env('PAYPAL_MODE', ''),
    'http.ConnectionTimeOut' => 30,
    'log.LogEnabled' => true,
    'log.FileName' => storage_path().'/logs/paypal.log',
    'log.LogLevel' => 'ERROR'
]];

and the PAYPAL credentials exists in .env file.

I have tried clearing the config and caching it again, but it didn't work.

Update

Seems like the register method isn't working in the service provider. But I have registered the provider in config/app.php

2
Have you tried dd($paypalConf, $app['config']->get('app.name')), in the service provider after first line? I smell a typo kind of error.Kyslik
@Kyslik dd isn't working there.Saud Qureshi
Have you registered the service provider in config/app.php?Kyslik
@Kyslik yes the provider is registered in the providers array.Saud Qureshi
Then dd out of the closure, you need to know if the register method is executing.Kyslik

2 Answers

3
votes

Without more information ...

clear the config cache: php artisan config:clear

If you have a cached config and you add anything to the config files or env, including another service provider to config/app.php, it won't get picked up because its using a cached version of your config.

You can verify this kind of stuff just by going into tinker and requesting these config values after making changes to any config files or env.

0
votes

In the config folder paypal.php not paypal.config :

<?php

    return [
        'client_id' => env('PAYPAL_CLIENT_ID', ''),
        'client_secret' => env('PAYPAL_SECRET', ''),
        'settings' => [
            'mode' => env('PAYPAL_MODE', ''),
            'http.ConnectionTimeOut' => 30,
            'log.LogEnabled' => true,
            'log.FileName' => storage_path().'/logs/paypal.log',
            'log.LogLevel' => 'ERROR'
    ]];

Then you can get the values :

Config::get('paypal.client_id');
Config::get('paypal.settings.mode');