@@ -13,6 +13,13 @@ def valid_notification_hmac?(notification_request_item, hmac_key)
1313 valid_webhook_hmac? ( notification_request_item , hmac_key )
1414 end
1515
16+ # validates the HMAC signature of the NotificationRequestItem object. Use for webhooks that provide the
17+ # hmacSignature as part of the payload `AdditionalData` (i.e. Payments)
18+ #
19+ # @param webhook_request_item [Object] The webhook request item.
20+ # @param hmac_key [String] The HMAC key used to validate the payload.
21+
22+ # @return [Boolean] Returns true if the HMAC signature is valid, otherwise false.
1623 def valid_webhook_hmac? ( webhook_request_item , hmac_key )
1724 expected_sign = calculate_webhook_hmac ( webhook_request_item , hmac_key )
1825 merchant_sign =
@@ -21,12 +28,32 @@ def valid_webhook_hmac?(webhook_request_item, hmac_key)
2128 expected_sign == merchant_sign
2229 end
2330
31+ # validates the HMAC signature of a payload against an expected signature. Use for webhooks that provide the
32+ # hmacSignature in the HTTP header (i.e. Banking, Management API)
33+ #
34+ # @param hmac_signature [String] The HMAC signature to validate.
35+ # @param hmac_key [String] The HMAC key used to validate the payload.
36+ # @param payload [String] The webhook payload.
37+
38+ # @return [Boolean] Returns true if the HMAC signature is valid, otherwise false.
39+ def valid_webhook_payload_hmac? ( hmac_signature , hmac_key , payload )
40+ expected_sign = calculate_webhook_payload_hmac ( payload , hmac_key )
41+ puts ( expected_sign )
42+ expected_sign == hmac_signature
43+ end
44+
2445 # <b>DEPRECATED:</b> Please use calculate_webhook_hmac() instead.
2546 def calculate_notification_hmac ( notification_request_item , hmac_key )
2647 calculate_webhook_hmac ( notification_request_item , hmac_key )
2748 end
2849
2950
51+ def calculate_webhook_payload_hmac ( data , hmac_key )
52+ Base64 . strict_encode64 (
53+ OpenSSL ::HMAC . digest ( HMAC_ALGORITHM , [ hmac_key ] . pack ( 'H*' ) , data )
54+ )
55+ end
56+
3057 def calculate_webhook_hmac ( webhook_request_item , hmac_key )
3158 data = data_to_sign ( webhook_request_item )
3259
@@ -35,6 +62,7 @@ def calculate_webhook_hmac(webhook_request_item, hmac_key)
3562 )
3663 end
3764
65+
3866 # TODO: Deprecate instead of aliasing
3967 alias valid_notification_hmac? valid_webhook_hmac?
4068 alias calculate_notification_hmac calculate_webhook_hmac
0 commit comments