solidus_six_saferpay 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +35 -0
  3. data/.gem_release.yml +5 -0
  4. data/.github/stale.yml +17 -0
  5. data/.gitignore +18 -0
  6. data/.rspec +2 -0
  7. data/.rubocop.yml +2 -0
  8. data/.travis.yml +12 -0
  9. data/Gemfile +33 -0
  10. data/LICENSE +26 -0
  11. data/README.md +33 -20
  12. data/Rakefile +4 -54
  13. data/app/assets/config/solidus_six_saferpay_manifest.js +1 -0
  14. data/app/assets/images/solidus_six_saferpay/.keep +0 -0
  15. data/app/assets/javascripts/spree/backend/solidus_six_saferpay.js +2 -0
  16. data/app/assets/javascripts/spree/frontend/solidus_six_saferpay.js +2 -0
  17. data/app/assets/stylesheets/spree/backend/solidus_six_saferpay.css +4 -0
  18. data/app/assets/stylesheets/spree/frontend/solidus_six_saferpay.css +4 -0
  19. data/app/controllers/spree/solidus_six_saferpay/checkout_controller.rb +49 -7
  20. data/bin/console +17 -0
  21. data/bin/rails +18 -0
  22. data/bin/rake +7 -0
  23. data/bin/setup +8 -0
  24. data/config/initializers/assets.rb +3 -0
  25. data/config/routes.rb +2 -2
  26. data/lib/generators/solidus_six_saferpay/install/install_generator.rb +15 -3
  27. data/lib/solidus_six_saferpay.rb +6 -3
  28. data/lib/solidus_six_saferpay/configuration.rb +4 -7
  29. data/lib/solidus_six_saferpay/engine.rb +19 -10
  30. data/lib/solidus_six_saferpay/version.rb +3 -1
  31. data/solidus_six_saferpay.gemspec +42 -0
  32. data/spec/controllers/spree/solidus_six_saferpay/checkout_controller_spec.rb +41 -0
  33. data/spec/controllers/spree/solidus_six_saferpay/payment_page/checkout_controller_spec.rb +205 -0
  34. data/spec/controllers/spree/solidus_six_saferpay/transaction/checkout_controller_spec.rb +228 -0
  35. data/spec/factories/payment_methods.rb +23 -0
  36. data/spec/factories/payments.rb +11 -0
  37. data/spec/factories/spree/six_saferpay_payments.rb +118 -0
  38. data/spec/models/spree/six_saferpay_payment_spec.rb +203 -0
  39. data/spec/rails_helper.rb +73 -0
  40. data/spec/services/spree/solidus_six_saferpay/assert_payment_page_spec.rb +148 -0
  41. data/spec/services/spree/solidus_six_saferpay/authorize_payment_spec.rb +39 -0
  42. data/spec/services/spree/solidus_six_saferpay/authorize_transaction_spec.rb +148 -0
  43. data/spec/services/spree/solidus_six_saferpay/initialize_payment_page_spec.rb +83 -0
  44. data/spec/services/spree/solidus_six_saferpay/initialize_payment_spec.rb +40 -0
  45. data/spec/services/spree/solidus_six_saferpay/initialize_transaction_spec.rb +85 -0
  46. data/spec/services/spree/solidus_six_saferpay/inquire_payment_page_payment_spec.rb +116 -0
  47. data/spec/services/spree/solidus_six_saferpay/inquire_payment_spec.rb +39 -0
  48. data/spec/services/spree/solidus_six_saferpay/inquire_transaction_payment_spec.rb +117 -0
  49. data/spec/services/spree/solidus_six_saferpay/payment_validator_spec.rb +100 -0
  50. data/spec/services/spree/solidus_six_saferpay/process_authorized_payment_spec.rb +39 -0
  51. data/spec/services/spree/solidus_six_saferpay/process_payment_page_payment_spec.rb +225 -0
  52. data/spec/services/spree/solidus_six_saferpay/process_transaction_payment_spec.rb +219 -0
  53. data/spec/solidus_six_saferpay/configuration_spec.rb +15 -0
  54. data/spec/solidus_six_saferpay/error_handler_spec.rb +65 -0
  55. data/spec/solidus_six_saferpay/gateway_response_spec.rb +70 -0
  56. data/spec/solidus_six_saferpay/gateway_spec.rb +378 -0
  57. data/spec/solidus_six_saferpay/payment_page_gateway_spec.rb +390 -0
  58. data/spec/solidus_six_saferpay/transaction_gateway_spec.rb +387 -0
  59. data/spec/spec_helper.rb +21 -0
  60. data/spec/support/route_access.rb +6 -0
  61. data/spec/support/uses_payment_page_gateway.rb +29 -0
  62. data/spec/support/uses_transaction_gateway.rb +28 -0
  63. metadata +241 -58
