Class: PedicelPay::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/pedicel-pay/helper.rb

Overview

Assistance/collection functions.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: PedicelPay.config, pedicel_instance: nil) ⇒ Helper

Returns a new instance of Helper.



6
7
8
9
# File 'lib/pedicel-pay/helper.rb', line 6

def initialize(config: PedicelPay.config, pedicel_instance: nil)
  @config = config
  @pedicel = pedicel_instance
end

Class Method Details

.bytestring_to_hex(string) ⇒ Object



19
20
21
# File 'lib/pedicel-pay/helper.rb', line 19

def self.bytestring_to_hex(string)
  string.unpack('H*').first
end

.ec_key_to_pkey_public_key(ec_key) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/pedicel-pay/helper.rb', line 11

def self.ec_key_to_pkey_public_key(ec_key)
  # EC#public_key is not a PKey public key, but an EC point.
  pub = OpenSSL::PKey::EC.new(ec_key.group)
  pub.public_key = ec_key.is_a?(OpenSSL::PKey::PKey) ? ec_key.public_key : ec_key

  pub
end

.encrypt(data:, key:) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/pedicel-pay/helper.rb', line 50

def self.encrypt(data:, key:)
  cipher = OpenSSL::Cipher.new('aes-256-gcm')
  cipher.encrypt
  cipher.key = key
  cipher.iv_len = 16
  cipher.iv = 0.chr * cipher.iv_len
  cipher.auth_data = ''
  cipher.update(data) + cipher.final + cipher.auth_tag
end

.hex_to_bytestring(hex) ⇒ Object



23
24
25
# File 'lib/pedicel-pay/helper.rb', line 23

def self.hex_to_bytestring(hex)
  [hex].pack('H*')
end

.merchant_id(x) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pedicel-pay/helper.rb', line 27

def self.merchant_id(x)
  case x
  when Client
    Pedicel::EC.merchant_id(certificate: x.certificate)
  when OpenSSL::X509::Certificate
    Pedicel::EC.merchant_id(certificate: x)
  when /\A[0-9a-fA-F]{64}\z/
    [x].pack('H*')
  when /\A.{32}\z/
    x
  else
    raise ArgumentError, "cannot extract 'merchant_id' from #{x}"
  end
end

.recipient_certificate(recipient:) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/pedicel-pay/helper.rb', line 42

def self.recipient_certificate(recipient:)
  case recipient
  when Client                     then recipient.certificate
  when OpenSSL::X509::Certificate then recipient
  else raise ArgumentError, 'invalid recipient'
  end
end