Module: PaymobAccept::Hmac

Defined in:
lib/paymob_accept/hmac.rb

Constant Summary collapse

FILTERED_TRANSACTION_KEYS =
%w[amount_cents created_at currency error_occured has_parent_transaction id
integration_id is_3d_secure is_auth is_capture is_refunded is_standalone_payment
is_voided order.id owner
pending source_data.pan source_data.sub_type source_data.type success].freeze
FILTERED_TOKEN_KEYS =
%w[card_subtype created_at email id masked_pan merchant_id order_id token].freeze

Class Method Summary collapse

Class Method Details

.validate(paymob_response:, hmac_key: PaymobAccept.configuration.hmac_key) ⇒ Object



12
13
14
# File 'lib/paymob_accept/hmac.rb', line 12

def validate(paymob_response:, hmac_key: PaymobAccept.configuration.hmac_key)
  validate_transaction?(paymob_response: paymob_response, hmac_key: hmac_key) || validate_token?(paymob_response: paymob_response, hmac_key: hmac_key)
end

.validate_token?(paymob_response:, hmac_key: PaymobAccept.configuration.hmac_key) ⇒ Boolean



25
26
27
28
29
30
31
32
# File 'lib/paymob_accept/hmac.rb', line 25

def validate_token?(paymob_response:, hmac_key: PaymobAccept.configuration.hmac_key)
  digest = OpenSSL::Digest.new('sha512')
  concatenated_str = FILTERED_TOKEN_KEYS.map do |element|
    paymob_response.dig('obj', *element.split('.'))
  end.join
  secure_hash = OpenSSL::HMAC.hexdigest(digest, hmac_key, concatenated_str)
  secure_hash == paymob_response['hmac']
end

.validate_transaction?(paymob_response:, hmac_key: PaymobAccept.configuration.hmac_key) ⇒ Boolean



16
17
18
19
20
21
22
23
# File 'lib/paymob_accept/hmac.rb', line 16

def validate_transaction?(paymob_response:, hmac_key: PaymobAccept.configuration.hmac_key)
  digest = OpenSSL::Digest.new('sha512')
  concatenated_str = FILTERED_TRANSACTION_KEYS.map do |element|
    paymob_response.dig('obj', *element.split('.'))
  end.join
  secure_hash = OpenSSL::HMAC.hexdigest(digest, hmac_key, concatenated_str)
  secure_hash == paymob_response['hmac']
end