@@ -17,17 +17,16 @@ abstract class WebhookSignature
1717 * @param array $header the contents of the signature header sent by
1818 * Tap
1919 * @param string $secret secret used to generate the signature
20- * @param int $tolerance maximum difference allowed between the header's
2120 * timestamp and the current time
2221 *
2322 * @return bool
2423 * @throws SignatureVerificationException if the verification fails
2524 *
2625 */
27- public static function verifyHeader ($ payload , $ header , $ secret , $ tolerance = null )
26+ public static function verifyHeader (array $ payload , array $ header , string $ secret ): bool
2827 {
2928 // Extract timestamp and signatures from header
30- $ signature = self ::getSignature ($ header, self :: EXPECTED_SCHEME );
29+ $ signature = self ::getSignature ($ header );
3130
3231 if (empty ($ signature )) {
3332 throw SignatureVerificationException::factory (
@@ -46,7 +45,7 @@ public static function verifyHeader($payload, $header, $secret, $tolerance = nul
4645 $ signatureFound = true ;
4746 }
4847
49- if (!$ signatureFound ) {
48+ if (! $ signatureFound ) {
5049 throw SignatureVerificationException::factory (
5150 'No signatures found matching the expected signature for payload ' , $ payload , $ header , $ expectedSignature , $ signature
5251 );
@@ -59,16 +58,15 @@ public static function verifyHeader($payload, $header, $secret, $tolerance = nul
5958 * Extracts the signatures matching a given scheme in a signature header.
6059 *
6160 * @param array $header the signature header
62- * @param string $scheme the signature scheme to look for
6361 *
6462 * @return string the signature matching the provided scheme
6563 */
66- private static function getSignature ($ header , $ scheme )
64+ private static function getSignature (array $ header ): string
6765 {
6866 $ signature = '' ;
6967
7068 foreach ($ header as $ key => $ value ) {
71- if ($ key === $ scheme ) {
69+ if ($ key === self :: EXPECTED_SCHEME ) {
7270 $ signature = $ value [0 ];
7371 }
7472 }
@@ -86,7 +84,7 @@ private static function getSignature($header, $scheme)
8684 *
8785 * @return string the signature as a string
8886 */
89- private static function computeSignature ($ payload , $ secret )
87+ private static function computeSignature (array $ payload , string $ secret ): string
9088 {
9189 return hash_hmac ('sha256 ' , self ::generateSignature ($ payload ), $ secret );
9290 }
@@ -95,24 +93,24 @@ private static function computeSignature($payload, $secret)
9593 * @param array $payload
9694 * @return string
9795 */
98- private static function generateSignature ($ payload )
96+ private static function generateSignature (array $ payload ): string
9997 {
10098 $ object = $ payload ['object ' ];
10199
102100 $ id = $ payload ['id ' ];
103101 $ currency = $ payload ['currency ' ];
104- $ amount = number_format ($ payload ['amount ' ], $ currency == 'KWD ' ? 3 : 2 );
102+ $ amount = number_format ($ payload ['amount ' ], $ currency == 'KWD ' ? 3 : 2 , ' . ' , '' );
105103 $ gateway_reference = $ payload ['reference ' ]['gateway ' ];
106104 $ payment_reference = $ payload ['reference ' ]['payment ' ];
107105 $ updated = $ payload ['updated ' ] ?? null ;
108106 $ status = $ payload ['status ' ];
109107 $ created = $ payload ['transaction ' ]['created ' ];
110108
111109 if ($ object === 'invoice ' ) {
112- $ toBeHashedString = 'x_id ' . $ id . 'x_amount ' . $ amount . 'x_currency ' . $ currency . 'x_updated ' . $ updated . 'x_status ' . $ status . 'x_created ' . $ created . '' ;
110+ $ toBeHashedString = 'x_id ' . $ id. 'x_amount ' . $ amount. 'x_currency ' . $ currency. 'x_updated ' . $ updated. 'x_status ' . $ status. 'x_created ' . $ created. '' ;
113111 } else {
114112 // Charge or Authorize - Create a hashstring from the posted response data + the data that are related to you.
115- $ toBeHashedString = 'x_id ' . $ id . 'x_amount ' . $ amount . 'x_currency ' . $ currency . 'x_gateway_reference ' . $ gateway_reference . 'x_payment_reference ' . $ payment_reference . 'x_status ' . $ status . 'x_created ' . $ created . '' ;
113+ $ toBeHashedString = 'x_id ' . $ id. 'x_amount ' . $ amount. 'x_currency ' . $ currency. 'x_gateway_reference ' . $ gateway_reference. 'x_payment_reference ' . $ payment_reference. 'x_status ' . $ status. 'x_created ' . $ created. '' ;
116114 }
117115
118116 return $ toBeHashedString ;
0 commit comments