0

I am using Docusign's REST API to create and send envelopes. I've included eventNotifications with requireAcknowledgment as true to get requests from Docusign whenever there's status change. I used ngrok while development and testing and everything worked as expected.

I've moved the project online and have edited the eventNotification's url to live url with https and that's when all the callbacks are getting logged in failed section in Docusign's admin panel.

The error message shown in admin panel is -

'https://xxx.xxxxxxx.com/webhook.php :: Error - The remote server returned an error: (400) Bad Request.'

I've downloaded the failed request's xml body and tried sending a request through postman and it worked as expected. Iv'e tried everything to debug this error and have not found error at my end.

Edit: The code that I've tried with is the same code from DocuSign's webhook sample page -

    $data = file_get_contents('php://input');
$xml = simplexml_load_string ($data, "SimpleXMLElement", LIBXML_PARSEHUGE);
$envelope_id = (string)$xml->EnvelopeStatus->EnvelopeID;
$time_generated = (string)$xml->EnvelopeStatus->TimeGenerated;

$files_dir = getcwd() . '/' . $this->xml_file_dir;
if(! is_dir($files_dir)) {mkdir ($files_dir, 0755);}
$envelope_dir = $files_dir . "E" . $envelope_id;
if(! is_dir($envelope_dir)) {mkdir ($envelope_dir, 0755);}

$filename = $envelope_dir . "/T" . 
    str_replace (':' , '_' , $time_generated) . ".xml"; // substitute _ for : for windows-land
$ok = file_put_contents ($filename, $data);

if ($ok === false) {
    error_log ("!!!!!! PROBLEM DocuSign Webhook: Couldn't store $filename !");
    exit (1);
}
// log the event
error_log ("DocuSign Webhook: created $filename");

if ((string)$xml->EnvelopeStatus->Status === "Completed") {
    // Loop through the DocumentPDFs element, storing each document.
    foreach ($xml->DocumentPDFs->DocumentPDF as $pdf) {
        $filename = $this->doc_prefix . (string)$pdf->DocumentID . '.pdf';
        $full_filename = $envelope_dir . "/" . $filename;
        file_put_contents($full_filename, base64_decode ( (string)$pdf->PDFBytes ));
    }
}

I've also tried with simple code that just sets header to 200

http_response_code(200);
Ash93
  • 1
  • 2
  • What does your code look like? What errors do you get, and where? – Dragonthoughts May 23 '18 at 19:32
  • I've used PHP and DocuSign REST API to create and send envelopes. Everything is working as expected but webhooks are not working on live server. DocuSign logs say the requests they made to my server failed with 400 error – Ash93 May 23 '18 at 19:36
  • does you live server accept POST call and an XML message payload? – Amit K Bist May 23 '18 at 20:18
  • Yes it does, I've downloaded the xml body from a successful webhook call from docusign and tried sending it using postman chrome extension, it worked as expected. – Ash93 May 23 '18 at 20:26
  • What content-type did you set in Postman? – Amit K Bist May 23 '18 at 20:33
  • I've tried text/xml and application/xml, both worked fine – Ash93 May 23 '18 at 20:36
  • Can you try setting up DS event notifications with the free public webhooks like `https://www.webhookapp.com` to be sure that your live server is actually coded/configured incorrectly, I hope your live server has proper SSL certificates since you are using HTTPS. – Amit K Bist May 23 '18 at 20:49
  • I've done that as well. I used https://webhook.site to set up a temporary webhook url. DocuSign could send request to that successfully. I've simply redirected the same request from webhook.site to my url and that worked. If my server wasn't using a proper SSL certificate then I guess the error code would've been different from what I've seen in few questions here – Ash93 May 23 '18 at 20:56
  • Can you please update qs with your controller/method code? – Amit K Bist May 23 '18 at 21:46
  • I tried with my code, the sample php code given in Docusign webhook sample page and with just a simple page that echo's success to any request. I've tried setting the header to 200 too with no success – Ash93 May 23 '18 at 22:38
  • @Ash93: is it resolved or still seeing the issue? – Amit K Bist May 24 '18 at 16:20
  • @AmitKBist: The issue has not been resolved. I've contacted DocuSign support but with no success – Ash93 May 24 '18 at 17:39
  • can you share your webhook code, update your qs with the code – Amit K Bist May 24 '18 at 17:44
  • @AmitKBist: Done – Ash93 May 24 '18 at 17:51
  • @AmitKBist: No luck yet – Ash93 May 25 '18 at 00:33
  • Can you try setting something in the response body and see if PostMan can show the response body, it seems your listener is returning 200 for everything if you test with Postman for GET/PUT etc anything – Amit K Bist May 25 '18 at 02:12
  • I tried setting envelopeID and other text in response body. It was working fine with PostMan but not with DocuSign. Since DocuSign is logging 400 error for every request I thought let's try setting response code manually to 200 – Ash93 May 25 '18 at 11:28
  • @AmitKBist No luck yet. Webhook is a very good feature I am unfortunately missing out on, nu luck yet in solving this and there are no resources online. I tried calling DocuSign support with no luck – Ash93 May 29 '18 at 18:14

