danhusaker / laravel-braintree
Laravel的Braintree桥梁
Requires
- php: >=7.2
- braintree/braintree_php: ^3.27|^4.1
- graham-campbell/manager: ^3.0|^4.0
- illuminate/support: ^5.1|^6.0|^7.0
Requires (Dev)
- graham-campbell/testbench: ^4.0|^5.0
- mockery/mockery: ^1.0
- phpunit/phpunit: ^6.5|^7.0|^8.0
README
Laravel的Braintree桥梁。
安装
使用Composer,在项目的根目录中要求此包。
$ composer require danhunsaker/laravel-braintree
配置
Laravel Braintree需要连接配置。要开始,你需要发布所有供应商资产
$ php artisan vendor:publish --provider="Danhunsaker\Braintree\BraintreeServiceProvider"
这将在你的应用中创建一个config/braintree.php
文件,你可以修改它来设置你的配置。同时,确保你检查这个包中原始配置文件在版本间的变化。
默认连接名称
此选项default
是您指定以下哪个连接作为您所有工作的默认连接的地方。当然,您可以使用管理器类同时使用多个连接。此设置的默认值为main
。
Braintree连接
此选项connections
是设置应用程序中每个连接的地方。已包含示例配置,但您可以添加尽可能多的连接。
用法
BraintreeManager
这是最感兴趣的类。它绑定到ioc容器中的braintree
,可以使用Danhunsaker\Braintree\Facades\Braintree
外观访问。此类通过扩展AbstractManager实现了ManagerInterface。接口和抽象类都是Graham Campbell的Laravel Manager包的一部分,因此您可能想查看该存储库中的文档,了解如何使用管理器类。注意,返回的连接类始终是Braintree\Braintree
的实例。
Facades\Braintree
此外观将动态将静态方法调用传递到ioc容器中的braintree
对象,默认情况下是BraintreeManager
类。
BraintreeServiceProvider
此类不包含任何有趣的公共方法。此类应添加到config/app.php
中的提供者数组。此类将设置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。所有安全漏洞将得到及时处理。