Webhooks Two

Here we have a Checkout button to buy a product (a Gnu). The product was created on the Stripe Dashboard of my account. Auto generated code from the Prices section of the product has been pasted below which generates the button and the code to enable the button to link to a Stripe hosted checkout page from which the user can pay for the product. The only part of the code I have altered is the content of the button element which I changed from "Checkout" to "Buy a Gnu".

The Gnu product created in my Stripe Account is a test product. It gives rise to a test checkout procedure so no money is involved. After making a test payment details about the payment are recorded in the Pyaments section of my Stripe Account. This may be all that is required but it could also be useful for the details about the payment to be returned to the webserver that initiated the transaction where they can be handled for whatever purpose. We will try and implement this webhook process and store the returned details about the payment in an SQLite database.

Within your Stripe Account you can add an endpoint. This means that you can arrange for particular types of events to be sent by http to a particular file (or endpoint) on your server. The trail from the Stripe Dashboard home page is: Home -> Developers -> Webhooks -> Add endpoint. Then you type in the URL of the script on your site you want to receive the event(s) and then you select what types of events you want sent there. It looks like if you choose an event type then all events of that type that occur on your Stripe Account will be sent. It does not look as if you can choose for only events of that type relating to a particular product can be sent. Therefore making this distinction will need to be applied by the script you provide at the endpoint on your server. For this reason I have put the endpoint script directly inside the stripe-payment directory rather than inside a directory relating to a specific Stripe Product.

At this point I have the following code inside stripe-payment/webhook-catcher/index.php:

  $msg = "x";
  $dsn = 'sqlite:/srv/stevespages.org.uk/public/htdocs/code-examples/stripe-payment/webhook-catcher/webhook.db';
  try {
  $db = new PDO($dsn);
  } catch (Exception $e) {
  $error = $e->getMessage();
  $sql = "INSERT INTO webhooks (msg) VALUES (:msg)";
  $stmt = $db->prepare($sql);
  $stmt->bindParam(':msg', $msg);

So, now if I open stevespages.org.uk/code-examples/stripe-payment/webhook-catcher/ in a browser or if I click on the "Buy a Gnu" button and make a test payment to Stripe or if I send a test webhook from my Stripe Account then I get a new row in the webhooks table. Now I want to modify the table so it has columns which relate to the event properties Stripe is sending in the webhook and write the PHP code that will insert the values of the properties in the event into the appropriate columns of the table.

In order to do this I will use Composer to download the Stripe API into my project directory,