Skip to content

Commit 00982e6

Browse files
committed
refactored DefaultSignatureValidator.php to handle all supported gateways webhook signature validation
1 parent ad62f8a commit 00982e6

File tree

6 files changed

+37
-74
lines changed

6 files changed

+37
-74
lines changed

config/multipayment-gateways.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
*/
3434
'name' => 'stripe',
3535

36+
/**
37+
* When set to false, the package will not verify the signature of the webhook call.
38+
*/
39+
'verify_signature' => true,
40+
3641
/*
3742
* This secret key is used to validate the signature of the webhook call.
3843
*/
@@ -82,6 +87,11 @@
8287
*/
8388
'name' => 'paystack',
8489

90+
/**
91+
* When set to false, the package will not verify the signature of the webhook call.
92+
*/
93+
'verify_signature' => true,
94+
8595
/*
8696
* This secret key is used to validate the signature of the webhook call.
8797
*/
@@ -131,6 +141,11 @@
131141
*/
132142
'name' => 'flutterwave',
133143

144+
/**
145+
* When set to false, the package will not verify the signature of the webhook call.
146+
*/
147+
'verify_signature' => true,
148+
134149
/*
135150
* This secret key is used to validate the signature of the webhook call.
136151
*/

src/Services/PaymentWebhookConfig.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class PaymentWebhookConfig
2222

2323
public string $paymentWebhookHandler;
2424

25+
public string $verify_signature;
26+
2527
public string|ProcessPaymentWebhookJob $paymentWebhookJobClass;
2628

2729
public string|PaymentWebhookReceivedEvent $paymentWebhookEventClass;
@@ -33,6 +35,8 @@ public function __construct(array $properties)
3335
{
3436
$this->name = $properties['name'];
3537

38+
$this->verify_signature = $properties['verify_signature'];
39+
3640
$this->signingSecret = $properties['signing_secret'] ?? '';
3741

3842
$this->signatureHeaderName = $properties['signature_header_name'] ?? '';

src/SignatureValidator/DefaultSignatureValidator.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ class DefaultSignatureValidator implements PaymentWebhookSignatureValidator
99
{
1010
public function isValid(Request $request, PaymentWebhookConfig $config): bool
1111
{
12-
return true;
12+
if (! config(key: $config->verify_signature)) return true;
13+
14+
if ((! $request->isMethod(method: 'post')) || ! $request->header(key: $config->signatureHeaderName)) return false;
15+
16+
$signature = $this->validateSignature(gatewayName: $config->name, requestContent: $request->getContent(), signingSecret: $config->signingSecret);
17+
18+
if ($signature !== $request->header(key: $config->signatureHeaderName)) return false;
19+
20+
return hash_equals(known_string: $signature, user_string: $request->header(key: $config->signatureHeaderName));
21+
}
22+
23+
private function validateSignature($gatewayName, $requestContent, $signingSecret): string
24+
{
25+
// @phpstan-ignore-next-line
26+
return match ($gatewayName) {
27+
'paystack' => hash_hmac(algo: 'sha512', data: $requestContent, key: $signingSecret),
28+
'stripe', 'flutterwave' => hash_hmac(algo: 'sha256', data: $requestContent, key: $signingSecret),
29+
};
1330
}
1431
}

src/SignatureValidator/FlutterwaveSignatureValidator.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/SignatureValidator/PaystackSignatureValidator.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/SignatureValidator/StripeSignatureValidator.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)