File tree Expand file tree Collapse file tree 5 files changed +45
-22
lines changed Expand file tree Collapse file tree 5 files changed +45
-22
lines changed Original file line number Diff line number Diff line change 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];
Original file line number Diff line number Diff 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
409409Laravel paystack provides you a predefined endpoint that listens to and validates incoming paystack's webhook events.
410410It 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 )
Original file line number Diff line number Diff line change 44
55use Myckhel \Paystack \Events \Hook ;
66use Illuminate \Http \Request ;
7- use Myckhel \Paystack \Traits \PaystackConfig ;
87
98class 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 ];
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 33namespace Myckhel \Paystack ;
44
55use Illuminate \Support \ServiceProvider ;
6+ use Myckhel \Paystack \Http \Middleware \ValidatePaystackHook ;
67use Myckhel \Paystack \Http \Middleware \DisabledRoute ;
78
89class 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 (
You can’t perform that action at this time.
0 commit comments