Module: Przelewy24Payment

Defined in:
lib/przelewy24_payment.rb,
lib/przelewy24_payment/engine.rb,
lib/przelewy24_payment/version.rb

Defined Under Namespace

Modules: PaymentHelper Classes: Engine, InstallGenerator

Constant Summary collapse

VERSION =
"0.2.0"
@@merchant_id =
''
@@pos_id =
''
@@language =
'PLN'
@@mode =
:development
@@url_status =
''
@@url_return =
''
@@api_version =
'3.2'
@@crc_key =
''
@@allowed_ips =
%w(91.216.191.181 91.216.191.182 91.216.191.183 91.216.191.184 91.216.191.185)
@@allowed_languages =
%w(pl en de es it)
@@country =
'PL'
@@allowed_countries =
%w(AD AT BE CY CZ DK EE FI FR EL ES NO PL PT SM SK SI CH SE HU GB IT NL IE IS LT LV LU MT US CA JP UA BY RY RU)
@@hostname =
{ :development => "http://127.0.0.1:3000" }

Class Method Summary collapse

Class Method Details

.calculate_sign(session, merchant, amount, currency, crc) ⇒ Object



93
94
95
# File 'lib/przelewy24_payment.rb', line 93

def self.calculate_sign(session,merchant,amount,currency,crc)
  Digest::MD5.hexdigest(session.to_s + "|" + merchant.to_s + "|" + amount.to_s + "|" + currency.to_s + "|" + crc.to_s)
end

.check_ip(ip) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/przelewy24_payment.rb', line 53

def self.check_ip(ip)
  if @@mode == :production
    allowed_ips.include?(ip)
  else
    true
  end
end

.complete_url(params) ⇒ Object



61
62
63
# File 'lib/przelewy24_payment.rb', line 61

def self.complete_url(params)
  params
end

.friendly_tokenObject



89
90
91
# File 'lib/przelewy24_payment.rb', line 89

def self.friendly_token
  SecureRandom.base64(15).tr('+/=lIO0', 'aqrsxyz')
end

.get_hostnameObject



97
98
99
# File 'lib/przelewy24_payment.rb', line 97

def self.get_hostname
  @@hostname[@@mode]
end

.get_url_returnObject



105
106
107
# File 'lib/przelewy24_payment.rb', line 105

def self.get_url_return
  get_hostname + @@url_return
end

.get_url_statusObject



101
102
103
# File 'lib/przelewy24_payment.rb', line 101

def self.get_url_status
  get_hostname + @@url_status
end

.make_p24_amount(price) ⇒ Object



109
110
111
# File 'lib/przelewy24_payment.rb', line 109

def self.make_p24_amount(price)
  price.present? ? (price.to_f.round(2) * 100).to_i : 0
end

.make_p24_country(data_country) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/przelewy24_payment.rb', line 122

def self.make_p24_country(data_country)
  country_code = (data_country || country)
  if allowed_countries.include?(country_code)
    country_code
  else
    'PL'
  end
end

.make_p24_currency(data_currency) ⇒ Object



131
132
133
# File 'lib/przelewy24_payment.rb', line 131

def self.make_p24_currency(data_currency)
  (data_currency || currency).upcase
end

.make_p24_language(data_language) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/przelewy24_payment.rb', line 113

def self.make_p24_language(data_language)
  lang = (data_language || language)
  if allowed_languages.include?(lang)
    lang
  else
    'pl'
  end
end

.parse_response(response) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/przelewy24_payment.rb', line 163

def self.parse_response(response)
  ret = OpenStruct.new
  response.split("&").each do |arg|
    line = arg.split('=')
    ret[line[0].strip] = line[1].force_encoding("ISO-8859-2").encode!("UTF-8")
  end
  ret
end

.prepare_form(data) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/przelewy24_payment.rb', line 135

def self.prepare_form(data)
  #prepare mandatory fields
  data[:merchant_id] ||=  merchant_id
  data[:pos_id] ||= pos_id
  data[:api_version] ||= api_version
  data[:p24_amount] = make_p24_amount(data[:amount])
  data[:currency] =  make_p24_currency (data[:currency])
  data[:language] = make_p24_language(data[:language]) if data[:language]
  data[:country] = make_p24_country(data[:country])
  data[:url_return] ||= get_url_return
  data[:url_status] ||= get_url_status
  data[:crc_key] ||= crc_key
  data[:encoding] ||= 'UTF-8'
  data[:p24_sign] = calculate_sign(data[:session_id],data[:merchant_id],data[:p24_amount],data[:currency], data[:crc_key])
  data
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



49
50
51
# File 'lib/przelewy24_payment.rb', line 49

def self.setup
  yield self
end

.test_connection_params(data = {}) ⇒ Object



152
153
154
155
156
157
# File 'lib/przelewy24_payment.rb', line 152

def self.test_connection_params(data={})
  data[:p24_merchant_id] ||=  merchant_id
  data[:p24_pos_id] ||= pos_id
  data[:p24_sign] = Digest::MD5.hexdigest(data[:p24_pos_id].to_s + "|" + crc_key.to_s)
  data
end

.test_connection_urlObject



65
66
67
68
69
70
71
# File 'lib/przelewy24_payment.rb', line 65

def self.test_connection_url
  if @@mode == :production
    'https://secure.przelewy24.pl/testConnection'
  else
    'https://sandbox.przelewy24.pl/testConnection'
  end
end

.transaction_request_urlObject



73
74
75
76
77
78
79
# File 'lib/przelewy24_payment.rb', line 73

def self.transaction_request_url
  if @@mode == :production
    'https://secure.przelewy24.pl/trnDirect'
  else
    'https://sandbox.przelewy24.pl/trnDirect'
  end
end

.verification_request_urlObject



81
82
83
84
85
86
87
# File 'lib/przelewy24_payment.rb', line 81

def self.verification_request_url
  if @@mode == :production
    'https://secure.przelewy24.pl/trnVerify'
  else
    'https://sandbox.przelewy24.pl/trnVerify'
  end
end

.verify_sign(data, params_new) ⇒ Object



159
160
161
# File 'lib/przelewy24_payment.rb', line 159

def self.verify_sign(data,params_new)
  Digest::MD5.hexdigest(params_new[:p24_session_id].to_s+"|"+params_new[:p24_order_id].to_s+"|"+make_p24_amount(data[:amount]).to_s+"|"+params_new[:p24_currency].to_s+"|"+data[:crc_key].to_s)
end