@@ -0,0 +1,390 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusSixSaferpay
4
+ RSpec.describe PaymentPageGateway do
5
+
6
+ # config options
7
+ let(:customer_id) { 'CUSTOMER_ID' }
8
+ let(:terminal_id) { 'TERMINAL_ID' }
9
+ let(:username) { 'USERNAME' }
10
+ let(:password) { 'PASSWORD' }
11
+ let(:success_url) { '/api/endpoints/success' }
12
+ let(:fail_url) { '/api/endpoints/fail' }
13
+ let(:base_url) { 'https://test.saferpay-api-host.test' }
14
+ let(:css_url) { '/custom/css/url' }
15
+
16
+ let(:gateway) do
17
+ described_class.new(
18
+ customer_id: customer_id,
19
+ terminal_id: terminal_id,
20
+ username: username,
21
+ password: password,
22
+ base_url: base_url,
23
+ css_url: css_url
24
+ )
25
+ end
26
+
27
+ let(:order) { create(:order, total: 100) }
28
+ let(:payment_method) { create(:saferpay_payment_method) }
29
+
30
+ let(:payment) { create(:six_saferpay_payment, order: order, payment_method: payment_method) }
31
+
32
+ before do
33
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_CUSTOMER_ID').and_return(customer_id)
34
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_TERMINAL_ID').and_return(terminal_id)
35
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_USERNAME').and_return(username)
36
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_PASSWORD').and_return(password)
37
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_BASE_URL').and_return(base_url)
38
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_CSS_URL').and_return(css_url)
39
+ end
40
+
41
+ describe '#initialize' do
42
+ it 'configures the API client with correct urls by default' do
43
+ described_class.new
44
+
45
+ config = SixSaferpay.config
46
+
47
+ expect(config.success_url).to eq(solidus_six_saferpay_payment_page_success_url)
48
+ expect(config.fail_url).to eq(solidus_six_saferpay_payment_page_fail_url)
49
+ end
50
+ end
51
+
52
+ describe '#initialize_payment' do
53
+
54
+ let(:saferpay_billing_address) do
55
+ instance_double("SixSaferpay::Address",
56
+ first_name: order.billing_address.first_name,
57
+ last_name: order.billing_address.last_name,
58
+ date_of_birth: nil,
59
+ company: nil,
60
+ gender: nil,
61
+ legal_form: nil,
62
+ street: order.billing_address.address1,
63
+ street2: order.billing_address.address2,
64
+ zip: order.billing_address.zipcode,
65
+ city: order.billing_address.city,
66
+ country_subdevision_code: nil,
67
+ country_code: order.billing_address.country.iso,
68
+ phone: nil,
69
+ email: nil,
70
+ )
71
+ end
72
+
73
+ let(:saferpay_shipping_address) do
74
+ instance_double("SixSaferpay::Address",
75
+ first_name: order.shipping_address.first_name,
76
+ last_name: order.shipping_address.last_name,
77
+ date_of_birth: nil,
78
+ company: nil,
79
+ gender: nil,
80
+ legal_form: nil,
81
+ street: order.shipping_address.address1,
82
+ street2: order.shipping_address.address2,
83
+ zip: order.shipping_address.zipcode,
84
+ city: order.shipping_address.city,
85
+ country_subdevision_code: nil,
86
+ country_code: order.shipping_address.country.iso,
87
+ phone: nil,
88
+ email: nil,
89
+ )
90
+ end
91
+
92
+ let(:saferpay_amount) do
93
+ instance_double("SixSaferpay::Amount",
94
+ value: (order.total * 100),
95
+ currency_code: order.currency
96
+ )
97
+ end
98
+
99
+ let(:saferpay_payment) do
100
+ instance_double("SixSaferpay::Payment",
101
+ amount: saferpay_amount,
102
+ order_id: order.number,
103
+ description: order.number
104
+ )
105
+ end
106
+
107
+ let(:saferpay_payer) do
108
+ instance_double("SixSaferpay::Payer",
109
+ language_code: I18n.locale,
110
+ billing_address: saferpay_billing_address,
111
+ delivery_address: saferpay_shipping_address
112
+ )
113
+ end
114
+
115
+ let(:initialize_params) do
116
+ {
117
+ payment: saferpay_payment,
118
+ payer: saferpay_payer
119
+ }
120
+ end
121
+
122
+ let(:saferpay_initialize) do
123
+ instance_double("SixSaferpay::SixPaymentPage::Initialize")
124
+ end
125
+
126
+ let(:api_initialize_response) do
127
+ SixSaferpay::SixPaymentPage::InitializeResponse.new(
128
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
129
+ token: 'TOKEN',
130
+ expiration: '2015-01-30T12:45:22.258+01:00',
131
+ redirect_url: '/redirect/url'
132
+ )
133
+ end
134
+
135
+
136
+ it 'initializes a payment page payment' do
137
+ # mock payment
138
+ expect(SixSaferpay::Amount).to receive(:new).with(
139
+ value: (order.total * 100),
140
+ currency_code: order.currency
141
+ ).and_return(saferpay_amount)
142
+ expect(SixSaferpay::Payment).to receive(:new).with(
143
+ amount: saferpay_amount,
144
+ order_id: order.number,
145
+ description: order.number
146
+ ).and_return(saferpay_payment)
147
+
148
+
149
+ # mock payer
150
+ expect(SixSaferpay::Address).to receive(:new).with(
151
+ first_name: order.billing_address.first_name,
152
+ last_name: order.billing_address.last_name,
153
+ date_of_birth: nil,
154
+ company: nil,
155
+ gender: nil,
156
+ legal_form: nil,
157
+ street: order.billing_address.address1,
158
+ street2: order.billing_address.address2,
159
+ zip: order.billing_address.zipcode,
160
+ city: order.billing_address.city,
161
+ country_subdevision_code: nil,
162
+ country_code: order.billing_address.country.iso,
163
+ phone: nil,
164
+ email: nil,
165
+ ).and_return(saferpay_billing_address)
166
+ expect(SixSaferpay::Address).to receive(:new).with(
167
+ first_name: order.shipping_address.first_name,
168
+ last_name: order.shipping_address.last_name,
169
+ date_of_birth: nil,
170
+ company: nil,
171
+ gender: nil,
172
+ legal_form: nil,
173
+ street: order.shipping_address.address1,
174
+ street2: order.shipping_address.address2,
175
+ zip: order.shipping_address.zipcode,
176
+ city: order.shipping_address.city,
177
+ country_subdevision_code: nil,
178
+ country_code: order.shipping_address.country.iso,
179
+ phone: nil,
180
+ email: nil,
181
+ ).and_return(saferpay_shipping_address)
182
+ expect(SixSaferpay::Payer).to receive(:new).with(
183
+ language_code: I18n.locale,
184
+ billing_address: saferpay_billing_address,
185
+ delivery_address: saferpay_shipping_address
186
+ ).and_return(saferpay_payer)
187
+
188
+ expect(SixSaferpay::SixPaymentPage::Initialize).to receive(:new).with(initialize_params).and_return(saferpay_initialize)
189
+
190
+ expect(SixSaferpay::Client).to receive(:post).with(saferpay_initialize).and_return(api_initialize_response)
191
+
192
+ gateway.initialize_payment(order, payment_method)
193
+ end
194
+
195
+ context 'when the payment initialization is successful' do
196
+ before do
197
+ expect(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixPaymentPage::Initialize)).and_return(api_initialize_response)
198
+ end
199
+
200
+ it 'returns a success gateway response' do
201
+ expect(GatewayResponse).to receive(:new).with(true, instance_of(String), api_initialize_response, {})
202
+ gateway.initialize_payment(order, payment_method)
203
+ end
204
+ end
205
+
206
+ context 'when the API raises an error' do
207
+ let(:six_saferpay_error) do
208
+ SixSaferpay::Error.new(
209
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
210
+ behavior: 'ABORT',
211
+ error_name: 'INVALID_TRANSACTION',
212
+ error_message: 'error_message'
213
+ )
214
+ end
215
+
216
+ before do
217
+ expect(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixPaymentPage::Initialize)).and_raise(six_saferpay_error)
218
+ end
219
+
220
+ it 'handles the error gracefully' do
221
+ expect(GatewayResponse).to receive(:new).with(false, six_saferpay_error.error_message, nil, error_name: six_saferpay_error.error_name)
222
+
223
+ gateway.initialize_payment(order, payment_method)
224
+ end
225
+ end
226
+ end
227
+
228
+ describe '#inquire' do
229
+
230
+ let(:saferpay_assert) do
231
+ instance_double("SixSaferpay::PaymentPage::Assert")
232
+ end
233
+
234
+ let(:transaction_status) { "AUTHORIZED" }
235
+ let(:transaction_id) { "723n4MAjMdhjSAhAKEUdA8jtl9jb" }
236
+ let(:transaction_date) { "2015-01-30T12:45:22.258+01:00" }
237
+ let(:amount_value) { "100" }
238
+ let(:amount_currency) { "USD" }
239
+ let(:brand_name) { 'PaymentBrand' }
240
+ let(:display_text) { "xxxx xxxx xxxx 1234" }
241
+ let(:six_transaction_reference) { "0:0:3:723n4MAjMdhjSAhAKEUdA8jtl9jb" }
242
+
243
+ let(:payment_means) do
244
+ SixSaferpay::ResponsePaymentMeans.new(
245
+ brand: SixSaferpay::Brand.new(name: brand_name),
246
+ display_text: display_text
247
+ )
248
+ end
249
+
250
+ let(:api_inquire_response) do
251
+ SixSaferpay::SixPaymentPage::AssertResponse.new(
252
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'test', spec_version: 'test'),
253
+ transaction: SixSaferpay::Transaction.new(
254
+ type: "PAYMENT",
255
+ status: transaction_status,
256
+ id: transaction_id,
257
+ date: transaction_date,
258
+ amount: SixSaferpay::Amount.new(value: amount_value, currency_code: amount_currency),
259
+ six_transaction_reference: six_transaction_reference,
260
+ ),
261
+ payment_means: payment_means
262
+ )
263
+ end
264
+
265
+ it 'performs an assert request' do
266
+ expect(SixSaferpay::SixPaymentPage::Assert).to receive(:new).with(token: payment.token).and_return(saferpay_assert)
267
+ expect(SixSaferpay::Client).to receive(:post).and_return(api_inquire_response)
268
+
269
+ gateway.inquire(payment)
270
+ end
271
+
272
+ context 'when the payment inquiry is successful' do
273
+ before do
274
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixPaymentPage::Assert)).and_return(api_inquire_response)
275
+ end
276
+
277
+ it 'returns a successful gateway response' do
278
+ expect(GatewayResponse).to receive(:new).with(true, instance_of(String), api_inquire_response, {})
279
+
280
+ gateway.inquire(payment)
281
+ end
282
+ end
283
+
284
+ context 'when the API returns an error' do
285
+ let(:six_saferpay_error) do
286
+ SixSaferpay::Error.new(
287
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
288
+ behavior: 'ABORT',
289
+ error_name: 'INVALID_TRANSACTION',
290
+ error_message: 'error_message'
291
+ )
292
+ end
293
+
294
+ before do
295
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixPaymentPage::Assert)).and_raise(six_saferpay_error)
296
+ end
297
+
298
+ it 'handles the error gracefully' do
299
+ expect(GatewayResponse).to receive(:new).with(false, six_saferpay_error.error_message, nil, error_name: six_saferpay_error.error_name)
300
+
301
+ gateway.inquire(payment)
302
+ end
303
+ end
304
+ end
305
+
306
+ describe '#authorize' do
307
+ it 'calls assert' do
308
+ expect(gateway).to receive(:assert).with(payment, {})
309
+ gateway.authorize(100, payment)
310
+ end
311
+ end
312
+
313
+ describe '#assert' do
314
+ let(:saferpay_assert) do
315
+ instance_double("SixSaferpay::PaymentPage::Assert")
316
+ end
317
+
318
+ let(:transaction_status) { "AUTHORIZED" }
319
+ let(:transaction_id) { "723n4MAjMdhjSAhAKEUdA8jtl9jb" }
320
+ let(:transaction_date) { "2015-01-30T12:45:22.258+01:00" }
321
+ let(:amount_value) { "100" }
322
+ let(:amount_currency) { "USD" }
323
+ let(:brand_name) { 'PaymentBrand' }
324
+ let(:display_text) { "xxxx xxxx xxxx 1234" }
325
+ let(:six_transaction_reference) { "0:0:3:723n4MAjMdhjSAhAKEUdA8jtl9jb" }
326
+
327
+ let(:payment_means) do
328
+ SixSaferpay::ResponsePaymentMeans.new(
329
+ brand: SixSaferpay::Brand.new(name: brand_name),
330
+ display_text: display_text
331
+ )
332
+ end
333
+
334
+ let(:api_assert_response) do
335
+ SixSaferpay::SixPaymentPage::AssertResponse.new(
336
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'test', spec_version: 'test'),
337
+ transaction: SixSaferpay::Transaction.new(
338
+ type: "PAYMENT",
339
+ status: transaction_status,
340
+ id: transaction_id,
341
+ date: transaction_date,
342
+ amount: SixSaferpay::Amount.new(value: amount_value, currency_code: amount_currency),
343
+ six_transaction_reference: six_transaction_reference,
344
+ ),
345
+ payment_means: payment_means
346
+ )
347
+ end
348
+
349
+ it 'performs an assert request' do
350
+ expect(SixSaferpay::SixPaymentPage::Assert).to receive(:new).with(token: payment.token).and_return(saferpay_assert)
351
+ expect(SixSaferpay::Client).to receive(:post).and_return(api_assert_response)
352
+
353
+ gateway.assert(payment)
354
+ end
355
+
356
+ context 'when the payment assert is successful' do
357
+ before do
358
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixPaymentPage::Assert)).and_return(api_assert_response)
359
+ end
360
+
361
+ it 'returns a successful gateway response' do
362
+ expect(GatewayResponse).to receive(:new).with(true, instance_of(String), api_assert_response, {})
363
+
364
+ gateway.assert(payment)
365
+ end
366
+ end
367
+
368
+ context 'when the API returns an error' do
369
+ let(:six_saferpay_error) do
370
+ SixSaferpay::Error.new(
371
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
372
+ behavior: 'ABORT',
373
+ error_name: 'INVALID_TRANSACTION',
374
+ error_message: 'error_message'
375
+ )
376
+ end
377
+
378
+ before do
379
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixPaymentPage::Assert)).and_raise(six_saferpay_error)
380
+ end
381
+
382
+ it 'handles the error gracefully' do
383
+ expect(GatewayResponse).to receive(:new).with(false, six_saferpay_error.error_message, nil, error_name: six_saferpay_error.error_name)
384
+
385
+ gateway.assert(payment)
386
+ end
387
+ end
388
+ end
389
+ end
390
+ end
@@ -0,0 +1,387 @@
1
+ require 'rails_helper'
2
+
3
+ module SolidusSixSaferpay
4
+ RSpec.describe TransactionGateway do
5
+
6
+ # config options
7
+ let(:customer_id) { 'CUSTOMER_ID' }
8
+ let(:terminal_id) { 'TERMINAL_ID' }
9
+ let(:username) { 'USERNAME' }
10
+ let(:password) { 'PASSWORD' }
11
+ let(:success_url) { '/api/endpoints/success' }
12
+ let(:fail_url) { '/api/endpoints/fail' }
13
+ let(:base_url) { 'https://test.saferpay-api-host.test' }
14
+ let(:css_url) { '/custom/css/url' }
15
+
16
+ let(:gateway) do
17
+ described_class.new(
18
+ customer_id: customer_id,
19
+ terminal_id: terminal_id,
20
+ username: username,
21
+ password: password,
22
+ base_url: base_url,
23
+ css_url: css_url
24
+ )
25
+ end
26
+
27
+ let(:order) { create(:order, total: 100) }
28
+ let(:payment_method) { create(:saferpay_payment_method) }
29
+
30
+ let(:payment) { create(:six_saferpay_payment, order: order, payment_method: payment_method) }
31
+
32
+ before do
33
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_CUSTOMER_ID').and_return(customer_id)
34
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_TERMINAL_ID').and_return(terminal_id)
35
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_USERNAME').and_return(username)
36
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_PASSWORD').and_return(password)
37
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_BASE_URL').and_return(base_url)
38
+ allow(ENV).to receive(:fetch).with('SIX_SAFERPAY_CSS_URL').and_return(css_url)
39
+ end
40
+
41
+ describe '#initialize' do
42
+ it 'configures the API client with correct urls by default' do
43
+ described_class.new
44
+
45
+ config = SixSaferpay.config
46
+
47
+ expect(config.success_url).to eq(solidus_six_saferpay_transaction_success_url)
48
+ expect(config.fail_url).to eq(solidus_six_saferpay_transaction_fail_url)
49
+ end
50
+ end
51
+
52
+ describe '#initialize_payment' do
53
+
54
+ let(:saferpay_billing_address) do
55
+ instance_double("SixSaferpay::Address",
56
+ first_name: order.billing_address.first_name,
57
+ last_name: order.billing_address.last_name,
58
+ date_of_birth: nil,
59
+ company: nil,
60
+ gender: nil,
61
+ legal_form: nil,
62
+ street: order.billing_address.address1,
63
+ street2: order.billing_address.address2,
64
+ zip: order.billing_address.zipcode,
65
+ city: order.billing_address.city,
66
+ country_subdevision_code: nil,
67
+ country_code: order.billing_address.country.iso,
68
+ phone: nil,
69
+ email: nil,
70
+ )
71
+ end
72
+
73
+ let(:saferpay_shipping_address) do
74
+ instance_double("SixSaferpay::Address",
75
+ first_name: order.shipping_address.first_name,
76
+ last_name: order.shipping_address.last_name,
77
+ date_of_birth: nil,
78
+ company: nil,
79
+ gender: nil,
80
+ legal_form: nil,
81
+ street: order.shipping_address.address1,
82
+ street2: order.shipping_address.address2,
83
+ zip: order.shipping_address.zipcode,
84
+ city: order.shipping_address.city,
85
+ country_subdevision_code: nil,
86
+ country_code: order.shipping_address.country.iso,
87
+ phone: nil,
88
+ email: nil,
89
+ )
90
+ end
91
+
92
+ let(:saferpay_amount) do
93
+ instance_double("SixSaferpay::Amount",
94
+ value: (order.total * 100),
95
+ currency_code: order.currency
96
+ )
97
+ end
98
+
99
+ let(:saferpay_payment) do
100
+ instance_double("SixSaferpay::Payment",
101
+ amount: saferpay_amount,
102
+ order_id: order.number,
103
+ description: order.number
104
+ )
105
+ end
106
+
107
+ let(:saferpay_payer) do
108
+ instance_double("SixSaferpay::Payer",
109
+ language_code: I18n.locale,
110
+ billing_address: saferpay_billing_address,
111
+ delivery_address: saferpay_shipping_address
112
+ )
113
+ end
114
+
115
+ let(:initialize_params) do
116
+ {
117
+ payment: saferpay_payment,
118
+ payer: saferpay_payer
119
+ }
120
+ end
121
+
122
+ let(:saferpay_initialize) do
123
+ instance_double("SixSaferpay::SixTransaction::Initialize")
124
+ end
125
+
126
+ let(:api_initialize_response) do
127
+ SixSaferpay::SixTransaction::InitializeResponse.new(
128
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
129
+ token: 'TOKEN',
130
+ expiration: '2015-01-30T12:45:22.258+01:00',
131
+ redirect_required: true,
132
+ redirect: SixSaferpay::Redirect.new(
133
+ redirect_url: '/redirect/url',
134
+ payment_means_required: true
135
+ )
136
+ )
137
+ end
138
+
139
+
140
+ it 'initializes a payment page payment' do
141
+ # mock payment
142
+ expect(SixSaferpay::Amount).to receive(:new).with(
143
+ value: (order.total * 100),
144
+ currency_code: order.currency
145
+ ).and_return(saferpay_amount)
146
+ expect(SixSaferpay::Payment).to receive(:new).with(
147
+ amount: saferpay_amount,
148
+ order_id: order.number,
149
+ description: order.number
150
+ ).and_return(saferpay_payment)
151
+
152
+
153
+ # mock payer
154
+ expect(SixSaferpay::Address).to receive(:new).with(
155
+ first_name: order.billing_address.first_name,
156
+ last_name: order.billing_address.last_name,
157
+ date_of_birth: nil,
158
+ company: nil,
159
+ gender: nil,
160
+ legal_form: nil,
161
+ street: order.billing_address.address1,
162
+ street2: order.billing_address.address2,
163
+ zip: order.billing_address.zipcode,
164
+ city: order.billing_address.city,
165
+ country_subdevision_code: nil,
166
+ country_code: order.billing_address.country.iso,
167
+ phone: nil,
168
+ email: nil,
169
+ ).and_return(saferpay_billing_address)
170
+ expect(SixSaferpay::Address).to receive(:new).with(
171
+ first_name: order.shipping_address.first_name,
172
+ last_name: order.shipping_address.last_name,
173
+ date_of_birth: nil,
174
+ company: nil,
175
+ gender: nil,
176
+ legal_form: nil,
177
+ street: order.shipping_address.address1,
178
+ street2: order.shipping_address.address2,
179
+ zip: order.shipping_address.zipcode,
180
+ city: order.shipping_address.city,
181
+ country_subdevision_code: nil,
182
+ country_code: order.shipping_address.country.iso,
183
+ phone: nil,
184
+ email: nil,
185
+ ).and_return(saferpay_shipping_address)
186
+ expect(SixSaferpay::Payer).to receive(:new).with(
187
+ language_code: I18n.locale,
188
+ billing_address: saferpay_billing_address,
189
+ delivery_address: saferpay_shipping_address
190
+ ).and_return(saferpay_payer)
191
+
192
+ expect(SixSaferpay::SixTransaction::Initialize).to receive(:new).with(initialize_params).and_return(saferpay_initialize)
193
+
194
+ expect(SixSaferpay::Client).to receive(:post).with(saferpay_initialize).and_return(api_initialize_response)
195
+
196
+ gateway.initialize_payment(order, payment_method)
197
+ end
198
+
199
+ context 'when the payment initialization is successful' do
200
+ before do
201
+ expect(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Initialize)).and_return(api_initialize_response)
202
+ end
203
+
204
+ it 'returns a success gateway response' do
205
+ expect(GatewayResponse).to receive(:new).with(true, instance_of(String), api_initialize_response, {})
206
+ gateway.initialize_payment(order, payment_method)
207
+ end
208
+ end
209
+
210
+ context 'when the API raises an error' do
211
+ let(:six_saferpay_error) do
212
+ SixSaferpay::Error.new(
213
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
214
+ behavior: 'ABORT',
215
+ error_name: 'INVALID_TRANSACTION',
216
+ error_message: 'error_message'
217
+ )
218
+ end
219
+
220
+ before do
221
+ expect(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Initialize)).and_raise(six_saferpay_error)
222
+ end
223
+
224
+ it 'handles the error gracefully' do
225
+ expect(GatewayResponse).to receive(:new).with(false, six_saferpay_error.error_message, nil, error_name: six_saferpay_error.error_name)
226
+
227
+ gateway.initialize_payment(order, payment_method)
228
+ end
229
+ end
230
+ end
231
+
232
+ describe '#inquire' do
233
+
234
+ let(:saferpay_inquire) do
235
+ instance_double("SixSaferpay::SixTransaction::Inquire")
236
+ end
237
+
238
+ let(:transaction_status) { "AUTHORIZED" }
239
+ let(:transaction_id) { "723n4MAjMdhjSAhAKEUdA8jtl9jb" }
240
+ let(:transaction_date) { "2015-01-30T12:45:22.258+01:00" }
241
+ let(:amount_value) { "100" }
242
+ let(:amount_currency) { "USD" }
243
+ let(:brand_name) { 'PaymentBrand' }
244
+ let(:display_text) { "xxxx xxxx xxxx 1234" }
245
+ let(:six_transaction_reference) { "0:0:3:723n4MAjMdhjSAhAKEUdA8jtl9jb" }
246
+
247
+ let(:payment_means) do
248
+ SixSaferpay::ResponsePaymentMeans.new(
249
+ brand: SixSaferpay::Brand.new(name: brand_name),
250
+ display_text: display_text
251
+ )
252
+ end
253
+
254
+ let(:api_inquire_response) do
255
+ SixSaferpay::SixTransaction::InquireResponse.new(
256
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'test', spec_version: 'test'),
257
+ transaction: SixSaferpay::Transaction.new(
258
+ type: "PAYMENT",
259
+ status: transaction_status,
260
+ id: transaction_id,
261
+ date: transaction_date,
262
+ amount: SixSaferpay::Amount.new(value: amount_value, currency_code: amount_currency),
263
+ six_transaction_reference: six_transaction_reference,
264
+ ),
265
+ payment_means: payment_means
266
+ )
267
+ end
268
+
269
+ it 'performs an inquire request' do
270
+ expect(SixSaferpay::SixTransaction::Inquire).to receive(:new).with(transaction_reference: payment.transaction_id).and_return(saferpay_inquire)
271
+ expect(SixSaferpay::Client).to receive(:post).and_return(api_inquire_response)
272
+
273
+ gateway.inquire(payment)
274
+ end
275
+
276
+ context 'when the payment inquiry is successful' do
277
+ before do
278
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Inquire)).and_return(api_inquire_response)
279
+ end
280
+
281
+ it 'returns a successful gateway response' do
282
+ expect(GatewayResponse).to receive(:new).with(true, instance_of(String), api_inquire_response, {})
283
+
284
+ gateway.inquire(payment)
285
+ end
286
+ end
287
+
288
+ context 'when the API returns an error' do
289
+ let(:six_saferpay_error) do
290
+ SixSaferpay::Error.new(
291
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
292
+ behavior: 'ABORT',
293
+ error_name: 'INVALID_TRANSACTION',
294
+ error_message: 'error_message'
295
+ )
296
+ end
297
+
298
+ before do
299
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Inquire)).and_raise(six_saferpay_error)
300
+ end
301
+
302
+ it 'handles the error gracefully' do
303
+ expect(GatewayResponse).to receive(:new).with(false, six_saferpay_error.error_message, nil, error_name: six_saferpay_error.error_name)
304
+
305
+ gateway.inquire(payment)
306
+ end
307
+ end
308
+ end
309
+
310
+ describe '#authorize' do
311
+ let(:saferpay_authorize) do
312
+ instance_double("SixSaferpay::Transaction::Authorize")
313
+ end
314
+
315
+ let(:transaction_status) { "AUTHORIZED" }
316
+ let(:transaction_id) { "723n4MAjMdhjSAhAKEUdA8jtl9jb" }
317
+ let(:transaction_date) { "2015-01-30T12:45:22.258+01:00" }
318
+ let(:amount_value) { "100" }
319
+ let(:amount_currency) { "USD" }
320
+ let(:brand_name) { 'PaymentBrand' }
321
+ let(:display_text) { "xxxx xxxx xxxx 1234" }
322
+ let(:six_transaction_reference) { "0:0:3:723n4MAjMdhjSAhAKEUdA8jtl9jb" }
323
+
324
+ let(:payment_means) do
325
+ SixSaferpay::ResponsePaymentMeans.new(
326
+ brand: SixSaferpay::Brand.new(name: brand_name),
327
+ display_text: display_text
328
+ )
329
+ end
330
+
331
+ let(:api_authorize_response) do
332
+ SixSaferpay::SixTransaction::AuthorizeResponse.new(
333
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'test', spec_version: 'test'),
334
+ transaction: SixSaferpay::Transaction.new(
335
+ type: "PAYMENT",
336
+ status: transaction_status,
337
+ id: transaction_id,
338
+ date: transaction_date,
339
+ amount: SixSaferpay::Amount.new(value: amount_value, currency_code: amount_currency),
340
+ six_transaction_reference: six_transaction_reference,
341
+ ),
342
+ payment_means: payment_means
343
+ )
344
+ end
345
+
346
+ it 'performs an authorize request' do
347
+ expect(SixSaferpay::SixTransaction::Authorize).to receive(:new).with(token: payment.token).and_return(saferpay_authorize)
348
+ expect(SixSaferpay::Client).to receive(:post).and_return(api_authorize_response)
349
+
350
+ gateway.authorize(payment.order.total, payment)
351
+ end
352
+
353
+ context 'when the payment authorize is successful' do
354
+ before do
355
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Authorize)).and_return(api_authorize_response)
356
+ end
357
+
358
+ it 'returns a successful gateway response' do
359
+ expect(GatewayResponse).to receive(:new).with(true, instance_of(String), api_authorize_response, {})
360
+
361
+ gateway.authorize(payment.order.total, payment)
362
+ end
363
+ end
364
+
365
+ context 'when the API returns an error' do
366
+ let(:six_saferpay_error) do
367
+ SixSaferpay::Error.new(
368
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'request_id', spec_version: 'test'),
369
+ behavior: 'ABORT',
370
+ error_name: 'INVALID_TRANSACTION',
371
+ error_message: 'error_message'
372
+ )
373
+ end
374
+
375
+ before do
376
+ allow(SixSaferpay::Client).to receive(:post).with(instance_of(SixSaferpay::SixTransaction::Authorize)).and_raise(six_saferpay_error)
377
+ end
378
+
379
+ it 'handles the error gracefully' do
380
+ expect(GatewayResponse).to receive(:new).with(false, six_saferpay_error.error_message, nil, error_name: six_saferpay_error.error_name)
381
+
382
+ gateway.authorize(payment.order.total, payment)
383
+ end
384
+ end
385
+ end
386
+ end
387
+ end