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

mstdmstd's avatar

With StripeClient I got error No API key provided

Hello,

in my Laravel 8 / with stripe/stripe-php": "^7.75" I try to connect and create account on stripe side and got error : No API key provided. Set your API key when constructing the StripeClient instance, or provide it on a per-request basis using the api_key key in the $opts argument.

with code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Session;
use Stripe;
use Stripe\StripeClient;
use App\Http\Controllers\Controller;
use Illuminate\Database\DatabaseManager;

use App\Models\Settings;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use App\Models\User;
use App\Models\StripeToken;

class SellerController extends Controller
{
	protected StripeClient $stripeClient;
	protected DatabaseManager $databaseManager;
	public function __construct(StripeClient $stripeClient, DatabaseManager $databaseManager)
	{

		\Log::info(varDump($stripeClient, ' -1 SellerController ::'));
		$this->stripeClient = $stripeClient;
		$this->databaseManager = $databaseManager;
	}

	public function showProfile($id)
	{
		\Log::info('-1 showProfile $id ::' . print_r($id, true));
		$seller = User::find($id);
		if (!$seller) {
			abort(404);
		}
		return view('market.seller', [
			'seller' => $seller,
			'balance' => null
		]); // /_wwwroot/lar/tAdsBack/resources/views/admin/stripe/stripe.blade.php

	} // public function showProfile($id)

