barzahlen 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,50 +0,0 @@
1
- # coding: utf-8
2
-
3
- module Barzahlen
4
- module Api
5
- module Online
6
-
7
- # Checks requests from Barzahlen-API
8
- class NotificationHandler
9
- TRANSACTION_PAID = 'paid'
10
- TRANSACTION_EXPIRED = 'expired'
11
- REFUND_COMPLETED = 'refund_completed'
12
- REFUND_EXPIRED = 'refund_expired'
13
-
14
- def initialize(api)
15
- @api = api
16
- end
17
-
18
- # validates request and return the parameters
19
- def validate_request(request_parameters)
20
- parameters_order = case request_parameters[:state]
21
- when TRANSACTION_PAID then [:state, :transaction_id, :shop_id, :customer_email,
22
- :amount, :currency, :order_id, :custom_var_0,
23
- :custom_var_1, :custom_var_2, :notification_key]
24
- when TRANSACTION_EXPIRED then [:state, :transaction_id, :shop_id, :customer_email,
25
- :amount, :currency, :order_id, :custom_var_0,
26
- :custom_var_1, :custom_var_2, :notification_key]
27
- when REFUND_COMPLETED then [:state, :refund_transaction_id, :origin_transaction_id,
28
- :shop_id, :customer_email, :amount, :currency,
29
- :origin_order_id, :custom_var_0, :custom_var_1,
30
- :custom_var_2, :notification_key]
31
- when REFUND_EXPIRED then [:state, :refund_transaction_id, :origin_transaction_id,
32
- :shop_id, :customer_email, :amount, :currency,
33
- :origin_order_id, :custom_var_0, :custom_var_1,
34
- :custom_var_2, :notification_key]
35
- else raise RuntimeError, "Unknown state '#{request_parameters[:state]}'"
36
- end
37
-
38
- expected_hash = @api.build_hash(:notification_key, parameters_order, request_parameters)
39
- if request_parameters[:hash] != expected_hash
40
- raise RuntimeError, 'Expected hash does not equal passed hash.' +
41
- 'Passed: ' + request_parameters[:hash] + ', ' +
42
- 'Expected: ' + expected_hash
43
- end
44
-
45
- request_parameters
46
- end
47
- end
48
- end
49
- end
50
- end
@@ -1,26 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'rexml/document'
4
-
5
- module Barzahlen
6
- module Api
7
- module Online
8
-
9
- # Parses Barzahlen-API response
10
- class ResponseParser
11
-
12
- # Parses xml and returns hash
13
- def self.parse(response_body)
14
- response_parsed = {}
15
-
16
- doc = REXML::Document.new response_body
17
- doc.elements.each('/response/*') do |element|
18
- response_parsed[element.name.gsub(/\-/, '_').to_sym] = element.text
19
- end
20
-
21
- response_parsed
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,57 +0,0 @@
1
- # coding: utf-8
2
-
3
- module Barzahlen
4
- module Api
5
- module Online
6
-
7
- # Manipulates transactions via Barzahlen-API
8
- class Transaction
9
- def initialize(api)
10
- @api = api
11
- end
12
-
13
- # create a transaction
14
- # necessary fields of the transaction are:
15
- # :customer_email, :amount, :currency, :language, :order_id,
16
- # :customer_street_nr, :customer_zipcode, :customer_city, :customer_country
17
- # optional are:
18
- # :custom_var_0, :custom_var_1, :custom_var_2
19
- def create(transaction)
20
- parameters_order = [:shop_id, :customer_email, :amount, :currency, :language, :order_id, :customer_street_nr,
21
- :customer_zipcode, :customer_city, :customer_country, :custom_var_0, :custom_var_1,
22
- :custom_var_2, :payment_key]
23
- @api.request('v1/transactions/create', :payment_key, parameters_order, transaction)
24
- end
25
-
26
- # update order-id of a transaction
27
- def update_order_id(transaction_id, order_id)
28
- parameters_order = [:shop_id, :transaction_id, :order_id, :payment_key]
29
- @api.request('v1/transactions/update', :payment_key, parameters_order,
30
- {:transaction_id => transaction_id, :order_id => order_id})
31
- end
32
-
33
- # resend email for a transaction
34
- def resend_email(transaction_id, language)
35
- parameters_order = [:shop_id, :transaction_id, :language, :payment_key]
36
- @api.request('v1/transactions/resend_email', :payment_key, parameters_order,
37
- {:transaction_id => transaction_id, :language => language})
38
- end
39
-
40
- # cancels a transaction
41
- def cancel(transaction_id, language)
42
- parameters_order = [:shop_id, :transaction_id, :language, :payment_key]
43
- @api.request('v1/transactions/cancel', :payment_key, parameters_order,
44
- {:transaction_id => transaction_id, :language => language})
45
- end
46
-
47
- # create a refund for a transaction
48
- def refund(transaction_id, amount, currency, language)
49
- parameters_order = [:shop_id, :transaction_id, :amount, :currency, :language, :payment_key]
50
- @api.request('v1/transactions/refund', :payment_key, parameters_order,
51
- {:transaction_id => transaction_id, :amount => amount,
52
- :currency => currency, :language => language})
53
- end
54
- end
55
- end
56
- end
57
- end
@@ -1,9 +0,0 @@
1
- # coding: utf-8
2
-
3
- module Barzahlen
4
- module Api
5
- module Online
6
- VERSION = '1.0.0'
7
- end
8
- end
9
- end
@@ -1,5 +0,0 @@
1
- require "ruby_sdk/version"
2
-
3
- module RubySdk
4
- # Your code goes here...
5
- end
@@ -1,3 +0,0 @@
1
- module RubySdk
2
- VERSION = "0.0.1"
3
- end
@@ -1,86 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Barzahlen::Api::Online::Api do
6
- context 'request' do
7
- it 'should call the correct url' do
8
- http = double('Barzahlen::Online::Api:Http')
9
- http.should_receive(:request)
10
- .with(URI('https://api.barzahlen.de/path'), anything)
11
- .and_return(get_successful_http_response)
12
-
13
- api = Barzahlen::Api::Online::Api.new http, get_config
14
- api.request 'path', :payment_key, [], {}
15
- end
16
-
17
- it 'should pass the correct parameters' do
18
- http = double('Barzahlen::Online::Api:Http')
19
- http.should_receive(:request)
20
- .with(anything, :foo1 => 'foo1', :foo2 => 'bar2', :foo3 => 'bar3', :shop_id => 12345,
21
- :hash => '539a7a8cf60febe6865ecb64369e8df819940249cbd6cdbe1a3ff72d3ca7087b1fdcd5c9396f2a0da6bced901364bfc5a2a4430b8dd405b96a4705c533eedeb0')
22
- .and_return(get_successful_http_response)
23
-
24
- api = Barzahlen::Api::Online::Api.new(http, get_config)
25
- api.request 'path', :payment_key, [:foo1, :foo2, :foo3, :payment_key],
26
- :foo1 => 'foo1', :foo2 => 'bar2', :foo3 => 'bar3'
27
- end
28
-
29
- it 'should return the correct result' do
30
- http = double('Barzahlen::Online::Api:Http')
31
- allow(http).to receive(:request).and_return(get_successful_http_response)
32
-
33
- api = Barzahlen::Api::Online::Api.new http, get_config
34
- result = api.request 'path', :payment_key, [], {}
35
-
36
- result.should == {
37
- :transaction_id => '227840174',
38
- :result => '0',
39
- :hash => '0714be1522e16fe67f397767c98cf37e37fd9c2a5391f49f8b01cb66642150751c77a0cce1ef7268e7bdeee792ed63eab53ef9b860f721c9d9d47ffc871ee3f9'
40
- }
41
- end
42
-
43
- it 'should raise a runtime error on a response code that different to 200' do
44
- http = double('Barzahlen::Online::Api:Http')
45
- allow(http).to receive(:request).and_return(get_http_response('500', 'error'))
46
-
47
- api = Barzahlen::Api::Online::Api.new http, get_config
48
- expect { api.request 'path', :payment_key, [], {} }.to raise_error
49
- end
50
- end
51
-
52
- context 'build_hash' do
53
- it 'should generate hash' do
54
- http = double('Barzahlen::Online::Api:Http')
55
-
56
- parameters_order = [:foo1, :foo2, :foo3, :payment_key]
57
-
58
- api = Barzahlen::Api::Online::Api.new http, get_config
59
- hash = api.build_hash :payment_key, parameters_order,
60
- :foo1 => 'foo1', :foo2 => 'bar2', :foo3 => 'bar3'
61
-
62
- hash.should == '539a7a8cf60febe6865ecb64369e8df819940249cbd6cdbe1a3ff72d3ca7087b1fdcd5c9396f2a0da6bced901364bfc5a2a4430b8dd405b96a4705c533eedeb0'
63
- end
64
- end
65
-
66
- private
67
- def get_config
68
- {:host => 'api.barzahlen.de',
69
- :shop_id => 12345,
70
- :payment_key => 'foobarfoobar',
71
- :notification_key => 'barfoobarfoo',
72
- :language => 'de',}
73
- end
74
-
75
- def get_successful_http_response
76
- get_http_response '200', "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response><transaction-id>227840174</transaction-id><result>0</result><hash>0714be1522e16fe67f397767c98cf37e37fd9c2a5391f49f8b01cb66642150751c77a0cce1ef7268e7bdeee792ed63eab53ef9b860f721c9d9d47ffc871ee3f9</hash></response>"
77
- end
78
-
79
- def get_http_response(code, body)
80
- http_response = double 'Net::HTTPResponse'
81
- allow(http_response).to receive(:code).and_return(code)
82
- allow(http_response).to receive(:body).and_return(body)
83
-
84
- http_response
85
- end
86
- end
@@ -1,15 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Barzahlen::Api::Online::HashBuilder do
6
- it 'should generate hash' do
7
- parameters_order = [:foo1, :foo2, :foo3, :payment_key]
8
- key_parameter_name = :payment_key
9
-
10
- hash_builder = Barzahlen::Api::Online::HashBuilder.new(parameters_order, key_parameter_name)
11
- hash = hash_builder.build_hash 'foobarfoobar', :foo1 => 'foo1', :foo2 => 'bar2', :foo3 => 'bar3'
12
-
13
- hash.should == '539a7a8cf60febe6865ecb64369e8df819940249cbd6cdbe1a3ff72d3ca7087b1fdcd5c9396f2a0da6bced901364bfc5a2a4430b8dd405b96a4705c533eedeb0'
14
- end
15
- end
@@ -1,13 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
- require 'net/http'
5
-
6
- describe Barzahlen::Api::Online::Http do
7
- it 'should return HTTPResponse' do
8
- http = Barzahlen::Api::Online::Http.new
9
- response = http.request URI('http://www.barzahlen.de/'), {}
10
-
11
- expect(response).to be_a Net::HTTPResponse
12
- end
13
- end
@@ -1,240 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe Barzahlen::Api::Online::NotificationHandler do
6
- context 'validate_request TRANSACTION_PAID' do
7
- it 'should return correct request on transaction-paid notification' do
8
- request_parameters = {
9
- :state => Barzahlen::Api::Online::NotificationHandler::TRANSACTION_PAID,
10
- :transaction_id => '123',
11
- :shop_id => '456',
12
- :customer_email => 'foo@example.com',
13
- :amount => '13.37',
14
- :currency => 'EUR',
15
- :order_id => '123',
16
- :custom_var_0 => 'custom_var_0',
17
- :custom_var_1 => 'custom_var_1',
18
- :custom_var_2 => 'custom_var_2',
19
- :hash => 'some hash'
20
- }
21
-
22
- notification_handler = Barzahlen::Api::Online::NotificationHandler.new get_api
23
- request = notification_handler.validate_request(request_parameters)
24
- request.should == {
25
- :state => Barzahlen::Api::Online::NotificationHandler::TRANSACTION_PAID,
26
- :transaction_id => '123',
27
- :shop_id => '456',
28
- :customer_email => 'foo@example.com',
29
- :amount => '13.37',
30
- :currency => 'EUR',
31
- :order_id => '123',
32
- :custom_var_0 => 'custom_var_0',
33
- :custom_var_1 => 'custom_var_1',
34
- :custom_var_2 => 'custom_var_2',
35
- :hash => 'some hash'
36
- }
37
- end
38
-
39
- it 'should raise a runtime error on wrong hash on transaction-paid notification' do
40
- request_parameters = {
41
- :state => Barzahlen::Api::Online::NotificationHandler::TRANSACTION_PAID,
42
- :transaction_id => '123',
43
- :shop_id => '456',
44
- :customer_email => 'foo@example.com',
45
- :amount => '13.37',
46
- :currency => 'EUR',
47
- :order_id => '123',
48
- :custom_var_0 => 'custom_var_0',
49
- :custom_var_1 => 'custom_var_1',
50
- :custom_var_2 => 'custom_var_2',
51
- :hash => '1111'
52
- }
53
-
54
- notification_handler = Barzahlen::Api::Online::NotificationHandler.new get_api
55
- expect { notification_handler.validate_request(request_parameters) }.to raise_error
56
- end
57
- end
58
-
59
- context 'validate_request TRANSACTION_EXPIRED' do
60
- it 'should return correct request on transaction-expired notification' do
61
- request_parameters = {
62
- :state => Barzahlen::Api::Online::NotificationHandler::TRANSACTION_EXPIRED,
63
- :transaction_id => '123',
64
- :shop_id => '456',
65
- :customer_email => 'foo@example.com',
66
- :amount => '13.37',
67
- :currency => 'EUR',
68
- :order_id => '123',
69
- :custom_var_0 => 'custom_var_0',
70
- :custom_var_1 => 'custom_var_1',
71
- :custom_var_2 => 'custom_var_2',
72
- :hash => 'some hash'
73
- }
74
-
75
- notification_handler = Barzahlen::Api::Online::NotificationHandler.new get_api
76
- request = notification_handler.validate_request(request_parameters)
77
- request.should == {
78
- :state => Barzahlen::Api::Online::NotificationHandler::TRANSACTION_EXPIRED,
79
- :transaction_id => '123',
80
- :shop_id => '456',
81
- :customer_email => 'foo@example.com',
82
- :amount => '13.37',
83
- :currency => 'EUR',
84
- :order_id => '123',
85
- :custom_var_0 => 'custom_var_0',
86
- :custom_var_1 => 'custom_var_1',
87
- :custom_var_2 => 'custom_var_2',
88
- :hash => 'some hash'
89
- }
90
- end
91
-
92
- it 'should raise a runtime error on wrong hash on transaction-expired notification' do
93
- request_parameters = {
94
- :state => Barzahlen::Api::Online::NotificationHandler::TRANSACTION_EXPIRED,
95
- :transaction_id => '123',
96
- :shop_id => '456',
97
- :customer_email => 'foo@example.com',
98
- :amount => '13.37',
99
- :currency => 'EUR',
100
- :order_id => '123',
101
- :custom_var_0 => 'custom_var_0',
102
- :custom_var_1 => 'custom_var_1',
103
- :custom_var_2 => 'custom_var_2',
104
- :hash => '2222'
105
- }
106
-
107
- notification_handler = Barzahlen::Api::Online::NotificationHandler.new get_api
108
- expect { notification_handler.validate_request(request_parameters) }.to raise_error
109
- end
110
- end
111
-
112
- context 'validate_request REFUND_COMPLETED' do
113
- it 'should return correct request on refund-completed notification' do
114
- request_parameters = {
115
- :state => Barzahlen::Api::Online::NotificationHandler::REFUND_COMPLETED,
116
- :refund_transaction_id => '123',
117
- :origin_transaction_id => '444',
118
- :shop_id => '456',
119
- :customer_email => 'foo@example.com',
120
- :amount => '13.37',
121
- :currency => 'EUR',
122
- :origin_order_id => '123',
123
- :custom_var_0 => 'custom_var_0',
124
- :custom_var_1 => 'custom_var_1',
125
- :custom_var_2 => 'custom_var_2',
126
- :hash => 'some hash'
127
- }
128
-
129
- notification_handler = Barzahlen::Api::Online::NotificationHandler.new get_api
130
- request = notification_handler.validate_request(request_parameters)
131
- request.should == {
132
- :state => Barzahlen::Api::Online::NotificationHandler::REFUND_COMPLETED,
133
- :refund_transaction_id => '123',
134
- :origin_transaction_id => '444',
135
- :shop_id => '456',
136
- :customer_email => 'foo@example.com',
137
- :amount => '13.37',
138
- :currency => 'EUR',
139
- :origin_order_id => '123',
140
- :custom_var_0 => 'custom_var_0',
141
- :custom_var_1 => 'custom_var_1',
142
- :custom_var_2 => 'custom_var_2',
143
- :hash => 'some hash'
144
- }
145
- end
146
-
147
- it 'should raise a runtime error on wrong hash on refund-complete notification' do
148
- request_parameters = {
149
- :state => Barzahlen::Api::Online::NotificationHandler::REFUND_COMPLETED,
150
- :refund_transaction_id => '123',
151
- :origin_transaction_id => '111',
152
- :shop_id => '456',
153
- :customer_email => 'foo@example.com',
154
- :amount => '13.37',
155
- :currency => 'EUR',
156
- :origin_order_id => '123',
157
- :custom_var_0 => 'custom_var_0',
158
- :custom_var_1 => 'custom_var_1',
159
- :custom_var_2 => 'custom_var_2',
160
- :hash => '333'
161
- }
162
-
163
- notification_handler = Barzahlen::Api::Online::NotificationHandler.new get_api
164
- expect { notification_handler.validate_request(request_parameters) }.to raise_error
165
- end
166
- end
167
-
168
- context 'validate_request REFUND_EXPIRED' do
169
- it 'should return correct request on refund-expired notification' do
170
- request_parameters = {
171
- :state => Barzahlen::Api::Online::NotificationHandler::REFUND_EXPIRED,
172
- :refund_transaction_id => '123',
173
- :origin_transaction_id => '444',
174
- :shop_id => '456',
175
- :customer_email => 'foo@example.com',
176
- :amount => '13.37',
177
- :currency => 'EUR',
178
- :origin_order_id => '123',
179
- :custom_var_0 => 'custom_var_0',
180
- :custom_var_1 => 'custom_var_1',
181
- :custom_var_2 => 'custom_var_2',
182
- :hash => 'some hash'
183
- }
184
-
185
- notification_handler = Barzahlen::Api::Online::NotificationHandler.new get_api
186
- request = notification_handler.validate_request(request_parameters)
187
- request.should == {
188
- :state => Barzahlen::Api::Online::NotificationHandler::REFUND_EXPIRED,
189
- :refund_transaction_id => '123',
190
- :origin_transaction_id => '444',
191
- :shop_id => '456',
192
- :customer_email => 'foo@example.com',
193
- :amount => '13.37',
194
- :currency => 'EUR',
195
- :origin_order_id => '123',
196
- :custom_var_0 => 'custom_var_0',
197
- :custom_var_1 => 'custom_var_1',
198
- :custom_var_2 => 'custom_var_2',
199
- :hash => 'some hash'
200
- }
201
- end
202
-
203
- it 'should raise a runtime error on wrong hash on refund-complete notification' do
204
- request_parameters = {
205
- :state => Barzahlen::Api::Online::NotificationHandler::REFUND_EXPIRED,
206
- :refund_transaction_id => '123',
207
- :origin_transaction_id => '123',
208
- :shop_id => '456',
209
- :customer_email => 'foo@example.com',
210
- :amount => '13.37',
211
- :currency => 'EUR',
212
- :origin_order_id => '123',
213
- :custom_var_0 => 'custom_var_0',
214
- :custom_var_1 => 'custom_var_1',
215
- :custom_var_2 => 'custom_var_2',
216
- :hash => '444'
217
- }
218
-
219
- notification_handler = Barzahlen::Api::Online::NotificationHandler.new get_api
220
- expect { notification_handler.validate_request(request_parameters) }.to raise_error
221
- end
222
- end
223
-
224
- it 'should raise a runtime error on wrong state' do
225
- request_parameters = {
226
- :state => 'foobar',
227
- :hash => 'hash'
228
- }
229
-
230
- notification_handler = Barzahlen::Api::Online::NotificationHandler.new get_api
231
- expect { notification_handler.validate_request(request_parameters) }.to raise_error
232
- end
233
-
234
- private
235
- def get_api
236
- api = double 'Barzahlen::Api::Online::Api'
237
- allow(api).to receive(:build_hash).and_return('some hash')
238
- api
239
- end
240
- end