BrainTree PHP Webhook

Hi Guys,

I am trying to create a webhook for Braintree for the Disbursements. I am using WordPress and below is the code I am using but getting the following error:

“Fatal error: Class ‘Braintree_Configuration’ not found in /public_html/domain name/Braintree.php on line 4”

<?php
require_once('Braintree.php');

Braintree_Configuration::environment('sandbox');
Braintree_Configuration::merchantId('#');
Braintree_Configuration::publicKey('#');
Braintree_Configuration::privateKey('#');

$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->disbursement->id . "\n";
    file_put_contents("/tmp/webhook.log", $message, FILE_APPEND);
}

?>

Thanks

1 Like

Just a wild guess as I’m not familiar with Braintree so maybe it can be called statically?

Maybe if you did more than only “require” it? i.e.

require .....

$btc = new Braintree_Configuration;
$btc->environment('sandbox');

Thanks for your suggestion. I am still getting the same error :frowning:

I downloaded it and had a look. Where did you get the underscore from? That messes up the autoloader namespace stuff. i.e. the README has

require_once 'PATH_TO_BRAINTREE/lib/Braintree.php';

Braintree\Configuration::environment('sandbox');
Braintree\Configuration::merchantId('your_merchant_id');
Braintree\Configuration::publicKey('your_public_key');
Braintree\Configuration::privateKey('your_private_key');

Try this:

$fileName = 'Braintree.php';
if ( file_exists($fileName) ) {
  require_once $fileName;

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

1 Like

Thank you John,

I am now getting the following error: “Parse error: syntax error, unexpected ‘else’ (T_ELSE) in /#/public_html/#/Braintree.php on line 7”

adjacent key typo
pipe | instead of curly brace }

1 Like

Thank you! What do you mean?

John has a typo in the code example he posted. Fix that and it should run OK

1 Like
$fileName = 'Braintree.php';
if ( file_exists($fileName) ) {
  require_once $fileName;

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

I did fix that but I am still getting the error: “Parse error: syntax error, unexpected ‘else’ (T_ELSE) in /#/public_html/#/Braintree.php on line 7”

That’s because you removed the typo instead of fixing it.

Try the syntax in the first example here (i.e. pay particular attention to the curly braces)

http://php.net/manual/en/control-structures.elseif.php

1 Like

Thank you!

$fileName = 'Braintree.php';
if ( file_exists($fileName) ) {
  require_once $fileName;
}
|else{
   echo 'Yes we have no ' .$fileName;
   exit; // no point in continuing.
}

This that correct now?

I made the changes and now I am getting the following error:

“Fatal error: Class ‘Braintree_Configuration’ not found in /home3/#/public_html/#/Braintree.php on line 13”

Yes, as I pointed out in post # 4, there is no such thing as a Braintree_Configuration class.
“Braintree” is the namespace needed for the autoloader and the class is “Configuration”

1 Like
<?php

define('BRAINTREE_ENVIRONMENT','sandbox');
define('BRAINTREE_MERCHANT_ID','#');
define('BRAINTREE_PUBLIC_KEY','#');
define('BRAINTREE_PRIVATE_KEY','#');

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

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

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

$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->disbursement->id . "\n";
    file_put_contents("/tmp/webhook.log", $message, FILE_APPEND);
}

?>

Fatal error: Class 'Braintree\Configuration' not found in /#/#/public_html/#/Braintree.php on line 18

I’m not sure how you are getting that when I’m trying to debug what you have and I just got a fresh clean copy from https://github.com/braintree/braintree_php. I’m getting a different message which is

PHP Fatal error:  Uncaught Braintree\\Exception\\InvalidChallenge: challenge contains non-hex characters in

My question to you is, did you modify any part of the source code at all? If you have, please change it back because this could be the problem. If not, check where Braintree.php is located. Mine from the github repository is in lib. So you do require_once 'lib/Braintree.php; and it should work.

Also, Braintree_WebhookNotification should be Braintree\WebhookNotification. You need to get a grasp of how namespaces work in order to complete this sample snippet of yours.

1 Like

Thank you, I am using WordPress so added the PHP file to the root where the wp-config-php is.

Did doing what I suggested work? If not, post the updated snippet here.

Also, to make it easier for you to understand namespaces and classes, you are using what is called a static call which isn’t preferred, but can still be used.

In static calls, the best and easiest way to remember what each segment means is by doing a small comment and telling yourself how it’s setup. Like so.

// {namespace}/{class}::{function}();
SitePoint/User::SpaceShipTrooper();

Thank you.

Here is the new code:

<?php

namespace Braintree;

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

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

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);
}

?>

Still getting error: Fatal error: Class 'Braintree\Braintree\Configuration' not found in /#/#/public_html/#/webhook.php on line 15

What am trying to do is create a Disbursement webhook for Braintree.