mprince/braintree

Laravel 的 Braintree 桥接器

dev-main 2021-12-27 04:04 UTC

This package is auto-updated.

Last update: 2024-09-27 10:01:06 UTC


README

Build Status PHP from Packagist Latest Version License

为 Laravel 提供的 Braintree 桥接器。

安装

使用 Composer,在项目根目录中要求此包。

$ composer require artisanry/braintree

配置

Laravel Braintree 需要连接配置。要开始,您需要发布所有供应商资源

$ php artisan vendor:publish --provider="Artisanry\Braintree\BraintreeServiceProvider"

这将在您的应用程序中创建一个 config/braintree.php 文件,您可以根据需要修改它来设置配置。同时,请确保检查此包中原始配置文件在版本之间的更改。

默认连接名称

此选项 default 是您可能指定要使用以下哪个连接作为您所有工作的默认连接的地方。当然,您可以使用许多连接同时使用管理类。此设置的默认值为 main

Braintree 连接

此选项 connections 是您为应用程序设置的每个连接的位置。示例配置已包括在内,但您可以添加您想要的任意数量的连接。

使用方法

BraintreeManager

这是最有兴趣的类。它绑定到 ioc 容器作为 braintree,可以使用 Facades\Braintree 外观访问。此类通过扩展 AbstractManager 实现了 ManagerInterface。该接口和抽象类都是 Graham Campbell 的 Laravel Manager 包的一部分,因此您可能想查看该存储库中的文档以了解如何使用管理类。请注意,返回的连接类始终是 Braintree\Braintree 的实例。

Facades\Braintree

此外观将动态地将静态方法调用传递到 ioc 容器中的 braintree 对象,默认情况下是 BraintreeManager 类。

BraintreeServiceProvider

此类不包含任何有价值的公共方法。此类应添加到 config/app.php 中的 providers 数组。此类将设置 ioc 绑定。

示例

在这里,您可以看到一个如何简单使用此包的示例。默认情况下,默认适配器是 main。在您在配置文件中输入认证详细信息后,它将正常工作

// You can alias this in config/app.php.
use Artisanry\Braintree\Facades\Braintree;

Braintree::getTransaction()->sale([
    'amount' => '10.00',
    'paymentMethodNonce' => $nonceFromTheClient,
    'options' => ['submitForSettlement' => true]
]);
// We're done here - how easy was that, it just works!

Braintree 管理器将表现得像一个 Braintree\Braintree。如果您想调用特定的连接,您可以使用连接方法做到这一点

use Artisanry\Braintree\Facades\Braintree;

// Writing this…
Braintree::connection('main')->getCharge()->([
    'amount' => '10.00',
    'paymentMethodNonce' => $nonceFromTheClient,
    'options' => ['submitForSettlement' => true]
]);

// …is identical to writing this
Braintree::getCharge()->([
    'amount' => '10.00',
    'paymentMethodNonce' => $nonceFromTheClient,
    'options' => ['submitForSettlement' => true]
]);

// and is also identical to writing this.
Braintree::connection()->getCharge()->([
    'amount' => '10.00',
    'paymentMethodNonce' => $nonceFromTheClient,
    'options' => ['submitForSettlement' => true]
]);

// This is because the main connection is configured to be the default.
Braintree::getDefaultConnection(); // This will return main.

// We can change the default connection.
Braintree::setDefaultConnection('alternative'); // The default is now alternative.

如果您像我一样更喜欢使用依赖注入而不是外观,则可以注入管理器

use Artisanry\Braintree\BraintreeManager;

class Foo
{
    protected $braintree;

    public function __construct(BraintreeManager $braintree)
    {
        $this->braintree = $braintree;
    }

    public function bar($params)
    {
        $this->braintree->getCharge()->([
            'amount' => '10.00',
            'paymentMethodNonce' => $nonceFromTheClient,
            'options' => ['submitForSettlement' => true]
        ]);
    }
}

App::make('Foo')->bar($params);

文档

此包中还有其他未在此处记录的类。这是因为该包是 官方 Braintree 包 的 Laravel 包装器。

测试

$ phpunit

安全

如果您在此包中发现安全漏洞,请发送电子邮件至 hello@basecode.sh。所有安全漏洞都将得到及时处理。

致谢

此项目归功于所有 贡献者

许可证

Mozilla 公共许可证版本 2.0 (MPL-2.0)。