具有Braintree JS嵌入式UI的Symfony 4应用
我创建了一个名为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();
}
我遇到以下错误:
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?
编辑:
添加配置
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:
堆栈跟踪:
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:
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();
}
}
现在是错误:
Catchable Fatal Error: Object of class Braintree\Gateway could not be converted to string
我越来越接近生成客户端令牌吗?
答案 0 :(得分:1)
.env
文件实际上并未填充系统环境。相反,当未设置环境时,它将作为备用。您对getenv()
的呼叫仅考虑了系统环境。为了使文件生效,您必须使用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)%'
特殊参数%env()%
将首先在系统环境中检查变量,如果未设置,它将通过.env
文件查看是否定义了后备。您也可以在文档中阅读:https://symfony.com/doc/current/configuration/external_parameters.html#environment-variables
这将提供您在服务内部手动构建的服务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
}