Module: ActiveMerchant::Billing::Integrations::Banklink::Common

Overview

Calculation using method VK_VERSION=008: VK_MAC is RSA signature of the request fields coded into BASE64. VK_MAC will be calculated using secret key of the sender using RSA. Signature will be calculated for string that consists of all field lengths and contents in the query. Also empty fields are used in calculation – lenght “000”. Unnumbered (optional) fields are not used in calculation. MAC(x1,x2,…,xn) := RSA( SHA-1(p(x1 )|| x1|| p(x2 )|| x2 || … ||p( xn )||xn),d,n) where: || is string concatenation mark x1, x2, …, xn are parameters of the query p(x) is the length of the field x in bytes, represented by three digits d is RSA secret exponent n is RSA modulus

Instance Method Summary collapse

Instance Method Details

#func_p(val) ⇒ Object

p(x) is the length of the field x in bytes, represented by three digits



124
125
126
# File 'lib/active_merchant/billing/integrations/banklink.rb', line 124

def func_p(val)
  sprintf("%03i", val.bytesize)
end

#generate_data_string(service_msg_number, sigparams) ⇒ Object

Generate a string to be signed out of service message parameters. p(x1 )|| x1|| p(x2 )|| x2 || … ||p( xn )||xn || is string concatenation mark p(x) is length of the field x represented by three digits Parameters val1, val2, value3 would be turned into: ‘003val1003val2006value3’



134
135
136
137
138
139
140
141
# File 'lib/active_merchant/billing/integrations/banklink.rb', line 134

def generate_data_string(service_msg_number, sigparams)
  str = ''
  required_service_params[Integer(service_msg_number)].each do |param|
    val = sigparams[param].to_s # nil goes to ''
    str << func_p(val) << val
  end
  str
end

#generate_mac(service_msg_number, sigparams) ⇒ Object



148
149
150
# File 'lib/active_merchant/billing/integrations/banklink.rb', line 148

def generate_mac(service_msg_number, sigparams)
  Base64.encode64(generate_signature(service_msg_number, sigparams)).gsub(/\n/,'')
end

#generate_signature(service_msg_number, sigparams) ⇒ Object



143
144
145
146
# File 'lib/active_merchant/billing/integrations/banklink.rb', line 143

def generate_signature(service_msg_number, sigparams)
  privkey = self.class.parent.get_private_key
  privkey.sign(OpenSSL::Digest::SHA1.new, generate_data_string(service_msg_number, sigparams))
end

#required_service_paramsObject

Define required fields for each service message. We need to know this in order to calculate VK_MAC from a given hash of parameters. Order of the parameters is important.



48
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/active_merchant/billing/integrations/banklink.rb', line 48

def required_service_params
  {
    1001 => [
      'VK_SERVICE',
      'VK_VERSION',
      'VK_SND_ID',
      'VK_STAMP',
      'VK_AMOUNT',
      'VK_CURR',
      'VK_ACC',
      'VK_NAME',
      'VK_REF',
      'VK_MSG'],
    2001 => [
      'VK_SERVICE',
      'VK_VERSION',
      'VK_SND_ID',
      'VK_STAMP',
      'VK_AMOUNT',
      'VK_CURR',
      'VK_ACC',
      'VK_PANK',
      'VK_NAME',
      'VK_REF',
      'VK_MSG'],
    1002 => [
      'VK_SERVICE',
      'VK_VERSION',
      'VK_SND_ID',
      'VK_STAMP',
      'VK_AMOUNT',
      'VK_CURR',
      'VK_REF',
      'VK_MSG' ],
    1101 => [
      'VK_SERVICE',
      'VK_VERSION',
      'VK_SND_ID',
      'VK_REC_ID',
      'VK_STAMP',
      'VK_T_NO',
      'VK_AMOUNT',
      'VK_CURR',
      'VK_REC_ACC',
      'VK_REC_NAME',
      'VK_SND_ACC',
      'VK_SND_NAME',
      'VK_REF',
      'VK_MSG',
      'VK_T_DATE'],
    1201 => [
      'VK_SERVICE',
      'VK_VERSION',
      'VK_SND_ID',
      'VK_REC_ID',
      'VK_STAMP',
      'VK_AMOUNT',
      'VK_CURR',
      'VK_REC_ACC',
      'VK_REC_NAME',
      'VK_SND_ACC',
      'VK_SND_NAME',
      'VK_REF',
      'VK_MSG'],
    1901 => [
      'VK_SERVICE',
      'VK_VERSION',
      'VK_SND_ID',
      'VK_REC_ID',
      'VK_STAMP',
      'VK_REF',
      'VK_MSG']
  }
end