-->

How does one handle Webhooks in BrainTree

2019-07-18 04:17发布

问题:

I am trying to use the BrainTree webhooks for subscription transactions but have been unable to get my page to verify.

From BrainTree: https://www.braintreepayments.com/docs/php/webhooks/destination_verification

When you attempt to add the destination, our servers will make a GET request to the provided URL with a query param named bt_challenge. This query param should be passed to the verify method. The result of calling this method should be returned as the body of the response.

Braintree_WebhookNotification::verify(bt_challenge_param);

First, I tried in NodeJS (as our transactions are successfully done this way):

//WEBHOOK GET PROCESS FOR BRAINTREE SUBSCRIPTION
app.get('/getwebhook', function(req, res){

    var bt_challenge_param = req.param('bt_challenge_param', null);
    var jsObj = new Object();

    jsObj.response = gateway.webhookNotification.verify(bt_challenge_param);

    res.json(JSON.stringify(jsObj));
});

where my PHP page communicated with the NodeJS process and puts the result in the body. Once this failed verification, I wrote a test page directly in PHP:

<?php
require_once 'lib/Braintree.php';

Braintree_Configuration::environment('production');
Braintree_Configuration::merchantId('mymid');
Braintree_Configuration::publicKey('mypubkey');
Braintree_Configuration::privateKey('myprodkey');

$bt_challenge = "";
if(isset($_GET['bt_challenge']))
{
    $bt_challenge = $_GET['bt_challenge'];
}


?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <title>Webhooks</title>
  <meta name="viewport" content="width=device-width; initial-scale=1.0" />
</head>
<body>
<?php
if(isset($bt_challenge) && $bt_challenge != ""){
    echo Braintree_WebhookNotification::verify($bt_challenge);
}
?>
</body>
</html>

However, this too failed verification. Not sure what is wrong as there is no test for verification or any indication of what is wrong. I tried contacting BrainTree support but with no response back.

回答1:

You need to return the result of

Braintree_WebhookNotification::verify($bt_challenge);

as the body of the response, not the body of an HTML document that is in turn the body of the response. In other words, your entire file should be something like:

<?php
require_once 'lib/Braintree.php';

Braintree_Configuration::environment('production');
Braintree_Configuration::merchantId('mymid');
Braintree_Configuration::publicKey('mypubkey');
Braintree_Configuration::privateKey('myprodkey');

$bt_challenge = "";
if(isset($_GET['bt_challenge']))
{
    $bt_challenge = $_GET['bt_challenge'];
}
if(isset($bt_challenge) && $bt_challenge != ""){
    echo Braintree_WebhookNotification::verify($bt_challenge);
}
?>

If you have any more questions, please feel free to reach out to Braintree Support.

Disclosure: I work at Braintree.