banklink_lv 1.0.2 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,163 +0,0 @@
1
- module Banklink
2
-
3
- class Notification
4
- include Banklink::Common
5
- attr_accessor :params
6
- attr_accessor :raw
7
-
8
- # set this to an array in the subclass, to specify which IPs are allowed to send requests
9
- attr_accessor :production_ips
10
-
11
- def initialize(post, options = {})
12
- @options = options
13
- empty!
14
- parse(post)
15
- end
16
-
17
- def gross_cents
18
- (gross.to_f * 100.0).round
19
- end
20
-
21
- # This combines the gross and currency and returns a proper Money object.
22
- # this requires the money library located at http://dist.leetsoft.com/api/money
23
- def amount
24
- return gross_cents
25
- end
26
-
27
- # reset the notification.
28
- def empty!
29
- @params = Hash.new
30
- @raw = ""
31
- end
32
-
33
- # Check if the request comes from an official IP
34
- def valid_sender?(ip)
35
- return true if Rails.env == :test || production_ips.blank?
36
- production_ips.include?(ip)
37
- end
38
-
39
- # A helper method to parse the raw post of the request & return
40
- # the right Notification subclass based on the sender id.
41
- #def self.get_notification(http_raw_data)
42
- # params = ActiveMerchant::Billing::Integrations::Notification.new(http_raw_data).params
43
- # Banklink.get_class(params)::Notification.new(http_raw_data)
44
- #end
45
-
46
- def get_data_string
47
- generate_data_string(params['VK_SERVICE'], params)
48
- end
49
-
50
- def bank_signature_valid?(bank_signature, service_msg_number, sigparams)
51
- Swedbank.get_bank_public_key.verify(OpenSSL::Digest::SHA1.new, bank_signature, generate_data_string(service_msg_number, sigparams))
52
- end
53
-
54
- def complete?
55
- params['VK_SERVICE'] == '1101'
56
- end
57
-
58
- def wait?
59
- params['VK_SERVICE'] == '1201'
60
- end
61
-
62
- def failed?
63
- params['VK_SERVICE'] == '1901'
64
- end
65
-
66
- def currency
67
- params['VK_CURR']
68
- end
69
-
70
- # The order id we passed to the form helper.
71
- def item_id
72
- params['VK_STAMP']
73
- end
74
-
75
- def transaction_id
76
- params['VK_REF']
77
- end
78
-
79
- def sender_name
80
- params['VK_SND_NAME']
81
- end
82
-
83
- def sender_bank_account
84
- params['VK_SND_ACC']
85
- end
86
-
87
- def reciever_name
88
- params['VK_REC_NAME']
89
- end
90
-
91
- def reciever_bank_account
92
- params['VK_REC_ACC']
93
- end
94
-
95
- # When was this payment received by the client.
96
- # We're expecting a dd.mm.yyyy format.
97
- def received_at
98
- date = params['VK_T_DATE']
99
- return nil unless date
100
- day, month, year = *date.split('.').map(&:to_i)
101
- Date.civil(year, month, day)
102
- end
103
-
104
- def signature
105
- Base64.decode64(params['VK_MAC'])
106
- end
107
-
108
- # The money amount we received, string.
109
- def gross
110
- params['VK_AMOUNT']
111
- end
112
-
113
- # Was this a test transaction?
114
- def test?
115
- params['VK_REC_ID'] == 'testvpos'
116
- end
117
-
118
- # TODO what should be here?
119
- def status
120
- complete? ? 'Completed' : 'Failed'
121
- end
122
-
123
- # If our request was sent automatically by the bank (true) or manually
124
- # by the user triggering the callback by pressing a "return" button (false).
125
- def automatic?
126
- params['VK_AUTO'].upcase == 'Y'
127
- end
128
-
129
- def success?
130
- acknowledge && complete?
131
- end
132
-
133
- # We don't actually acknowledge the notification by making another request ourself,
134
- # instead, we check the notification by checking the signature that came with the notification.
135
- # This method has to be called when handling the notification & deciding whether to process the order.
136
- # Example:
137
- #
138
- # def notify
139
- # notify = Notification.new(params)
140
- #
141
- # if notify.acknowledge
142
- # ... process order ... if notify.complete?
143
- # else
144
- # ... log possible hacking attempt ...
145
- # end
146
- def acknowledge
147
- bank_signature_valid?(signature, params['VK_SERVICE'], params)
148
- end
149
-
150
-
151
- private
152
-
153
- # Take the posted data and move the relevant data into a hash
154
- def parse(post)
155
- @raw = post.to_s
156
- for line in @raw.split('&')
157
- key, value = *line.scan( %r{^([A-Za-z0-9_.]+)\=(.*)$} ).flatten
158
- params[key] = CGI.unescape(value)
159
- end
160
- end
161
- end
162
- end
163
-