class Przelewy24::Transaction

Attributes

conf[W]
options[RW]
token[R]
transaction_url[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/przelewy24/transaction.rb, line 9
def initialize(options = {})
  @conf = Przelewy24.config
  @options = p24_options(@conf.default_transaction_options).merge(p24_options(options))
end

Public Instance Methods

confirm_transaction() click to toggle source
# File lib/przelewy24/transaction.rb, line 46
def confirm_transaction
  params = create_params @conf.confirm_transaction_params
  sign params, %w(p24_session_id p24_order_id p24_amount p24_currency)
  verify_params params
  response = query_p24 @conf.confirm_transaction_url, params
  response['error'] == '0'
end
register_transaction() click to toggle source
# File lib/przelewy24/transaction.rb, line 22
def register_transaction
  params = create_params @conf.register_transaction_params
  unless @options[:p24_order_id].present?
    sign params, %w(p24_session_id p24_merchant_id p24_amount p24_currency)
    verify_params params
    response = query_p24 @conf.register_url, params
    @token = response['token']
    params.merge({:p24_token => @token})
  else
    @token = params[:p24_token]
  end
  @transaction_url = @conf.request_url + @token
end
test_connection() click to toggle source
# File lib/przelewy24/transaction.rb, line 14
def test_connection
  params = create_params @conf.test_connection_params
  sign params, %w(p24_pos_id)
  verify_params params
  response = query_p24 @conf.test_url, params
  response['error'] == '0'
end
verify_transaction_status(params) click to toggle source
# File lib/przelewy24/transaction.rb, line 36
def verify_transaction_status(params)
  test = [params[:p24_session_id],params[:p24_order_id],params[:p24_amount],params[:p24_currency],@conf.crc]
  raise 'malformed sign' unless Digest::MD5.hexdigest(test.join('|')) == params[:p24_sign]
  %i(p24_session_id p24_amount p24_currency).each do |t|
    raise "param #{t} not match" unless params[t].to_s == @options[t].to_s
  end
  @options[:p24_order_id] = params[:p24_order_id]
  true
end

Private Instance Methods

create_params(source_params = {}) click to toggle source
# File lib/przelewy24/transaction.rb, line 81
def create_params(source_params = {})
  params = {}
  source_params.each do |k, v|
    params[k] = @options[k]
  end
  params
end
p24_options(options) click to toggle source
# File lib/przelewy24/transaction.rb, line 58
def p24_options(options)
  out = {}
  options.each do |k, v|
    out[('p24_'+k.to_s).to_sym] = v
  end
  out[:p24_pos_id] = @conf.merchant_id unless out[:p24_pos_id].present?
  out[:p24_amount] = (out[:p24_amount]*100).to_int if out[:p24_amount].present?
  out
end
query_p24(url, params) click to toggle source
# File lib/przelewy24/transaction.rb, line 68
def query_p24(url, params)
  response = HTTParty.post url, body: params
  response = Rack::Utils.parse_nested_query response.parsed_response
  raise response['error']+': '+response['errorMessage'] unless response['error'] == '0'
  response
end
sign(params, with_params = {}) click to toggle source
# File lib/przelewy24/transaction.rb, line 89
def sign(params, with_params = {})
  p = []
  with_params.each do |k|
    p << @options[k.to_sym]
  end
  p << @conf.crc
  s = Digest::MD5.hexdigest p.join('|')
  params[:p24_sign] = s
end
verify_params(params) click to toggle source
# File lib/przelewy24/transaction.rb, line 75
def verify_params(params)
  params.each do |p, v|
    raise "#{p} cannot be nil" unless v.present?
  end
end