I a having error trying to access api key from .env file. I have tried several pages on internet to fix issue but it is not giving me any solution. I have tried to add services into services.php but nothing works out. I have also tried php artisan config:cache and also tried added into config/services.php
'stripe' => [
'stripe_key' => env('STRIPE_KEY'),
'secret_key' => env('STRIPE_SECRET'),
]
],
StripePaymentController.php
<?php
namespace App\Http\Controllers;
use Stripe;
use Illuminate\View\View;
use Illuminate\Http\Request;
class StripePaymentController extends Controller
{
//
public function stripe():View {
return view('stripe');
}
public function stripeCheckout(Request $request){
$stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
$redirectUrl = route('stripe.checkout.success').'?session_id={CHECKOUT_SESSION_ID}';
$response = $stripe->checkout->sessions->create([
'success_url' => $redirectUrl,
'customer_email' => '[email protected]',
'payment_method_type' => ['link','card'],
'lne_items' => [
[
'price_data' => [
'product_data' => [
'name' => $request->product,
],
'unit_price' => 100 * $request->price,
'currency' => 'NZD',
],
'quantity' => 1,
],
],
'mode' => 'payment',
'allow_promotion_codes' => true,
]);
return redirect($response['url']);
}
public function stripeCheckoutSuccess(Request $request){
$stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
$response = $stripe->checkout->sessions->retrieve($request->session_id);
return redirect()->route('stripe.index')->with('success','Payment Successful.');
}
}
I a having error trying to access api key from .env file. I have tried several pages on internet to fix issue but it is not giving me any solution. I have tried to add services into services.php but nothing works out. I have also tried php artisan config:cache and also tried added into config/services.php
'stripe' => [
'stripe_key' => env('STRIPE_KEY'),
'secret_key' => env('STRIPE_SECRET'),
]
],
StripePaymentController.php
<?php
namespace App\Http\Controllers;
use Stripe;
use Illuminate\View\View;
use Illuminate\Http\Request;
class StripePaymentController extends Controller
{
//
public function stripe():View {
return view('stripe');
}
public function stripeCheckout(Request $request){
$stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
$redirectUrl = route('stripe.checkout.success').'?session_id={CHECKOUT_SESSION_ID}';
$response = $stripe->checkout->sessions->create([
'success_url' => $redirectUrl,
'customer_email' => '[email protected]',
'payment_method_type' => ['link','card'],
'lne_items' => [
[
'price_data' => [
'product_data' => [
'name' => $request->product,
],
'unit_price' => 100 * $request->price,
'currency' => 'NZD',
],
'quantity' => 1,
],
],
'mode' => 'payment',
'allow_promotion_codes' => true,
]);
return redirect($response['url']);
}
public function stripeCheckoutSuccess(Request $request){
$stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
$response = $stripe->checkout->sessions->retrieve($request->session_id);
return redirect()->route('stripe.index')->with('success','Payment Successful.');
}
}
Share
Improve this question
edited Nov 19, 2024 at 5:55
Nihar Shah
asked Nov 19, 2024 at 5:39
Nihar ShahNihar Shah
355 bronze badges
5
|
1 Answer
Reset to default -3Change this line new \Stripe\StripeClient(env('STRIPE_SECRET'));
to Stripe::setApiKey(env('STRIPE_SECRET'));
$stripe = new \Stripe\StripeClient('sk_xxx')
, does it work? If it does, this means that yourenv('STRIPE_SECRET')
doesn't retrieve the secret key correctly from your .env file. – yuting Commented Nov 19, 2024 at 6:59services.php
config file if you're not using it in your code? You can changeenv('STRIPE_SECRET')
toconfig('services.stripe.stripe_secret')
. But yeah, it might be that the secret is not fetched properly causing errors. Try what @yuting said. – geertjanknapen Commented Nov 19, 2024 at 13:53env()
outside of files inconfig/*.php
in Laravel. I don't recall the instances, but there are multiple times whereenv('SOME_VALUE')
will returnnull
instead of the expected value from.env
. Usingenv('SOME_VALUE')
in aconfig/example.php
file, then callingconfig('example.some_value')
will always return the configured value (which could still benull
, but only if explicitly configured that way, or it's missing from.env
or other environment sources such as CLI argument, etc.) – Tim Lewis Commented Nov 19, 2024 at 16:51