1 Answers1

0

Sorry you're having so much trouble with the webhook feature. Hopefully this answer will be of assistance.

1. Try a test PHP program to check connectivity, etc:

<?php
header('Content-Type: text/html');
echo "OK!";
$h = fopen('/tmp/listener_access.log', 'a');
if ($h) {
    $now = DateTime::createFromFormat('U.u', microtime(true), new DateTimeZone('UTC'));
    fwrite ($h,  $now->format ("Y-m-d_l.h.i.s.v "));
    fwrite($h, $_SERVER["REMOTE_ADDR"] . " " . $msg . "\n");
    fclose($h);
} else {
    error_log ("### Could not open log file!");
}

Store it as ok.php in your web server directory. Then try (from a browser on a different network) to reach https://yourserver.company.com/ok.php You should see "ok" in the browser window.

Then use the same url in your eventNotification section of your Envelopes::create call to see if it all works. You should see a success in the Connect log, etc.

2. Stepwise debugging of your PHP listener

There are a number of issues to rule-out (as diagnosticians say) with your php listener app.

  1. Basic connectivity with the listener. Since DocuSign is receiving a 400 error from your app, connectivity is ok.
  2. Platform errors with your app. This is a problem with your PHP software stack setup that is causing the stack (not your PHP app, per se) to reject the request.

    The usual indicator for this is your web server's error log. Have you looked at it? What is on the logging line where you see the 400 response from your server to DocuSign? If you don't see the 400 response to DocuSign then something is wrong with your web server's setup.

  3. A common platform error with PHP and other stacks when default settings are used is maximum_post_size_exceeded. This is happens if you have requested that DocuSign include your envelope's documents in the notification message.

    A good test is to temporarily change your envelope create code to not include documents in the notification messages.

    Fixes: a good fix is to not include the envelope documents in the notification message. Instead, after the message is received, make separate API calls to retrieve the documents.

    The other fix is to increase the maximum post body size. You may need to increase it both in the PHP settings and in the underlying web server's settings too. (By the way, which web server are you using?)

  4. You are processing the incoming notification in the response thread of your server/php app. This really isn't the best technique (I will be updating the DocuSign example page in the future with this information.)

The best technique is to use the following pattern:

1. Receive the incoming notification message from DocuSign
2. Put the message onto a reliable FIFO queue
3. Respond to DocuSign with a 200 response.


Then, in a separate thread of execution, a 
different software application "works off" the 
entries in the queue.

Your web server, for example, may be timing out your PHP program. This probably isn't happening in your case, but others may experience this.

  1. I think I would, next, add more debugging statements to your PHP program to try and understand what is happening to it. You can either do this with error-log or copy the technique from my example (above) and write to a file in /tmp. (Assuming a Linux server.)

    The other option is to increase the debugging level of the php stack itself, or of your web server, or both.

3. Last thoughts

400 in the DocuSign logs usually indicates that DocuSign reached your server and it returned a 400 status code. This can be confirmed by examining your server log (regular or error), there should be a matching entry.

If there isn't an entry in your server log, but the "ok.php" program from above does work, then it would be time to comment out big chunks of code from your PHP program, and then do another test from DocuSign. Eventually, using a binary-search technique (See step 8 in the article), you find the code that is causing the problem.

Commenting out code as part of a binary-search to find the bug is a very common, and powerful debugging technique.

HTH, Larry

Larry K
  • 47,808
  • 15
  • 87
  • 140
  • Thanks for your reply Larry. 1. I've tried your code and it showed OK! in the browser but DocuSign connect failed with 400 error again. 2. I've been monitoring the logs closely since facing this problem and strangely nothing is getting logged. Neither in access_log nor in error_log. 3. I've checked the maximum post size and maximum upload size parameters and they are set at 100mb. Just to be sure I've disabled include documents and still no luck. I've tried the same post load from PostMan and it worked. 4. I am sending the response back to DocuSign and then processing the post load. – Ash93 May 31 '18 at 23:47
  • 5. I've tried commenting out every possible line of code to debug. I have tested with a simple code that appends the request body to a file and sends 200 response back, even this failed in connect logs with error 400 – Ash93 May 31 '18 at 23:50
  • Since my code is not working, that's the first thing to try and fix. I think you need to escalate within DocuSign customer service. Ask to contact the developer support team. – Larry K Jun 01 '18 at 14:53
  • I've tried contacting CS. They have asked me to buy bundle hours to get in touch with someone from developer support. I am not sure to go with that option because bundle hours are not cheap – Ash93 Jun 01 '18 at 19:11
  • It works fine in demo, but not in production? Unfortunately I am out of good ideas. Is your firewall blocking requests from DocuSign? Can you test with a different server from a cheap server company? – Larry K Jun 02 '18 at 17:39
  • I did some further investigation on my side. We may have a problem with the front end machine that is sending messages to you. We'll know more by Tuesday. – Larry K Jun 02 '18 at 18:48