solidus_six_saferpay 0.1.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) 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 +82 -16
  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/services/spree/solidus_six_saferpay/order_not_found_handler.rb +28 -0
  30. data/app/services/spree/solidus_six_saferpay/payment_not_found_handler.rb +28 -0
  31. data/app/services/spree/solidus_six_saferpay/payment_processing_success_handler.rb +26 -0
  32. data/app/views/spree/checkout/payment/_saferpay_payment.html.erb +2 -2
  33. data/app/views/spree/solidus_six_saferpay/checkout/{iframe_breakout_redirect.html.erb → iframe_breakout_redirect.erb} +1 -1
  34. data/bin/console +17 -0
  35. data/bin/rails +18 -0
  36. data/bin/rake +7 -0
  37. data/bin/setup +8 -0
  38. data/config/initializers/assets.rb +3 -0
  39. data/config/locales/de.yml +3 -0
  40. data/config/locales/en.yml +3 -0
  41. data/config/locales/fr.yml +3 -0
  42. data/config/routes.rb +8 -8
  43. data/lib/generators/solidus_six_saferpay/install/install_generator.rb +15 -3
  44. data/lib/solidus_six_saferpay.rb +6 -3
  45. data/lib/solidus_six_saferpay/configuration.rb +2 -7
  46. data/lib/solidus_six_saferpay/engine.rb +19 -10
  47. data/lib/solidus_six_saferpay/gateway.rb +11 -11
  48. data/lib/solidus_six_saferpay/payment_page_gateway.rb +8 -9
  49. data/lib/solidus_six_saferpay/transaction_gateway.rb +8 -9
  50. data/lib/solidus_six_saferpay/version.rb +3 -1
  51. data/solidus_six_saferpay.gemspec +42 -0
  52. data/spec/controllers/spree/solidus_six_saferpay/payment_page/checkout_controller_spec.rb +16 -0
  53. data/spec/controllers/spree/solidus_six_saferpay/transaction/checkout_controller_spec.rb +16 -0
  54. data/spec/factories/payment_methods.rb +23 -0
  55. data/spec/factories/payments.rb +11 -0
  56. data/spec/factories/spree/six_saferpay_payments.rb +118 -0
  57. data/spec/models/spree/six_saferpay_payment_spec.rb +203 -0
  58. data/spec/rails_helper.rb +73 -0
  59. data/spec/services/spree/solidus_six_saferpay/assert_payment_page_spec.rb +22 -0
  60. data/spec/services/spree/solidus_six_saferpay/authorize_payment_spec.rb +39 -0
  61. data/spec/services/spree/solidus_six_saferpay/authorize_transaction_spec.rb +24 -0
  62. data/spec/services/spree/solidus_six_saferpay/cancel_authorized_payment_spec.rb +58 -0
  63. data/spec/services/spree/solidus_six_saferpay/initialize_payment_page_spec.rb +81 -0
  64. data/spec/services/spree/solidus_six_saferpay/initialize_payment_spec.rb +40 -0
  65. data/spec/services/spree/solidus_six_saferpay/initialize_transaction_spec.rb +84 -0
  66. data/spec/services/spree/solidus_six_saferpay/inquire_payment_page_payment_spec.rb +20 -0
  67. data/spec/services/spree/solidus_six_saferpay/inquire_payment_spec.rb +39 -0
  68. data/spec/services/spree/solidus_six_saferpay/inquire_transaction_payment_spec.rb +21 -0
  69. data/spec/services/spree/solidus_six_saferpay/order_not_found_handler_spec.rb +30 -0
  70. data/spec/services/spree/solidus_six_saferpay/payment_not_found_handler_spec.rb +30 -0
  71. data/spec/services/spree/solidus_six_saferpay/payment_processing_success_handler_spec.rb +34 -0
  72. data/spec/services/spree/solidus_six_saferpay/payment_validator_spec.rb +100 -0
  73. data/spec/services/spree/solidus_six_saferpay/process_authorized_payment_spec.rb +39 -0
  74. data/spec/services/spree/solidus_six_saferpay/process_payment_page_payment_spec.rb +20 -0
  75. data/spec/services/spree/solidus_six_saferpay/process_transaction_payment_spec.rb +20 -0
  76. data/spec/solidus_six_saferpay/configuration_spec.rb +12 -0
  77. data/spec/solidus_six_saferpay/error_handler_spec.rb +65 -0
  78. data/spec/solidus_six_saferpay/gateway_response_spec.rb +70 -0
  79. data/spec/solidus_six_saferpay/gateway_spec.rb +365 -0
  80. data/spec/solidus_six_saferpay/payment_page_gateway_spec.rb +392 -0
  81. data/spec/solidus_six_saferpay/transaction_gateway_spec.rb +390 -0
  82. data/spec/spec_helper.rb +21 -0
  83. data/spec/support/route_access.rb +6 -0
  84. data/spec/support/shared_examples/authorize_payment.rb +132 -0
  85. data/spec/support/shared_examples/checkout_controller.rb +294 -0
  86. data/spec/support/shared_examples/inquire_payment.rb +118 -0
  87. data/spec/support/shared_examples/process_authorized_payment.rb +202 -0
  88. data/spec/support/uses_payment_page_gateway.rb +29 -0
  89. data/spec/support/uses_transaction_gateway.rb +28 -0
  90. metadata +261 -62
  91. data/app/services/spree/solidus_six_saferpay/inquire_transaction.rb +0 -7
