Class: KazkomEpay::Epay

Inherits:
Object
  • Object
show all
Defined in:
lib/kazkom_epay.rb

Class Method Summary collapse

Class Method Details

.amountObject



95
96
97
# File 'lib/kazkom_epay.rb', line 95

def amount
  settings[:amount]
end

.base64_encoded_signed_xmlObject

КЛЮЧЕВОЙ МОМЕНТ при формировании запроса для банка



58
59
60
# File 'lib/kazkom_epay.rb', line 58

def base64_encoded_signed_xml
  Base64.encode64(signed_xml).gsub("\n", '')
end

.cert_idObject



83
84
85
# File 'lib/kazkom_epay.rb', line 83

def cert_id
  settings[:cert_id]
end

.check_signed_xml(xml) ⇒ Object

КЛЮЧЕВОЙ МОМЕНТ при проверке ответа от банка



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kazkom_epay.rb', line 63

def check_signed_xml xml
  # Hash.from_xml
  require 'active_support/core_ext/hash/conversions'

  bank_sign_raw_base64 = Hash.from_xml(xml)['document']['bank_sign']

  bank_part_regexp = /\A<document>(.+)<bank_sign.*\z/
  bank_sign_regexp = /(<bank_sign .+<\/bank_sign>)/

  check_this = bank_part_regexp.match(xml)[1]
  bank_sign_raw = Base64.decode64 bank_sign_raw_base64
  bank_sign_raw.reverse! if reverse_signature

  digest = OpenSSL::Digest::SHA1.new
  cert = OpenSSL::X509::Certificate.new File.read(settings[:public_key_path])
  public_key = cert.public_key

  check_result = public_key.verify digest, bank_sign_raw, check_this
end

.currencyObject



99
100
101
# File 'lib/kazkom_epay.rb', line 99

def currency
  settings[:currency]
end

.keyObject



35
36
37
# File 'lib/kazkom_epay.rb', line 35

def key
  settings[:key]
end

.merchant_idObject



103
104
105
# File 'lib/kazkom_epay.rb', line 103

def merchant_id
  settings[:merchant_id]
end

.merchant_nameObject



87
88
89
# File 'lib/kazkom_epay.rb', line 87

def merchant_name
  settings[:merchant_name]
end

.order_idObject



91
92
93
# File 'lib/kazkom_epay.rb', line 91

def order_id
  settings[:order_id]
end

.reverse_signatureObject



107
108
109
# File 'lib/kazkom_epay.rb', line 107

def reverse_signature
  true
end

.settingsObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/kazkom_epay.rb', line 14

def settings
  @@settings ||= {
    cert_id: "00C182B189", # test cert_id
    currency: 398, # KZT
    merchant_name: "Some Merchant",
    merchant_id: 92061101, # test merchant_id

    private_key_path: KazkomEpay::root.join('cert', 'test', "test_prv.pem"), # test private key path
    private_key_password: "nissan", # test private key password
    public_key_path: KazkomEpay::root.join('cert', 'test', "kkbca.pem")
  }
end

.setup(with_params) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/kazkom_epay.rb', line 27

def setup with_params
  @@settings ||= settings
  with_params.each_pair do |key, value|
    @@settings[key.to_sym] = value
  end
  self
end

.signed_xmlObject



53
54
55
# File 'lib/kazkom_epay.rb', line 53

def signed_xml
  "<document>" + xml + xml_sign + "</document>"
end

.xmlObject



39
40
41
# File 'lib/kazkom_epay.rb', line 39

def xml
  %Q|<merchant cert_id="#{cert_id}" name="#{merchant_name}"><order order_id="#{order_id}" amount="#{amount}" currency="#{currency}"><department merchant_id="#{merchant_id}" amount="#{amount}"/></order></merchant>|
end

.xml_signObject



43
44
45
46
47
48
49
50
51
# File 'lib/kazkom_epay.rb', line 43

def xml_sign
  pkey = OpenSSL::PKey::RSA.new(File.read(settings[:private_key_path]), settings[:private_key_password])

  signature = pkey.sign(OpenSSL::Digest::SHA1.new, xml)
  signature.reverse! if reverse_signature

  signature_base64_encoded_without_newlines = Base64.encode64(signature).gsub("\n", '')
  '<merchant_sign type="RSA">' + signature_base64_encoded_without_newlines + '</merchant_sign>'
end