danhusaker/laravel-braintree

为 Laravel 提供的 Braintree 桥接器

6.0.0 2020-10-09 21:59 UTC

This package is auto-updated.

Last update: 2024-09-10 06:34:31 UTC


README

Build Status PHP from Packagist Latest Version License

为 Laravel 提供的 Braintree 桥接器。

安装

使用 Composer 在项目的根目录中安装此包。

$ composer require danhunsaker/laravel-braintree

配置

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

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

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

默认连接名称

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

Braintree 连接

此选项 connections 是设置应用程序中的每个连接的地方。已包含示例配置,但您可以添加任意数量的连接。

用法

BraintreeManager

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

Facades\Braintree

此门面将动态地将静态方法调用传递到 ioc 容器中的 braintree 对象,默认为 BraintreeManager 类。

BraintreeServiceProvider

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

示例

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

// You can alias this in config/app.php.
use Danhunsaker\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 Danhunsaker\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 Danhunsaker\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

安全

如果您在此包中发现安全漏洞,请发送电子邮件至 danhunsaker@gmail.com。所有安全漏洞都将得到迅速解决。

致谢

许可

MIT © Brian Faust 和 Dan Hunsaker