nikolag / core
Nikolag Core 是一个为 Laravel 提供支付集成功能的包,这些功能不属于 Cashier
Requires
- php: >=8.2
- illuminate/config: 10.x
- illuminate/console: 10.x
- illuminate/container: 10.x
- illuminate/contracts: 10.x
- illuminate/database: 10.x
- illuminate/filesystem: 10.x
- illuminate/support: 10.x
README
为构建与 nikolag
包和 Laravel >=5.5
相关的额外支付网关集成而设计的核心包
安装指南
composer require nikolag/core
使用方法
此包包含了所有必要的代码,以帮助您开始构建与 nikolag
包相关的支付网关的额外集成。首先,有几个重要的层和结构,您必须遵循
1. 配置文件
配置文件在这里非常重要,因为它将包含您的库所需的全部预设选项。文件必须命名为 nikolag.php,并且必须位于 src/config 文件夹中。
return [ /* |-------------------------------------------------------------------------- | Nikolag Configuration |-------------------------------------------------------------------------- | | This represents the default connection name that will be used when | u have multiple available connections. For all available connections | take a look a the code documentation of 'connections' just below. | */ 'default' => '{$default}', /* |-------------------------------------------------------------------------- | Nikolag Connections |-------------------------------------------------------------------------- | | Here you will find all available connections you have in your project. | For all available connections you can take a look at the link below. | | https://github.com/NikolaGavric94/nikolag-core/blob/master/DRIVERS.md | */ 'connections' => [ /* |-------------------------------------------------------------------------- | {$default} Configuration |-------------------------------------------------------------------------- | | The {$default} configuration determines the default application_id | and {$default} token when doing any of the calls to {$default}. These values will | be used when there is no merchant provided as a seller. You have to change | these values. | */ '{$default}' => [ 'namespace' => '{$namespace}', 'application_id' => env('{$default}_APPLICATION_ID'), 'access_token' => env('{$default}_TOKEN'), 'sandbox' => env('{$default}_SANDBOX', false), /* |-------------------------------------------------------------------------- | {$default} Merchant Configuration |-------------------------------------------------------------------------- | | The {$default} merchant configuration determines the default namespace for | merchant model and it's identifier which will be used in various | relationships when retrieving models. You are encouraged to change these | values to better reflect your application. | */ 'user' => [ 'namespace' => env('{$default}_USER_NAMESPACE', '\App\User'), 'identifier' => env('{$default}_USER_IDENTIFIER', 'id') ], /* |-------------------------------------------------------------------------- | {$default} Order Configuration |-------------------------------------------------------------------------- | | The {$default} order configuration determines the default namespace for | order model and it's identifier which CAN be used when charging a customer. | You can relate that model to a certain transaction. You are encouraged to | change these values to better reflect your application. | */ 'order' => [ 'namespace' => env('{$default}_ORDER_NAMESPACE', '\App\Order'), 'identifier' => env('{$default}_ORDER_IDENTIFIER', 'id'), 'service_identifier' => env('SQUARE_PAYMENT_IDENTIFIER', 'payment_service_id') ] ], ] ];
这只是启动片段,您可以根据自己的库进行修改。这里有几个重要的 变量
需要更改,以及您必须遵守的规则。将 {$default}
替换为您的包名称(paypal、payeer、payoneer 等),并且用您的服务完全限定名称(命名空间)替换 {$namespace}
。您可以查看 nikolag.php 中的配置文件示例。 SquareConfig.php 是配置类应如何查看的示例。
2. 主服务
您的包必须至少有 1 个主服务,该服务负责所有与您的包之间的 rest 调用通信。它还必须扩展 Nikolag\Core\Abstracts\CorePaymentService,并且必须实现 Nikolag\Core\Contracts\PaymentServiceContract,这将在下一步中解释。这种类型的类示例可以在 SquareService.php 中找到。
3. 依赖注入
您的库必须至少有 1 个协议,其名称应为 {$serviceName}ServiceContract.php
,其中 {$serviceName}
是您尝试与 nikolag 包和 Laravel 集成的服务的名称。它还必须扩展 Nikolag\Core\Contracts\PaymentServiceContract。**任何与您的服务文件无关的协议都不应扩展上述协议**。此类名称的示例包括 PaypalServiceContract.php
、PayeerServiceContract.php
、PayoneerServiceContract.php
。协议的示例可以在 SquareContract.php 中找到。
4. 迁移和工厂
所有迁移和工厂都必须位于 src/database
文件夹下,并且每个迁移和工厂都将分别有自己的子文件夹 src/database/factories
和 src/database/migrations
。此核心包已经包含了一些迁移,因此您不必自己编写它们,但如果需要,您可以扩展它们。
5. 外观
所有外观都在 src/facades
下,并且您的包必须至少有 1 个外观,该外观必须是主服务类的别名。
//Facades $this->app->alias(SquareService::class, 'square');
6. 模型
所有模型必须位于 src/models
目录下。有2个核心模型:客户 和 交易。
您必须扩展这2个核心模型,并从它们创建自己的模型,包括所有可用的关系,并添加
/** * The model's attributes. * * @var array */ protected $attributes = [ //where `{$serviceName}` is the name of service you are trying to integrate with nikolag packages and Laravel //example: 'paypal', 'payoneer', 'payeer' 'payment_service_type' => {$serviceName} ];
以上要求仅适用于2个基础模型,您可能创建的任何其他模型都不需要扩展,可以正常创建。示例请见 Customer.php。
7. 提供者
您可以注册多个提供者,并且它们应该分别根据在库中的作用命名。您可以在 src/providers
目录下找到它们。以下是一些示例:ExceptionServiceProvider, MyServiceProvider, ExternalLibrariesProvider 等。
8. 特性
所有特性必须位于 src/traits
目录下。
9. 测试
您的所有测试都必须位于 tests
目录下,并且分为两种类型:集成
和 单元
,每种类型都有自己的子目录。
src/ tests/ integration/ unit/
10. 工具类
您的所有工具类必须位于 src/utils
目录下。
所有可用的核心方法
CoreService
/** * Returns instance of the specified service * * @param string $driver * @return Nikolag\Core\Contracts\PaymentServiceContract */ public function use(string $driver) {} /** * Returns instance of the default service * * @return Nikolag\Core\Contracts\PaymentServiceContract */ public function default() {} /** * Returns all available drivers * which u have installed. * * @return array */ public function availableDrivers() {}
CoreService 示例
获取对任何已安装支付API的底层驱动程序的访问权限
$squareAPI = Nikolag\Core\Facades\CoreService::use('square'); $squareAPI->setCustomer($customer)->charge($options); //or $myServiceAPI = Nikolag\Core\Facades\CoreService::use('my-service'); $myServiceAPI->setCustomer($customer)->charge($options);
您还可以使用您的默认驱动程序向客户收费
$api = Nikolag\Core\Facades\CoreService::default(); $api->setCustomer($customer)->charge($options);
您还可以列出所有可用的驱动程序
$drivers = Nikolag\Core\Facades\CoreService::availableDrivers(); echo json_decode(json_encode($drivers)); //or Laravel specific helper method dd($drivers); //or plain php var_dump($drivers); die();
更多示例
有关如何构建此核心包的完整示例,请参阅 nikolag-core-starter
贡献
欢迎任何人向此仓库贡献,只需打开一个问题并标记请求,无论它是问题、错误还是功能。对于其他询问,请发送电子邮件至 nikola.gavric94@gmail.com
贡献者
许可
MIT 许可
版权所有 (c) Nikola Gavrić nikola.gavric94@gmail.com
在此授予任何人免费获得本软件及其相关文档文件(“软件”)副本的权利,不受任何限制地处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向提供软件的人员做上述事情,前提是符合以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论是否因合同行为、侵权行为或其他行为而产生,无论是否与软件或其使用或其他交易有关。