Class: GooglePayRuby::GooglePaymentMethodTokenContext

Inherits:
Object
  • Object
show all
Defined in:
lib/google_pay_ruby/google_payment_method_token_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ GooglePaymentMethodTokenContext

Returns a new instance of GooglePaymentMethodTokenContext.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/google_pay_ruby/google_payment_method_token_context.rb', line 7

def initialize(options)
  @merchants = options[:merchants] || []
  
  if @merchants.empty?
    raise GooglePaymentDecryptionError.new(
      'No merchant configuration provided for decryption context.'
    )
  end
  
  validate_merchant_configurations!
end

Instance Attribute Details

#merchantsObject (readonly)

Returns the value of attribute merchants.



5
6
7
# File 'lib/google_pay_ruby/google_payment_method_token_context.rb', line 5

def merchants
  @merchants
end

Instance Method Details

#decrypt(token) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/google_pay_ruby/google_payment_method_token_context.rb', line 19

def decrypt(token)
  protocol_version = token['protocolVersion'] || token[:protocolVersion]
  unless protocol_version == 'ECv2'
    raise GooglePaymentDecryptionError.new(
      "Unsupported decryption for protocol version #{protocol_version}"
    )
  end

  errors = []
  signed_message = token['signedMessage'] || token[:signedMessage]

  @merchants.each do |merchant|
    begin
      strategy = EcV2DecryptionStrategy.new(merchant[:private_key_pem])
      return strategy.decrypt(signed_message)
    rescue StandardError => e
      e.define_singleton_method(:merchant_identifier) { merchant[:identifier] }
      errors << e
    end
  end

  raise GooglePaymentDecryptionError.new(
    'Failed to decrypt payment data using provided merchant configuration(s).',
    errors
  )
end