How to test Braintree websites with different types of notifications

I want to test my webhook functions with various notifications. Now I can check it only for unsubscribing (by unsubscribing from the braintree backend).

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

I 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 returned by the API is not satisfactory. It always returns the same array for all types of notifications, whether the subscription identifier exists or not.

+5
source share
2 answers

You are correct that Braintree_WebhookTesting::sampleNotification does not know about the state of your Braintree repository. This method is designed to quickly emulate all types of webhook notifications that could be received, since setting up a test environment to receive web moves can be quite complicated.

If you want to receive actual web recordings with different types of notifications, you will need to create a Subscription, Merchant Account or Braintree account for which you hope to receive a web check.

Full disclosure: I'm a Braintree developer.

+3
source

Here is my testing script that sends an example of testing post data to the localhost web host 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 parameters to this script, the first kind and the second id . This will allow you to change the appearance of events - check the documentation . Follow the example how to generate a subscription_canceled event:

 php webhook.tests.php subscription_canceled 123456 > output.txt 
+1
source

Source: https://habr.com/ru/post/1210723/


All Articles