Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to test braintree webhooks with different notification types

I want to test my webhook functionality with different notifications. Right now I am able to test it only for canceling subscriptions (by cancelling the subscription from the backend of braintree).

$webhookNotification = Braintree_WebhookNotification::parse($sampleNotification["bt_signature"], $sampleNotification["bt_payload"]);        

I have also tried https://www.braintreepayments.com/docs/php/webhooks/testing:

$sampleNotification = Braintree_WebhookTesting::sampleNotification(Braintree_WebhookNotification::SUBSCRIPTION_WENT_ACTIVE,'1234qwe');
$webhookNotification = Braintree_WebhookNotification::parse($sampleNotification["bt_signature"], $sampleNotification["bt_payload"]);

But the result the API returns is not satisfactory. It always returns the same array for all notification types whether the subscription id exists or not.

like image 966
user2971353 Avatar asked Jan 09 '15 10:01

user2971353


2 Answers

You are correct that the Braintree_WebhookTesting::sampleNotification is unaware of the state of your Braintree vault. That method is intended to be used to quickly emulate all of the webhook notification types one might receive since setting up a testing environment to receive webhooks can be rather involved.

If you are looking to receive actual webhooks with the different notification types, you will have to create the Subscription, Merchant Account, or Braintree object for which you're hoping to receive a webhook.

Full disclosure: I am a Braintree developer.

like image 133
openbl Avatar answered Oct 11 '22 23:10

openbl


Here is my testing script that send example testing post data to localhost webhook URL:

<?php
require_once __DIR__ . '/vendor/autoload.php';

// your sandbox data
\Braintree\Configuration::environment('env...');
\Braintree\Configuration::merchantId('id');
\Braintree\Configuration::publicKey('your key');
\Braintree\Configuration::privateKey('your key');

$kind = isset($argv[1]) ? $argv[1] : \Braintree\WebhookNotification::CHECK;
$id = isset($argv[2]) ? $argv[2] : null;

$sampleNotification = \Braintree\WebhookTesting::sampleNotification($kind, $id);
$signature = $sampleNotification['bt_signature'];
$payload = $sampleNotification['bt_payload'];

// Submit a payload and signature to handler
$ch = curl_init('http://localhost/braintree.hook.php'); // Your URL
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
  ['bt_signature' => $signature, 'bt_payload' => $payload]
);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

echo curl_exec($ch);

You can send two params to this script first kind and second id. That allow you to change event kind - check out documentation. Follow an example, how to generate subscription_canceled event:

php webhook.tests.php subscription_canceled 123456 > output.txt
like image 40
OzzyCzech Avatar answered Oct 11 '22 22:10

OzzyCzech