@@ -13,6 +13,13 @@ def valid_notification_hmac?(notification_request_item, hmac_key)
13
13
valid_webhook_hmac? ( notification_request_item , hmac_key )
14
14
end
15
15
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.
16
23
def valid_webhook_hmac? ( webhook_request_item , hmac_key )
17
24
expected_sign = calculate_webhook_hmac ( webhook_request_item , hmac_key )
18
25
merchant_sign =
@@ -21,12 +28,32 @@ def valid_webhook_hmac?(webhook_request_item, hmac_key)
21
28
expected_sign == merchant_sign
22
29
end
23
30
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
+
24
45
# <b>DEPRECATED:</b> Please use calculate_webhook_hmac() instead.
25
46
def calculate_notification_hmac ( notification_request_item , hmac_key )
26
47
calculate_webhook_hmac ( notification_request_item , hmac_key )
27
48
end
28
49
29
50
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
+
30
57
def calculate_webhook_hmac ( webhook_request_item , hmac_key )
31
58
data = data_to_sign ( webhook_request_item )
32
59
@@ -35,6 +62,7 @@ def calculate_webhook_hmac(webhook_request_item, hmac_key)
35
62
)
36
63
end
37
64
65
+
38
66
# TODO: Deprecate instead of aliasing
39
67
alias valid_notification_hmac? valid_webhook_hmac?
40
68
alias calculate_notification_hmac calculate_webhook_hmac
0 commit comments