paystack-webhook-signature.php 894 B

123456789101112131415161718192021222324
  1. <?php
  2. require __DIR__ . '/../../helpers/PayStack.php';
  3. use common\helpers\PayStack;
  4. $body = '{"event":"charge.success","data":{"reference":"OS123"}}';
  5. $secret = 'standalone-test-secret';
  6. $signature = hash_hmac('sha512', $body, $secret);
  7. $checks = [
  8. 'accepts the matching SHA512 signature' => PayStack::verifyWebhookSignature($body, $signature, $secret),
  9. 'rejects a different signature' => !PayStack::verifyWebhookSignature($body, str_repeat('0', 128), $secret),
  10. 'rejects an empty signature' => !PayStack::verifyWebhookSignature($body, '', $secret),
  11. 'signature binds to the raw request body' => !PayStack::verifyWebhookSignature($body . ' ', $signature, $secret),
  12. ];
  13. foreach ($checks as $description => $passed) {
  14. if (!$passed) {
  15. fwrite(STDERR, "FAIL: {$description}\n");
  16. exit(1);
  17. }
  18. }
  19. echo 'PASS: ' . count($checks) . " PayStack signature checks\n";