最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

laravel - Stripe payment gateway error $config must be a string or an array - Stack Overflow

programmeradmin1浏览0评论

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 Please include the full error in your post as well as the ways you tried to fix this. – ewokx Commented Nov 19, 2024 at 5:43
  • 1 If you hard code the secret key in StripeClient, e.g. $stripe = new \Stripe\StripeClient('sk_xxx'), does it work? If it does, this means that your env('STRIPE_SECRET') doesn't retrieve the secret key correctly from your .env file. – yuting Commented Nov 19, 2024 at 6:59
  • 2 Please do not upload images of code/data/errors. – Peter Pointer Commented Nov 19, 2024 at 9:29
  • Why create the services.php config file if you're not using it in your code? You can change env('STRIPE_SECRET') to config('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:53
  • Yeah, never use env() outside of files in config/*.php in Laravel. I don't recall the instances, but there are multiple times where env('SOME_VALUE') will return null instead of the expected value from .env. Using env('SOME_VALUE') in a config/example.php file, then calling config('example.some_value') will always return the configured value (which could still be null, 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
Add a comment  | 

1 Answer 1

Reset to default -3

Change this line new \Stripe\StripeClient(env('STRIPE_SECRET')); to Stripe::setApiKey(env('STRIPE_SECRET'));

发布评论

评论列表(0)

  1. 暂无评论