question

davidrichied avatar image
davidrichied asked davidrichied answered

How to Access Webhook Verification JSON data?

This is probably a simple question. I am trying to get the verification code ( https://docs.clover.com/build/webhooks/#verificati... from the Webhook Post request. Here is the code that I have in my PHP file.

$file = 'verification.txt';
$newdata = json_decode($_REQUEST);
$current = 'data: ' . json_encode($newdata);
file_put_contents($file, $current);

Typically, this would write the contents of a post, get, or request to a file in the same directory as the PHP file. I can confirm that the PHP file is being reached because the verification.txt file is created, but the $_REQUEST just appears as empty brackets ([ ]). I tried the same for $_POST, but that was empty as well. How can I easily retrieve the verification code? Is there a better way?

Thanks for the help!
API Token
2 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sam avatar image sam commented ·

I'm not too familiar with PHP but can you print directly onto the server console? My webhook logic simply looks like this: console.log(request.body);

0 Likes 0 ·
davidrichied avatar image davidrichied sam commented ·

Hi Sam,

Thanks for the reply! I don't think it's possible to instantly see a request or post that a person or program makes to a server. var_dump() is kind of like the PHP equivalent of JS's console.log(). PHP does not have the same console that JS has, but either way, I don't think it'd be possible to instantly see the post from the person or program.

0 Likes 0 ·
Bryanne Vega avatar image
Bryanne Vega answered sam Deactivated edited
First send the SUCCESS message on header, Clover Webhook won't work without it. (It wont accept 200 OK) (Emailed dev@clover.com for a feature request for this already).

header(trim("HTTP/1.1 200 SUCCESS"));

Then get the input:
$content = json_decode(file_get_contents('php://input'),true);

Then save it to your file. Don't enable any modules yes so you don't get a big file P:

Hope it helped (It worked for me).
5 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

davidrichied avatar image davidrichied commented ·

Yes! That worked! Thanks, Bryanne! I haven't been using PHP for too long (about 2 years), but I've never seen that method used before. I wonder if I would've been able to figure it out, eventually.

0 Likes 0 ·
davidrichied avatar image davidrichied commented ·
Hey @Bryanne Vega, another quick question. Do I have to do something with the X-Clover-Auth Code ? I ticked "Inventory - When inventory items are created, updated, or deleted." for the Webhook Subscriptions, but when I update the quantity of a product, I do not receive anything. Here is what I'm doing in my file. Thanks, again, for the help!

<?php
header(trim("HTTP/1.1 200 SUCCESS"));
$content = json_decode(file_get_contents('php://input'),true);
$file = 'clover-data.txt';
file_put_contents($file, $content);
0 Likes 0 ·
sam avatar image sam davidrichied commented ·

Are you changing the quantity in the Inventory App? It should return an inventory update:

2017-06-08T22:41:41.714199+00:00 app[web.1]: {"appId":"4W4XWSS3FSVP2","merchants":{"EJE2ZH35JJAG2":[{"objectId":"I:2YJSH2SQ96G02","type":"UPDATE","ts":1496961700859}]}}
2017-06-08T22:41:41.714475+00:00 app[web.1]: I, [2017-06-08T22:41:41.714414 #4] INFO -- : [00ee00ee-00ee-00ee-00ee-df047d7b20fe] Completed 200 OK in 0ms (Views: 0.1ms)
0 Likes 0 ·
screen.png (85.0 KiB)
davidrichied avatar image davidrichied sam commented ·

Hi Sam,

Thanks for the reply. Yeah. That's how I'm updating the quantity (/www.clover.com/inventory/). After updating the quantity, I assumed it would send some data to my PHP file (westcoastboatzincs.com/test.php), and I also assumed I would be able to grab that data through the $_GET or $_REQUEST param, but it seems like it never contacts the PHP file.

Have you possibly set up a webhook before? Could I take a look at your code? I might just need to see an example of how to get the data. Thanks for the help!

0 Likes 0 ·
Show more comments
davidrichied avatar image
davidrichied answered
Hi @Sam Thanks for the suggestion to use requestb.in! I set up a request bin and got a verification, but if I update the quantity of an inventory item, nothing else happens. Here's the URL to the bin: https://requestb.in/rpf2v5rp?inspect I must be overlooking a something in Clover. Do you know if there are any other steps I should take besides subscribing to events? Do I have to set an application URL, application ID, or use the app secret somewhere? Does manually updating an inventory item via the web app ( https://www.clover.com/inventory) possibly not fire off a webhook request? Thanks, again for the help.
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community