简体   繁体   English

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

[英]Braintree dropin UI - Access token needs to be passed to Braintree\Gateway

Symfony 4 app with Braintree JS drop-in UI带有 Braintree JS 嵌入式 UI 的 Symfony 4 应用程序

I have created a service called Braintree to start testing.我创建了一个名为 Braintree 的服务来开始测试。

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();
}

I am getting the following error:我收到以下错误:

HTTP 500 Internal Server Error
Braintree\Configuration::merchantId needs to be set (or accessToken needs to be passed to Braintree\Gateway).

The BT config has been properly entered into the .env file. BT 配置已正确输入到 .env 文件中。 Why is it not setting the MERCHANT_ID?为什么不设置 MERCHANT_ID?

Edit:编辑:

Add config添加配置

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)%'

Edit 2:编辑2:

The stack trace:堆栈跟踪:

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)

Edit 3:编辑3:

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();
    }

}

Error is now:错误现在是:

Catchable Fatal Error: Object of class Braintree\Gateway could not be converted to string

Am I getting closer to generating the client token??我是否越来越接近生成客户端令牌?

The .env -file does not actually populate the system environment. .env文件实际上并未填充系统环境。 Instead it works as a fallback when the environment is not set.相反,当环境未设置时,它作为后备。 Your call to getenv() only takes into account the system environment.您对getenv()调用仅考虑系统环境。 For your file to take effect you have to use the Service container.要使您的文件生效,您必须使用 Service 容器。

#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)%'

The special parameter %env()% will check your system environment for the variable first and if that is not set it will go through the .env files to see if there is a fallback defined.特殊参数%env()%将首先检查系统环境中的变量,如果未设置,它将通过.env文件查看是否定义了回退。 You can also read up on this in the docs: https://symfony.com/doc/current/configuration/external_parameters.html#environment-variables您还可以在文档中阅读此内容: https : //symfony.com/doc/current/configuration/external_parameters.html#environment-variables

This will give the service Braintree_Gateway , that you built manually inside your service.这将提供您在服务中手动构建的服务Braintree_Gateway Just like with any other service you can inject it into your service instead and autowiring will pass in an already generated Braintree Gateway:就像任何其他服务一样,您可以将其注入到您的服务中,并且自动装配将传入一个已经生成的 Braintree 网关:

namespace App\Services;

use Braintree\ClientToken;

class Braintree
{
    private $gateway;

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

    # ... methods using the gateway
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM