简体   繁体   中英

Integrate CHARGEBEE library to LARAVEL

Im starting using LARAVEL last version 7.12 and I'm having an issue trying to integrate CHARGEBEE 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 This stack overflow question here:

That for the user to correctly use this Library-Package I need to create a ServiceProvider so I did:

 php artisan make:provider ChargeBeeServiceProvider

then I really don't know how to write the REGISTER() function here, I added also this line: App\Providers\ChargebeeServiceProvider::class, to /config/app.php to 'providers'

ChargebeeServiceProvider

Right now I have a controller: /app/http/controllers/PortalController and I'm trying to use 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(); 
}

PortalController

BUT on the frontend it's giving me an error:

Error Class 'App\Http\Controllers\ChargeBee_Customer' not found

Not really sure how i can use this custom Chargebee library here on Laravel.

can somebody please help to integrate in the correct way?

Thanks!

The real problem here is that ChargeBee_Environment class doesn't exists.

It should be Chargebee\ChargeBee\Environment . And the Models are in their own folder, for example the Portal is ChargeBee\ChargeBee\Models\PortalSession

WPTC-Troop's answer provides some great information on the issue of using a service provider vs library directly, but in the end you need to call the correct classes.

My working code for creating a portal session is below. It would be similar for a Customer object:

    \ChargeBee\ChargeBee\Environment::configure("test-site","test_api_key");
    $result = \ChargeBee\ChargeBee\Models\PortalSession::create(["customer" => ["id" => "AzZlwTSSVw9871E9g"]]);
    $portalSession = $result->portalSession();
    $output = $portalSession->getValues();
    
    return $output;

Two things, you can use any third party library as Service Provider(DI) in Laravel or use it directly where ever you needed(mostly in controller)

Not just third party library you can ease the instance creation of class etc in Service Provider. Basically it's for dependency injection, Check IoC in general you'll understand it better.

You tried/mixed both in your case and didn't completely use any single approach.

  1. You've created a Service Provider called ChargeBeeServiceProvider but it doesn't return/bind any resource or no API initialization done in register method. This is where you register and make use of DI. You can bind in properties as well. Here is the official doc to it.

  2. You tried to use directly by initializing ChargeBee_Environment and use ChargeBee_Customer but you didn't mention where to look for this class that is the reason you get the error(Error Class 'App\Http\Controllers\ChargeBee_Customer' not found). I mean compiler will look for ChargeBee_Environment from the same local space but ChargeBee_Environment is available from global scope.

Simple solution to your same code. Add \ to the class to look from global scope or make use of use statement in the top of your file similar to import in java but still we need to require/include the file in php but don't worry, chargebee make use of autoloader(Check here -> composer.json ) so it will be loaded automatically in both approach.

\ChargeBee_Environment::configure("sitename","apikeyvalue"); 

$all = \ChargeBee_Customer::all(array( "firstName[is]" => "John", 
"lastName[is]" => "Doe", "email[is]" => "john@test.com" ));

Try the above and let me know. Answered from mobile device. Code is not tested fully(Just copied your and added \ to it).

Excuse brevity:)

I have a solution to use ChargeBee and Laravel. Don't need any: register provider, add to aliases or add path to autoload in composer.json by file path. Just add in use classes that you need in code your Service or your Controller. Like use ChargeBee_Environment and other.

use ChargeBee_Environment;
use ChargeBee_Customer;

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM