Skip to content

Instantly share code, notes, and snippets.

@jhauraw
Last active August 29, 2015 13:57
Show Gist options
  • Save jhauraw/9924877 to your computer and use it in GitHub Desktop.
Save jhauraw/9924877 to your computer and use it in GitHub Desktop.
Stripe Webhook PHP Example
<?php
// You can save this file as index.php and make it available in a public webhook directory in your app:
// http://example.com/webhooks/stripe/index.php
// In your Stripe Dashboard -> Webhooks page, add a LIVE and/or TEST URL:
// http://example.com/webhooks/stripe/
// Load the Stripe library
require_once 'lib/Stripe.php';
// Set your LIVE or TEST secret key
Stripe::setApiKey('YOUR_STRIPE_SECRET_KEY');
// Retrieve the request's body
$body = @file_get_contents('php://input');
if (!$body) return;
// Parse $body as JSON
$event_json = json_decode($body);
if (!$event_json) return;
// Quietly exit out if incomplete data
if ($event_json->object != 'event') return;
if (!isset($event_json->id)) return;
$event_id = $event_json->id;
try {
// Retrieve authentic event directly from Stripe
$event = Stripe_Event::retrieve($event_id);
} catch (Exception $e) {
$err_msg = 'Error: Stripe Webhook. Event id: ' . $event_id . '. ' . $e;
// Additionally, send email if needed
error_log($err_msg, 0);
}
// Match up each event type with a function and pass it the event
switch ($event->type) {
case 'account.updated':
account_updated($event);
break;
// Add more 'case' clauses as necessary...
default:
break;
}
// Example function for account.updated event
function account_updated($event)
{
// function code...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment