Hello !
Im starting using LARAVEL last version 7.12 and Im newbie here with this framework and Im having issues trying to integrate CHARGEBEEE library to make request to Chargebee api, I was reading that I can install packages with composer, I did it: composer require chargebee/chargebee-php:'>=2, <3' doing that now I have downloaded chargebee lib here: /vendor/chargebee/chargebee-php/
now also I saw here: https://stackoverflow.com/questions/49346680/how-to-require-and-configure-a-dependency-in-laravel that to user correctly this Library-Package I need to create a ServiceProvider so I did it: php artisan make:provider ChargeBeeServiceProvider
then I really dont know how to write the REGISTER() function here, I added also this line: App\Providers\ChargebeeServiceProvider::class, to /config/app.php to 'providers'
Right now I have a controller here: /app/http/controllers/PortalController and Im trying to user this: ChargeBee_Environment::configure("sitename","apikeyvalue"); $all = ChargeBee_Customer::all(array( "firstName[is]" => "John", "lastName[is]" => "Doe", "email[is]" => "john@test.com" )); foreach($all as $entry){ $customer = $entry->customer(); $card = $entry->card(); }
BUT on the frontend its giving me an error: Error Class 'App\Http\Controllers\ChargeBee_Customer' not found
so not really sure how can I use this custom Chargebee library here on LARAVEL, can somebody please help to integrate in the correct way ?
Thanks ! charge!
You can use any PHP library in Laravel without a service provider. What the service provider does is simplify it. In case of APIs they mostly require an api_key and api_secret. The provider should take those from the environment/configuration and provide you with a ready to use set of services. From what I see the provider should call
ChargeBee_Environment::configure(config("chargebee.site"), config("chargebee.api_key")); // Create a file called chargebee.php in the config folder, fetching the values from environment.
After registering the provider you should be able to call the ChargeBee_Customer::all(...)
from anywhere in the controller.
The above in unrelated to your problem. What the error is saying is that you are missing a using statement at the top of your controller code. You need to add use ChargeBee_Customer;
. This won't work out of the box because PHP doesn't know where to look for this class. It's because the library doesn't comply with PS4 standard, which allows autoloading. You have to handle that yourself. To do so modify your composer.json file
"autoload": {
"psr-4": {
"App\\": "app/"
},
"files": [
"vendor/chargebee/chargebee-php/lib/ChargeBee/Models/Customer.php"
//Add other files you need here
],
"classmap": [
"database/seeds",
"database/factories"
]
},
After that run composer dump-autoload
and see if PHP manages to find the class.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community