Class: GooglePayRuby::GooglePaymentMethodTokenContext
- Inherits:
-
Object
- Object
- GooglePayRuby::GooglePaymentMethodTokenContext
- Defined in:
- lib/google_pay_ruby/google_payment_method_token_context.rb
Instance Attribute Summary collapse
-
#merchants ⇒ Object
readonly
Returns the value of attribute merchants.
Instance Method Summary collapse
- #decrypt(token) ⇒ Object
-
#initialize(options) ⇒ GooglePaymentMethodTokenContext
constructor
A new instance of GooglePaymentMethodTokenContext.
Constructor Details
#initialize(options) ⇒ GooglePaymentMethodTokenContext
Returns a new instance of GooglePaymentMethodTokenContext.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/google_pay_ruby/google_payment_method_token_context.rb', line 19 def initialize() @merchants = [:merchants] || [] @recipient_id = [:recipient_id] @root_signing_keys = [:root_signing_keys] @gateway_merchant_id = [:gateway_merchant_id] @test = .fetch(:test, false) @verify_signature = .fetch(:verify_signature, true) @verify_expiration = .fetch(:verify_expiration, true) @verify_merchant_id = .fetch(:verify_merchant_id, !@gateway_merchant_id.nil?) if @merchants.empty? raise GooglePaymentDecryptionError.new( 'No merchant configuration provided for decryption context.' ) end if @verify_signature && (@recipient_id.nil? || @recipient_id.empty?) raise ArgumentError, ':recipient_id is required when signature verification is enabled' end validate_merchant_configurations! end |
Instance Attribute Details
#merchants ⇒ Object (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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/google_pay_ruby/google_payment_method_token_context.rb', line 46 def decrypt(token) if token.is_a?(String) raw_token_json = token token = JSON.parse(token) end protocol_version = token['protocolVersion'] || token[:protocolVersion] unless protocol_version == 'ECv2' raise GooglePaymentDecryptionError.new( "Unsupported decryption for protocol version #{protocol_version}" ) end # Steps 1-4: Verify signatures before decryption if @verify_signature verifier = SignatureVerifier.new( root_signing_keys: @root_signing_keys, recipient_id: @recipient_id, test: @test ) verifier.verify!(token, raw_token_json: raw_token_json) end # Step 5: Decrypt the payload errors = [] = token['signedMessage'] || token[:signedMessage] decrypted_data = nil @merchants.each do |merchant| begin strategy = EcV2DecryptionStrategy.new(merchant[:private_key_pem]) decrypted_data = strategy.decrypt() break rescue StandardError => e e.define_singleton_method(:merchant_identifier) { merchant[:identifier] } errors << e end end unless decrypted_data raise GooglePaymentDecryptionError.new( 'Failed to decrypt payment data using provided merchant configuration(s).', errors ) end # Step 6: Verify message hasn't expired if @verify_expiration (decrypted_data) end # Step 7: Verify gatewayMerchantId matches expected value if @verify_merchant_id verify_gateway_merchant_id!(decrypted_data) end decrypted_data end |