简体   繁体   中英

Braintree with laravel

I am using Braintree PHP SDK with Laravel framework.
I installed Braintree through composer.
Then, in AppServiceProvider.php , I have added below code in boot() :

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

When trying to generate client_token , I get below error:

Symfony\\Component\\Debug\\Exception\\FatalThrowableError: Class 'App\\Providers\\Braintree_Configuration' not found in AppServiceProvider.php on line 34

The way you are using braintree seems to follow a deprecated example (my guess previous version of Braintre_php) as Braintree_Configuration class does not exist in the current package.

And you also need to use an "\\" before calling the autoloaded class, ex : \\Braintree.

This should work in your app/Providers/AppServiceProvider.php file with 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'
    ]);
}

You can have an up to date example here to see some basics sdk function to get started :https://developers.braintreepayments.com/start/hello-server/php

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

use App\Providers\Braintree_Configuration;

Please see examples below for braintree/paypal specific examples. This is the most elegant solution that worked for me:

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]);*/ 
}

// examplefile.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
    ]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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