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.
My controller
<?php
namespace AppHttpControllers;
use AppModelsUser;
use IlluminateDatabaseDatabaseManager;
use IlluminateHttpRequest;
use StripeStripeClient;
use IlluminateSupportStr;
use StripeExceptionApiErrorException;
use StripeStripe;
class SellerController extends Controller
{
protected StripeClient $stripeClient;
protected DatabaseManager $databaseManager;
public function __construct(StripeClient $stripeClient, DatabaseManager $databaseManager)
{
$this->stripeClient = $stripeClient;
$this->databaseManager = $databaseManager;
}
public function showProfile($id){
$seller = User::find($id);
if(is_null($seller)){
abort(404);
}
return view('seller', [
'seller' => $seller,
'balande' => null
]);
}
public function redirectToStripe($id){
$seller = User::find($id);
if(is_null($seller)){
abort(404);
}
if(!$seller->completed_stripe_onboarding){
$token = Str::random();
$this->databaseManager->table('stripe_state_tokens')->insert([
'created_at' => now(),
'updated_at' => now(),
'seller_id' => $seller->id,
'token' => $token
]);
// account id
if (is_null($seller->stripe_connect_id)) {
// Create account
$account = $this->stripeClient->accounts->create([
'country' => 'FR',
'type' => 'express',
'email' => $seller->email,
]);
$seller->update(['stripe_connect_id' => $account->id]);
$seller->fresh();
}
$onboardLink =$this->stripeClient->accountLinks->create([
'account' => $seller->stripe_connect_id,
'refresh_url' => route('redirect.stripe', ['id' =>$seller->id]),
'return_utl' => route('save.stripe', ['token' => $token]),
'type' => 'acccount_onboarding'
]);
return redirect($onboardLink->url);
}
$loginLink = $this->stripeClient->accounts->createloginLink($seller->stripe_connect_id);
return redirect($loginLink->url);
}
public function saveStripeAccount($token){
$stripeToken = $this->databaseManager->table('stripe_state_tokens')
->where('token', '=', $token)
->first();
if(is_null($stripeToken)){
abort(404);
}
$seller = User::find($stripeToken->seller_id);
$seller->update([
'completed_stripe_unboarding' => true
]);
return redirect()->route('seller.profile', ['id' => $seller->id]);
}
}
services.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Third Party Services
|--------------------------------------------------------------------------
|
| This file is for storing the credentials for third party services such
| as Mailgun, Postmark, AWS and more. This file provides the de facto
| location for this type of information, allowing packages to have
| a conventional file to locate the various service credentials.
|
*/
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
'scheme' => 'https',
],
'postmark' => [
'token' => env('POSTMARK_TOKEN'),
],
'ses' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
'stripe' => [
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
];
appserviceprovider
<?php
namespace AppProviders;
use IlluminateSupportServiceProvider;
use IlluminateSupportFacadesSchema;
use Carbon;
use StripeStripeCLient;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->app->singleton(StripeClient::class, function(){
return new StripeClient(config('stripe.secret'));
});
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
$this->app->singleton(StripeClient::class, function(){
return new StripeClient(config('stripe.secret'));
});
}
}
How to fix this error ? i suppose i have to inject to modify somethings about StripeClient no?
2
Answers
In your
AppServiceProvider
file, you are missing the config file to look for the stripe details. Instead it needs to look like this:You can test the return value of
config
by using thedd
function (before thereturn
statement), e.g.:EDIT:
You only need the singleton definition in the AppServiceProvider
register
function, not in theboot
function.Finally, you may need to get out of Stripe sandbox mode by using the
sk_test_xxxxxxx
api key before you can use thesk_live_xxxxxxx
api key.Make sure that the Stripe API key is set in your .env file
Also clear your config cache:
If you’re using a caching mechanism, such as OPcache or APC, make sure to clear the cache after updating the configuration files.