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

e4stwood's avatar

Include External Library

Hey,

Basically, I want to include an external library in Laravel. In this case, I want to include the Stripe library (Not with Cashier because I really just need one time payments and Cashier doesnt really support this).

How would I go about doing this? I've installed the library using composer.

Cheers

0 likes
6 replies
DarkRoast's avatar
Level 8

You can set your stripe key in the register method of a service provider:

public function register()
{
  Stripe::setApiKey('your-key'); // best to store key in .env file
}

Controller might look something like:

use Stripe\Charge;

class BillingController extends Controller 
{
  public function charge()
  {
    $myCard = array('number' => '4242424242424242', 'exp_month' => 8, 'exp_year' => 2018);
    $charge = Charge::create(array('card' => $myCard, 'amount' => 2000, 'currency' => 'usd'));
    echo $charge;
  }
}
1 like
e4stwood's avatar

So by simply instilling in composer, it'll be available in all files providing I use use Stripe\Charge; ?

e4stwood's avatar

Thank you. My main question was whether installing it via composer was enough for it to be accessible.

Thank you for all your help.

e4stwood's avatar

Coming back to what we've done. I've created a service called 'StripeServiceProvider' using the php artisan command. I've then done this as the code http://pastebin.com/wejF9s5h

When I run very similar code to what you mentioned in my controller, I get that the API key isnt set. What would cause this? (aside from it not being set)

DarkRoast's avatar

Make sure you register the service provider in the providers array in config/app.php:

'providers' => [
  // ...
  App\Providers\StripeServiceProvider::class,
];

Please or to participate in this conversation.