BrainTree PHP Webhook

You’re declaring Braintree twice. First, you declare a new namespace at the top Braintree (Once), and when you instantiate Braintree\Configuration (Twice). This will cause the same error message because PHP is looking for that specific class. It’s looking for Braintree\Braintree\Configuration. To look for just Braintree\Configuration, you’ll need to either use

namespace Braintree;
...
...
...
...

\Configuration::publicKey();

Or

Braintree\Configuration::publicKey();

With the second option, you’ll need to remove the namespace Braintree at the top of the page because that’s already your first Braintree.

I tried with \Configuration::publicKey(); Fatal error: Class ‘Configuration’ not found in line 15.

Remove the backslash I added in there. Just use Configuration::publicKey(); instead of \Configuration::publicKey();

1 Like

Still the same error “Fatal error: Class ‘Braintree\Configuration’ not found on line 15”

namespace Braintree;

$fileName = ‘Braintree.php’;
if ( file_exists($fileName) ) {
require_once $fileName;
}

else{
echo 'Yes we have no ’ .$fileName;
exit; // no point in continuing.
}

Configuration::environment(‘sandbox’);
Configuration::merchantId(‘#’);
Configuration::publicKey(‘#’);
Configuration::privateKey(‘#’);

Thank you for your help!

Is this file stored in the same place as Braintree.php? If it isn’t, you need to point to where Braintree.php is located. Where did you download the Braintree files from?

1 Like

I downloaded the zip file you pointed me to.

I have the lib folder in my root and at first that’s where I had the webhook file too but I was getting the same error, so I got the Braintree.php, autoload.php and webhook.php all now in the root. So I am not sure why I still have the error.

"<?php

require_once(‘Braintree/autoload.php’);
require_once(‘Braintree.php’);
use Braintree;

Braintree\Configuration::environment(‘sandbox’);
Braintree\Configuration::merchantId(‘#’);
Braintree\Configuration::publicKey(‘#’);
Braintree\Configuration::privateKey(‘#’);

final class Disbursement extends Base
{
private $_merchantAccount;

protected function _initialize($disbursementAttribs)
{
    $this->_attributes = $disbursementAttribs;
    $this->merchantAccountDetails = $disbursementAttribs['merchantAccount'];

    if (isset($disbursementAttribs['merchantAccount'])) {
        $this->_set('merchantAccount',
            MerchantAccount::factory($disbursementAttribs['merchantAccount'])
        );
    }
}

public function transactions()
{
    $collection = Transaction::search([
        TransactionSearch::ids()->in($this->transactionIds),
    ]);

    return $collection;
}

public static function factory($attributes)
{
    $instance = new self();
    $instance->_initialize($attributes);
    return $instance;
}

public function  __toString()
{
    $display = [
        'id', 'merchantAccountDetails', 'exceptionMessage', 'amount',
        'disbursementDate', 'followUpAction', 'retry', 'success',
        'transactionIds'
        ];

    $displayAttributes = [];
    foreach ($display AS $attrib) {
        $displayAttributes[$attrib] = $this->$attrib;
    }
    return __CLASS__ . '[' .
            Util::attributesToString($displayAttributes) .']';
}

}
class_alias(‘Braintree\Disbursement’, ‘Braintree_Disbursement’);

$bt_challenge_param = $_GET[‘bt_challenge’];
echo $response = Braintree\WebhookNotification::verify($bt_challenge_param);

if(
isset($_POST[“bt_signature”]) &&
isset($_POST[“bt_payload”])
) {
$webhookNotification = Braintree\WebhookNotification::parse(
$_POST[“bt_signature”], $_POST[“bt_payload”]
);
$message =
"[Webhook Received " . $webhookNotification->timestamp->format(‘Y-m-d H:i:s’) . "] "
. “Kind: " . $webhookNotification->kind . " | "
. “Subscription: " . $webhookNotification->Braintree_Disbursement->id . “\n”;
file_put_contents(”/tmp/webhook.log”, $message, FILE_APPEND);
}

?>"

So, I am trying something different and now I am getting this error:

“Parse error: syntax error, unexpected ‘;’, expecting identifier (T_STRING) in /#/#/public_html/#/webhook.php on line 5”

Granted, error messages can be somewhat cryptic at times.

What that’s saying is
“PHP first noticed there was an error when it got to line # 5. It saw Braintree\ and was expecting a String to be next but instead saw a semi-colon”

That is, the line needs to be
use Braintree\something;
or
use Braintree;

I did what you said, “use Braintree;” said non-compound effect. So removed that and now the errors are:

Warning: require_once(Braintree/autoload.php): failed to open stream: No such file or directory in /#/#/public_html/#/webhook.php on line 3

Fatal error: require_once(): Failed opening required ‘Braintree/autoload.php’ (include_path=‘.:/opt/php54/lib/php’) in /#/public_html/#/webhook.php on line 3

How do I include this path: (include_path=‘.:/opt/php54/lib/php’)

The files are all in the root folder.

require_once(‘Braintree/autoload.php’);
require_once(‘Braintree.php’);

Thank you for your help!

Put those files back in the lib folder and require them by pointing to that folder. It’s because you’re requiring them while some of the files might require something else in the lib folder. This will be a problem. Also, you don’t need to require the autoload file as it is required in the Braintree.php file already.

Another thing you need to know, (include_path='.:/opt/php54/lib/php') isn’t really needed. It’s just an error. Mostly, you should pay more attention to what the error message is telling you rather than trying to include (include_path='.:/opt/php54/lib/php').

Thank you for your help. I have put back the files, still getting the same error.

Warning: require_once(Braintree/autoload.php): failed to open stream:
No such file or directory in /#/#/public_html/#/webhook.php on line 3

Fatal error: require_once(): Failed opening required
‘Braintree/autoload.php’ (include_path=‘.:/opt/php54/lib/php’) in
/#/public_html/#/webhook.php on line 3

<?php require_once('Braintree.php'); I feel like I am going crazy with this :(

Assuming you followed the README and unzipped the “lib” folder the same level as your test file, try

require_once 'lib/Braintree.php'; 
1 Like

Thank you for your help. I really appreciate you!

Did what you suggested and now another new error:

“Fatal error: Call to undefined method Braintree\Configuration::privateKe() in /#/#/public_html/#/webhook.php on line 8”

I have double checked the private key :frowning:

Key is typed wrong.

Fixed that one and another error again :frowning: :frowning: :frowning:

“Fatal error: Class ‘Base’ not found in /#/#/public_html/#/webhook.php on line 11”

<?php

require_once 'lib/Braintree.php';

Braintree\Configuration::environment('sandbox');
Braintree\Configuration::merchantId('#');
Braintree\Configuration::publicKey('#');
Braintree\Configuration::privateKey('#');

final class Disbursement extends Base
{
    private $_merchantAccount;

    protected function _initialize($disbursementAttribs)
    {
        $this->_attributes = $disbursementAttribs;
        $this->merchantAccountDetails = $disbursementAttribs['merchantAccount'];

        if (isset($disbursementAttribs['merchantAccount'])) {
            $this->_set('merchantAccount',
                MerchantAccount::factory($disbursementAttribs['merchantAccount'])
            );
        }
    }

    public function transactions()
    {
        $collection = Transaction::search([
            TransactionSearch::ids()->in($this->transactionIds),
        ]);

        return $collection;
    }

    public static function factory($attributes)
    {
        $instance = new self();
        $instance->_initialize($attributes);
        return $instance;
    }

    public function  __toString()
    {
        $display = [
            'id', 'merchantAccountDetails', 'exceptionMessage', 'amount',
            'disbursementDate', 'followUpAction', 'retry', 'success',
            'transactionIds'
            ];

        $displayAttributes = [];
        foreach ($display AS $attrib) {
            $displayAttributes[$attrib] = $this->$attrib;
        }
        return __CLASS__ . '[' .
                Util::attributesToString($displayAttributes) .']';
    }
}
class_alias('Braintree\Disbursement', 'Braintree_Disbursement');

$bt_challenge_param = $_GET['bt_challenge'];
echo $response = Braintree\WebhookNotification::verify($bt_challenge_param);

if(
    isset($_POST["bt_signature"]) &&
    isset($_POST["bt_payload"])
) {
    $webhookNotification = Braintree\WebhookNotification::parse(
        $_POST["bt_signature"], $_POST["bt_payload"]
    );
    $message =
        "[Webhook Received " . $webhookNotification->timestamp->format('Y-m-d H:i:s') . "] "
        . "Kind: " . $webhookNotification->kind . " | "
        . "Subscription: " . $webhookNotification->Braintree_Disbursement->id . "\n";
    file_put_contents("/tmp/webhook.log", $message, FILE_APPEND);
}

?>

Is Base being called anywhere in any of the lib files? If it’s not, then I’m not sure why you would need it.

Only in the Disbursement file:

<?php namespace Braintree; final class Disbursement extends Base { private $_merchantAccount; protected function _initialize($disbursementAttribs) { $this->_attributes = $disbursementAttribs; $this->merchantAccountDetails = $disbursementAttribs['merchantAccount']; if (isset($disbursementAttribs['merchantAccount'])) { $this->_set('merchantAccount', MerchantAccount::factory($disbursementAttribs['merchantAccount']) ); } } public function transactions() { $collection = Transaction::search([ TransactionSearch::ids()->in($this->transactionIds), ]); return $collection; } public static function factory($attributes) { $instance = new self(); $instance->_initialize($attributes); return $instance; } public function __toString() { $display = [ 'id', 'merchantAccountDetails', 'exceptionMessage', 'amount', 'disbursementDate', 'followUpAction', 'retry', 'success', 'transactionIds' ]; $displayAttributes = []; foreach ($display AS $attrib) { $displayAttributes[$attrib] = $this->$attrib; } return __CLASS__ . '[' . Util::attributesToString($displayAttributes) .']'; } } class_alias('Braintree\Disbursement', 'Braintree_Disbursement'); So, the thing I am trying to do is create a Disbursement webhook. The above is the file they have in the folder.

If Base isn’t a class and it doesn’t exist, you don’t need to extend it in a file that doesn’t know what Base is.

Yes, we know you are trying to create a Disbursement webhook, but you need to understand what each error message is telling you. It doesn’t sound like you are familiar with PHP so your best bet is to read up on namespaces and understand how they work. Also, read up on the extends option too. It’ll help you better your debugging. You asking us to do your work for you won’t really help. We can tell you to do this and that, but you aren’t learning a thing.

There is a Base. It’s in the Braintree folder that is inside of the lib folder.
Or at least it should be.

Yes, I see it too!