首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Braintree dropin UI -访问令牌需要传递给Braintree\Gateway

Braintree dropin UI -访问令牌需要传递给Braintree\Gateway
EN

Stack Overflow用户
提问于 2019-01-12 03:57:35
回答 1查看 1.1K关注 0票数 1

带有Braintree JS插入UI的Symfony 4应用程序

我创建了一个名为Braintree的服务来开始测试。

代码语言:javascript
复制
namespace App\Services;

use Braintree\ClientToken;


class Braintree
{

    // environment variables:
    const ENVIRONMENT = 'BRAINTREE_ENVIRONMENT';
    const MERCHANT_ID = 'BRAINTREE_MERCHANT_ID';
    const PUBLIC_KEY = 'BRAINTREE_PUBLIC_KEY';
    const PRIVATE_KEY = 'BRAINTREE_PRIVATE_KEY';

    /** @var \Braintree_Gateway */
    private $gateway;

    function __construct() {
        $gateway = new \Braintree_Gateway([
            'environment' => getenv(self::ENVIRONMENT),
            'merchantId' => getenv(self::MERCHANT_ID),
            'publicKey' => getenv(self::PUBLIC_KEY),
            'privateKey' => getenv(self::PRIVATE_KEY)
        ]);
    }

public function generate() {
    return ClientToken::generate();
}

我得到了以下错误:

代码语言:javascript
复制
HTTP 500 Internal Server Error
Braintree\Configuration::merchantId needs to be set (or accessToken needs to be passed to Braintree\Gateway).

BT配置已经正确地输入到.env文件中。为什么它不设置MERCHANT_ID?

编辑:

添加配置

代码语言:javascript
复制
Braintree_Gateway:
    class: Braintree_Gateway
    arguments:
        -
          'environment': '%env(BRAINTREE_ENVIRONMENT)%'
          'merchantId': '%env(BRAINTREE_MERCHANT_ID)%'
          'publicKey': '%env(BRAINTREE_PUBLIC_KEY)%'
          'privateKey': '%env(BRAINTREE_PRIVATE_KEY)%'

编辑2:

堆栈跟踪:

代码语言:javascript
复制
Braintree\Exception\
Configuration
in vendor\braintree\braintree_php\lib\Braintree\Configuration.php (line 261)
    public function assertHasAccessTokenOrKeys()    {        if (empty($this->_accessToken)) {            if (empty($this->_merchantId)) {                throw new Exception\Configuration('Braintree\\Configuration::merchantId needs to be set (or accessToken needs to be passed to Braintree\\Gateway).');            } else if (empty($this->_environment)) {                throw new Exception\Configuration('Braintree\\Configuration::environment needs to be set.');            } else if (empty($this->_publicKey)) {                throw new Exception\Configuration('Braintree\\Configuration::publicKey needs to be set.');            } else if (empty($this->_privateKey)) {
Configuration->assertHasAccessTokenOrKeys()
in vendor\braintree\braintree_php\lib\Braintree\ClientTokenGateway.php (line 34)
ClientTokenGateway->__construct(object(Gateway))
in vendor\braintree\braintree_php\lib\Braintree\Gateway.php (line 59)
Gateway->clientToken()
in vendor\braintree\braintree_php\lib\Braintree\ClientToken.php (line 18)
ClientToken::generate()
in src\Services\Braintree.php (line 25)
Braintree->generate()
in src\Controller\ProfileController.php (line 50)
ProfileController->booking_new(object(EntityManager), object(Request), object(Braintree))
in vendor\symfony\http-kernel\HttpKernel.php (line 149)

编辑3:

代码语言:javascript
复制
namespace App\Services;

use Braintree_Gateway;

class Braintree extends Braintree_Gateway
{

    //Configure Braintree Environment
    public function __construct(Braintree_Gateway $gateway)
    {
        $this->$gateway = new Braintree_Gateway([
            'environment' => 'sandbox',
            'merchantId' => 'n5z3tjxh8zd6272k',
            'publicKey' => 'v4rjdzqk3gykw4kv',
            'privateKey' => '4ab8b962e81ee8c43bf6fa837cecfb97'
        ]);
    }

    //Generate a client token
    public function generate() {
        return $clientToken = $this->clientToken()->generate();
    }

}

错误现在是:

代码语言:javascript
复制
Catchable Fatal Error: Object of class Braintree\Gateway could not be converted to string

我离生成客户端令牌越来越近了吗?

EN

回答 1

Stack Overflow用户

发布于 2019-01-12 08:48:13

.env-file实际上并不填充系统环境。相反,当没有设置环境时,它会作为后盾。对getenv()的调用只考虑了系统环境。要使文件生效,必须使用服务容器。

代码语言:javascript
复制
#config/services.yaml
services:
    _defaults:
        autowire: true
        autoconfigure: true

    #... all the existing services

    Braintree_Gateway:
        class: Braintree_Gateway
        arguments:
            -
              'environment': '%env(BRAINTREE_ENVIRONMENT)%'
              'merchantId': '%env(BRAINTREE_MERCHANT_ID)%'
              'publicKey': '%env(BRAINTREE_PUBLIC_KEY)%'
              'privateKey': '%env(BRAINTREE_PRIVATE_KEY)%'

特殊参数%env()%将首先检查您的系统环境中的变量,如果没有设置,它将遍历.env文件,以查看是否定义了回退。您还可以在docs:parameters.html#environment-variables中阅读这方面的内容。

这将给出您在服务中手动构建的服务Braintree_Gateway。与任何其他服务一样,您可以将其插入到服务中,自动装配将传入一个已经生成的Braintree网关:

代码语言:javascript
复制
namespace App\Services;

use Braintree\ClientToken;

class Braintree
{
    private $gateway;

    public function __construct(\Braintree_Gateway $gateway)
    {
        $this->gateway = $gateway;
    }

    # ... methods using the gateway
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54156682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档