Last active
June 22, 2022 22:17
-
-
Save ibrahimlawal/2f987cccfa87f7ec4cb3dcb40e61dedd to your computer and use it in GitHub Desktop.
Paystack Webhook Code Skeleton, PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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