简体   繁体   English

带有 Laravel 的 Braintree

[英]Braintree with laravel

I am using Braintree PHP SDK with Laravel framework.我正在使用带有 Laravel 框架的 Braintree PHP SDK。
I installed Braintree through composer.我通过作曲家安装了 Braintree。
Then, in AppServiceProvider.php , I have added below code in boot() :然后,在AppServiceProvider.php 中,我在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:尝试生成client_token 时,出现以下错误:

Symfony\\Component\\Debug\\Exception\\FatalThrowableError: Class 'App\\Providers\\Braintree_Configuration' not found in AppServiceProvider.php on line 34 Symfony\\Component\\Debug\\Exception\\FatalThrowableError:在第 34 行的 AppServiceProvider.php 中找不到“App\\Providers\\Braintree_Configuration”类

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.您使用 Braintree 的方式似乎遵循了一个已弃用的示例(我猜测以前版本的 Braintre_php),因为当前包中不存在 Braintree_Configuration 类。

And you also need to use an "\\" before calling the autoloaded class, ex : \\Braintree.并且您还需要在调用自动​​加载的类之前使用“\\”,例如:\\Braintree。

This should work in your app/Providers/AppServiceProvider.php file with Braintree 5.x :这应该在你的 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'
    ]);
}

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你可以在这里有一个最新的例子来查看一些基本的 sdk 功能来开始:https ://developers.braintreepayments.com/start/hello-server/php

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

use App\Providers\Braintree_Configuration;

Please see examples below for braintree/paypal specific examples.请参阅下面的示例,了解 Braintree/paypal 的具体示例。 This is the most elegant solution that worked for me:这是对我有用的最优雅的解决方案:

app\\Providers\\AppServiceProvider.php: 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: // 示例文件.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
    ]);
}

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

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