Class: Pay24::ResponseHandler

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/pay24/response_handler.rb

Defined Under Namespace

Classes: PaymentIsZero

Constant Summary collapse

EMAIL_REGEX =
/\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/
IDENTIFICATOR_REGEX =
/\A\d+\z/
REQUEST_HANDLERS =
{
  pay:   :osmp_pay,
  check: :check_if_account_is_valid,
}.freeze

Constants included from Errors

Errors::ERROR_CANNOT_CHECK_USER_ACCOUNT_STATE, Errors::ERROR_INACTIVE_USER, Errors::ERROR_INTERNAL, Errors::ERROR_INVALID_USER_ID_FORMAT, Errors::ERROR_PAYMENT_DENIED_BY_PROVIDER, Errors::ERROR_PAYMENT_DENIED_FOR_TECHNICAL_REASON, Errors::ERROR_SUM_IS_TOO_LARGE, Errors::ERROR_SUM_IS_TOO_SMALL, Errors::ERROR_USER_NOT_FOUND, Errors::PAYMENT_PROCESSING_NOT_FINISHED, Errors::SUCCESS, Errors::TEMPORARY_ERROR

Instance Attribute Summary collapse

Attributes included from Errors

#code, #message, #result_code

Instance Method Summary collapse

Constructor Details

#initialize(params: {}, find_user_callback: nil, payment_callback: nil, raise_on_error: false) ⇒ ResponseHandler

Returns a new instance of ResponseHandler.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pay24/response_handler.rb', line 28

def initialize(params: {}, find_user_callback: nil, payment_callback: nil, raise_on_error: false)
  @command            = params[:command]
  @account            = params[:account]
  @transaction_id     = params[:txn_id]
  @transaction_date   = params[:txn_date]
  @sum                = params[:sum]
  @find_user_callback = find_user_callback
  @payment_callback   = payment_callback
  @raise_on_error     = raise_on_error
  freeze
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



18
19
20
# File 'lib/pay24/response_handler.rb', line 18

def 
  @account
end

#commandObject (readonly)

Returns the value of attribute command.



18
19
20
# File 'lib/pay24/response_handler.rb', line 18

def command
  @command
end

#sumObject (readonly)

Returns the value of attribute sum.



18
19
20
# File 'lib/pay24/response_handler.rb', line 18

def sum
  @sum
end

#transaction_dateObject (readonly)

Returns the value of attribute transaction_date.



18
19
20
# File 'lib/pay24/response_handler.rb', line 18

def transaction_date
  @transaction_date
end

#transaction_idObject (readonly)

Returns the value of attribute transaction_id.



18
19
20
# File 'lib/pay24/response_handler.rb', line 18

def transaction_id
  @transaction_id
end

Instance Method Details

#command_is_valid?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/pay24/response_handler.rb', line 40

def command_is_valid?
  @command.to_sym.in?(REQUEST_HANDLERS.keys)
end

#generate_xml(result, comment = '') ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pay24/response_handler.rb', line 62

def generate_xml(result, comment = '')
  xml = Builder::XmlMarkup.new indent: 2
  xml.instruct!
  xml.response do |req|
    req.result  result.code
    req.comment comment
    if payment_command?
      req.txn_id      @transaction_id
      req.sum         @sum
    end
  end
end

#payment_command?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/pay24/response_handler.rb', line 44

def payment_command?
  @command.to_sym == :pay
end

#requested_accountObject



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

def 
  @find_user_callback.(@account) rescue ERROR_CANNOT_CHECK_USER_ACCOUNT_STATE
end

#run_commandObject



48
49
50
51
52
53
54
55
56
# File 'lib/pay24/response_handler.rb', line 48

def run_command
  result_code = if command_is_valid?
                  send(REQUEST_HANDLERS[@command.to_sym]) || ERROR_INTERNAL
                else
                  ERROR_INTERNAL
                end

  Pay24::Result.new(result_code)
end