Skip to content

Commit

Permalink
Created Validate Paystack Hook Middleware (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
myckhel authored Jul 24, 2022
1 parent 4d75e99 commit 47f9a07
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
6 changes: 3 additions & 3 deletions config/paystack.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"merchant_email" => env("PAYSTACK_MERCHANT_EMAIL"),

"route" => [
"middleware" => ['paystack_route_disabled', 'api'], // For injecting middleware to the package's routes
"prefix" => 'api', // For injecting middleware to the package's routes
'hook_middleware' => ['paystack_route_disabled', 'api']
"middleware" => ["paystack_route_disabled", "api"], // For injecting middleware to the package's routes
"prefix" => "api", // For injecting middleware to the package's routes
"hook_middleware" => ["validate_paystack_hook", "api"]
],
];
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ return [
"route" => [
"middleware" => ["paystack_route_disabled", "api"], // For injecting middleware to the package's routes
"prefix" => "api", // For injecting middleware to the package's routes
"hook_middleware" => ["paystack_route_disabled", "api"]
"hook_middleware" => ["validate_paystack_hook", "api"]
],
];
```
Expand Down Expand Up @@ -408,6 +408,7 @@ Miscellaneous::listStates($params);
### Using WebHook route
Laravel paystack provides you a predefined endpoint that listens to and validates incoming paystack's webhook events.
It emits `Myckhel\Paystack\Events\Hook` on every incoming hooks which could be listened to.
The hook request is validated with `validate_paystack_hook` middleware by using the paystack's config `secret_key` against the incoming request.

## Setup Paystack Webhook
[Check official page to read more about paystack webhook](https://paystack.com/docs/payments/webhooks/#introduction)
Expand Down
18 changes: 0 additions & 18 deletions src/Http/Controllers/HookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,11 @@

use Myckhel\Paystack\Events\Hook;
use Illuminate\Http\Request;
use Myckhel\Paystack\Traits\PaystackConfig;

class HookController extends Controller
{
use PaystackConfig;

public function hook(Request $request)
{
$signature = $request->header('x-paystack-signature');
if (!$signature) {
abort(403);
}

$signingSecret = $this->config('secret_key');

if (empty($signingSecret)) {
abort(403, 'Signing Secret Not Set');
}

$computedSignature = hash_hmac('sha512', $request->getContent(), $signingSecret);

if (!hash_equals($signature, $computedSignature)) return abort(403);

event(new Hook($request->all()));

return ['status' => true];
Expand Down
38 changes: 38 additions & 0 deletions src/Http/Middleware/ValidatePaystackHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Myckhel\Paystack\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Myckhel\Paystack\Traits\PaystackConfig;

class ValidatePaystackHook
{
use PaystackConfig;
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$signature = $request->header('x-paystack-signature');
if (!$signature) {
abort(403, 'Signature header not found');
}

$signingSecret = $this->config('secret_key');

if (empty($signingSecret)) {
abort(403, 'Signing Secret Not Set');
}

$computedSignature = hash_hmac('sha512', $request->getContent(), $signingSecret);

if (!hash_equals($signature, $computedSignature)) return abort(403, "Invalid Secret Signature");

return $next($request);
}
}
2 changes: 2 additions & 0 deletions src/PaystackServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Myckhel\Paystack;

use Illuminate\Support\ServiceProvider;
use Myckhel\Paystack\Http\Middleware\ValidatePaystackHook;
use Myckhel\Paystack\Http\Middleware\DisabledRoute;

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

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

// Register the service the package provides.
$this->app->singleton(
Expand Down

0 comments on commit 47f9a07

Please sign in to comment.