Class: DPay::CardEncryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/dpay/card/card_encryptor.rb,
sig/dpay/card/card_encryptor.rbs

Instance Method Summary collapse

Instance Method Details

#encrypt(card, transaction_id, public_key_pem, timestamp: Time.now.to_i) ⇒ String

Parameters:

  • card (CardData)
  • transaction_id (String)
  • public_key_pem (String)
  • timestamp: (Integer) (defaults to: Time.now.to_i)

Returns:

  • (String)


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

def encrypt(card, transaction_id, public_key_pem, timestamp: Time.now.to_i)
  key = load_key(public_key_pem)
  payload = Internal::PHP.json_encode(
    { "PN" => card.pan, "SC" => card.cvv, "DT" => card.expiry, "ID" => transaction_id, "TX" => timestamp },
    escape_slashes: true
  )

  [key.public_encrypt(payload, OpenSSL::PKey::RSA::PKCS1_PADDING)].pack("m0")
rescue OpenSSL::PKey::PKeyError => e
  raise CardEncryptionError, "Card data encryption failed: #{e.message}"
end

#load_key(public_key_pem) ⇒ OpenSSL::PKey::RSA

Parameters:

  • public_key_pem (String)

Returns:

  • (OpenSSL::PKey::RSA)


21
22
23
24
25
# File 'lib/dpay/card/card_encryptor.rb', line 21

def load_key(public_key_pem)
  OpenSSL::PKey::RSA.new(public_key_pem.to_s)
rescue OpenSSL::PKey::PKeyError, ArgumentError, TypeError
  raise CardEncryptionError, "Invalid RSA public key"
end