[英]Integrate CHARGEBEE library to LARAVEL
我开始使用 LARAVEL 最新版本 7.12,我在尝试集成CHARGEBEE库以向Chargebee api发出请求时遇到问题。
我读到我可以用作曲家安装包,我做到了:
composer require chargebee/chargebee-php:'>=2, <3'
这样做现在我已经在这里下载了 chargebee 库: /vendor/chargebee/chargebee-php/
现在我也在这里看到了这个堆栈溢出问题:
为了让用户正确使用这个 Library-Package,我需要创建一个ServiceProvider
,所以我做了:
php artisan make:provider ChargeBeeServiceProvider
那么我真的不知道如何在这里写REGISTER()
function,我还添加了这一行: App\Providers\ChargebeeServiceProvider::class,
到/config/app.php
到'providers'
现在我有一个 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();
}
但是在前端它给了我一个错误:
错误 Class 未找到“App\Http\Controllers\ChargeBee_Customer”
不太确定如何在 Laravel 上使用此自定义Chargebee库。
有人可以帮助以正确的方式整合吗?
谢谢!
这里真正的问题是ChargeBee_Environment
class 不存在。
它应该是Chargebee\ChargeBee\Environment
。 模型在它们自己的文件夹中,例如 Portal 是ChargeBee\ChargeBee\Models\PortalSession
WPTC-Troop 的回答提供了一些有关直接使用服务提供商与库的问题的重要信息,但最终您需要调用正确的类。
我创建门户 session 的工作代码如下。 客户 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;
两件事,您可以在 Laravel 中使用任何第三方库作为服务提供者 (DI),或者在需要的地方直接使用它(主要在控制器中)
不仅是第三方库,您还可以在 Service Provider 中简化 class 等的实例创建。 基本上它用于依赖注入,一般检查IoC你会更好地理解它。
您在您的情况下尝试/混合了这两种方法,并且没有完全使用任何单一方法。
您已经创建了一个名为ChargeBeeServiceProvider
的服务提供程序,但它不返回/绑定任何资源或没有在register
方法中完成 API 初始化。 这是您注册和使用 DI 的地方。 您也可以绑定属性。 这是它的官方文档。
您尝试通过初始化ChargeBee_Environment
并使用ChargeBee_Customer
来直接使用,但您没有提到在哪里查找此 class,这是您收到错误的原因(错误 Class 'App\Http\Controllers\ChargeBee_Customer' not found)。 我的意思是编译器将从同一个本地空间中查找ChargeBee_Environment
,但ChargeBee_Environment
可从全局 scope 获得。
相同代码的简单解决方案。 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" ));
试试上面的,让我知道。 从移动设备回答。 代码未经过全面测试(只需复制您的并添加\
即可)。
原谅简洁:)
我有一个使用 ChargeBee 和 Laravel 的解决方案。 不需要任何:注册提供程序,添加别名或添加路径以在 composer.json 中通过文件路径自动加载。 只需在您的服务或 Controller 代码中添加您需要的use
类。 喜欢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.