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

Gabotronix's avatar

Doubt regarding Stripe package

Hi everybody, I'm tinkering with Stripe and laravel and I was wondering one thing, so in all the examples I have seen Stripe needs to be initialized in each controller function, like this:

Stripe::setApiKey(config('services.stripe.secret'));

But is it neccesary to do in all methods? and if it's neccesary is there any way I can move that code and do it "globally", if that makes sense, it just feels kinda silly to me repeating it.

Please help a noob here and thanks in advance.

0 likes
1 reply
Reached's avatar
Reached
Best Answer
Level 11

Hi there,

You could extract it to its own class, and then pull in that class whenever you need it.

Then in the constructor of that class, you pull in the API keys. and set it up.

Hope it helps!

Please or to participate in this conversation.