简体   繁体   English

集成CHARGEBEE库到LARAVEL

[英]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 .我开始使用 LARAVEL 最新版本 7.12,我在尝试集成CHARGEBEE库以向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/这样做现在我已经在这里下载了 chargebee 库/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:为了让用户正确使用这个 Library-Package,我需要创建一个ServiceProvider ,所以我做了:

 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'那么我真的不知道如何在这里写REGISTER() function,我还添加了这一行: App\Providers\ChargebeeServiceProvider::class,/config/app.php'providers'

ChargebeeServiceProvider Chargebee服务提供商

Right now I have a controller: /app/http/controllers/PortalController and I'm trying to use this:现在我有一个 controller: /app/http/controllers/PortalController我正在尝试使用它:

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错误 Class 未找到“App\Http\Controllers\ChargeBee_Customer”

Not really sure how i can use this custom Chargebee library here on Laravel.不太确定如何在 Laravel 上使用此自定义Chargebee库。

can somebody please help to integrate in the correct way?有人可以帮助以正确的方式整合吗?

Thanks!谢谢!

The real problem here is that ChargeBee_Environment class doesn't exists.这里真正的问题是ChargeBee_Environment class 不存在。

It should be Chargebee\ChargeBee\Environment .它应该是Chargebee\ChargeBee\Environment And the Models are in their own folder, for example the Portal is ChargeBee\ChargeBee\Models\PortalSession模型在它们自己的文件夹中,例如 Portal 是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. WPTC-Troop 的回答提供了一些有关直接使用服务提供商与库的问题的重要信息,但最终您需要调用正确的类。

My working code for creating a portal session is below.我创建门户 session 的工作代码如下。 It would be similar for a Customer object:客户 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)两件事,您可以在 Laravel 中使用任何第三方库作为服务提供者 (DI),或者在需要的地方直接使用它(主要在控制器中)

Not just third party library you can ease the instance creation of class etc in Service Provider.不仅是第三方库,您还可以在 Service Provider 中简化 class 等的实例创建。 Basically it's for dependency injection, Check IoC in general you'll understand it better.基本上它用于依赖注入,一般检查IoC你会更好地理解它。

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.您已经创建了一个名为ChargeBeeServiceProvider的服务提供程序,但它不返回/绑定任何资源或没有在register方法中完成 API 初始化。 This is where you register and make use of DI.这是您注册和使用 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).您尝试通过初始化ChargeBee_Environment并使用ChargeBee_Customer来直接使用,但您没有提到在哪里查找此 class,这是您收到错误的原因(错误 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.我的意思是编译器将从同一个本地空间中查找ChargeBee_Environment ,但ChargeBee_Environment可从全局 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. 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 (在此处查看 -> composer.json )因此它将在两种方法中自动加载。

\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.我有一个使用 ChargeBee 和 Laravel 的解决方案。 Don't need any: register provider, add to aliases or add path to autoload in composer.json by file path.不需要任何:注册提供程序,添加别名或添加路径以在 composer.json 中通过文件路径自动加载。 Just add in use classes that you need in code your Service or your Controller.只需在您的服务或 Controller 代码中添加您需要的use类。 Like use ChargeBee_Environment and other.喜欢use ChargeBee_Environment等。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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