Class: PaymentsJs

Inherits:
Object
  • Object
show all
Extended by:
Configuration
Defined in:
lib/paymentsjs-rails.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configuration

configuration, define_setting

Constructor Details

#initialize(args = {}) ⇒ PaymentsJs

Returns a new instance of PaymentsJs.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/paymentsjs-rails.rb', line 29

def initialize(args = {})

  @api_key      = PaymentsJs.api_key
  @mid          = PaymentsJs.mid
  @mkey         = PaymentsJs.mkey
  @api_secret   = PaymentsJs.api_secret

  @request_type = args[:request_type].present? ? args[:request_type] : PaymentsJs.request_type
  @request_id   = args[:request_id]
  @postback_url = args[:postback_url].present? ? args[:postback_url] : PaymentsJs.postback_url
  @amount       = args[:amount]
  @pre_auth     = args[:pre_auth].present? ? args[:pre_auth] : PaymentsJs.pre_auth
  @environment  = args[:environment].present? ? args[:environment] : PaymentsJs.environment

  # Generate the salt and iv at initialization so they can be consistently called
  @iv = OpenSSL::Random.pseudo_bytes(16)
  @salt = generate_salt(@iv)

end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def address
  @address
end

#amountObject

Returns the value of attribute amount.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def amount
  @amount
end

#api_keyObject

Returns the value of attribute api_key.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def api_key
  @api_key
end

#auth_keyObject

Returns the value of attribute auth_key.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def auth_key
  @auth_key
end

#cityObject

Returns the value of attribute city.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def city
  @city
end

#environmentObject

Returns the value of attribute environment.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def environment
  @environment
end

#midObject

Returns the value of attribute mid.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def mid
  @mid
end

#nameObject

Returns the value of attribute name.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def name
  @name
end

#postback_urlObject

Returns the value of attribute postback_url.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def postback_url
  @postback_url
end

#pre_authObject

Returns the value of attribute pre_auth.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def pre_auth
  @pre_auth
end

#request_idObject

Returns the value of attribute request_id.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def request_id
  @request_id
end

#request_typeObject

Returns the value of attribute request_type.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def request_type
  @request_type
end

#saltObject

Returns the value of attribute salt.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def salt
  @salt
end

#stateObject

Returns the value of attribute state.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def state
  @state
end

#zipObject

Returns the value of attribute zip.



27
28
29
# File 'lib/paymentsjs-rails.rb', line 27

def zip
  @zip
end

Instance Method Details

#generate_salt(iv) ⇒ Object

Generate a salt



14
15
16
17
18
19
# File 'lib/paymentsjs-rails.rb', line 14

def generate_salt(iv)
  salt = iv.unpack('H*').first
  salt = salt.bytes.to_a
  salt = salt.pack('U*')
  Base64.strict_encode64(salt)
end

#get_auth_keyObject



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
# File 'lib/paymentsjs-rails.rb', line 49

def get_auth_key
  cipher = OpenSSL::Cipher::AES.new(256, :CBC)
  cipher.encrypt

  req  = {
    "apiKey"      => @api_key,
    "merchantId"  => @mid,
    "merchantKey" => @mkey,
    "requestType" => @request_type,
    "requestId"   => @request_id,
    "postbackUrl" => @postback_url,
    "amount"      => @amount,
    "nonce"       => @salt,
    "preAuth"     => @pre_auth,
    "environment" => @environment
  }

  data       = JSON.generate(req)
  key        = OpenSSL::PKCS5.pbkdf2_hmac_sha1(@api_secret, @salt, 1500, 32)
  cipher.key = key
  cipher.iv  = @iv
  auth_key    = cipher.update(data) + cipher.final()

  Base64.strict_encode64(auth_key)
end

#get_saltObject



75
76
77
# File 'lib/paymentsjs-rails.rb', line 75

def get_salt
  @salt
end