Class: Banklink::Seb::Notification

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/banklink/seb/notification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#encode_to_utf8, #func_p, #generate_data_string, #generate_mac, #generate_signature, #parse

Constructor Details

#initialize(post, options = {}) ⇒ Notification

Returns a new instance of Notification.



12
13
14
15
16
# File 'lib/banklink/seb/notification.rb', line 12

def initialize(post, options = {})
  @options = options
  empty!
  parse(post)
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



6
7
8
# File 'lib/banklink/seb/notification.rb', line 6

def params
  @params
end

#production_ipsObject

set this to an array in the subclass, to specify which IPs are allowed to send requests



10
11
12
# File 'lib/banklink/seb/notification.rb', line 10

def production_ips
  @production_ips
end

#rawObject

Returns the value of attribute raw.



7
8
9
# File 'lib/banklink/seb/notification.rb', line 7

def raw
  @raw
end

Instance Method Details

#acknowledgeObject

We don’t actually acknowledge the notification by making another request ourself, instead, we check the notification by checking the signature that came with the notification. This method has to be called when handling the notification & deciding whether to process the order. Example:

def notify
  notify = Notification.new(params)

  if notify.acknowledge
    ... process order ... if notify.complete?
  else
    ... log possible hacking attempt ...
  end


130
131
132
# File 'lib/banklink/seb/notification.rb', line 130

def acknowledge
  bank_signature_valid?(signature, params['IB_SERVICE'], params)
end

#amountObject

This combines the gross and currency and returns a proper Money object. this requires the money library located at dist.leetsoft.com/api/money



24
25
26
# File 'lib/banklink/seb/notification.rb', line 24

def amount
  return gross_cents
end

#automatic?Boolean

If our request was sent automatically by the bank (true) or manually by the user triggering the callback by pressing a “return” button (false).

Returns:

  • (Boolean)


109
110
111
# File 'lib/banklink/seb/notification.rb', line 109

def automatic?
  (params['IB_FROM_SERVER'].present? && params['IB_FROM_SERVER'].upcase == 'Y')
end

#bank_signature_valid?(bank_signature, service_msg_number, sigparams) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/banklink/seb/notification.rb', line 138

def bank_signature_valid?(bank_signature, service_msg_number, sigparams)
  Seb.get_bank_public_key.verify(OpenSSL::Digest::SHA1.new, bank_signature, generate_data_string(service_msg_number, sigparams, Seb.required_service_params))
end

#complete?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/banklink/seb/notification.rb', line 40

def complete?
  params['IB_STATUS'] == 'ACCOMPLISHED'
end

#currencyObject



52
53
54
# File 'lib/banklink/seb/notification.rb', line 52

def currency
  params['IB_CURR']
end

#empty!Object

reset the notification.



29
30
31
32
# File 'lib/banklink/seb/notification.rb', line 29

def empty!
  @params  = Hash.new
  @raw     = ""
end

#failed?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/banklink/seb/notification.rb', line 48

def failed?
  params['IB_SERVICE'] == '1901'
end

#get_data_stringObject



134
135
136
# File 'lib/banklink/seb/notification.rb', line 134

def get_data_string
  generate_data_string(params['IB_SERVICE'], params, Seb.required_service_params)
end

#grossObject

The money amount we received, string.



93
94
95
# File 'lib/banklink/seb/notification.rb', line 93

def gross
  params['IB_AMOUNT']
end

#gross_centsObject



18
19
20
# File 'lib/banklink/seb/notification.rb', line 18

def gross_cents
  (gross.to_f * 100.0).round
end

#received_atObject

When was this payment received by the client. We’re expecting a dd.mm.yyyy format.



80
81
82
83
84
85
86
# File 'lib/banklink/seb/notification.rb', line 80

def received_at
  require 'date'
  date = params['IB_T_DATE']
  return nil unless date
  day, month, year = *date.split('.').map(&:to_i)
  Date.civil(year, month, day)
end

#receiver_bank_accountObject Also known as: reciever_bank_account



73
74
75
# File 'lib/banklink/seb/notification.rb', line 73

def 
  params['IB_REC_ACC']
end

#receiver_nameObject Also known as: reciever_name



68
69
70
# File 'lib/banklink/seb/notification.rb', line 68

def receiver_name
  params['IB_REC_NAME']
end

#sender_bank_accountObject



64
65
66
# File 'lib/banklink/seb/notification.rb', line 64

def 
  params['IB_PAYER_ACC']
end

#sender_nameObject



60
61
62
# File 'lib/banklink/seb/notification.rb', line 60

def sender_name
  params['IB_PAYER_NAME']
end

#signatureObject



88
89
90
# File 'lib/banklink/seb/notification.rb', line 88

def signature
  Base64.decode64(params['IB_CRC'])
end

#statusObject

TODO what should be here?



103
104
105
# File 'lib/banklink/seb/notification.rb', line 103

def status
  complete? ? 'Completed' : 'Failed'
end

#success?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/banklink/seb/notification.rb', line 113

def success?
  acknowledge && complete?
end

#test?Boolean

Was this a test transaction?

Returns:

  • (Boolean)


98
99
100
# File 'lib/banklink/seb/notification.rb', line 98

def test?
  params['IB_REC_ID'] == 'testvpos'
end

#transaction_idObject



56
57
58
# File 'lib/banklink/seb/notification.rb', line 56

def transaction_id
  params['IB_PAYMENT_ID']
end

#valid_sender?(ip) ⇒ Boolean

Check if the request comes from an official IP

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/banklink/seb/notification.rb', line 35

def valid_sender?(ip)
  return true if Rails.env == :test || production_ips.blank?
  production_ips.include?(ip)
end

#wait?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/banklink/seb/notification.rb', line 44

def wait?
  params['IB_SERVICE'] == '1201'
end