1

I wish to add chargebee php api to my laravel project

For that I first ran

composer require chargebee/chargebee-php

This added the chargebee API to vendor/chargbee

Next thing I would need to do is to include and configure the api. This can be done by:

require_once base_path('vendor/chargebee/ChargeBee.php');
ChargeBee_Environment::configure("your_site", "{your_site_api_key}");

My question is where should i do this? I would like the including of ChargBee.php and configuring it to be in a generic place so i can later use it in any Controller and it would already be set up. What is the default way to do this in Laravel?

Kyslik
  • 8,217
  • 5
  • 54
  • 87
user1985273
  • 1,817
  • 15
  • 50
  • 85
  • 1
    Write yourself a service provider https://laravel.com/docs/5.6/providers#writing-service-providers thats all you need. – Kyslik Mar 18 '18 at 10:37
  • Can not help it but did you use google at all? https://github.com/TijmenWierenga/LaravelChargebee featured right on the https://www.chargebee.com/open-source-laravel/ – Kyslik Mar 18 '18 at 12:41
  • @Kyslik - Thanks, service providers were what i needed. The second answer isn't quite appropriate tho. You are pointing to a laravel specific package that offers some additional functionality built on top of the original chargebee api. My question was what is the preferred way to setup chargbee api (or any other) composer dependency in laravel. – user1985273 Mar 18 '18 at 12:54

1 Answers1

1

You should set up a new service provider, like described in the official documentation.

Hint 1: Run php artisan make:provider ChargeBeeServiceProvider from command line to generate the new service provider.

Hint 2: take a closer look at the register() method. You should register the ChargeBee "service" there, most probably like a singleton. That's the place, where the API configuration should be put.

Since autoload is being used in the chargebee library (see its composer.json), there is no need for the require_once base_path('vendor/chargebee/ChargeBee.php'); line. Just simply use the classes.

Zoli Szabó
  • 4,366
  • 1
  • 13
  • 19