Webhooks
Stripe's CLI was installed locally. One terminal was given the listen command which was forwarded to a URL on my local machine:
steve:~$ stripe listen --forward-to localhost/steve/code-examples/stripe-payment/webhooks/log-events.php
> Ready! Your webhook signing secret is whsec_6EyW27XX4GmjjDHKrHdAkCZfM9zfhgBA (^C to quit)
On another terminal events were triggered:
steve:~$ stripe trigger payment_intent.succeeded
Setting up fixture for: payment_intent
Trigger succeeded! Check dashboard for event details.
There should be a file at the forwarding address that returns an http 200 response. If there is not you get something like this in the listening terminal:
2020-07-14 22:09:50 --> payment_intent.created [evt_1H4vNqGmidqBA55lfg4V9imU]
2020-07-14 22:09:50 <-- [404] POST http://localhost/steve/stripe/webhooks/ [evt_1H4vNqGmidqBA55lfg4V9imU]
A file (webhooks/log-events at the forwarding address was created which returns a 200 http response and which also writes to an sqlite database. The code in the file is shown here:
<?php
http_response_code(200);
$msg = "x";
$dsn = 'sqlite:/home/steve/Public/steve/code-examples/stripe-payment/webhooks/webhook.db';
try {
$db = new PDO($dsn);
} catch (Exception $e) {
$error = $e->getMessage();
}
// $db = new PDO($dsn);
$sql = "INSERT INTO webhooks (column1) VALUES (:msg)";
$stmt = $db->prepare($sql);
$stmt->bindParam(':msg', $msg);
$stmt->execute();
Now when we trigger an event using the Stripe CLI we find that our SQLite database has rows inserted into it: one for each event. I now need to work on extracting the event data from the JSON sent by the trigger and storing that in the SQLite database.