Skip to content

Instantly share code, notes, and snippets.

@ibrahimlawal
Last active June 22, 2022 22:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ibrahimlawal/2f987cccfa87f7ec4cb3dcb40e61dedd to your computer and use it in GitHub Desktop.
Save ibrahimlawal/2f987cccfa87f7ec4cb3dcb40e61dedd to your computer and use it in GitHub Desktop.
Paystack Webhook Code Skeleton, PHP
<?php
if ((strtoupper($_SERVER['REQUEST_METHOD']) != 'POST' ) || !array_key_exists('HTTP_X_PAYSTACK_SIGNATURE', $_SERVER) ) {
// only a post with paystack signature header gets our attention
exit();
}
// Retrieve the request's body
$input = @file_get_contents("php://input");
define('PAYSTACK_SECRET_KEY','sk_xxxx_xxxxxx');
if(!$_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] || ($_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] !== hash_hmac('sha512', $input, PAYSTACK_SECRET_KEY))){
// silently forget this ever happened
exit();
}
http_response_code(200);
// parse event (which is json string) as object
// Do something - that will not take long - with $event
$event = json_decode($input);
switch($event->event){
// subscription.create
case 'subscription.create':
break;
// charge.success
case 'charge.success':
break;
// subscription.disable
case 'subscription.disable':
break;
// invoice.create and invoice.update
case 'invoice.create':
case 'invoice.update':
break;
}
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment