solidus_six_saferpay 0.1.5 → 0.3.0

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