	public function redirectToStripe($id)
	{
		\Log::info('-1 redirectToStripe $id ::' . print_r($id, true));
		$seller = User::find($id);
		if (!$seller) {
			abort(404);
		}
		$appEnv = strtolower(config('app.env'));

		if ($appEnv == 'local' or $appEnv == 'dev') {
			\Log::info('-1 config(app.STRIPE_TEST_KEY)::' . print_r(config('app.STRIPE_TEST_KEY'), true));
			\Stripe\Stripe::setApiKey(config('app.STRIPE_TEST_KEY')); // LOOKS LIKE THAT IS NOT APPLIED
			$this->stripeClient->apiKey = config('app.STRIPE_TEST_KEY'); // THIS DOES NOT HELP
			$this->stripeClient->api_key = config('app.STRIPE_TEST_KEY');  // THIS DOES NOT HELP
		}
		if ($appEnv == 'production') {
			\Stripe\Stripe::setApiKey(config('app.STRIPE_LIVE_KEY'));
			$this->stripeClient->opts['api_key'] = config('app.STRIPE_LIVE_KEY');
		}


		if (!$seller->completed_stripe_onboarding) { // Complete onboarding process
			$str = \Str::random();
			$stripeToken = new StripeToken();
			$stripeToken->token =  $str;
			$stripeToken->seller_id =  $id;
			$stripeToken->save();


			if (empty($seller->stripe_connect_id)) {  // if has no stripe account
				// // Create a new Stripe Connect Account object.
				\Log::info('-1 $$this->stripeClient ::' . print_r($this->stripeClient, true));

Checking log I see :

[2021-03-22 05:40:30] local.INFO: -1 $$this->stripeClient ::Stripe\StripeClient Object
(
    [coreServiceFactory:Stripe\StripeClient:private] => 
    [config:Stripe\BaseStripeClient:private] => Array
        (
            [api_key] => 
            [client_id] => 
            [stripe_account] => 
            [stripe_version] => 
            [api_base] => https://api.stripe.com
            [connect_base] => https://connect.stripe.com
            [files_base] => https://files.stripe.com
        )

    [defaultOpts:Stripe\BaseStripeClient:private] => Stripe\Util\RequestOptions Object
        (
            [apiKey] => 
            [headers] => Array
                (
                    [Stripe-Account] => 
                    [Stripe-Version] => 
                )

            [apiBase] => 
        )

    [api_key] => sk_test_NNNNNN

How to fix this error ?

I suppose I need to set api_key in some other way, not

			$this->stripeClient->apiKey = config('app.STRIPE_TEST_KEY'); // THIS DOES NOT HELP
			$this->stripeClient->api_key = config('app.STRIPE_TEST_KEY');  // THIS DOES NOT HELP

but how?

Thanks!

0 likes
5 replies
mstdmstd's avatar

I have line with logging app.STRIPE_TEST_KEY

if ($appEnv == 'local' or $appEnv == 'dev') {
	\Log::info('-1 config(app.STRIPE_TEST_KEY)::' . print_r(config('app.STRIPE_TEST_KEY'), true));

and checking log file I see valid sk_test_NNN value in log line, but not inside of coreServiceFactory:Stripe\StripeClient object(I show it in my topic)

martinbean's avatar
Level 80

@mstdmstd If you want to inject the StripeClient into classes like that, then you’ll need to register it with the service container (with the key set):

class StripeServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(StripeClient::class, function () {
            return new StripeClient($this->app['config']['services.stripe.secret']);
        });
    }
}

Obviously don‘t forget to set a key in your config/services.php file with your Stripe API key:

'stripe' => [
    'key' => env('STRIPE_KEY'),
    'secret' => env('STRIPE_SECRET'),
],

Now when you type-hint StripeClient in a class, a well-formed instance with the API key set will be injected.

2 likes
mstdmstd's avatar

Thanks! I made next step. Also could you please give a hint or link to a flow of similar decision as In my app registered users (customers )get help from other registered users (consultants) and they both pay to company(the owner of stripe account).

Also Company pays to consultants in cases they consulted customers. I try to find out valid flow, but it is still vague,,,

martinbean's avatar

@mstdmstd I think that’s a bit outside the scope of your original question (pass API key to Stripe client).

elminson's avatar

What works for me create the file config/cashier.php and set this inside


use Laravel\Cashier\Invoices\DompdfInvoiceRenderer;

return [

	/*
	|--------------------------------------------------------------------------
	| Stripe Keys
	|--------------------------------------------------------------------------
	|
	| The Stripe publishable key and secret key give you access to Stripe's
	| API. The "publishable" key is typically used when interacting with
	| Stripe.js while the "secret" key accesses private API endpoints.
	|
	*/

	'key' => env('STRIPE_MODE') == 'live' ? env('STRIPE_LIVE_KEY') : env('STRIPE_TEST_KEY'),

	'secret' => env('STRIPE_MODE') == 'live' ? env('STRIPE_LIVE_SECRET') :  env('STRIPE_TEST_SECRET'),

	/*
	|--------------------------------------------------------------------------
	| Cashier Path
	|--------------------------------------------------------------------------
	|
	| This is the base URI path where Cashier's views, such as the payment
	| verification screen, will be available from. You're free to tweak
	| this path according to your preferences and application design.
	|
	*/

	'path' => env('CASHIER_PATH', 'stripe'),

	/*
	|--------------------------------------------------------------------------
	| Stripe Webhooks
	|--------------------------------------------------------------------------
	|
	| Your Stripe webhook secret is used to prevent unauthorized requests to
	| your Stripe webhook handling controllers. The tolerance setting will
	| check the drift between the current time and the signed request's.
	|
	*/

	'webhook' => [
		'secret' => env('STRIPE_WEBHOOK_SECRET'),
		'tolerance' => env('STRIPE_WEBHOOK_TOLERANCE', 300),
	],

	/*
	|--------------------------------------------------------------------------
	| Currency
	|--------------------------------------------------------------------------
	|
	| This is the default currency that will be used when generating charges
	| from your application. Of course, you are welcome to use any of the
	| various world currencies that are currently supported via Stripe.
	|
	*/

	'currency' => env('CASHIER_CURRENCY', 'usd'),

	/*
	|--------------------------------------------------------------------------
	| Currency Locale
	|--------------------------------------------------------------------------
	|
	| This is the default locale in which your money values are formatted in
	| for display. To utilize other locales besides the default en locale
	| verify you have the "intl" PHP extension installed on the system.
	|
	*/

	'currency_locale' => env('CASHIER_CURRENCY_LOCALE', 'en'),

	/*
	|--------------------------------------------------------------------------
	| Payment Confirmation Notification
	|--------------------------------------------------------------------------
	|
	| If this setting is enabled, Cashier will automatically notify customers
	| whose payments require additional verification. You should listen to
	| Stripe's webhooks in order for this feature to function correctly.
	|
	*/

	'payment_notification' => env('CASHIER_PAYMENT_NOTIFICATION'),

	/*
	|--------------------------------------------------------------------------
	| Invoice Settings
	|--------------------------------------------------------------------------
	|
	| The following options determine how Cashier invoices are converted from
	| HTML into PDFs. You're free to change the options based on the needs
	| of your application or your preferences regarding invoice styling.
	|
	*/

	'invoices' => [
		'renderer' => env('CASHIER_INVOICE_RENDERER', DompdfInvoiceRenderer::class),

		'options' => [
			// Supported: 'letter', 'legal', 'A4'
			'paper' => env('CASHIER_PAPER', 'letter'),
		],
	],

	/*
	|--------------------------------------------------------------------------
	| Stripe Logger
	|--------------------------------------------------------------------------
	|
	| This setting defines which logging channel will be used by the Stripe
	| library to write log messages. You are free to specify any of your
	| logging channels listed inside the "logging" configuration file.
	|
	*/

	'logger' => env('CASHIER_LOGGER'),

];

Please or to participate in this conversation.