Class: Vpago::VattanacMiniAppDataHandler

Inherits:
Object
  • Object
show all
Defined in:
app/services/vpago/vattanac_mini_app_data_handler.rb

Instance Method Summary collapse

Instance Method Details

#aes_keyObject



29
30
31
# File 'app/services/vpago/vattanac_mini_app_data_handler.rb', line 29

def aes_key
 ENV['VATTANAC_AES_SECRET_KEY'].presence || Rails.application.credentials.vattanac.aes_secret_key
end

#bookmeplus_private_keyObject



25
26
27
# File 'app/services/vpago/vattanac_mini_app_data_handler.rb', line 25

def bookmeplus_private_key 
  ENV['BOOKMEPLUS_PRIVATE_KEY'].presence  || Rails.application.credentials.bookmeplus.private_key
end

#decrypt_data(data) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'app/services/vpago/vattanac_mini_app_data_handler.rb', line 3

def decrypt_data(data)
  encrypted_data, signature = data.to_s.split('.', 2)
  
  return nil unless Vpago::RsaHandler.new(public_key: vattanac_public_key).verify(encrypted_data, signature)

  decrypted = Vpago::AesEncrypter.decrypt(encrypted_data, aes_key)
  JSON.parse(decrypted) rescue nil
   
end

#encrypted_data(payload) ⇒ Object



13
14
15
16
17
18
19
# File 'app/services/vpago/vattanac_mini_app_data_handler.rb', line 13

def encrypted_data(payload)
  json_payload = payload.to_json
  encrypted_data = Vpago::AesEncrypter.encrypt(json_payload, aes_key)
  rsa_service = Vpago::RsaHandler.new(private_key: bookmeplus_private_key)
  signed_data = rsa_service.sign(encrypted_data)
  signed_data
end

#vattanac_public_keyObject



21
22
23
# File 'app/services/vpago/vattanac_mini_app_data_handler.rb', line 21

def vattanac_public_key 
  ENV['VATTANAC_PUBLIC_KEY'].presence || Rails.application.credentials.vattanac.public_key
end