Skip to content

Commit 47f9a07

Browse files
authored
Created Validate Paystack Hook Middleware (#41)
1 parent 4d75e99 commit 47f9a07

File tree

5 files changed

+45
-22
lines changed

5 files changed

+45
-22
lines changed

config/paystack.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"merchant_email" => env("PAYSTACK_MERCHANT_EMAIL"),
88

99
"route" => [
10-
"middleware" => ['paystack_route_disabled', 'api'], // For injecting middleware to the package's routes
11-
"prefix" => 'api', // For injecting middleware to the package's routes
12-
'hook_middleware' => ['paystack_route_disabled', 'api']
10+
"middleware" => ["paystack_route_disabled", "api"], // For injecting middleware to the package's routes
11+
"prefix" => "api", // For injecting middleware to the package's routes
12+
"hook_middleware" => ["validate_paystack_hook", "api"]
1313
],
1414
];

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ return [
3838
"route" => [
3939
"middleware" => ["paystack_route_disabled", "api"], // For injecting middleware to the package's routes
4040
"prefix" => "api", // For injecting middleware to the package's routes
41-
"hook_middleware" => ["paystack_route_disabled", "api"]
41+
"hook_middleware" => ["validate_paystack_hook", "api"]
4242
],
4343
];
4444
```
@@ -408,6 +408,7 @@ Miscellaneous::listStates($params);
408408
### Using WebHook route
409409
Laravel paystack provides you a predefined endpoint that listens to and validates incoming paystack's webhook events.
410410
It emits `Myckhel\Paystack\Events\Hook` on every incoming hooks which could be listened to.
411+
The hook request is validated with `validate_paystack_hook` middleware by using the paystack's config `secret_key` against the incoming request.
411412

412413
## Setup Paystack Webhook
413414
[Check official page to read more about paystack webhook](https://paystack.com/docs/payments/webhooks/#introduction)

src/Http/Controllers/HookController.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,11 @@
44

55
use Myckhel\Paystack\Events\Hook;
66
use Illuminate\Http\Request;
7-
use Myckhel\Paystack\Traits\PaystackConfig;
87

98
class HookController extends Controller
109
{
11-
use PaystackConfig;
12-
1310
public function hook(Request $request)
1411
{
15-
$signature = $request->header('x-paystack-signature');
16-
if (!$signature) {
17-
abort(403);
18-
}
19-
20-
$signingSecret = $this->config('secret_key');
21-
22-
if (empty($signingSecret)) {
23-
abort(403, 'Signing Secret Not Set');
24-
}
25-
26-
$computedSignature = hash_hmac('sha512', $request->getContent(), $signingSecret);
27-
28-
if (!hash_equals($signature, $computedSignature)) return abort(403);
29-
3012
event(new Hook($request->all()));
3113

3214
return ['status' => true];
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Myckhel\Paystack\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Myckhel\Paystack\Traits\PaystackConfig;
8+
9+
class ValidatePaystackHook
10+
{
11+
use PaystackConfig;
12+
/**
13+
* Handle an incoming request.
14+
*
15+
* @param \Illuminate\Http\Request $request
16+
* @param \Closure $next
17+
* @return mixed
18+
*/
19+
public function handle(Request $request, Closure $next)
20+
{
21+
$signature = $request->header('x-paystack-signature');
22+
if (!$signature) {
23+
abort(403, 'Signature header not found');
24+
}
25+
26+
$signingSecret = $this->config('secret_key');
27+
28+
if (empty($signingSecret)) {
29+
abort(403, 'Signing Secret Not Set');
30+
}
31+
32+
$computedSignature = hash_hmac('sha512', $request->getContent(), $signingSecret);
33+
34+
if (!hash_equals($signature, $computedSignature)) return abort(403, "Invalid Secret Signature");
35+
36+
return $next($request);
37+
}
38+
}

src/PaystackServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Myckhel\Paystack;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Myckhel\Paystack\Http\Middleware\ValidatePaystackHook;
67
use Myckhel\Paystack\Http\Middleware\DisabledRoute;
78

89
class PaystackServiceProvider extends ServiceProvider
@@ -19,6 +20,7 @@ public function register()
1920
$this->mergeConfigFrom(__DIR__ . '/../config/paystack.php', 'paystack');
2021

2122
$this->app['router']->aliasMiddleware('paystack_route_disabled', DisabledRoute::class);
23+
$this->app['router']->aliasMiddleware('validate_paystack_hook', ValidatePaystackHook::class);
2224

2325
// Register the service the package provides.
2426
$this->app->singleton(

0 commit comments

Comments
 (0)