@@ -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
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Configure Rails Environment
4
+ ENV['RAILS_ENV'] = 'test'
5
+
6
+ # Run Coverage report
7
+ require 'solidus_dev_support/rspec/coverage'
8
+
9
+ require File.expand_path('dummy/config/environment.rb', __dir__)
10
+
11
+ # Requires factories and other useful helpers defined in spree_core.
12
+ require 'solidus_dev_support/rspec/feature_helper'
13
+
14
+ # Requires supporting ruby files with custom matchers and macros, etc,
15
+ # in spec/support/ and its subdirectories.
16
+ Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f }
17
+
18
+ RSpec.configure do |config|
19
+ config.infer_spec_type_from_file_location!
20
+ config.use_transactional_fixtures = false
21
+ end
@@ -0,0 +1,6 @@
1
+ RSpec.shared_examples "it has route access" do
2
+
3
+ it 'has access to routes' do
4
+ expect(subject).to respond_to(:url_helpers)
5
+ end
6
+ end
@@ -0,0 +1,132 @@
1
+ RSpec.shared_examples 'authorize_payment' do
2
+
3
+ let(:transaction_status) { "AUTHORIZED" }
4
+ let(:transaction_id) { "723n4MAjMdhjSAhAKEUdA8jtl9jb" }
5
+ let(:transaction_date) { "2015-01-30T12:45:22.258+01:00" }
6
+ let(:amount_value) { "100" }
7
+ let(:amount_currency) { "USD" }
8
+ let(:brand_name) { 'PaymentBrand' }
9
+ let(:display_text) { "xxxx xxxx xxxx 1234" }
10
+ let(:six_transaction_reference) { "0:0:3:723n4MAjMdhjSAhAKEUdA8jtl9jb" }
11
+
12
+ let(:payment_means) do
13
+ SixSaferpay::ResponsePaymentMeans.new(
14
+ brand: SixSaferpay::Brand.new(name: brand_name),
15
+ display_text: display_text
16
+ )
17
+ end
18
+
19
+ # https://saferpay.github.io/jsonapi/#Payment_v1_PaymentPage_Assert
20
+ # https://saferpay.github.io/jsonapi/#Payment_v1_Transaction_Authorize
21
+ let(:api_response) do
22
+ api_response_class.new(
23
+ response_header: SixSaferpay::ResponseHeader.new(request_id: 'test', spec_version: 'test'),
24
+ transaction: SixSaferpay::Transaction.new(
25
+ type: "PAYMENT",
26
+ status: transaction_status,
27
+ id: transaction_id,
28
+ date: transaction_date,
29
+ amount: SixSaferpay::Amount.new(value: amount_value, currency_code: amount_currency),
30
+ six_transaction_reference: six_transaction_reference,
31
+ ),
32
+ payment_means: payment_means
33
+ )
34
+ end
35
+
36
+ let(:gateway_response) do
37
+ ::SolidusSixSaferpay::GatewayResponse.new(
38
+ gateway_success,
39
+ "initialize success: #{gateway_success}",
40
+ api_response
41
+ )
42
+ end
43
+
44
+ # stub gateway to return our mock response
45
+ before do
46
+ allow(subject).to receive(:gateway).
47
+ and_return(double('gateway', authorize: gateway_response))
48
+ end
49
+
50
+ context 'when not successful' do
51
+ let(:gateway_success) { false }
52
+
53
+ it 'indicates failure' do
54
+ subject.call
55
+
56
+ expect(subject).not_to be_success
57
+ end
58
+
59
+ it 'does not update the payment attributes' do
60
+ expect { subject.call }.not_to change { payment.transaction_id }
61
+ expect { subject.call }.not_to change { payment.transaction_status }
62
+ expect { subject.call }.not_to change { payment.transaction_date }
63
+ expect { subject.call }.not_to change { payment.six_transaction_reference }
64
+ expect { subject.call }.not_to change { payment.display_text }
65
+ expect { subject.call }.not_to change { payment.response_hash }
66
+ end
67
+ end
68
+
69
+ context 'when successful' do
70
+ let(:gateway_success) { true }
71
+
72
+ it 'updates the transaction_id' do
73
+ expect { subject.call }.to change { payment.transaction_id }.from(nil).to(transaction_id)
74
+ end
75
+
76
+ it 'updates the transaction status' do
77
+ expect { subject.call }.to change { payment.transaction_status }.from(nil).to(transaction_status)
78
+ end
79
+
80
+ it 'updates the transaction date' do
81
+ expect { subject.call }.to change { payment.transaction_date }.from(nil).to(DateTime.parse(transaction_date))
82
+ end
83
+
84
+ it 'updates the six_transaction_reference' do
85
+ expect { subject.call }.to change { payment.six_transaction_reference }.from(nil).to(six_transaction_reference)
86
+ end
87
+
88
+ it 'updates the display_text' do
89
+ expect { subject.call }.to change { payment.display_text }.from(nil).to(display_text)
90
+ end
91
+
92
+ it 'updates the response hash' do
93
+ expect { subject.call }.to change { payment.response_hash }.from(payment.response_hash).to(api_response.to_h)
94
+ end
95
+
96
+ context 'when the payment was made with a card' do
97
+ let(:masked_number) { "xxxx xxxx xxxx 5555" }
98
+ let(:exp_year) { "19" }
99
+ let(:exp_month) { "5" }
100
+ let(:payment_means) do
101
+ SixSaferpay::ResponsePaymentMeans.new(
102
+ brand: SixSaferpay::Brand.new(name: brand_name),
103
+ display_text: display_text,
104
+ card: SixSaferpay::ResponseCard.new(
105
+ masked_number: masked_number,
106
+ exp_year: exp_year,
107
+ exp_month: exp_month
108
+ )
109
+ )
110
+ end
111
+
112
+ it 'updates the masked number' do
113
+ expect { subject.call }.to change { payment.masked_number }.from(nil).to(masked_number)
114
+ end
115
+
116
+ it 'updates the expiry year' do
117
+ expect { subject.call }.to change { payment.expiration_year }.from(nil).to(exp_year)
118
+ end
119
+
120
+ it 'updates the expiry month' do
121
+ expect { subject.call }.to change { payment.expiration_month }.from(nil).to(exp_month)
122
+ end
123
+ end
124
+
125
+ it 'indicates success' do
126
+ subject.call
127
+
128
+ expect(subject).to be_success
129
+ end
130
+ end
131
+
132
+ end