【问题标题】:Braintree with laravelBraintree 与 laravel
【发布时间】:2020-07-04 17:32:29
【问题描述】:

我正在使用带有 Laravel 框架的 Braintree PHP SDK。
我通过 composer 安装了 Braintree。
然后,在 AppServiceProvider.php 中,我在 boot() 中添加了以下代码:

Braintree_Configuration::environment('sandbox');
Braintree_Configuration::merchantId('merchand_id');
Braintree_Configuration::publicKey('public_key');
Braintree_Configuration::privateKey('private_key');

当尝试生成 client_token 时,出现以下错误:

Symfony\Component\Debug\Exception\FatalThrowableError: 类 'App\Providers\Braintree_Configuration' 未在 AppServiceProvider.php 第 34 行

【问题讨论】:

    标签: php laravel braintree-sandbox


    【解决方案1】:

    有关 Braintree/paypal 的具体示例,请参阅下面的示例。这是对我有用的最优雅的解决方案:

    app\Providers\AppServiceProvider.php:

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // braintree setup
        $environment = env('BRAINTREE_ENV');
        $braintree = new \Braintree\Gateway([
            'environment' => $environment,
            'merchantId' => 'merchant_id_example',
            'publicKey' => 'public_key_example',
            'privateKey' => 'private_key_example'
        ]);
        config(['braintree' => $braintree]); 
    
        // braintree setup for specifically for paypal direct integration for those who need it
        /*$accessToken = env('PAYPAL_ACCESS_TOKEN');
        $braintree = new \Braintree\Gateway([
            'accessToken' => $accessToken
        ]);
        config(['braintree' => $braintree]);*/ 
    }
    

    // 示例文件.php:

    public function token()
    {
        $braintree = config('braintree');
        $clienttoken = $braintree->clientToken()->generate();
    }
    
    public function sale()
    {
        $braintree = config('braintree');
        $result = $braintree->transaction()->sale([
            'amount' => $amount,
            'paymentMethodNonce' => $nonce
        ]);
    }
    

    【讨论】:

      【解决方案2】:

      您使用 Braintree 的方式似乎遵循了一个已弃用的示例(我猜是以前版本的 Braintre_php),因为当前包中不存在 Braintree_Configuration 类。

      而且你还需要在调用自动​​加载的类之前使用“\”,例如:\Braintree。

      这应该可以在您的 app/Providers/AppServiceProvider.php 文件中使用 Braintree 5.x :

      /**
       * Bootstrap any application services.
       *
       * @return void
       */
      public function boot()
      {
          //
          $gateway = new \Braintree\Gateway([
              'environment' => 'sandbox',
              'merchantId' => 'use_your_merchant_id',
              'publicKey' => 'use_your_public_key',
              'privateKey' => 'use_your_private_key'
          ]);
      }
      

      您可以在此处查看最新示例以了解一些基本的 sdk 功能以开始使用: https://developers.braintreepayments.com/start/hello-server/php

      【讨论】:

        【解决方案3】:

        您是否在 AppServiceProvider 中添加了 use 语句?

        use App\Providers\Braintree_Configuration;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-07-30
          • 2016-07-31
          • 2018-06-17
          • 2023-03-11
          • 1970-01-01
          • 2018-01-13
          • 2015-10-08
          • 1970-01-01
          相关资源
          最近更新 更多