6

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.

hairboat
  • 650
  • 19
  • 29
user2971353
  • 67
  • 1
  • 6

2 Answers2

3

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.

openbl
  • 51
  • 2
  • I am receiving actual webhook notification type only for cancelling subscription .How can I achieve the actual webhook for other notifications type? I have tried adding a transaction from the branitree account (https://sandbox.braintreegateway.com/) but it doesn't seems to work for me. – user2971353 Jan 12 '15 at 07:53
  • 1
    @openbl: is it possible to somehow populate the relevant fields of the sample webhook notification? It is really a must when it comes to test use cases dependant on for example: billingEndDate. BTW: I'm using the Java SDK. – sanya Jan 25 '15 at 18:00
  • 1
    @sanya, aside from the Subscription ID it's not possible to populate other fields in the test webhook. – openbl Jan 29 '15 at 18:43
  • thanks for the clarification. this makes it very hard test application logic based the fields of the actual subscription. – sanya Jan 30 '15 at 15:11
  • 4
    I agree with @sanya. Without the objects being fully populated like they would be in a normal webhook call, the sampleNotification functionality doesn't really help me much with testing. I can see that the notification will hit the correct portion of my code for a certain notification, but I can't really test how I am handling that notification without the proper fields populated. – LeftyCoder Feb 23 '15 at 15:12
  • 1
    I agree that these empty notifications aren't particularly useful and I really need a way to create a 'mock' response object with some valid data for each notification type. It was suggested to me that one approach is to serialise the incoming notification objects to a byte array and save them for reference as a sample of each type, but that means that I need find a way to generate every notification type in a state that I need to test it. If Braintree could at least publish some more detailed examples of the content of notifications for each notification and state that would help. – Liam Weston Feb 28 '15 at 17:16
  • 1
    Any updates on this? I can't find example of full webhooks notifications in the docs. It would be cool if we could have a "send test webhook" in sandbox control panel. – Stev May 15 '15 at 12:09
  • 1
    Same here. I am quite surprised that the `WebhookNotification` object is empty. Since we have a `BraintreeGateway` object anyway which is e.g. configured for sandbox-mode, why is it a problem to request such a notification from the sandbox in order to forward it to something like `http://localhost:8080/bt/webhooks`? – Stefan Falk Aug 13 '16 at 19:19
2

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
OzzyCzech
  • 9,713
  • 3
  • 50
  • 34