@@ -43,24 +43,21 @@ class AuthorizeNet extends PluginAbstract
4343 /**
4444 * Validate ANet webhook signature (HMAC-SHA512).
4545 *
46+ * The Signature Key is used as a raw ASCII string in the HMAC — not hex-decoded.
47+ *
4648 * @param string $rawBody
4749 * @param array $headers
48- * @param string $signatureKeyHex
49- * @return array{valid: bool,expected:string,received:string}
50+ * @param string $signatureKey
51+ * @return bool
5052 */
51- public static function verifySignature (string $ rawBody , array $ headers , string $ signatureKeyHex ): array
53+ public static function verifySignature (string $ rawBody , array $ headers , string $ signatureKey ): bool
5254 {
5355 $ received = $ headers ['X-ANET-Signature ' ] ?? ($ headers ['x-anet-signature ' ] ?? '' );
54- if (empty ($ signatureKeyHex ) || ! ctype_xdigit ( $ signatureKeyHex ) || empty ($ received )) {
55- return [ ' valid ' => false , ' expected ' => '' , ' received ' => $ received ] ;
56+ if (empty ($ signatureKey ) || empty ($ received )) {
57+ return false ;
5658 }
57- $ keyBin = hex2bin ($ signatureKeyHex );
58- $ expected = 'sha512= ' . hash_hmac ('sha512 ' , $ rawBody , $ keyBin );
59- return [
60- 'valid ' => hash_equals ($ expected , $ received ),
61- 'expected ' => $ expected ,
62- 'received ' => $ received
63- ];
59+ $ expected = 'sha512= ' . strtolower (hash_hmac ('sha512 ' , $ rawBody , $ signatureKey ));
60+ return hash_equals ($ expected , strtolower ($ received ));
6461 }
6562
6663 /**
@@ -86,22 +83,17 @@ public static function verifySignature(string $rawBody, array $headers, string $
8683 */
8784 public static function parseWebhookRequest (string $ rawBody , array $ headers , array $ allowedEvents = ['net.authorize.payment.authcapture.created ' ]): array
8885 {
89- $ cfg = self ::getConfig ();
90- $ sig = self ::verifySignature ($ rawBody , $ headers , trim ($ cfg ->signatureKey ?? '' ));
86+ $ cfg = self ::getConfig ();
87+ $ sigValid = self ::verifySignature ($ rawBody , $ headers , trim (( string )( $ cfg ->signatureKey ?? '' ) ));
9188
9289 $ json = json_decode ($ rawBody , true );
9390 if (!is_array ($ json )) {
94- return ['error ' => true , 'msg ' => 'Invalid JSON ' , 'signatureValid ' => $ sig [ ' valid ' ] ];
91+ return ['error ' => true , 'msg ' => 'Invalid JSON ' , 'signatureValid ' => $ sigValid ];
9592 }
9693
97- $ eventType = $ json ['eventType ' ] ?? '' ;
94+ $ eventType = $ json ['eventType ' ] ?? '' ;
9895 if (!in_array ($ eventType , $ allowedEvents )) {
99- return [
100- 'error ' => true ,
101- 'msg ' => 'Ignored event type ' ,
102- 'eventType ' => $ eventType ,
103- 'signatureValid ' => $ sig ['valid ' ]
104- ];
96+ return ['error ' => true , 'msg ' => 'Ignored event type ' , 'eventType ' => $ eventType , 'signatureValid ' => $ sigValid ];
10597 }
10698
10799 $ payload = $ json ['payload ' ] ?? [];
@@ -110,21 +102,20 @@ public static function parseWebhookRequest(string $rawBody, array $headers, arra
110102 $ currency = $ payload ['currencyCode ' ] ?? ($ payload ['currency ' ] ?? null );
111103 $ metadata = $ payload ['metadata ' ] ?? [];
112104 $ users_id = isset ($ metadata ['users_id ' ]) ? (int )$ metadata ['users_id ' ] : null ;
113-
114- $ uniq_key = sha1 ($ eventType . ($ transactionId ?? 'no-txn ' ));
105+ $ uniq_key = sha1 ($ eventType . ($ transactionId ?? 'no-txn ' ));
115106
116107 return [
117- 'error ' => false ,
118- 'data ' => $ json ,
119- 'payload ' => $ payload ,
120- 'eventType ' => $ eventType ,
121- 'transactionId ' => $ transactionId ,
122- 'amount ' => $ amount ,
123- 'currency ' => $ currency ,
124- 'metadata ' => $ metadata ,
125- 'users_id ' => $ users_id ,
126- 'uniq_key ' => $ uniq_key ,
127- 'signatureValid ' => $ sig [ ' valid ' ]
108+ 'error ' => false ,
109+ 'data ' => $ json ,
110+ 'payload ' => $ payload ,
111+ 'eventType ' => $ eventType ,
112+ 'transactionId ' => $ transactionId ,
113+ 'amount ' => $ amount ,
114+ 'currency ' => $ currency ,
115+ 'metadata ' => $ metadata ,
116+ 'users_id ' => $ users_id ,
117+ 'uniq_key ' => $ uniq_key ,
118+ 'signatureValid ' => $ sigValid ,
128119 ];
129120 }
130121
@@ -1024,7 +1015,7 @@ public static function getTransactionDetails(string $transactionId): array
10241015 $ submitTime = method_exists ($ txn , 'getSubmitTimeUTC ' ) ? $ txn ->getSubmitTimeUTC () : null ;
10251016 $ responseCode = $ txn ->getResponseCode () ?? '' ;
10261017 $ status = $ txn ->getTransactionStatus () ?? '' ;
1027- $ isApproved = $ responseCode == 1 && in_array ($ status , ['capturedPendingSettlement ' , 'settledSuccessfully ' ], true );
1018+ $ isApproved = $ responseCode == 1 && in_array (strtolower ( $ status) , ['capturedpendingsettlement ' , 'settledsuccessfully ' ], true );
10281019
10291020 // ---- NEW: get description and decode metadata ----
10301021 $ orderDescription = ($ order && method_exists ($ order , 'getDescription ' )) ? (string )$ order ->getDescription () : null ;
0 commit comments