how to test braintree webhooks with different notification types
Asked Answered
B

2

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.

Burdelle answered 9/1, 2015 at 10:41 Comment(0)
S
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.

Sanitation answered 9/1, 2015 at 17:3 Comment(8)
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 (sandbox.braintreegateway.com) but it doesn't seems to work for me.Burdelle
@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.Divisible
@sanya, aside from the Subscription ID it's not possible to populate other fields in the test webhook.Sanitation
thanks for the clarification. this makes it very hard test application logic based the fields of the actual subscription.Divisible
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.Iatrochemistry
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.Advancement
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.Payer
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?Foresaid
M
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
Melia answered 15/11, 2017 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.